kpdb/types/
string_value.rs1use secstr::SecStr;
10
11#[derive(Clone, Debug, PartialEq)]
13pub enum StringValue {
14 Plain(String),
16
17 Protected(SecStr),
19}
20
21impl StringValue {
22 pub fn new<S: Into<String>>(value: S, protected: bool) -> StringValue {
33 if protected {
34 StringValue::Protected(SecStr::from(value.into()))
35 } else {
36 StringValue::Plain(value.into())
37 }
38 }
39}
40
41#[cfg(test)]
42mod tests {
43
44 use super::*;
45 use secstr::SecStr;
46
47 #[test]
48 fn test_new_with_plain_value_returns_correct_string_value() {
49 let value = "FooBar";
50 let expected = StringValue::Plain(String::from(value));
51 let actual = StringValue::new(value, false);
52 assert_eq!(actual, expected);
53 }
54
55 #[test]
56 fn test_new_with_protected_value_returns_correct_string_value() {
57 let value = "FooBar";
58 let expected = StringValue::Protected(SecStr::from(value));
59 let actual = StringValue::new(value, true);
60 assert_eq!(actual, expected);
61 }
62}