ratatui_core/layout/
direction.rs

1use strum::{Display, EnumString};
2
3/// Defines the direction of a layout.
4///
5/// This enumeration is used with [`Layout`](crate::layout::Layout) to specify whether layout
6/// segments should be arranged horizontally or vertically.
7///
8/// - `Horizontal`: Layout segments are arranged side by side (left to right)
9/// - `Vertical`: Layout segments are arranged top to bottom (default)
10///
11/// For comprehensive layout documentation and examples, see the [`layout`](crate::layout) module.
12#[derive(Debug, Default, Display, EnumString, Clone, Copy, Eq, PartialEq, Hash)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14pub enum Direction {
15    /// Layout segments are arranged side by side (left to right).
16    Horizontal,
17    /// Layout segments are arranged top to bottom (default).
18    #[default]
19    Vertical,
20}
21
22impl Direction {
23    /// The perpendicular direction to this direction.
24    ///
25    /// `Horizontal` returns `Vertical`, and `Vertical` returns `Horizontal`.
26    #[inline]
27    #[must_use = "returns the perpendicular direction"]
28    pub const fn perpendicular(self) -> Self {
29        match self {
30            Self::Horizontal => Self::Vertical,
31            Self::Vertical => Self::Horizontal,
32        }
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use alloc::string::ToString;
39
40    use strum::ParseError;
41
42    use super::*;
43
44    #[test]
45    fn direction_to_string() {
46        assert_eq!(Direction::Horizontal.to_string(), "Horizontal");
47        assert_eq!(Direction::Vertical.to_string(), "Vertical");
48    }
49
50    #[test]
51    fn direction_from_str() {
52        assert_eq!("Horizontal".parse::<Direction>(), Ok(Direction::Horizontal));
53        assert_eq!("Vertical".parse::<Direction>(), Ok(Direction::Vertical));
54        assert_eq!("".parse::<Direction>(), Err(ParseError::VariantNotFound));
55    }
56
57    #[test]
58    fn other() {
59        use Direction::*;
60        assert_eq!(Horizontal.perpendicular(), Vertical);
61        assert_eq!(Vertical.perpendicular(), Horizontal);
62    }
63}