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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub enum StringKey {
Notes,
Other(String),
Password,
Title,
Url,
Username,
}
impl StringKey {
pub fn from_string(string: &str) -> StringKey {
match string.to_lowercase().as_str() {
"notes" => StringKey::Notes,
"password" => StringKey::Password,
"title" => StringKey::Title,
"url" => StringKey::Url,
"username" => StringKey::Username,
_ => StringKey::Other(String::from(string)),
}
}
pub fn to_string(&self) -> String {
match *self {
StringKey::Notes => String::from("Notes"),
StringKey::Other(ref string) => string.clone(),
StringKey::Password => String::from("Password"),
StringKey::Title => String::from("Title"),
StringKey::Url => String::from("URL"),
StringKey::Username => String::from("UserName"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_from_string_returns_correct_string_key() {
for tuple in get_test_tuples() {
let expected = tuple.1;
let actual = StringKey::from_string(tuple.0);
assert_eq!(actual, expected);
}
}
#[test]
fn test_to_string_returns_correct_string() {
for tuple in get_test_tuples() {
let expected = tuple.0;
let actual = tuple.1.to_string();
assert_eq!(actual, expected);
}
}
fn get_test_tuples() -> Vec<(&'static str, StringKey)> {
vec![
("Notes", StringKey::Notes),
("Password", StringKey::Password),
("Title", StringKey::Title),
("URL", StringKey::Url),
("UserName", StringKey::Username),
("FooBar", StringKey::Other(String::from("FooBar"))),
]
}
}