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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use std::fmt;
use crate::{
    margin, padding, position, size,
    unit::{Length, Percent},
};
#[derive(Debug, PartialOrd, PartialEq, Clone)]
pub enum Calc {
    
    Length(Box<Length>),
    Percent(Percent),
    CssVariable(String),
    
    Sum(Box<Calc>, Box<Calc>),
    Sub(Box<Calc>, Box<Calc>),
    Mul(Box<Calc>, f32),
    Div(Box<Calc>, f32),
}
impl From<Length> for Calc {
    fn from(source: Length) -> Self {
        Calc::Length(Box::new(source))
    }
}
impl TryFrom<padding::Length> for Calc {
    type Error = &'static str;
    fn try_from(value: padding::Length) -> Result<Self, Self::Error> {
        match value {
            padding::Length::Length(len) => Ok(len.into()),
            padding::Length::Percent(per) => Ok(per.into()),
            padding::Length::Inherit => Err("Calc cannot accept inherit as value"),
        }
    }
}
impl TryFrom<margin::Length> for Calc {
    type Error = &'static str;
    fn try_from(value: margin::Length) -> Result<Self, Self::Error> {
        match value {
            margin::Length::Length(len) => Ok(len.into()),
            margin::Length::Percent(per) => Ok(per.into()),
            _ => Err("Calc cannot accept values such as inherit, auto ..etc"),
        }
    }
}
impl TryFrom<size::Length> for Calc {
    type Error = &'static str;
    fn try_from(value: size::Length) -> Result<Self, Self::Error> {
        match value {
            size::Length::Length(len) => Ok(len.into()),
            size::Length::Percent(per) => Ok(per.into()),
            _ => Err("Calc cannot accept values such as inherit, auto ..etc"),
        }
    }
}
impl TryFrom<position::PostionLength> for Calc {
    type Error = &'static str;
    fn try_from(value: position::PostionLength) -> Result<Self, Self::Error> {
        match value {
            position::PostionLength::Length(len) => Ok(len.into()),
            position::PostionLength::Percent(per) => Ok(per.into()),
            _ => Err("Calc cannot accept values such as inherit, auto ..etc"),
        }
    }
}
impl From<Percent> for Calc {
    fn from(source: Percent) -> Self {
        Calc::Percent(source)
    }
}
impl From<String> for Calc {
    fn from(source: String) -> Self {
        Calc::CssVariable(source)
    }
}
impl From<&str> for Calc {
    fn from(source: &str) -> Self {
        Calc::CssVariable(source.to_string())
    }
}
impl fmt::Display for Calc {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fn fmt_calc(calc: &Calc) -> String {
            match calc {
                Calc::Length(val) => val.to_string(),
                Calc::Percent(val) => val.to_string(),
                Calc::CssVariable(val) => format!("var({})", val.to_string()),
                Calc::Sum(c, val) => format!("({} + {})", fmt_calc(c), fmt_calc(val)),
                Calc::Sub(c, val) => format!("({} - {})", fmt_calc(c), fmt_calc(val)),
                Calc::Mul(c, val) => format!("({} * {})", fmt_calc(c), val),
                Calc::Div(c, val) => format!("({} / {})", fmt_calc(c), val),
            }
        }
        match self {
            Self::Length(val) => write!(f, "{}", val),
            Self::Percent(val) => write!(f, "{}", val),
            Self::CssVariable(val) => write!(f, "calc(var({}))", val),
            _ => write!(f, "calc{}", fmt_calc(self)),
        }
    }
}
impl Calc {
    pub fn sum(self, val: impl Into<Calc>) -> Self {
        Self::Sum(Box::new(self), Box::new(val.into()))
    }
    pub fn sub(self, val: impl Into<Calc>) -> Self {
        Self::Sub(Box::new(self), Box::new(val.into()))
    }
    pub fn mul(self, val: f32) -> Self {
        Self::Mul(Box::new(self), val)
    }
    pub fn div(self, val: f32) -> Self {
        Self::Div(Box::new(self), val)
    }
    pub fn try_sum(self, val: Option<impl Into<Calc>>) -> Self {
        if let Some(val) = val {
            self.sum(val)
        } else {
            self
        }
    }
    pub fn try_sub(self, val: Option<impl Into<Calc>>) -> Self {
        if let Some(val) = val {
            self.sub(val)
        } else {
            self
        }
    }
    pub fn try_mul(self, val: Option<f32>) -> Self {
        if let Some(val) = val {
            self.mul(val)
        } else {
            self
        }
    }
    pub fn try_div(self, val: Option<f32>) -> Self {
        if let Some(val) = val {
            self.div(val)
        } else {
            self
        }
    }
}
#[derive(Clone, Debug, PartialEq, Display, From)]
pub enum CalcArgument {
    Length(Length),
    Percent(Percent),
    CssVariable(String),
}
impl From<&str> for CalcArgument {
    fn from(source: &str) -> Self {
        CalcArgument::CssVariable(source.to_string())
    }
}
pub fn calc(value: impl Into<CalcArgument>) -> Calc {
    match value.into() {
        CalcArgument::Length(len) => Calc::Length(Box::new(len)),
        CalcArgument::Percent(per) => Calc::Percent(per),
        CalcArgument::CssVariable(var) => Calc::CssVariable(var),
    }
}