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
71
72
73
74
75
76
77
78
use std::{borrow::Cow, fmt::Debug, str::FromStr};
use thiserror::Error;
#[derive(Error, PartialEq, Debug)]
pub enum ResizeModeParsingError {
#[error("Unknown facing mode: {value}")]
UnknownValue { value: String },
}
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Debug)]
pub enum ResizeMode {
None,
CropAndScale,
}
impl FromStr for ResizeMode {
type Err = ResizeModeParsingError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut string = Cow::from(s);
if !s.chars().all(|c| c.is_lowercase()) {
string.to_mut().make_ascii_lowercase();
}
match string.as_ref() {
"none" => Ok(Self::None),
"crop-and-scale" => Ok(Self::CropAndScale),
_ => Err(Self::Err::UnknownValue {
value: s.to_owned(),
}),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const FACING_MODE: &'static str = "environment";
#[test]
fn from_str_success() {
let scenarios = [
("none", ResizeMode::None),
("crop-and-scale", ResizeMode::CropAndScale),
];
for (string, expected) in scenarios {
let actual = ResizeMode::from_str(string).unwrap();
assert_eq!(actual, expected);
}
}
#[test]
fn from_str_failure() {
let actual = ResizeMode::from_str("INVALID");
let expected = Err(ResizeModeParsingError::UnknownValue {
value: "INVALID".to_owned(),
});
assert_eq!(actual, expected);
}
}