Skip to main content

xript_ratatui/
layout.rs

1use ratatui::layout::{Constraint, Direction};
2
3pub fn parse_constraint(s: &str) -> Option<Constraint> {
4    let (kind, value) = s.split_once(':')?;
5
6    match kind {
7        "Length" => {
8            let n: u16 = value.parse().ok()?;
9            Some(Constraint::Length(n))
10        }
11        "Min" => {
12            let n: u16 = value.parse().ok()?;
13            Some(Constraint::Min(n))
14        }
15        "Max" => {
16            let n: u16 = value.parse().ok()?;
17            Some(Constraint::Max(n))
18        }
19        "Percentage" => {
20            let n: u16 = value.parse().ok()?;
21            Some(Constraint::Percentage(n))
22        }
23        "Ratio" => {
24            let (num, den) = value.split_once(',')?;
25            let num: u32 = num.trim().parse().ok()?;
26            let den: u32 = den.trim().parse().ok()?;
27            Some(Constraint::Ratio(num, den))
28        }
29        "Fill" => {
30            let n: u16 = value.parse().ok()?;
31            Some(Constraint::Fill(n))
32        }
33        _ => None,
34    }
35}
36
37pub fn parse_direction(s: &str) -> Direction {
38    match s {
39        "Horizontal" => Direction::Horizontal,
40        _ => Direction::Vertical,
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn parses_length_constraint() {
50        assert_eq!(parse_constraint("Length:3"), Some(Constraint::Length(3)));
51    }
52
53    #[test]
54    fn parses_min_constraint() {
55        assert_eq!(parse_constraint("Min:1"), Some(Constraint::Min(1)));
56    }
57
58    #[test]
59    fn parses_max_constraint() {
60        assert_eq!(parse_constraint("Max:10"), Some(Constraint::Max(10)));
61    }
62
63    #[test]
64    fn parses_percentage_constraint() {
65        assert_eq!(
66            parse_constraint("Percentage:50"),
67            Some(Constraint::Percentage(50))
68        );
69    }
70
71    #[test]
72    fn parses_ratio_constraint() {
73        assert_eq!(
74            parse_constraint("Ratio:1,3"),
75            Some(Constraint::Ratio(1, 3))
76        );
77    }
78
79    #[test]
80    fn parses_ratio_with_spaces() {
81        assert_eq!(
82            parse_constraint("Ratio:1, 3"),
83            Some(Constraint::Ratio(1, 3))
84        );
85    }
86
87    #[test]
88    fn parses_fill_constraint() {
89        assert_eq!(parse_constraint("Fill:1"), Some(Constraint::Fill(1)));
90    }
91
92    #[test]
93    fn returns_none_for_unknown_kind() {
94        assert_eq!(parse_constraint("Flex:1"), None);
95    }
96
97    #[test]
98    fn returns_none_for_missing_colon() {
99        assert_eq!(parse_constraint("Length3"), None);
100    }
101
102    #[test]
103    fn returns_none_for_invalid_number() {
104        assert_eq!(parse_constraint("Length:abc"), None);
105    }
106
107    #[test]
108    fn parses_vertical_direction() {
109        assert_eq!(parse_direction("Vertical"), Direction::Vertical);
110    }
111
112    #[test]
113    fn parses_horizontal_direction() {
114        assert_eq!(parse_direction("Horizontal"), Direction::Horizontal);
115    }
116
117    #[test]
118    fn defaults_to_vertical_for_unknown() {
119        assert_eq!(parse_direction("Diagonal"), Direction::Vertical);
120    }
121}