-
Notifications
You must be signed in to change notification settings - Fork 108
/
method.rs
50 lines (43 loc) · 1.23 KB
/
method.rs
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
use crate::error::ParseError;
use std::fmt;
use std::str::FromStr;
#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Method {
Trigram,
Alphabet,
#[default]
Combined,
}
impl FromStr for Method {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().trim() {
"trigram" => Ok(Method::Trigram),
"alphabet" => Ok(Method::Alphabet),
"combined" => Ok(Method::Combined),
_ => Err(ParseError::Method(s.to_string())),
}
}
}
impl fmt::Display for Method {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let name = match self {
Method::Trigram => "Trigram",
Method::Alphabet => "Alphabet",
Method::Combined => "Combined",
};
write!(f, "{}", name)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_from_str() {
assert_eq!("trigram".parse::<Method>().unwrap(), Method::Trigram);
assert_eq!("ALPHABET".parse::<Method>().unwrap(), Method::Alphabet);
let result = "foobar".parse::<Method>();
assert!(result.is_err());
}
}