Skip to main content

leptos_chartistry/
edge.rs

1use std::str::FromStr;
2
3/// Identifies a rectangle edge.
4#[derive(Copy, Clone, Debug, PartialEq)]
5pub enum Edge {
6    /// The top edge.
7    Top,
8    /// The right edge.
9    Right,
10    /// The bottom edge.
11    Bottom,
12    /// The left edge.
13    Left,
14}
15
16impl Edge {
17    /// Returns true if the edge is horizontal (top and bottom).
18    pub fn is_horizontal(&self) -> bool {
19        match self {
20            Self::Top | Self::Bottom => true,
21            Self::Right | Self::Left => false,
22        }
23    }
24
25    /// Returns true if the edge is vertical (left and right).
26    pub fn is_vertical(&self) -> bool {
27        !self.is_horizontal()
28    }
29}
30
31impl std::fmt::Display for Edge {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        match self {
34            Self::Top => write!(f, "top"),
35            Self::Right => write!(f, "right"),
36            Self::Bottom => write!(f, "bottom"),
37            Self::Left => write!(f, "left"),
38        }
39    }
40}
41
42impl FromStr for Edge {
43    type Err = String;
44
45    fn from_str(s: &str) -> Result<Self, Self::Err> {
46        match s.to_lowercase().as_str() {
47            "top" => Ok(Self::Top),
48            "right" => Ok(Self::Right),
49            "bottom" => Ok(Self::Bottom),
50            "left" => Ok(Self::Left),
51            _ => Err(format!("unknown edge: `{}`", s)),
52        }
53    }
54}