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
use secstr::SecStr;
#[derive(Clone, Debug, PartialEq)]
pub enum StringValue {
Plain(String),
Protected(SecStr),
}
impl StringValue {
pub fn new<S: Into<String>>(value: S, protected: bool) -> StringValue {
if protected {
StringValue::Protected(SecStr::from(value.into()))
} else {
StringValue::Plain(value.into())
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use secstr::SecStr;
#[test]
fn test_new_with_plain_value_returns_correct_string_value() {
let value = "FooBar";
let expected = StringValue::Plain(String::from(value));
let actual = StringValue::new(value, false);
assert_eq!(actual, expected);
}
#[test]
fn test_new_with_protected_value_returns_correct_string_value() {
let value = "FooBar";
let expected = StringValue::Protected(SecStr::from(value));
let actual = StringValue::new(value, true);
assert_eq!(actual, expected);
}
}