summaryrefslogtreecommitdiff
path: root/src/values/sizing.rs
blob: e6a508628019d668ca0cdcbe4962148bca4f3c36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use std::num::ParseFloatError;
use std::str::FromStr;

use iced::Length;
use iced::widget::grid::Sizing;

use super::Value;
use super::length::ParseLengthError;

#[derive(Debug, thiserror::Error, Clone, PartialEq)]
pub enum ParseSizingError {
    #[error("float parsing error: {0}")]
    ParseFloatError(ParseFloatError),
    #[error("length parsing error: {0}")]
    ParseLengthError(#[from] ParseLengthError),
    #[error("invalid prefix")]
    InvalidPrefix,
    #[error("missing prefix")]
    MissingPrefix,
    #[error("cannot parse sizing from empty string")]
    Empty,
}

impl From<ParseFloatError> for ParseSizingError {
    fn from(value: ParseFloatError) -> Self {
        Self::ParseFloatError(value)
    }
}

impl Value for Sizing {
    type Err = ParseSizingError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let s = s.trim();

        if s.is_empty() {
            return Err(ParseSizingError::Empty);
        }

        if s.starts_with(|c: char| c.is_ascii_digit()) {
            return Err(ParseSizingError::MissingPrefix);
        }

        let (prefix, value) = s.split_at(2);
        match prefix.to_lowercase().as_str() {
            "ar" => Ok(Self::AspectRatio(f32::from_str(value)?)),
            "ed" => Ok(Self::EvenlyDistribute(Length::from_str(value)?)),
            _ => Err(ParseSizingError::InvalidPrefix),
        }
    }

    fn to_string(&self) -> String {
        match self {
            Self::AspectRatio(ratio) => format!("ar{ratio}"),
            Self::EvenlyDistribute(length) => {
                format!("ed{}", length.to_string())
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn can_parse_ratio() {
        assert_eq!(Sizing::from_str("ar5.1"), Ok(Sizing::AspectRatio(5.1)))
    }

    #[test]
    fn can_parse_distribute_with_space() {
        assert_eq!(
            Sizing::from_str("  edfp2 "),
            Ok(Sizing::EvenlyDistribute(Length::FillPortion(2)))
        )
    }

    #[test]
    fn cant_parse_invalid_prefix() {
        assert_eq!(
            Sizing::from_str("bc4.1 "),
            Err(ParseSizingError::InvalidPrefix)
        )
    }

    #[test]
    fn cant_parse_invalid_float() {
        assert_eq!(
            Sizing::from_str(" ar2.a"),
            Err(ParseSizingError::ParseFloatError(
                f32::from_str("2.a").expect_err("float parse should fail")
            ))
        )
    }

    #[test]
    fn cant_parse_with_missing_prefix() {
        assert_eq!(
            Sizing::from_str("2.4"),
            Err(ParseSizingError::MissingPrefix)
        )
    }

    #[test]
    fn cant_parse_empty_string() {
        assert_eq!(Sizing::from_str(" "), Err(ParseSizingError::Empty))
    }
}