Skip to main content

surrealml_core/storage/header/
version.rs

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