azul-css 0.0.7

Common datatypes used for styling applications using the Azul desktop GUI framework
Documentation
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//! CSS properties for controlling fragmentation (page/column breaks).

use alloc::string::{String, ToString};
use core::num::ParseIntError;

use crate::props::formatter::PrintAsCssValue;

// --- break-before / break-after ---

/// Represents a `break-before` or `break-after` CSS property value.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub enum PageBreak {
    Auto,
    Avoid,
    Always,
    All,
    Page,
    AvoidPage,
    Left,
    Right,
    Recto,
    Verso,
    Column,
    AvoidColumn,
}

impl Default for PageBreak {
    fn default() -> Self {
        Self::Auto
    }
}

impl PrintAsCssValue for PageBreak {
    fn print_as_css_value(&self) -> String {
        String::from(match self {
            Self::Auto => "auto",
            Self::Avoid => "avoid",
            Self::Always => "always",
            Self::All => "all",
            Self::Page => "page",
            Self::AvoidPage => "avoid-page",
            Self::Left => "left",
            Self::Right => "right",
            Self::Recto => "recto",
            Self::Verso => "verso",
            Self::Column => "column",
            Self::AvoidColumn => "avoid-column",
        })
    }
}

// --- break-inside ---

/// Represents a `break-inside` CSS property value.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub enum BreakInside {
    Auto,
    Avoid,
    AvoidPage,
    AvoidColumn,
}

impl Default for BreakInside {
    fn default() -> Self {
        Self::Auto
    }
}

impl PrintAsCssValue for BreakInside {
    fn print_as_css_value(&self) -> String {
        String::from(match self {
            Self::Auto => "auto",
            Self::Avoid => "avoid",
            Self::AvoidPage => "avoid-page",
            Self::AvoidColumn => "avoid-column",
        })
    }
}

// --- widows / orphans ---

/// CSS `widows` property - minimum number of lines in a block container
/// that must be shown at the top of a page, region, or column.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct Widows {
    pub inner: u32,
}

impl Default for Widows {
    fn default() -> Self {
        Self { inner: 2 }
    }
}

impl PrintAsCssValue for Widows {
    fn print_as_css_value(&self) -> String {
        self.inner.to_string()
    }
}

/// CSS `orphans` property - minimum number of lines in a block container
/// that must be shown at the bottom of a page, region, or column.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct Orphans {
    pub inner: u32,
}

impl Default for Orphans {
    fn default() -> Self {
        Self { inner: 2 }
    }
}

impl PrintAsCssValue for Orphans {
    fn print_as_css_value(&self) -> String {
        self.inner.to_string()
    }
}

// --- box-decoration-break ---

/// Represents a `box-decoration-break` CSS property value.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub enum BoxDecorationBreak {
    Slice,
    Clone,
}

impl Default for BoxDecorationBreak {
    fn default() -> Self {
        Self::Slice
    }
}

impl PrintAsCssValue for BoxDecorationBreak {
    fn print_as_css_value(&self) -> String {
        String::from(match self {
            Self::Slice => "slice",
            Self::Clone => "clone",
        })
    }
}

// Formatting to Rust code
impl crate::format_rust_code::FormatAsRustCode for PageBreak {
    fn format_as_rust_code(&self, _tabs: usize) -> String {
        match self {
            PageBreak::Auto => String::from("PageBreak::Auto"),
            PageBreak::Avoid => String::from("PageBreak::Avoid"),
            PageBreak::Always => String::from("PageBreak::Always"),
            PageBreak::All => String::from("PageBreak::All"),
            PageBreak::Page => String::from("PageBreak::Page"),
            PageBreak::AvoidPage => String::from("PageBreak::AvoidPage"),
            PageBreak::Left => String::from("PageBreak::Left"),
            PageBreak::Right => String::from("PageBreak::Right"),
            PageBreak::Recto => String::from("PageBreak::Recto"),
            PageBreak::Verso => String::from("PageBreak::Verso"),
            PageBreak::Column => String::from("PageBreak::Column"),
            PageBreak::AvoidColumn => String::from("PageBreak::AvoidColumn"),
        }
    }
}

impl crate::format_rust_code::FormatAsRustCode for BreakInside {
    fn format_as_rust_code(&self, _tabs: usize) -> String {
        match self {
            BreakInside::Auto => String::from("BreakInside::Auto"),
            BreakInside::Avoid => String::from("BreakInside::Avoid"),
            BreakInside::AvoidPage => String::from("BreakInside::AvoidPage"),
            BreakInside::AvoidColumn => String::from("BreakInside::AvoidColumn"),
        }
    }
}

impl crate::format_rust_code::FormatAsRustCode for Widows {
    fn format_as_rust_code(&self, _tabs: usize) -> String {
        format!("Widows {{ inner: {} }}", self.inner)
    }
}

