From 90a77aaf8b2bcb32a32ebd2d1a0f0be0d3b14fe1 Mon Sep 17 00:00:00 2001 From: b-guild <148972828+b-guild@users.noreply.github.com> Date: Tue, 11 Nov 2025 21:15:58 -0800 Subject: [PATCH] feat(Direction): add `Direction::perpendicular(self)` (#2197) --- ratatui-core/src/layout/direction.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/ratatui-core/src/layout/direction.rs b/ratatui-core/src/layout/direction.rs index 6acc4d98..c10546ae 100644 --- a/ratatui-core/src/layout/direction.rs +++ b/ratatui-core/src/layout/direction.rs @@ -12,11 +12,27 @@ use strum::{Display, EnumString}; #[derive(Debug, Default, Display, EnumString, Clone, Copy, Eq, PartialEq, Hash)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub enum Direction { + /// Layout segments are arranged side by side (left to right). Horizontal, + /// Layout segments are arranged top to bottom (default). #[default] Vertical, } +impl Direction { + /// The perpendicular direction to this direction. + /// + /// `Horizontal` returns `Vertical`, and `Vertical` returns `Horizontal`. + #[inline] + #[must_use = "returns the perpendicular direction"] + pub const fn perpendicular(self) -> Self { + match self { + Self::Horizontal => Self::Vertical, + Self::Vertical => Self::Horizontal, + } + } +} + #[cfg(test)] mod tests { use alloc::string::ToString; @@ -37,4 +53,11 @@ mod tests { assert_eq!("Vertical".parse::(), Ok(Direction::Vertical)); assert_eq!("".parse::(), Err(ParseError::VariantNotFound)); } + + #[test] + fn other() { + use Direction::*; + assert_eq!(Horizontal.perpendicular(), Vertical); + assert_eq!(Vertical.perpendicular(), Horizontal); + } }