Skip to main content

sequoia_style/
text_style.rs

1use sequoia_core::Color;
2
3#[derive(Clone, Debug, Default, PartialEq)]
4pub struct TextStyle {
5    pub size: Option<f32>,
6
7    pub color: Option<Color>,
8
9    pub weight: Option<u16>,
10
11    pub family: Option<String>,
12
13    pub italic: Option<bool>,
14
15    pub letter_spacing: Option<f32>,
16
17    pub underline: Option<bool>,
18
19    pub strikethrough: Option<bool>,
20}
21
22impl TextStyle {
23    pub const fn new() -> Self {
24        Self {
25            size: None,
26            color: None,
27            weight: None,
28            family: None,
29            italic: None,
30            letter_spacing: None,
31            underline: None,
32            strikethrough: None,
33        }
34    }
35
36    pub const fn size(mut self, size: f32) -> Self {
37        self.size = Some(size);
38        self
39    }
40
41    pub const fn color(mut self, color: Color) -> Self {
42        self.color = Some(color);
43        self
44    }
45
46    pub const fn weight(mut self, weight: u16) -> Self {
47        self.weight = Some(weight);
48        self
49    }
50
51    pub fn family(mut self, family: impl Into<String>) -> Self {
52        self.family = Some(family.into());
53        self
54    }
55
56    pub const fn italic(mut self, on: bool) -> Self {
57        self.italic = Some(on);
58        self
59    }
60
61    pub const fn letter_spacing(mut self, spacing: f32) -> Self {
62        self.letter_spacing = Some(spacing);
63        self
64    }
65
66    pub const fn underline(mut self, on: bool) -> Self {
67        self.underline = Some(on);
68        self
69    }
70
71    pub const fn strikethrough(mut self, on: bool) -> Self {
72        self.strikethrough = Some(on);
73        self
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn const_composition_keeps_base() {
83        const TITLE: TextStyle = TextStyle::new()
84            .size(20.0)
85            .color(Color::WHITE)
86            .weight(700)
87            .italic(true);
88        assert_eq!(TITLE.size, Some(20.0));
89        assert_eq!(TITLE.color, Some(Color::WHITE));
90        assert_eq!(TITLE.weight, Some(700));
91        assert_eq!(TITLE.italic, Some(true));
92        assert_eq!(TITLE.family, None);
93        assert_eq!(TITLE.underline, None);
94    }
95
96    #[test]
97    fn family_accepts_owned_names() {
98        let family = String::from("SequoiaTest");
99        assert_eq!(TextStyle::new().family(family.clone()).family, Some(family));
100    }
101
102    #[test]
103    fn default_is_empty() {
104        assert_eq!(TextStyle::default(), TextStyle::new());
105    }
106}