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
//! Defines a generic string value for the header.


/// Defines a generic string value for the header.
/// 
/// # Fields
/// * `value` - The value of the string.
#[derive(Debug, PartialEq)]
pub struct StringValue {
    pub value: Option<String>,
}


impl StringValue {

    /// Creates a new string value with no value.
    /// 
    /// # Returns
    /// A new string value with no value.
    pub fn fresh() -> Self {
        StringValue {
            value: None,
        }
    }

    /// Creates a new string value from a string.
    /// 
    /// # Arguments
    /// * `value` - The value of the string.
    /// 
    /// # Returns
    /// A new string value.
    pub fn from_string(value: String) -> Self {
        match value.as_str() {
            "" => StringValue {
                value: None,
            },
            _ => StringValue {
                value: Some(value),
            },
        }
    }

    /// Converts the string value to a string.
    /// 
    /// # Returns
    /// The string value as a string.
    pub fn to_string(&self) -> String {
        match &self.value {
            Some(value) => value.to_string(),
            None => String::from(""),
        }
    }

}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_fresh() {
        let string_value = StringValue::fresh();
        assert_eq!(string_value, StringValue {
            value: None,
        });
    }

    #[test]
    fn test_from_string() {
        let string_value = StringValue::from_string(String::from("test"));
        assert_eq!(string_value, StringValue {
            value: Some(String::from("test")),
        });
    }

    #[test]
    fn test_from_string_none() {
        let string_value = StringValue::from_string(String::from(""));
        assert_eq!(string_value, StringValue {
            value: None,
        });
    }

    #[test]
    fn test_to_string() {
        let string_value = StringValue::from_string(String::from("test"));
        assert_eq!(string_value.to_string(), String::from("test"));
    }

    #[test]
    fn test_to_string_none() {
        let string_value = StringValue {
            value: None,
        };
        assert_eq!(string_value.to_string(), String::from(""));
    }
}