1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use std::str::FromStr;

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum DebugFormat {
    /// Human readable text format
    Text,
    /// Machine readable JSON format
    Json,
}

impl FromStr for DebugFormat {
    type Err = ();

    fn from_str(val: &str) -> Result<Self, Self::Err> {
        match val {
            "text" => Ok(DebugFormat::Text),
            "json" => Ok(DebugFormat::Json),
            _ => Err(()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn from_str() {
        let opt: DebugFormat = "text".parse().unwrap();
        assert_eq!(opt, DebugFormat::Text);
        let opt: DebugFormat = "json".parse().unwrap();
        assert_eq!(opt, DebugFormat::Json);

        assert!("foo".parse::<DebugFormat>().is_err());
    }
}