feat(Direction): add Direction::perpendicular(self) (#2197)

This commit is contained in:
b-guild
2025-11-11 21:15:58 -08:00
committed by GitHub
parent d709b0380f
commit 90a77aaf8b

View File

@@ -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::<Direction>(), Ok(Direction::Vertical));
assert_eq!("".parse::<Direction>(), Err(ParseError::VariantNotFound));
}
#[test]
fn other() {
use Direction::*;
assert_eq!(Horizontal.perpendicular(), Vertical);
assert_eq!(Vertical.perpendicular(), Horizontal);
}
}