semver.rs 10.3 KB
Newer Older
Z
Zack Corr 已提交
1 2 3 4 5 6 7 8 9 10 11 12
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Semver parsing and logic

13 14
#[allow(missing_doc)];

15 16
use core::prelude::*;

17
use core::iterator::IteratorUtil;
18
use core::char;
19
use core::cmp;
20 21 22 23 24 25
use core::io::{ReaderUtil};
use core::io;
use core::option::{Option, Some, None};
use core::str;
use core::to_str::ToStr;
use core::uint;
Z
Zack Corr 已提交
26

27
#[deriving(Eq)]
G
Graydon Hoare 已提交
28 29 30 31 32 33 34
pub enum Identifier {
    Numeric(uint),
    AlphaNumeric(~str)
}

impl cmp::Ord for Identifier {
    #[inline(always)]
35
    fn lt(&self, other: &Identifier) -> bool {
G
Graydon Hoare 已提交
36 37 38 39 40 41 42 43
        match (self, other) {
            (&Numeric(a), &Numeric(b)) => a < b,
            (&Numeric(_), _) => true,
            (&AlphaNumeric(ref a), &AlphaNumeric(ref b)) => *a < *b,
            (&AlphaNumeric(_), _) => false
        }
    }
    #[inline(always)]
44
    fn le(&self, other: &Identifier) -> bool {
G
Graydon Hoare 已提交
45 46 47
        ! (other < self)
    }
    #[inline(always)]
48
    fn gt(&self, other: &Identifier) -> bool {
G
Graydon Hoare 已提交
49 50 51
        other < self
    }
    #[inline(always)]
52
    fn ge(&self, other: &Identifier) -> bool {
G
Graydon Hoare 已提交
53 54 55 56 57 58
        ! (self < other)
    }
}

impl ToStr for Identifier {
    #[inline(always)]
59
    fn to_str(&self) -> ~str {
G
Graydon Hoare 已提交
60 61 62 63 64 65 66 67
        match self {
            &Numeric(n) => n.to_str(),
            &AlphaNumeric(ref s) => s.to_str()
        }
    }
}


68
#[deriving(Eq)]
Z
Zack Corr 已提交
69 70 71 72
pub struct Version {
    major: uint,
    minor: uint,
    patch: uint,
G
Graydon Hoare 已提交
73 74
    pre: ~[Identifier],
    build: ~[Identifier],
Z
Zack Corr 已提交
75 76
}

G
Graydon Hoare 已提交
77
impl ToStr for Version {
Z
Zack Corr 已提交
78
    #[inline(always)]
79
    fn to_str(&self) -> ~str {
G
Graydon Hoare 已提交
80 81 82 83 84
        let s = fmt!("%u.%u.%u", self.major, self.minor, self.patch);
        let s = if self.pre.is_empty() {
            s
        } else {
            s + "-" + str::connect(self.pre.map(|i| i.to_str()), ".")
Z
Zack Corr 已提交
85
        };
G
Graydon Hoare 已提交
86 87 88 89 90
        if self.build.is_empty() {
            s
        } else {
            s + "+" + str::connect(self.build.map(|i| i.to_str()), ".")
        }
Z
Zack Corr 已提交
91 92 93
    }
}

G
Graydon Hoare 已提交
94
impl cmp::Ord for Version {
95
    #[inline(always)]
96
    fn lt(&self, other: &Version) -> bool {
G
Graydon Hoare 已提交
97

98
        self.major < other.major ||
G
Graydon Hoare 已提交
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

            (self.major == other.major &&
             self.minor < other.minor) ||

            (self.major == other.major &&
             self.minor == other.minor &&
             self.patch < other.patch) ||

            (self.major == other.major &&
             self.minor == other.minor &&
             self.patch == other.patch &&
             // 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) => false,
                 (0, _) => false,
                 (_, 0) => true,
                 (_, _) => self.pre < other.pre
             })) ||

            (self.major == other.major &&
             self.minor == other.minor &&
             self.patch == other.patch &&
             self.pre == other.pre &&
             self.build < other.build)
126
    }
G
Graydon Hoare 已提交
127

128
    #[inline(always)]
129
    fn le(&self, other: &Version) -> bool {
G
Graydon Hoare 已提交
130
        ! (other < self)
131 132
    }
    #[inline(always)]