impl crate::format_rust_code::FormatAsRustCode for Orphans {
    fn format_as_rust_code(&self, _tabs: usize) -> String {
        format!("Orphans {{ inner: {} }}", self.inner)
    }
}

impl crate::format_rust_code::FormatAsRustCode for BoxDecorationBreak {
    fn format_as_rust_code(&self, _tabs: usize) -> String {
        match self {
            BoxDecorationBreak::Slice => String::from("BoxDecorationBreak::Slice"),
            BoxDecorationBreak::Clone => String::from("BoxDecorationBreak::Clone"),
        }
    }
}

// --- PARSERS ---

#[cfg(feature = "parser")]
mod parser {
    use super::*;

    // -- PageBreak parser (`break-before`, `break-after`)

    #[derive(Clone, PartialEq)]
    pub enum PageBreakParseError<'a> {
        InvalidValue(&'a str),
    }

    impl_debug_as_display!(PageBreakParseError<'a>);
    impl_display! { PageBreakParseError<'a>, {
        InvalidValue(v) => format!("Invalid break value: \"{}\"", v),
    }}

    #[derive(Debug, Clone, PartialEq)]
    pub enum PageBreakParseErrorOwned {
        InvalidValue(String),
    }

    impl<'a> PageBreakParseError<'a> {
        pub fn to_contained(&self) -> PageBreakParseErrorOwned {
            match self {
                Self::InvalidValue(s) => PageBreakParseErrorOwned::InvalidValue(s.to_string()),
            }
        }
    }

    impl PageBreakParseErrorOwned {
        pub fn to_shared<'a>(&'a self) -> PageBreakParseError<'a> {
            match self {
                Self::InvalidValue(s) => PageBreakParseError::InvalidValue(s.as_str()),
            }
        }
    }

    pub fn parse_page_break<'a>(input: &'a str) -> Result<PageBreak, PageBreakParseError<'a>> {
        match input.trim() {
            "auto" => Ok(PageBreak::Auto),
            "avoid" => Ok(PageBreak::Avoid),
            "always" => Ok(PageBreak::Always),
            "all" => Ok(PageBreak::All),
            "page" => Ok(PageBreak::Page),
            "avoid-page" => Ok(PageBreak::AvoidPage),
            "left" => Ok(PageBreak::Left),
            "right" => Ok(PageBreak::Right),
            "recto" => Ok(PageBreak::Recto),
            "verso" => Ok(PageBreak::Verso),
            "column" => Ok(PageBreak::Column),
            "avoid-column" => Ok(PageBreak::AvoidColumn),
            _ => Err(PageBreakParseError::InvalidValue(input)),
        }
    }

    // -- BreakInside parser

    #[derive(Clone, PartialEq)]
    pub enum BreakInsideParseError<'a> {
        InvalidValue(&'a str),
    }

    impl_debug_as_display!(BreakInsideParseError<'a>);
    impl_display! { BreakInsideParseError<'a>, {
        InvalidValue(v) => format!("Invalid break-inside value: \"{}\"", v),
    }}

    #[derive(Debug, Clone, PartialEq)]
    pub enum BreakInsideParseErrorOwned {
        InvalidValue(String),
    }

    impl<'a> BreakInsideParseError<'a> {
        pub fn to_contained(&self) -> BreakInsideParseErrorOwned {
            match self {
                Self::InvalidValue(s) => BreakInsideParseErrorOwned::InvalidValue(s.to_string()),
            }
        }
    }

    impl BreakInsideParseErrorOwned {
        pub fn to_shared<'a>(&'a self) -> BreakInsideParseError<'a> {
            match self {
                Self::InvalidValue(s) => BreakInsideParseError::InvalidValue(s.as_str()),
            }
        }
    }

    pub fn parse_break_inside<'a>(
        input: &'a str,
    ) -> Result<BreakInside, BreakInsideParseError<'a>> {
        match input.trim() {
            "auto" => Ok(BreakInside::Auto),
            "avoid" => Ok(BreakInside::Avoid),
            "avoid-page" => Ok(BreakInside::AvoidPage),
            "avoid-column" => Ok(BreakInside::AvoidColumn),
            _ => Err(BreakInsideParseError::InvalidValue(input)),
        }
    }

    // -- Widows / Orphans parsers

    macro_rules! define_widow_orphan_parser {
        ($fn_name:ident, $struct_name:ident, $error_name:ident, $error_owned_name:ident, $prop_name:expr) => {
            #[derive(Clone, PartialEq)]
            pub enum $error_name<'a> {
                ParseInt(ParseIntError, &'a str),
                NegativeValue(&'a str),
            }

            impl_debug_as_display!($error_name<'a>);
            impl_display! { $error_name<'a>, {
                ParseInt(e, s) => format!("Invalid integer for {}: \"{}\". Reason: {}", $prop_name, s, e),
                NegativeValue(s) => format!("Invalid value for {}: \"{}\". Value cannot be negative.", $prop_name, s),
            }}

            #[derive(Debug, Clone, PartialEq)]
            pub enum $error_owned_name {
                ParseInt(String, String),
                NegativeValue(String),
            }

            impl<'a> $error_name<'a> {
                pub fn to_contained(&self) -> $error_owned_name {
                    match self {
                        Self::ParseInt(e, s) => $error_owned_name::ParseInt(e.to_string(), s.to_string()),
                        Self::NegativeValue(s) => $error_owned_name::NegativeValue(s.to_string()),
                    }
                }
            }

            impl $error_owned_name {
                pub fn to_shared<'a>(&'a self) -> $error_name<'a> {
                     match self {
                        // Can't reconstruct ParseIntError
                        Self::ParseInt(_e, s) => $error_name::NegativeValue(s),
                        Self::NegativeValue(s) => $error_name::NegativeValue(s),
                    }
                }
            }

            pub fn $fn_name<'a>(input: &'a str) -> Result<$struct_name, $error_name<'a>> {
                let trimmed = input.trim();
                let val: i32 = trimmed.parse().map_err(|e| $error_name::ParseInt(e, trimmed))?;
                if val < 0 {
                    return Err($error_name::NegativeValue(trimmed));
                }
                Ok($struct_name { inner: val as u32 })
            }
        };
    }

    define_widow_orphan_parser!(
        parse_widows,
        Widows,
        WidowsParseError,
        WidowsParseErrorOwned,
        "widows"
    );
    define_widow_orphan_parser!(
        parse_orphans,
        Orphans,
        OrphansParseError,
        OrphansParseErrorOwned,
        "orphans"
    );

    // -- BoxDecorationBreak parser

    #[derive(Clone, PartialEq)]
    pub enum BoxDecorationBreakParseError<'a> {
        InvalidValue(&'a str),
    }

    impl_debug_as_display!(BoxDecorationBreakParseError<'a>);
    impl_display! { BoxDecorationBreakParseError<'a>, {
        InvalidValue(v) => format!("Invalid box-decoration-break value: \"{}\"", v),
    }}

    #[derive(Debug, Clone, PartialEq)]
    pub enum BoxDecorationBreakParseErrorOwned {
        InvalidValue(String),
    }

    impl<'a> BoxDecorationBreakParseError<'a> {
        pub fn to_contained(&self) -> BoxDecorationBreakParseErrorOwned {
            match self {
                Self::InvalidValue(s) => {
                    BoxDecorationBreakParseErrorOwned::InvalidValue(s.to_string())
                }
            }
        }
    }

    impl BoxDecorationBreakParseErrorOwned {
        pub fn to_shared<'a>(&'a self) -> BoxDecorationBreakParseError<'a> {
            match self {
                Self::InvalidValue(s) => BoxDecorationBreakParseError::InvalidValue(s.as_str()),
            }
        }
    }

    pub fn parse_box_decoration_break<'a>(
        input: &'a str,
    ) -> Result<BoxDecorationBreak, BoxDecorationBreakParseError<'a>> {
        match input.trim() {
            "slice" => Ok(BoxDecorationBreak::Slice),
            "clone" => Ok(BoxDecorationBreak::Clone),
            _ => Err(BoxDecorationBreakParseError::InvalidValue(input)),
        }
    }
}

