sdp_rs/lines/attribute/
orientation.rs

1use std::convert::TryFrom;
2
3/// The `orient` attribute as it appears in the attribute line(s) (`a=`).
4#[derive(Debug, PartialEq, Eq, Ord, PartialOrd, Clone, Copy)]
5pub enum Orientation {
6    Portrait,
7    Landscape,
8    Seascape,
9}
10
11impl<'a> TryFrom<&'a str> for Orientation {
12    type Error = crate::Error;
13
14    fn try_from(from: &'a str) -> Result<Self, Self::Error> {
15        match from {
16            s if s.eq("portrait") => Ok(Self::Portrait),
17            s if s.eq("landscape") => Ok(Self::Landscape),
18            s if s.eq("seascape") => Ok(Self::Seascape),
19            s => Err(crate::Error::parser(
20                "orient attribute",
21                format!("unknown value `{}`", s),
22            )),
23        }
24    }
25}
26
27impl std::fmt::Display for Orientation {
28    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29        match self {
30            Self::Portrait => write!(f, "portrait"),
31            Self::Landscape => write!(f, "landscape"),
32            Self::Seascape => write!(f, "seascape"),
33        }
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn from_str1() {
43        assert_eq!(Orientation::try_from("portrait"), Ok(Orientation::Portrait));
44    }
45
46    #[test]
47    fn from_str2() {
48        assert!(Orientation::try_from("Portrait").is_err());
49    }
50
51    #[test]
52    fn display1() {
53        assert_eq!(Orientation::Seascape.to_string(), "seascape");
54    }
55}