133
    fn gt(&self, other: &Version) -> bool {
G
Graydon Hoare 已提交
134
        other < self
135 136
    }
    #[inline(always)]
137
    fn ge(&self, other: &Version) -> bool {
G
Graydon Hoare 已提交
138
        ! (self < other)
139 140 141
    }
}

G
Graydon Hoare 已提交
142 143 144
condition! {
    bad_parse: () -> ();
}
Z
Zack Corr 已提交
145

146
fn take_nonempty_prefix(rdr: @io::Reader,
G
Graydon Hoare 已提交
147
                        ch: char,
148
                        pred: &fn(char) -> bool) -> (~str, char) {
G
Graydon Hoare 已提交
149 150 151 152 153
    let mut buf = ~"";
    let mut ch = ch;
    while pred(ch) {
        str::push_char(&mut buf, ch);
        ch = rdr.read_char();
Z
Zack Corr 已提交
154
    }
G
Graydon Hoare 已提交
155 156 157 158 159
    if buf.is_empty() {
        bad_parse::cond.raise(())
    }
    debug!("extracted nonempty prefix: %s", buf);
    (buf, ch)
Z
Zack Corr 已提交
160 161
}

162
fn take_num(rdr: @io::Reader, ch: char) -> (uint, char) {
G
Graydon Hoare 已提交
163 164 165 166 167 168
    let (s, ch) = take_nonempty_prefix(rdr, ch, char::is_digit);
    match uint::from_str(s) {
        None => { bad_parse::cond.raise(()); (0, ch) },
        Some(i) => (i, ch)
    }
}
Z
Zack Corr 已提交
169

170
fn take_ident(rdr: @io::Reader, ch: char) -> (Identifier, char) {
G
Graydon Hoare 已提交
171
    let (s,ch) = take_nonempty_prefix(rdr, ch, char::is_alphanumeric);
172
    if s.iter().all(char::is_digit) {
G
Graydon Hoare 已提交
173 174 175
        match uint::from_str(s) {
            None => { bad_parse::cond.raise(()); (Numeric(0), ch) },
            Some(i) => (Numeric(i), ch)
Z
Zack Corr 已提交
176
        }
G
Graydon Hoare 已提交
177 178 179 180
    } else {
        (AlphaNumeric(s), ch)
    }
}
Z
Zack Corr 已提交
181

G
Graydon Hoare 已提交
182 183 184
fn expect(ch: char, c: char) {
    if ch != c {
        bad_parse::cond.raise(())
Z
Zack Corr 已提交
185
    }
G
Graydon Hoare 已提交
186
}
Z
Zack Corr 已提交
187

188
fn parse_reader(rdr: @io::Reader) -> Version {
G
Graydon Hoare 已提交
189 190 191 192 193
    let (major, ch) = take_num(rdr, rdr.read_char());
    expect(ch, '.');
    let (minor, ch) = take_num(rdr, rdr.read_char());
    expect(ch, '.');
    let (patch, ch) = take_num(rdr, rdr.read_char());
Z
Zack Corr 已提交
194

G
Graydon Hoare 已提交
195 196
    let mut pre = ~[];
    let mut build = ~[];
Z
Zack Corr 已提交
197

G
Graydon Hoare 已提交
198 199 200 201 202 203 204 205
    let mut ch = ch;
    if ch == '-' {
        loop {
            let (id, c) = take_ident(rdr, rdr.read_char());
            pre.push(id);
            ch = c;
            if ch != '.' { break; }
        }
Z
Zack Corr 已提交
206 207
    }

G
Graydon Hoare 已提交
208 209 210 211 212 213
    if ch == '+' {
        loop {
            let (id, c) = take_ident(rdr, rdr.read_char());
            build.push(id);
            ch = c;
            if ch != '.' { break; }
Z
Zack Corr 已提交
214
        }
G
Graydon Hoare 已提交
215
    }
Z
Zack Corr 已提交
216

G
Graydon Hoare 已提交
217 218 219 220 221 222 223
    Version {
        major: major,
        minor: minor,
        patch: patch,
        pre: pre,
        build: build,
    }
Z
Zack Corr 已提交
224 225 226
}


