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
58
59
60
61
62
63
64
65
66
67
68
69
70
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Format {
XML,
JSON,
TXT,
CSV,
PDF,
EPUB,
MOBI,
}
impl fmt::Display for Format {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::Format::*;
write!(
f,
"{}",
match self {
XML => "xml",
JSON => "json",
TXT => "txt",
CSV => "csv",
PDF => "pdf",
EPUB => "epub",
MOBI => "mobi",
}
)
}
}
impl Format {
pub fn try_from(s: &str) -> Option<Self> {
use self::Format::*;
match s.to_lowercase().as_ref() {
"xml" => Some(XML),
"json" => Some(JSON),
"txt" => Some(TXT),
"csv" => Some(CSV),
"pdf" => Some(PDF),
"epub" => Some(EPUB),
"mobi" => Some(MOBI),
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_format_from_str() {
assert_eq!(Format::try_from("csv"), Some(Format::CSV));
assert_eq!(Format::try_from("Csv"), Some(Format::CSV));
assert_eq!(Format::try_from("EPUB"), Some(Format::EPUB));
assert_eq!(Format::try_from("EPUB_"), None);
assert_eq!(Format::try_from("wat"), None);
assert_eq!(Format::try_from(""), None);
}
}