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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use serde::{Deserialize, Serialize};

// version eg: v1.2.3-RC
#[derive(Serialize, Deserialize, PartialEq)]
pub struct Version {
    major: u8,
    minor: u8,
    patch: u8,
    build: Option<String>,
}

const SUFFIX_CANDIDATE: [&str; 2] = ["M", "RC"];

impl Version {
    pub fn new(version_str: &str) -> Result<Version, &'static str> {
        let vp: Vec<&str> = version_str.split(".").collect();
        if vp.len() != 3 {
            return Err("version should be like 1.0.0 or 1.2.3 or 1.2.3-RC");
        }

        let path_with_build = vp.get(2).unwrap();
        let vb: Vec<&str> = path_with_build.split("-").collect();
        if vb.len() > 2 {
            return Err("version should be like 1.0.0 or 1.2.3 or 1.2.3-RC");
        }

        let major_with_prefix = vp.get(0).unwrap();
        let mut major_str = major_with_prefix.to_string();
        if major_str.starts_with("v") {
            major_str = major_str.to_uppercase().replace("V", "");
        }

        let version = Version {
            major: major_str.parse().unwrap(),
            minor: vp.get(1).unwrap().parse().unwrap(),
            patch: vb.get(0).unwrap().parse().unwrap(),
            build: if vb.len() == 2 {
                Some(vb.get(1).unwrap().to_string())
            } else {
                None
            },
        };

        if version.build.is_some() {
            let suffix = version.build.as_ref().unwrap();
            let mut build_valid = false;
            for suf in SUFFIX_CANDIDATE {
                if suffix.starts_with(suf) {
                    build_valid = true;
                }
            }
            if !build_valid {
                return Err("Version suffix should be like RC1 or RC2");
            }
        }
        Ok(version)
    }

    fn compare(&self, comparison: &Version) -> i8 {
        let self_v_num = self.major * 100 + self.minor * 10 + self.patch;
        let comp_v_num = comparison.major * 100 + comparison.minor * 10 + comparison.patch;
        if self_v_num > comp_v_num {
            return 1;
        }

        if self_v_num < comp_v_num {
            return -1;
        }

        // must compare the build
        if self.build.is_some() && comparison.build.is_none() {
            return -1;
        }

        if self.build.is_none() && comparison.build.is_some() {
            return 1;
        }

        if self.build.is_none() && comparison.build.is_none() {
            return 0;
        }

        let self_build_v: u8 = self
            .build
            .as_ref()
            .unwrap()
            .replace("RC", "0")
            .parse()
            .unwrap();
        let comparison_build_v: u8 = comparison
            .build
            .as_ref()
            .unwrap()
            .replace("RC", "0")
            .parse()
            .unwrap();

        return if self_build_v == comparison_build_v {
            0
        } else if self_build_v > comparison_build_v {
            1
        } else {
            -1
        };
    }

    pub fn gt(&self, com: &Version) -> bool {
        if self.compare(com) == 1 {
            true
        } else {
            false
        }
    }

    pub fn eq(&self, com: &Version) -> bool {
        if self.compare(com) == 0 {
            true
        } else {
            false
        }
    }

    pub fn lt(&self, com: &Version) -> bool {
        !self.gt(com)
    }
}

#[cfg(test)]
mod version_unit_test {
    use crate::version::Version;

    #[test]
    fn test_parse_version() {
        let v = Version::new("1.2.3").unwrap();
        assert_eq!(v.major, 1);
        assert_eq!(v.minor, 2);
        assert_eq!(v.patch, 3);
    }

    #[test]
    fn test_parse_version_with_invalid() {
        let v = Version::new("r1.2.3");
        assert_eq!(v.is_err(), true);

        let v2 = Version::new("1.2.3.RC");
        assert_eq!(v2.is_err(), true);

        let v3 = Version::new("1.2.3-RCX");
        assert_eq!(v3.is_err(), true);

        let v4 = Version::new("1.2.3.4-RC");
        assert_eq!(v4.is_err(), true);
    }

    #[test]
    fn test_parse_version_with_build() {
        let v = Version::new("1.2.3-RC").unwrap();
        assert_eq!(v.major, 1);
        assert_eq!(v.minor, 2);
        assert_eq!(v.patch, 3);
        assert_eq!(v.build, Some("RC".to_string()));
    }

    #[test]
    fn test_parse_version_with_prefix() {
        let v = Version::new("v1.2.3").unwrap();
        assert_eq!(v.major, 1);
        assert_eq!(v.minor, 2);
        assert_eq!(v.patch, 3);
    }

    #[test]
    fn test_parse_version_with_prefix_build() {
        let v = Version::new("v1.2.3-RC").unwrap();
        assert_eq!(v.major, 1);
        assert_eq!(v.minor, 2);
        assert_eq!(v.patch, 3);
        assert_eq!(v.build, Some("RC".to_string()))
    }

    #[test]
    fn test_version_newer() {
        let v = Version::new("1.2.3").unwrap();
        let v2 = Version::new("2.2.3").unwrap();
        assert_eq!(v.compare(&v2), -1);

        let v3 = Version::new("1.3.3").unwrap();
        assert_eq!(v.compare(&v3), -1);

        let v4 = Version::new("1.2.4").unwrap();
        assert_eq!(v.compare(&v4), -1);
    }

    #[test]
    fn test_version_newer_with_prefix() {
        let v = Version::new("v1.2.3").unwrap();
        let v2 = Version::new("v2.2.3").unwrap();
        assert_eq!(v.compare(&v2), -1);

        let v3 = Version::new("v1.3.3").unwrap();
        assert_eq!(v.compare(&v3), -1);

        let v4 = Version::new("1.2.4").unwrap();
        assert_eq!(v.compare(&v4), -1);
    }

    #[test]
    fn test_version_newer_with_build() {
        let v = Version::new("v1.2.3").unwrap();
        let v2 = Version::new("v2.2.3").unwrap();
        assert_eq!(v.compare(&v2), -1);

        let v3 = Version::new("v1.3.3").unwrap();
        assert_eq!(v.compare(&v3), -1);

        let v4 = Version::new("1.2.4").unwrap();
        assert_eq!(v.compare(&v4), -1);

        let v5 = Version::new("1.2.3-RC").unwrap();
        assert_eq!(v.compare(&v5), 1);

        let v6 = Version::new("1.2.3-RC1").unwrap();
        assert_eq!(v6.compare(&v5), 1);

        let v7 = Version::new("1.2.3-RC2").unwrap();
        assert_eq!(v7.compare(&v6), 1);
    }

    #[test]
    fn test_version_equal() {
        let _v = Version::new("1.2.3").unwrap();
        let _v1 = Version::new("v1.2.3").unwrap();
    }
}