interface_rs/interface/
method.rs

1use std::fmt;
2use std::str::FromStr;
3
4/// Represents the configuration method in the `interfaces(5)` file.
5///
6/// The `Method` enum constrains the `method` field to known values, while still
7/// allowing custom methods through the `Other` variant.
8///
9/// # Variants
10///
11/// - `Static`: Static IP configuration.
12/// - `Dhcp`: DHCP client configuration.
13/// - `Loopback`: Loopback interface configuration.
14/// - `Manual`: Manual configuration (no automatic setup).
15/// - `Other`: Any other method not explicitly supported.
16///
17/// # Examples
18///
19/// Parsing a `Method` from a string:
20///
21/// ```rust
22/// use interface_rs::interface::Method;
23/// use std::str::FromStr;
24///
25/// let method = Method::from_str("dhcp").unwrap();
26/// assert_eq!(method, Method::Dhcp);
27///
28/// // Unknown methods are captured as Other
29/// let custom = Method::from_str("ppp").unwrap();
30/// assert_eq!(custom, Method::Other("ppp".to_string()));
31/// ```
32#[derive(Debug, Clone, PartialEq)]
33pub enum Method {
34    /// Static IP configuration.
35    Static,
36    /// DHCP client configuration.
37    Dhcp,
38    /// Loopback interface configuration.
39    Loopback,
40    /// Manual configuration (no automatic setup).
41    Manual,
42    /// Any other method not explicitly supported.
43    Other(String),
44}
45
46impl fmt::Display for Method {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        let method_str = match self {
49            Method::Static => "static",
50            Method::Dhcp => "dhcp",
51            Method::Loopback => "loopback",
52            Method::Manual => "manual",
53            Method::Other(s) => s.as_str(),
54        };
55        write!(f, "{}", method_str)
56    }
57}
58
59impl FromStr for Method {
60    type Err = std::convert::Infallible;
61
62    /// Parses a `Method` from a string slice.
63    ///
64    /// This implementation never fails - unknown methods are captured as `Other`.
65    fn from_str(s: &str) -> Result<Self, Self::Err> {
66        Ok(match s {
67            "static" => Method::Static,
68            "dhcp" => Method::Dhcp,
69            "loopback" => Method::Loopback,
70            "manual" => Method::Manual,
71            other => Method::Other(other.to_string()),
72        })
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn test_method_from_str() {
82        assert_eq!(Method::from_str("static").unwrap(), Method::Static);
83        assert_eq!(Method::from_str("dhcp").unwrap(), Method::Dhcp);
84        assert_eq!(Method::from_str("loopback").unwrap(), Method::Loopback);
85        assert_eq!(Method::from_str("manual").unwrap(), Method::Manual);
86        assert_eq!(
87            Method::from_str("ppp").unwrap(),
88            Method::Other("ppp".to_string())
89        );
90    }
91
92    #[test]
93    fn test_method_display() {
94        assert_eq!(Method::Static.to_string(), "static");
95        assert_eq!(Method::Dhcp.to_string(), "dhcp");
96        assert_eq!(Method::Loopback.to_string(), "loopback");
97        assert_eq!(Method::Manual.to_string(), "manual");
98        assert_eq!(Method::Other("ppp".to_string()).to_string(), "ppp");
99    }
100}