kpdb/types/
string_key.rs

1// Copyright (c) 2016-2017 Martijn Rijkeboer <[email protected]>
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9/// A key for the map with strings.
10#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
11pub enum StringKey {
12    /// The value is a notes string.
13    Notes,
14
15    /// The value is an other string with specified name.
16    Other(String),
17
18    /// The value is a password string.
19    Password,
20
21    /// The value is a title string.
22    Title,
23
24    /// The value is a URL string.
25    Url,
26
27    /// The value is a username string.
28    Username,
29}
30
31impl StringKey {
32    /// Create a string key from a string.
33    ///
34    /// # Examples
35    ///
36    /// ```rust
37    /// use kpdb::StringKey;
38    ///
39    /// let notes_key = StringKey::from_string("Notes");
40    /// let password_key = StringKey::from_string("Password");
41    /// let title_key = StringKey::from_string("Title");
42    /// let url_key = StringKey::from_string("URL");
43    /// let username_key = StringKey::from_string("UserName");
44    /// let other_key = StringKey::from_string("Foo");
45    /// ```
46    pub fn from_string(string: &str) -> StringKey {
47        match string.to_lowercase().as_str() {
48            "notes" => StringKey::Notes,
49            "password" => StringKey::Password,
50            "title" => StringKey::Title,
51            "url" => StringKey::Url,
52            "username" => StringKey::Username,
53            _ => StringKey::Other(String::from(string)),
54        }
55    }
56
57    /// Gets the string representation of the string key.
58    ///
59    /// # Examples
60    ///
61    /// ```
62    /// use kpdb::StringKey;
63    ///
64    /// let notes_key = StringKey::Notes;
65    /// let string = notes_key.to_string();
66    /// ```
67    pub fn to_string(&self) -> String {
68        match *self {
69            StringKey::Notes => String::from("Notes"),
70            StringKey::Other(ref string) => string.clone(),
71            StringKey::Password => String::from("Password"),
72            StringKey::Title => String::from("Title"),
73            StringKey::Url => String::from("URL"),
74            StringKey::Username => String::from("UserName"),
75        }
76    }
77}
78
79#[cfg(test)]
80mod tests {
81
82    use super::*;
83
84    #[test]
85    fn test_from_string_returns_correct_string_key() {
86        for tuple in get_test_tuples() {
87            let expected = tuple.1;
88            let actual = StringKey::from_string(tuple.0);
89            assert_eq!(actual, expected);
90        }
91    }
92
93    #[test]
94    fn test_to_string_returns_correct_string() {
95        for tuple in get_test_tuples() {
96            let expected = tuple.0;
97            let actual = tuple.1.to_string();
98            assert_eq!(actual, expected);
99        }
100    }
101
102    fn get_test_tuples() -> Vec<(&'static str, StringKey)> {
103        vec![
104            ("Notes", StringKey::Notes),
105            ("Password", StringKey::Password),
106            ("Title", StringKey::Title),
107            ("URL", StringKey::Url),
108            ("UserName", StringKey::Username),
109            ("FooBar", StringKey::Other(String::from("FooBar"))),
110        ]
111    }
112}