surrealml_core/storage/header/
version.rs

1//! Defines the process of managing the version of the `surml` file in the file.
2use crate::{
3    safe_eject_option,
4    safe_eject,
5    errors::error::{
6        SurrealError,
7        SurrealErrorStatus
8    }
9};
10
11
12/// The `Version` struct represents the version of the `surml` file.
13/// 
14/// # Fields
15/// * `one` - The first number in the version.
16/// * `two` - The second number in the version.
17/// * `three` - The third number in the version.
18#[derive(Debug, PartialEq)]
19pub struct Version {
20    pub one: u8,
21    pub two: u8,
22    pub three: u8,
23}
24
25
26impl Version {
27    
28    /// Creates a new `Version` struct with all zeros.
29    /// 
30    /// # Returns
31    /// A new `Version` struct with all zeros.
32    pub fn fresh() -> Self {
33        Version {
34            one: 0,
35            two: 0,
36            three: 0,
37        }
38    }
39
40    /// Translates the struct to a string.
41    /// 
42    /// # Returns
43    /// * `String` - The struct as a string.
44    pub fn to_string(&self) -> String {
45        if self.one == 0 && self.two == 0 && self.three == 0 {
46            return "".to_string();
47        }
48        format!("{}.{}.{}", self.one, self.two, self.three)
49    }
50
51    /// Creates a new `Version` struct from a string.
52    /// 
53    /// # Arguments
54    /// * `version` - The version as a string.
55    /// 
56    /// # Returns
57    /// A new `Version` struct.
58    pub fn from_string(version: String) -> Result<Self, SurrealError> {
59        if version == "".to_string() {
60            return Ok(Version::fresh())
61        }
62        let mut split = version.split(".");
63        let one_str = safe_eject_option!(split.next());
64        let two_str = safe_eject_option!(split.next());
65        let three_str = safe_eject_option!(split.next());
66
67        Ok(Version {
68            one: safe_eject!(one_str.parse::<u8>(), SurrealErrorStatus::BadRequest),
69            two: safe_eject!(two_str.parse::<u8>(), SurrealErrorStatus::BadRequest),
70            three: safe_eject!(three_str.parse::<u8>(), SurrealErrorStatus::BadRequest),
71        })
72    }
73
74    /// Increments the version by one.
75    pub fn increment(&mut self) {
76        self.three += 1;
77        if self.three == 10 {
78            self.three = 0;
79            self.two += 1;
80            if self.two == 10 {
81                self.two = 0;
82                self.one += 1;
83            }
84        }
85    }
86}
87
88
89#[cfg(test)]
90pub mod tests {
91
92    use super::*;
93
94    #[test]
95    fn test_from_string() {
96        let version = Version::from_string("0.0.0".to_string()).unwrap();
97        assert_eq!(version.one, 0);
98        assert_eq!(version.two, 0);
99        assert_eq!(version.three, 0);
100
101        let version = Version::from_string("1.2.3".to_string()).unwrap();
102        assert_eq!(version.one, 1);
103        assert_eq!(version.two, 2);
104        assert_eq!(version.three, 3);
105    }
106
107    #[test]
108    fn test_to_string() {
109        let version = Version{
110            one: 0,
111            two: 0,
112            three: 0,
113        };
114        assert_eq!(version.to_string(), "");
115
116        let version = Version{
117            one: 1,
118            two: 2,
119            three: 3,
120        };
121        assert_eq!(version.to_string(), "1.2.3");
122    }
123
124    #[test]
125    fn test_increment() {
126        let mut version = Version{
127            one: 0,
128            two: 0,
129            three: 0,
130        };
131        version.increment();
132        assert_eq!(version.to_string(), "0.0.1");
133
134        let mut version = Version{
135            one: 0,
136            two: 0,
137            three: 9,
138        };
139        version.increment();
140        assert_eq!(version.to_string(), "0.1.0");
141
142        let mut version = Version{
143            one: 0,
144            two: 9,
145            three: 9,
146        };
147        version.increment();
148        assert_eq!(version.to_string(), "1.0.0");
149
150        let mut version = Version{
151            one: 9,
152            two: 9,
153            three: 9,
154        };
155        version.increment();
156        assert_eq!(version.to_string(), "10.0.0");
157    }
158
159}