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
use torin::geometry::Length;
use torin::size::{DynamicCalculation, Size};

use crate::Parse;

#[derive(Debug, PartialEq, Eq)]
pub struct ParseSizeError;

impl Parse for Size {
    type Err = ParseSizeError;

    fn parse(value: &str) -> Result<Self, Self::Err> {
        if value == "auto" {
            Ok(Size::Inner)
        } else if value == "fill" {
            Ok(Size::Fill)
        } else if value.contains("calc") {
            Ok(Size::DynamicCalculations(Box::new(parse_calc(value)?)))
        } else if value.contains('%') {
            Ok(Size::Percentage(Length::new(
                value
                    .replace('%', "")
                    .parse::<f32>()
                    .map_err(|_| ParseSizeError)?,
            )))
        } else if value.contains('v') {
            Ok(Size::RootPercentage(Length::new(
                value
                    .replace('v', "")
                    .parse::<f32>()
                    .map_err(|_| ParseSizeError)?,
            )))
        } else {
            Ok(Size::Pixels(Length::new(
                value.parse::<f32>().map_err(|_| ParseSizeError)?,
            )))
        }
    }
}

pub fn parse_calc(mut value: &str) -> Result<Vec<DynamicCalculation>, ParseSizeError> {
    let mut calcs = Vec::new();

    value = value
        .strip_prefix("calc(")
        .ok_or(ParseSizeError)?
        .strip_suffix(')')
        .ok_or(ParseSizeError)?;

    let values = value.split_whitespace();

    for val in values {
        if val.contains('%') {
            calcs.push(DynamicCalculation::Percentage(
                val.replace('%', "").parse().map_err(|_| ParseSizeError)?,
            ));
        } else if val == "+" {
            calcs.push(DynamicCalculation::Add);
        } else if val == "-" {
            calcs.push(DynamicCalculation::Sub);
        } else if val == "/" {
            calcs.push(DynamicCalculation::Div);
        } else if val == "*" {
            calcs.push(DynamicCalculation::Mul);
        } else {
            calcs.push(DynamicCalculation::Pixels(
                val.parse::<f32>().map_err(|_| ParseSizeError)?,
            ));
        }
    }

    Ok(calcs)
}