#[cfg(feature = "parser")]
pub use parser::*;

#[cfg(all(test, feature = "parser"))]
mod tests {
    use super::*;

    #[test]
    fn test_parse_page_break() {
        assert_eq!(parse_page_break("auto").unwrap(), PageBreak::Auto);
        assert_eq!(parse_page_break("page").unwrap(), PageBreak::Page);
        assert_eq!(
            parse_page_break("avoid-column").unwrap(),
            PageBreak::AvoidColumn
        );
        assert!(parse_page_break("invalid").is_err());
    }

    #[test]
    fn test_parse_break_inside() {
        assert_eq!(parse_break_inside("auto").unwrap(), BreakInside::Auto);
        assert_eq!(parse_break_inside("avoid").unwrap(), BreakInside::Avoid);
        assert!(parse_break_inside("always").is_err());
    }

    #[test]
    fn test_parse_widows_orphans() {
        assert_eq!(parse_widows("3").unwrap().inner, 3);
        assert_eq!(parse_orphans("  1  ").unwrap().inner, 1);
        assert!(parse_widows("-2").is_err());
        assert!(parse_orphans("auto").is_err());
    }

    #[test]
    fn test_parse_box_decoration_break() {
        assert_eq!(
            parse_box_decoration_break("slice").unwrap(),
            BoxDecorationBreak::Slice
        );
        assert_eq!(
            parse_box_decoration_break("clone").unwrap(),
            BoxDecorationBreak::Clone
        );
        assert!(parse_box_decoration_break("copy").is_err());
    }
}