Skip to main content

crabular/
vertical_alignment.rs

1/// Vertical alignment for multi-line cells
2#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
3pub enum VerticalAlignment {
4    /// Align content to the top of the cell (default)
5    #[default]
6    Top,
7    /// Align content to the middle of the cell
8    Middle,
9    /// Align content to the bottom of the cell
10    Bottom,
11}
12
13impl core::str::FromStr for VerticalAlignment {
14    type Err = ();
15
16    fn from_str(s: &str) -> Result<Self, Self::Err> {
17        match s.to_lowercase().as_str() {
18            "top" | "t" => Ok(VerticalAlignment::Top),
19            "middle" | "m" | "center" => Ok(VerticalAlignment::Middle),
20            "bottom" | "b" => Ok(VerticalAlignment::Bottom),
21            _ => Err(()),
22        }
23    }
24}
25
26#[cfg(test)]
27mod tests {
28    use crate::VerticalAlignment;
29
30    #[test]
31    fn variants() {
32        let cases = [
33            (VerticalAlignment::Top, VerticalAlignment::Top, true),
34            (VerticalAlignment::Middle, VerticalAlignment::Middle, true),
35            (VerticalAlignment::Bottom, VerticalAlignment::Bottom, true),
36            (VerticalAlignment::Top, VerticalAlignment::Middle, false),
37            (VerticalAlignment::Middle, VerticalAlignment::Bottom, false),
38        ];
39        for (a, b, expected) in cases {
40            assert_eq!(a == b, expected);
41        }
42    }
43
44    #[test]
45    fn default_is_top() {
46        assert_eq!(VerticalAlignment::default(), VerticalAlignment::Top);
47    }
48
49    #[test]
50    fn copy_trait() {
51        let alignment = VerticalAlignment::Middle;
52        let copied = alignment;
53        assert_eq!(alignment, copied);
54    }
55
56    #[test]
57    fn debug_trait() {
58        assert_eq!(format!("{:?}", VerticalAlignment::Top), "Top");
59        assert_eq!(format!("{:?}", VerticalAlignment::Middle), "Middle");
60        assert_eq!(format!("{:?}", VerticalAlignment::Bottom), "Bottom");
61    }
62
63    #[test]
64    fn from_str() {
65        assert_eq!("top".parse(), Ok(VerticalAlignment::Top));
66        assert_eq!("t".parse(), Ok(VerticalAlignment::Top));
67        assert_eq!("middle".parse(), Ok(VerticalAlignment::Middle));
68        assert_eq!("m".parse(), Ok(VerticalAlignment::Middle));
69        assert_eq!("center".parse(), Ok(VerticalAlignment::Middle));
70        assert_eq!("bottom".parse(), Ok(VerticalAlignment::Bottom));
71        assert_eq!("b".parse(), Ok(VerticalAlignment::Bottom));
72        assert_eq!("invalid".parse::<VerticalAlignment>(), Err(()));
73    }
74}