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
use tracing::warn;

use super::error::VersionParseError;

/// Represents a mod's version, in (limited) semver format.
///
/// # Examples
///
/// ```
/// use facti_lib::version::Version;
///
/// let my_version = Version { major: 1, minor: 2, patch: 3 };
///
/// println!("My version is: {}", my_version);
/// ```
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Version {
    pub major: u64,
    pub minor: u64,
    pub patch: u64,
}

impl Version {
    pub fn new(major: u64, minor: u64, patch: u64) -> Self {
        Self {
            major,
            minor,
            patch,
        }
    }

    pub fn parse(s: &str) -> Result<Self, VersionParseError> {
        s.parse()
    }

    pub fn matches(&self, spec: VersionSpec) -> bool {
        spec.matches(*self)
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct FactorioVersion {
    pub major: u64,
    pub minor: u64,
    pub(crate) patch: Option<u64>,
}

impl FactorioVersion {
    pub fn new(major: u64, minor: u64) -> Self {
        Self {
            major,
            minor,
            patch: None,
        }
    }

    pub fn parse(s: &str) -> Result<Self, VersionParseError> {
        s.parse()
    }

    /// Constructs a potentially invalid Factorio version, which may include
    /// a patch version.
    ///
    /// Normally this should not be possible, but some mods on the portal have
    /// a patch version specified and will fail to parse if we don't allow it.
    pub(crate) fn create(major: u64, minor: u64, patch: Option<u64>) -> Self {
        if patch.is_some() {
            warn!(
                "Constructing invalid Factorio version: {}.{}.{:?}",
                major, minor, patch
            );
        }

        Self {
            major,
            minor,
            patch,
        }
    }
}

impl Default for FactorioVersion {
    fn default() -> Self {
        Self {
            major: 0,
            minor: 12,
            patch: None,
        }
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Op {
    Exact,
    Greater,
    GreaterEq,
    Less,
    LessEq,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum VersionReq {
    Latest,
    Spec(VersionSpec),
}

impl VersionReq {
    pub fn parse(s: &str) -> Result<Self, VersionParseError> {
        s.parse()
    }
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct VersionSpec {
    pub op: Op,
    pub version: Version,
}

impl VersionSpec {
    pub fn parse(s: &str) -> Result<Self, VersionParseError> {
        s.parse()
    }

    pub fn matches(&self, version: Version) -> bool {
        match self.op {
            Op::Exact => self.version == version,
            Op::Greater => self.version < version,
            Op::GreaterEq => self.version <= version,
            Op::Less => self.version > version,
            Op::LessEq => self.version >= version,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse() {
        assert_eq!(
            FactorioVersion::parse("1.80").unwrap(),
            FactorioVersion::new(1, 80)
        );
    }

    #[test]
    fn test_display() {
        assert_eq!(format!("{}", FactorioVersion::new(1, 2)), "1.2");
    }

    #[test]
    fn test_ordering() {
        let mut major_differs = vec![FactorioVersion::new(5, 1), FactorioVersion::new(1, 2)];
        major_differs.sort();
        assert_eq!(
            major_differs,
            vec![FactorioVersion::new(1, 2), FactorioVersion::new(5, 1)]
        );
        let mut minor_differs = vec![FactorioVersion::new(1, 5), FactorioVersion::new(1, 2)];
        minor_differs.sort();
        assert_eq!(
            minor_differs,
            vec![FactorioVersion::new(1, 2), FactorioVersion::new(1, 5)]
        );
    }

    macro_rules! test_specs {
        ($($name:ident($spec:literal, $version:literal, $expected:expr);)*) => {
            $(
                #[test]
                fn $name() {
                    let spec = VersionSpec::parse($spec).unwrap();
                    let version = Version::parse($version).unwrap();
                    assert_eq!(spec.matches(version), $expected, "expected {} when matching {version} against {spec}", $expected);
                }
            )*
        };
    }

    test_specs! {
        same_version_matches_exact("= 1.2.3", "1.2.3", true);
        diff_major_does_not_match_exact("= 1.2.3", "2.2.3", false);
        diff_minor_does_not_match_exact("= 1.2.3", "1.3.3", false);
        diff_patch_does_not_match_exact("= 1.2.3", "1.2.4", false);
        larger_major_matches_greater("> 1.2.3", "5.2.3", true);
        larger_minor_matches_greater("> 1.2.3", "1.5.3", true);
        larger_patch_matches_greater("> 1.2.3", "1.2.5", true);
        smaller_major_does_not_match_greater("> 1.2.3", "0.2.3", false);
        smaller_minor_greater_major_matches_greater("> 1.2.3", "3.1.3", true);
        smaller_patch_greater_major_matches_greater("> 1.2.3", "4.1.0", true);
    }
}