G
Graydon Hoare 已提交
227
pub fn parse(s: &str) -> Option<Version> {
228
    if !s.is_ascii() {
G
Graydon Hoare 已提交
229 230
        return None;
    }
231
    let s = s.trim();
G
Graydon Hoare 已提交
232 233 234 235
    let mut bad = false;
    do bad_parse::cond.trap(|_| { debug!("bad"); bad = true }).in {
        do io::with_str_reader(s) |rdr| {
            let v = parse_reader(rdr);
236
            if bad || v.to_str() != s.to_owned() {
Z
Zack Corr 已提交
237 238
                None
            } else {
G
Graydon Hoare 已提交
239
                Some(v)
Z
Zack Corr 已提交
240 241 242 243 244 245 246
            }
        }
    }
}

#[test]
fn test_parse() {
247 248 249 250 251 252 253 254 255 256
    assert_eq!(parse(""), None);
    assert_eq!(parse("  "), None);
    assert_eq!(parse("1"), None);
    assert_eq!(parse("1.2"), None);
    assert_eq!(parse("1.2"), None);
    assert_eq!(parse("1"), None);
    assert_eq!(parse("1.2"), None);
    assert_eq!(parse("1.2.3-"), None);
    assert_eq!(parse("a.b.c"), None);
    assert_eq!(parse("1.2.3 abc"), None);
P
Patrick Walton 已提交
257 258

    assert!(parse("1.2.3") == Some(Version {
Z
Zack Corr 已提交
259 260 261
        major: 1u,
        minor: 2u,
        patch: 3u,
G
Graydon Hoare 已提交
262 263
        pre: ~[],
        build: ~[],
264
    }));
P
Patrick Walton 已提交
265
    assert!(parse("  1.2.3  ") == Some(Version {
Z
Zack Corr 已提交
266 267 268
        major: 1u,
        minor: 2u,
        patch: 3u,
G
Graydon Hoare 已提交
269 270
        pre: ~[],
        build: ~[],
271
    }));
P
Patrick Walton 已提交
272
    assert!(parse("1.2.3-alpha1") == Some(Version {
Z
Zack Corr 已提交
273 274 275
        major: 1u,
        minor: 2u,
        patch: 3u,
G
Graydon Hoare 已提交
276 277
        pre: ~[AlphaNumeric(~"alpha1")],
        build: ~[]
278
    }));
P
Patrick Walton 已提交
279
    assert!(parse("  1.2.3-alpha1  ") == Some(Version {
Z
Zack Corr 已提交
280 281 282
        major: 1u,
        minor: 2u,
        patch: 3u,
G
Graydon Hoare 已提交
283 284
        pre: ~[AlphaNumeric(~"alpha1")],
        build: ~[]
285
    }));
P
Patrick Walton 已提交
286
    assert!(parse("1.2.3+build5") == Some(Version {
G
Graydon Hoare 已提交
287 288 289 290 291
        major: 1u,
        minor: 2u,
        patch: 3u,
        pre: ~[],
        build: ~[AlphaNumeric(~"build5")]
292
    }));
P
Patrick Walton 已提交
293
    assert!(parse("  1.2.3+build5  ") == Some(Version {
G
Graydon Hoare 已提交
294 295 296 297 298
        major: 1u,
        minor: 2u,
        patch: 3u,
        pre: ~[],
        build: ~[AlphaNumeric(~"build5")]
299
    }));
P
Patrick Walton 已提交
300
    assert!(parse("1.2.3-alpha1+build5") == Some(Version {
G
Graydon Hoare 已提交
301 302 303 304 305
        major: 1u,
        minor: 2u,
        patch: 3u,
        pre: ~[AlphaNumeric(~"alpha1")],
        build: ~[AlphaNumeric(~"build5")]
306
    }));
P
Patrick Walton 已提交
307
    assert!(parse("  1.2.3-alpha1+build5  ") == Some(Version {
G
Graydon Hoare 已提交
308 309 310 311 312
        major: 1u,
        minor: 2u,
        patch: 3u,
        pre: ~[AlphaNumeric(~"alpha1")],
        build: ~[AlphaNumeric(~"build5")]
313
    }));
P
Patrick Walton 已提交
314
    assert!(parse("1.2.3-1.alpha1.9+build5.7.3aedf  ") == Some(Version {
G
Graydon Hoare 已提交
315 316 317 318 319 320 321
        major: 1u,
        minor: 2u,
        patch: 3u,
        pre: ~[Numeric(1),AlphaNumeric(~"alpha1"),Numeric(9)],
        build: ~[AlphaNumeric(~"build5"),
                 Numeric(7),
                 AlphaNumeric(~"3aedf")]
322
    }));
G
Graydon Hoare 已提交
323

Z
Zack Corr 已提交
324 325 326 327
}

