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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use std::fmt;

use freya_engine::prelude::Color;
use torin::scaled::Scaled;

use crate::{
    ExtSplit,
    Fill,
    Parse,
    ParseError,
};

#[derive(Default, Clone, Debug, PartialEq)]
pub struct Border {
    pub fill: Fill,
    pub width: BorderWidth,
    pub alignment: BorderAlignment,
}

impl Border {
    #[inline]
    pub fn is_visible(&self) -> bool {
        !(self.width.top == 0.0
            && self.width.left == 0.0
            && self.width.bottom == 0.0
            && self.width.right == 0.0)
            && self.fill != Fill::Color(Color::TRANSPARENT)
    }
}

#[derive(Default, Clone, Copy, Debug, PartialEq)]
pub struct BorderWidth {
    pub top: f32,
    pub right: f32,
    pub bottom: f32,
    pub left: f32,
}

impl Scaled for BorderWidth {
    fn scale(&mut self, scale_factor: f32) {
        self.top *= scale_factor;
        self.left *= scale_factor;
        self.bottom *= scale_factor;
        self.right *= scale_factor;
    }
}

impl fmt::Display for BorderWidth {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{} {} {} {}",
            self.top, self.right, self.bottom, self.left,
        )
    }
}

#[derive(Default, Clone, Copy, Debug, PartialEq)]
pub enum BorderAlignment {
    #[default]
    Inner,
    Outer,
    Center,
}

impl Parse for BorderAlignment {
    fn parse(value: &str) -> Result<Self, ParseError> {
        Ok(match value {
            "inner" => BorderAlignment::Inner,
            "outer" => BorderAlignment::Outer,
            "center" => BorderAlignment::Center,
            _ => BorderAlignment::default(),
        })
    }
}

impl fmt::Display for BorderAlignment {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(match self {
            BorderAlignment::Inner => "inner",
            BorderAlignment::Outer => "outer",
            BorderAlignment::Center => "center",
        })
    }
}

impl Parse for Border {
    fn parse(value: &str) -> Result<Self, ParseError> {
        if value == "none" {
            return Ok(Self::default());
        }

        let mut border_values = value.split_ascii_whitespace_excluding_group('(', ')');

        Ok(match border_values.clone().count() {
            // <width> <style> <fill>
            3 => {
                let width = border_values
                    .next()
                    .ok_or(ParseError)?
                    .parse::<f32>()
                    .map_err(|_| ParseError)?;

                Border {
                    width: BorderWidth {
                        top: width,
                        left: width,
                        bottom: width,
                        right: width,
                    },
                    alignment: BorderAlignment::parse(border_values.next().ok_or(ParseError)?)?,
                    fill: Fill::parse(&border_values.collect::<Vec<&str>>().join(" "))
                        .map_err(|_| ParseError)?,
                }
            }

            // <vertical> <horizontal> <solid> <fill>
            4 => {
                let vertical_width = border_values
                    .next()
                    .ok_or(ParseError)?
                    .parse::<f32>()
                    .map_err(|_| ParseError)?;
                let horizontal_width = border_values
                    .next()
                    .ok_or(ParseError)?
                    .parse::<f32>()
                    .map_err(|_| ParseError)?;

                Border {
                    width: BorderWidth {
                        top: vertical_width,
                        left: horizontal_width,
                        bottom: vertical_width,
                        right: horizontal_width,
                    },
                    alignment: BorderAlignment::parse(border_values.next().ok_or(ParseError)?)?,
                    fill: Fill::parse(&border_values.collect::<Vec<&str>>().join(" "))
                        .map_err(|_| ParseError)?,
                }
            }
            // <top> <horizontal> <bottom> <style> <fill>
            5 => {
                let top_width = border_values
                    .next()
                    .ok_or(ParseError)?
                    .parse::<f32>()
                    .map_err(|_| ParseError)?;
                let horizontal_width = border_values
                    .next()
                    .ok_or(ParseError)?
                    .parse::<f32>()
                    .map_err(|_| ParseError)?;
                let bottom_width = border_values
                    .next()
                    .ok_or(ParseError)?
                    .parse::<f32>()
                    .map_err(|_| ParseError)?;

                Border {
                    width: BorderWidth {
                        top: top_width,
                        left: horizontal_width,
                        bottom: bottom_width,
                        right: horizontal_width,
                    },
                    alignment: BorderAlignment::parse(border_values.next().ok_or(ParseError)?)?,
                    fill: Fill::parse(&border_values.collect::<Vec<&str>>().join(" "))
                        .map_err(|_| ParseError)?,
                }
            }
            // <top> <right> <bottom> <left> <style> <fill>
            6 => Border {
                width: BorderWidth {
                    top: border_values
                        .next()
                        .ok_or(ParseError)?
                        .parse::<f32>()
                        .map_err(|_| ParseError)?,
                    right: border_values
                        .next()
                        .ok_or(ParseError)?
                        .parse::<f32>()
                        .map_err(|_| ParseError)?,
                    bottom: border_values
                        .next()
                        .ok_or(ParseError)?
                        .parse::<f32>()
                        .map_err(|_| ParseError)?,
                    left: border_values
                        .next()
                        .ok_or(ParseError)?
                        .parse::<f32>()
                        .map_err(|_| ParseError)?,
                },
                alignment: BorderAlignment::parse(border_values.next().ok_or(ParseError)?)?,
                fill: Fill::parse(&border_values.collect::<Vec<&str>>().join(" "))
                    .map_err(|_| ParseError)?,
            },
            _ => return Err(ParseError),
        })
    }
}

impl fmt::Display for Border {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} {} {}", self.width, self.alignment, self.fill,)
    }
}

impl Scaled for Border {
    fn scale(&mut self, scale_factor: f32) {
        self.width.scale(scale_factor);
    }
}