schema_model/model/
version.rs1use std::cmp::Ordering;
2use std::fmt;
3
4#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
5pub struct Version {
6 major_version: i32,
7 minor_version: i32,
8 patch_version: i32,
9 pre_release_suffix: bool,
10}
11
12impl Version {
13 pub fn new(major_version: i32, minor_version: i32) -> Self {
14 Self {
15 major_version,
16 minor_version,
17 patch_version: 0,
18 pre_release_suffix: false,
19 }
20 }
21
22 pub fn with_patch(major_version: i32, minor_version: i32, patch_version: i32) -> Self {
23 Self {
24 major_version,
25 minor_version,
26 patch_version,
27 pre_release_suffix: false,
28 }
29 }
30
31 pub fn with_patch_and_suffix(
32 major_version: i32,
33 minor_version: i32,
34 patch_version: i32,
35 pre_release_suffix: bool,
36 ) -> Self {
37 Self {
38 major_version,
39 minor_version,
40 patch_version,
41 pre_release_suffix,
42 }
43 }
44
45 pub fn parse(version_str: &str) -> Self {
46 let mut s = version_str.trim();
48 let pre = s.contains("-SNAPSHOT");
49 if pre
50 && let Some(idx) = s.find("-SNAPSHOT") {
51 s = &s[..idx];
52 }
53 let parts: Vec<&str> = s.split('.').collect();
54 let major = parts.first()
55 .and_then(|p| p.parse::<i32>().ok())
56 .unwrap_or(0);
57 let minor = parts
58 .get(1)
59 .and_then(|p| p.parse::<i32>().ok())
60 .unwrap_or(0);
61 let patch = parts
62 .get(2)
63 .and_then(|p| p.parse::<i32>().ok())
64 .unwrap_or(0);
65 Self {
66 major_version: major,
67 minor_version: minor,
68 patch_version: patch,
69 pre_release_suffix: pre,
70 }
71 }
72
73 pub fn major_version(&self) -> i32 {
74 self.major_version
75 }
76 pub fn minor_version(&self) -> i32 {
77 self.minor_version
78 }
79 pub fn patch_version(&self) -> i32 {
80 self.patch_version
81 }
82 pub fn is_pre_release_suffix(&self) -> bool {
83 self.pre_release_suffix
84 }
85}
86
87impl fmt::Display for Version {
88 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89 if self.pre_release_suffix {
90 if self.patch_version < 1 {
91 write!(
92 f,
93 "{:02}.{:02}-SNAPSHOT",
94 self.major_version, self.minor_version
95 )
96 } else {
97 write!(
98 f,
99 "{:02}.{:02}.{:02}-SNAPSHOT",
100 self.major_version, self.minor_version, self.patch_version
101 )
102 }
103 } else if self.patch_version < 1 {
104 write!(f, "{:02}.{:02}", self.major_version, self.minor_version)
105 } else {
106 write!(
107 f,
108 "{:02}.{:02}.{:02}",
109 self.major_version, self.minor_version, self.patch_version
110 )
111 }
112 }
113}
114
115impl PartialOrd for Version {
116 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
117 Some(self.cmp(other))
118 }
119}
120
121impl Ord for Version {
122 fn cmp(&self, other: &Self) -> Ordering {
123 if std::ptr::eq(self, other) {
124 return Ordering::Equal;
125 }
126 match self.major_version.cmp(&other.major_version) {
127 Ordering::Equal => {}
128 ord => return ord,
129 }
130 match self.minor_version.cmp(&other.minor_version) {
131 Ordering::Equal => {}
132 ord => return ord,
133 }
134 match self.patch_version.cmp(&other.patch_version) {
135 Ordering::Equal => {}
136 ord => return ord,
137 }
138 match (self.pre_release_suffix, other.pre_release_suffix) {
139 (true, false) => Ordering::Greater,
140 (false, true) => Ordering::Less,
141 _ => Ordering::Equal,
142 }
143 }
144}
145
146impl From<&str> for Version {
147 fn from(value: &str) -> Self {
148 Version::parse(value)
149 }
150}
151
152#[cfg(test)]
153mod tests {
154 use super::*;
155
156 #[test]
157 fn parse_and_display() {
158 let v = Version::parse("1.2");
159 assert_eq!(v.major_version(), 1);
160 assert_eq!(v.minor_version(), 2);
161 assert_eq!(v.patch_version(), 0);
162 assert!(!v.is_pre_release_suffix());
163 assert_eq!(v.to_string(), "01.02");
164
165 let v2 = Version::parse("1.2.3-SNAPSHOT");
166 assert_eq!(v2.to_string(), "01.02.03-SNAPSHOT");
167 assert!(v2.is_pre_release_suffix());
168 }
169
170 #[test]
171 fn ordering_matches_java_logic() {
172 let a = Version::with_patch(1, 2, 0);
173 let b = Version::with_patch(1, 2, 1);
174 assert!(a < b);
175
176 let c = Version::with_patch_and_suffix(1, 2, 3, true);
177 let d = Version::with_patch_and_suffix(1, 2, 3, false);
178 assert!(c > d);
180 }
181}