#[test]
fn test_eq() {
328 329
    assert_eq!(parse("1.2.3"), parse("1.2.3"));
    assert_eq!(parse("1.2.3-alpha1"), parse("1.2.3-alpha1"));
Z
Zack Corr 已提交
330 331 332 333
}

#[test]
fn test_ne() {
P
Patrick Walton 已提交
334 335 336 337
    assert!(parse("0.0.0")       != parse("0.0.1"));
    assert!(parse("0.0.0")       != parse("0.1.0"));
    assert!(parse("0.0.0")       != parse("1.0.0"));
    assert!(parse("1.2.3-alpha") != parse("1.2.3-beta"));
Z
Zack Corr 已提交
338 339 340 341
}

#[test]
fn test_lt() {
P
Patrick Walton 已提交
342 343 344 345 346 347
    assert!(parse("0.0.0")        < parse("1.2.3-alpha2"));
    assert!(parse("1.0.0")        < parse("1.2.3-alpha2"));
    assert!(parse("1.2.0")        < parse("1.2.3-alpha2"));
    assert!(parse("1.2.3-alpha1") < parse("1.2.3"));
    assert!(parse("1.2.3-alpha1") < parse("1.2.3-alpha2"));
    assert!(!(parse("1.2.3-alpha2") < parse("1.2.3-alpha2")));
Z
Zack Corr 已提交
348 349 350 351
}

#[test]
fn test_le() {
P
Patrick Walton 已提交
352 353 354 355 356
    assert!(parse("0.0.0")        <= parse("1.2.3-alpha2"));
    assert!(parse("1.0.0")        <= parse("1.2.3-alpha2"));
    assert!(parse("1.2.0")        <= parse("1.2.3-alpha2"));
    assert!(parse("1.2.3-alpha1") <= parse("1.2.3-alpha2"));
    assert!(parse("1.2.3-alpha2") <= parse("1.2.3-alpha2"));
Z
Zack Corr 已提交
357 358 359 360
}

#[test]
fn test_gt() {
P
Patrick Walton 已提交
361 362 363 364 365 366
    assert!(parse("1.2.3-alpha2") > parse("0.0.0"));
    assert!(parse("1.2.3-alpha2") > parse("1.0.0"));
    assert!(parse("1.2.3-alpha2") > parse("1.2.0"));
    assert!(parse("1.2.3-alpha2") > parse("1.2.3-alpha1"));
    assert!(parse("1.2.3")        > parse("1.2.3-alpha2"));
    assert!(!(parse("1.2.3-alpha2") > parse("1.2.3-alpha2")));
Z
Zack Corr 已提交
367 368 369 370
}

#[test]
fn test_ge() {
P
Patrick Walton 已提交
371 372 373 374 375
    assert!(parse("1.2.3-alpha2") >= parse("0.0.0"));
    assert!(parse("1.2.3-alpha2") >= parse("1.0.0"));
    assert!(parse("1.2.3-alpha2") >= parse("1.2.0"));
    assert!(parse("1.2.3-alpha2") >= parse("1.2.3-alpha1"));
    assert!(parse("1.2.3-alpha2") >= parse("1.2.3-alpha2"));
Z
Zack Corr 已提交
376
}
G
Graydon Hoare 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395

#[test]
fn test_spec_order() {

    let vs = ["1.0.0-alpha",
              "1.0.0-alpha.1",
              "1.0.0-beta.2",
              "1.0.0-beta.11",
              "1.0.0-rc.1",
              "1.0.0-rc.1+build.1",
              "1.0.0",
              "1.0.0+0.3.7",
              "1.3.7+build",
              "1.3.7+build.2.b8f12d7",
              "1.3.7+build.11.e0f985a"];
    let mut i = 1;
    while i < vs.len() {
        let a = parse(vs[i-1]).get();
        let b = parse(vs[i]).get();
P
Patrick Walton 已提交
396
        assert!(a < b);
G
Graydon Hoare 已提交
397 398
        i += 1;
    }
399
}