#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum LayoutDirection {
#[default]
Ltr,
Rtl,
}
impl LayoutDirection {
#[must_use]
pub fn flipped(self) -> Self {
match self {
LayoutDirection::Ltr => LayoutDirection::Rtl,
LayoutDirection::Rtl => LayoutDirection::Ltr,
}
}
#[must_use]
pub fn start_is_left(self) -> bool {
matches!(self, LayoutDirection::Ltr)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Edge {
Start,
End,
}
impl Edge {
#[must_use]
pub fn is_left_under(self, direction: LayoutDirection) -> bool {
match (direction, self) {
(LayoutDirection::Ltr, Edge::Start) => true,
(LayoutDirection::Ltr, Edge::End) => false,
(LayoutDirection::Rtl, Edge::Start) => false,
(LayoutDirection::Rtl, Edge::End) => true,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn edge_mapping_is_consistent() {
assert!(Edge::Start.is_left_under(LayoutDirection::Ltr));
assert!(!Edge::End.is_left_under(LayoutDirection::Ltr));
assert!(!Edge::Start.is_left_under(LayoutDirection::Rtl));
assert!(Edge::End.is_left_under(LayoutDirection::Rtl));
}
#[test]
fn flipping_is_idempotent_twice() {
let d = LayoutDirection::Ltr;
assert_eq!(d, d.flipped().flipped());
}
}