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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use std::str::FromStr;
use thiserror::Error;


#[derive(Error, Debug)]
pub enum OutputFormatError {
    #[error("unsupported format: {0}")]
    Unsupported(String)
}

/// Set of supported formats
#[derive(PartialEq, Debug, Default, Clone)]
pub enum OutputFormat {
    #[default]
    Html
}

impl OutputFormat {
    pub fn get_extension(&self) -> String {
        match self {
            OutputFormat::Html => String::from("html"),
        }
    } 
}

impl FromStr for OutputFormat {

    type Err = OutputFormatError;

    fn from_str(format: &str) -> Result<Self, Self::Err> {
        match format.to_lowercase().as_str() {
            "html" => Ok(Self::Html),
            
            _ => Err(OutputFormatError::Unsupported(String::from(format))),
        }
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn html_support() {

        match OutputFormat::from_str("html") {
            Ok(format) => assert_eq!(format, OutputFormat::Html),
            Err(err) => panic!("{}", err)
        }
    }

    #[test]
    fn unsupported_format() {
        assert!(OutputFormat::from_str("htm").is_err())
    }
}