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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//! Defines the process of managing the version of the `surml` file in the file.
use crate::{
    safe_eject_option,
    safe_eject,
    errors::error::{
        SurrealError,
        SurrealErrorStatus
    }
};


/// The `Version` struct represents the version of the `surml` file.
/// 
/// # Fields
/// * `one` - The first number in the version.
/// * `two` - The second number in the version.
/// * `three` - The third number in the version.
#[derive(Debug, PartialEq)]
pub struct Version {
    pub one: u8,
    pub two: u8,
    pub three: u8,
}


impl Version {
    
    /// Creates a new `Version` struct with all zeros.
    /// 
    /// # Returns
    /// A new `Version` struct with all zeros.
    pub fn fresh() -> Self {
        Version {
            one: 0,
            two: 0,
            three: 0,
        }
    }

    /// Translates the struct to a string.
    /// 
    /// # Returns
    /// * `String` - The struct as a string.
    pub fn to_string(&self) -> String {
        if self.one == 0 && self.two == 0 && self.three == 0 {
            return "".to_string();
        }
        format!("{}.{}.{}", self.one, self.two, self.three)
    }

    /// Creates a new `Version` struct from a string.
    /// 
    /// # Arguments
    /// * `version` - The version as a string.
    /// 
    /// # Returns
    /// A new `Version` struct.
    pub fn from_string(version: String) -> Result<Self, SurrealError> {
        if version == "".to_string() {
            return Ok(Version::fresh())
        }
        let mut split = version.split(".");
        let one_str = safe_eject_option!(split.next());
        let two_str = safe_eject_option!(split.next());
        let three_str = safe_eject_option!(split.next());

        Ok(Version {
            one: safe_eject!(one_str.parse::<u8>(), SurrealErrorStatus::BadRequest),
            two: safe_eject!(two_str.parse::<u8>(), SurrealErrorStatus::BadRequest),
            three: safe_eject!(three_str.parse::<u8>(), SurrealErrorStatus::BadRequest),
        })
    }

    /// Increments the version by one.
    pub fn increment(&mut self) {
        self.three += 1;
        if self.three == 10 {
            self.three = 0;
            self.two += 1;
            if self.two == 10 {
                self.two = 0;
                self.one += 1;
            }
        }
    }
}


#[cfg(test)]
pub mod tests {

    use super::*;

    #[test]
    fn test_from_string() {
        let version = Version::from_string("0.0.0".to_string()).unwrap();
        assert_eq!(version.one, 0);
        assert_eq!(version.two, 0);
        assert_eq!(version.three, 0);

        let version = Version::from_string("1.2.3".to_string()).unwrap();
        assert_eq!(version.one, 1);
        assert_eq!(version.two, 2);
        assert_eq!(version.three, 3);
    }

    #[test]
    fn test_to_string() {
        let version = Version{
            one: 0,
            two: 0,
            three: 0,
        };
        assert_eq!(version.to_string(), "");

        let version = Version{
            one: 1,
            two: 2,
            three: 3,
        };
        assert_eq!(version.to_string(), "1.2.3");
    }

    #[test]
    fn test_increment() {
        let mut version = Version{
            one: 0,
            two: 0,
            three: 0,
        };
        version.increment();
        assert_eq!(version.to_string(), "0.0.1");

        let mut version = Version{
            one: 0,
            two: 0,
            three: 9,
        };
        version.increment();
        assert_eq!(version.to_string(), "0.1.0");

        let mut version = Version{
            one: 0,
            two: 9,
            three: 9,
        };
        version.increment();
        assert_eq!(version.to_string(), "1.0.0");

        let mut version = Version{
            one: 9,
            two: 9,
            three: 9,
        };
        version.increment();
        assert_eq!(version.to_string(), "10.0.0");
    }

}