nmd_core/
output_format.rs1use std::str::FromStr;
2use thiserror::Error;
3
4
5#[derive(Error, Debug)]
6pub enum OutputFormatError {
7 #[error("unsupported format: {0}")]
8 Unsupported(String)
9}
10
11#[derive(PartialEq, Debug, Default, Clone)]
13pub enum OutputFormat {
14 #[default]
15 Html
16}
17
18impl OutputFormat {
19 pub fn get_extension(&self) -> String {
20 match self {
21 OutputFormat::Html => String::from("html"),
22 }
23 }
24}
25
26impl FromStr for OutputFormat {
27
28 type Err = OutputFormatError;
29
30 fn from_str(format: &str) -> Result<Self, Self::Err> {
31 match format.to_lowercase().as_str() {
32 "html" => Ok(Self::Html),
33
34 _ => Err(OutputFormatError::Unsupported(String::from(format))),
35 }
36 }
37}
38
39#[cfg(test)]
40mod tests {
41
42 use super::*;
43
44 #[test]
45 fn html_support() {
46
47 match OutputFormat::from_str("html") {
48 Ok(format) => assert_eq!(format, OutputFormat::Html),
49 Err(err) => panic!("{}", err)
50 }
51 }
52
53 #[test]
54 fn unsupported_format() {
55 assert!(OutputFormat::from_str("htm").is_err())
56 }
57}