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
use std::cmp::Ordering;

#[cfg(feature = "macros")]
#[macro_use]
mod macros;

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum Identifier {
    Numeric(u64),
    Alphanumeric(&'static str),
}

#[derive(Clone, Eq, Debug)]
pub struct Version {
    pub major: u64,
    pub minor: u64,
    pub patch: u64,
    pub pre: &'static [Identifier],
    pub build: &'static [Identifier],
}

impl Version {
    pub const fn new(major: u64, minor: u64, patch: u64) -> Self {
        Self {
            major,
            minor,
            patch,
            pre: &[],
            build: &[],
        }
    }

    pub const fn pre(self, identifiers: &'static [Identifier]) -> Self {
        Self {
            major: self.major,
            minor: self.minor,
            patch: self.patch,
            pre: identifiers,
            build: self.build,
        }
    }

    pub const fn build(self, build: &'static [Identifier]) -> Self {
        Self {
            major: self.major,
            minor: self.minor,
            patch: self.patch,
            pre: self.pre,
            build,
        }
    }
}

#[cfg(feature = "semver")]
impl From<&Identifier> for semver::Identifier {
    fn from(ident: &Identifier) -> Self {
        match ident {
            Identifier::Numeric(n) => semver::Identifier::Numeric(*n),
            Identifier::Alphanumeric(s) => semver::Identifier::AlphaNumeric(s.to_string()),
        }
    }
}

#[cfg(feature = "semver")]
impl From<Identifier> for semver::Identifier {
    fn from(ident: Identifier) -> Self {
        match ident {
            Identifier::Numeric(n) => semver::Identifier::Numeric(n),
            Identifier::Alphanumeric(s) => semver::Identifier::AlphaNumeric(s.to_string()),
        }
    }
}

#[cfg(feature = "semver")]
impl From<Version> for semver::Version {
    fn from(version: Version) -> semver::Version {
        semver::Version {
            major: version.major,
            minor: version.minor,
            patch: version.patch,
            pre: version.pre.iter().map(From::from).collect(),
            build: version.build.iter().map(From::from).collect(),
        }
    }
}

impl std::hash::Hash for Version {
    fn hash<H: std::hash::Hasher>(&self, into: &mut H) {
        self.major.hash(into);
        self.minor.hash(into);
        self.patch.hash(into);
        self.pre.hash(into);
    }
}

impl std::cmp::PartialEq for Version {
    #[inline]
    fn eq(&self, other: &Version) -> bool {
        self.major == other.major
            && self.minor == other.minor
            && self.patch == other.patch
            && self.pre == other.pre
    }
}

impl std::cmp::PartialOrd for Version {
    fn partial_cmp(&self, other: &Version) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl std::cmp::Ord for Version {
    fn cmp(&self, other: &Version) -> Ordering {
        match self.major.cmp(&other.major) {
            Ordering::Equal => {}
            r => return r,
        }

        match self.minor.cmp(&other.minor) {
            Ordering::Equal => {}
            r => return r,
        }

        match self.patch.cmp(&other.patch) {
            Ordering::Equal => {}
            r => return r,
        }

        // NB: semver spec says 0.0.0-pre < 0.0.0
        // but the version of ord defined for vec
        // says that [] < [pre] so we alter it here
        match (self.pre.len(), other.pre.len()) {
            (0, 0) => Ordering::Equal,
            (0, _) => Ordering::Greater,
            (_, 0) => Ordering::Less,
            (_, _) => self.pre.cmp(&other.pre),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{Identifier, Version};

    #[test]
    fn build() {
        let constructed = Version::new(1, 2, 3)
            .pre(&[Identifier::Alphanumeric("alpha")])
            .build(&[Identifier::Alphanumeric("amd64")]);

        let with_build = Version {
            major: 1,
            minor: 2,
            patch: 3,
            pre: &[Identifier::Alphanumeric("alpha")],
            build: &[Identifier::Alphanumeric("amd64")],
        };

        let without_build = Version {
            major: 1,
            minor: 2,
            patch: 3,
            pre: &[Identifier::Alphanumeric("alpha")],
            build: &[],
        };

        assert_eq!(with_build, constructed);
        assert_eq!(without_build, constructed);
    }

    #[test]
    fn convert() {
        let version = semver::Version::parse("1.2.3-alpha.3+amd64").unwrap();

        let const_correct: semver::Version = Version::new(1, 2, 3)
            .pre(&[Identifier::Alphanumeric("alpha"), Identifier::Numeric(3)])
            .build(&[Identifier::Alphanumeric("amd64")])
            .into();

        assert_eq!(version, const_correct);
    }

    #[test]
    #[cfg(feature = "macros")]
    fn test_macro() {
        let handmade = Version {
            major: 1,
            minor: 2,
            patch: 3,
            pre: &[Identifier::Alphanumeric("alpha"), Identifier::Numeric(4)],
            build: &[],
        };

        let version = version!(1, 2, 3 - alpha, 4);

        assert_eq!(version, handmade);
    }
}