2013-01-16 05:59:37 -06:00
|
|
|
// 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
|
|
|
|
|
2013-03-01 12:44:43 -06:00
|
|
|
use core::char;
|
2013-01-23 03:25:03 -06:00
|
|
|
use core::cmp;
|
2013-03-01 12:44:43 -06:00
|
|
|
use core::io::{ReaderUtil};
|
|
|
|
use core::io;
|
|
|
|
use core::option::{Option, Some, None};
|
|
|
|
use core::str;
|
|
|
|
use core::to_str::ToStr;
|
|
|
|
use core::uint;
|
2013-01-16 05:59:37 -06:00
|
|
|
|
2013-03-20 10:36:16 -05:00
|
|
|
#[deriving(Eq)]
|
2013-02-04 19:12:31 -06:00
|
|
|
pub enum Identifier {
|
|
|
|
Numeric(uint),
|
|
|
|
AlphaNumeric(~str)
|
|
|
|
}
|
|
|
|
|
|
|
|
impl cmp::Ord for Identifier {
|
|
|
|
#[inline(always)]
|
2013-03-21 23:34:30 -05:00
|
|
|
fn lt(&self, other: &Identifier) -> bool {
|
2013-02-04 19:12:31 -06:00
|
|
|
match (self, other) {
|
|
|
|
(&Numeric(a), &Numeric(b)) => a < b,
|
|
|
|
(&Numeric(_), _) => true,
|
|
|
|
(&AlphaNumeric(ref a), &AlphaNumeric(ref b)) => *a < *b,
|
|
|
|
(&AlphaNumeric(_), _) => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[inline(always)]
|
2013-03-21 23:34:30 -05:00
|
|
|
fn le(&self, other: &Identifier) -> bool {
|
2013-02-04 19:12:31 -06:00
|
|
|
! (other < self)
|
|
|
|
}
|
|
|
|
#[inline(always)]
|
2013-03-21 23:34:30 -05:00
|
|
|
fn gt(&self, other: &Identifier) -> bool {
|
2013-02-04 19:12:31 -06:00
|
|
|
other < self
|
|
|
|
}
|
|
|
|
#[inline(always)]
|
2013-03-21 23:34:30 -05:00
|
|
|
fn ge(&self, other: &Identifier) -> bool {
|
2013-02-04 19:12:31 -06:00
|
|
|
! (self < other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ToStr for Identifier {
|
|
|
|
#[inline(always)]
|
2013-03-21 23:34:30 -05:00
|
|
|
fn to_str(&self) -> ~str {
|
2013-02-04 19:12:31 -06:00
|
|
|
match self {
|
|
|
|
&Numeric(n) => n.to_str(),
|
|
|
|
&AlphaNumeric(ref s) => s.to_str()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-20 10:36:16 -05:00
|
|
|
#[deriving(Eq)]
|
2013-01-16 05:59:37 -06:00
|
|
|
pub struct Version {
|
|
|
|
major: uint,
|
|
|
|
minor: uint,
|
|
|
|
patch: uint,
|
2013-02-04 19:12:31 -06:00
|
|
|
pre: ~[Identifier],
|
|
|
|
build: ~[Identifier],
|
2013-01-16 05:59:37 -06:00
|
|
|
}
|
|
|
|
|
2013-02-04 19:12:31 -06:00
|
|
|
impl ToStr for Version {
|
2013-01-16 05:59:37 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:34:30 -05:00
|
|
|
fn to_str(&self) -> ~str {
|
2013-02-04 19:12:31 -06:00
|
|
|
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()), ".")
|
2013-01-16 05:59:37 -06:00
|
|
|
};
|
2013-02-04 19:12:31 -06:00
|
|
|
if self.build.is_empty() {
|
|
|
|
s
|
|
|
|
} else {
|
|
|
|
s + "+" + str::connect(self.build.map(|i| i.to_str()), ".")
|
|
|
|
}
|
2013-01-16 05:59:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-04 19:12:31 -06:00
|
|
|
impl cmp::Ord for Version {
|
2013-01-19 03:58:24 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:34:30 -05:00
|
|
|
fn lt(&self, other: &Version) -> bool {
|
2013-02-04 19:12:31 -06:00
|
|
|
|
2013-01-19 03:58:24 -06:00
|
|
|
self.major < other.major ||
|
2013-02-04 19:12:31 -06:00
|
|
|
|
|
|
|
(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)
|
2013-01-23 03:20:51 -06:00
|
|
|
}
|
2013-02-04 19:12:31 -06:00
|
|
|
|
2013-01-19 03:58:24 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:34:30 -05:00
|
|
|
fn le(&self, other: &Version) -> bool {
|
2013-02-04 19:12:31 -06:00
|
|
|
! (other < self)
|
2013-01-19 03:58:24 -06:00
|
|
|
}
|
|
|
|
#[inline(always)]
|
2013-03-21 23:34:30 -05:00
|
|
|
fn gt(&self, other: &Version) -> bool {
|
2013-02-04 19:12:31 -06:00
|
|
|
other < self
|
2013-01-19 03:58:24 -06:00
|
|
|
}
|
|
|
|
#[inline(always)]
|
2013-03-21 23:34:30 -05:00
|
|
|
fn ge(&self, other: &Version) -> bool {
|
2013-02-04 19:12:31 -06:00
|
|
|
! (self < other)
|
2013-01-19 03:58:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-04 19:12:31 -06:00
|
|
|
condition! {
|
|
|
|
bad_parse: () -> ();
|
|
|
|
}
|
2013-01-16 05:59:37 -06:00
|
|
|
|
2013-03-12 15:00:50 -05:00
|
|
|
fn take_nonempty_prefix(rdr: @io::Reader,
|
2013-02-04 19:12:31 -06:00
|
|
|
ch: char,
|
2013-03-07 16:38:38 -06:00
|
|
|
pred: &fn(char) -> bool) -> (~str, char) {
|
2013-02-04 19:12:31 -06:00
|
|
|
let mut buf = ~"";
|
|
|
|
let mut ch = ch;
|
|
|
|
while pred(ch) {
|
|
|
|
str::push_char(&mut buf, ch);
|
|
|
|
ch = rdr.read_char();
|
2013-01-16 05:59:37 -06:00
|
|
|
}
|
2013-02-04 19:12:31 -06:00
|
|
|
if buf.is_empty() {
|
|
|
|
bad_parse::cond.raise(())
|
|
|
|
}
|
|
|
|
debug!("extracted nonempty prefix: %s", buf);
|
|
|
|
(buf, ch)
|
2013-01-16 05:59:37 -06:00
|
|
|
}
|
|
|
|
|
2013-03-12 15:00:50 -05:00
|
|
|
fn take_num(rdr: @io::Reader, ch: char) -> (uint, char) {
|
2013-02-04 19:12:31 -06:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2013-01-16 05:59:37 -06:00
|
|
|
|
2013-03-12 15:00:50 -05:00
|
|
|
fn take_ident(rdr: @io::Reader, ch: char) -> (Identifier, char) {
|
2013-02-04 19:12:31 -06:00
|
|
|
let (s,ch) = take_nonempty_prefix(rdr, ch, char::is_alphanumeric);
|
|
|
|
if s.all(char::is_digit) {
|
|
|
|
match uint::from_str(s) {
|
|
|
|
None => { bad_parse::cond.raise(()); (Numeric(0), ch) },
|
|
|
|
Some(i) => (Numeric(i), ch)
|
2013-01-16 05:59:37 -06:00
|
|
|
}
|
2013-02-04 19:12:31 -06:00
|
|
|
} else {
|
|
|
|
(AlphaNumeric(s), ch)
|
|
|
|
}
|
|
|
|
}
|
2013-01-16 05:59:37 -06:00
|
|
|
|
2013-02-04 19:12:31 -06:00
|
|
|
fn expect(ch: char, c: char) {
|
|
|
|
if ch != c {
|
|
|
|
bad_parse::cond.raise(())
|
2013-01-16 05:59:37 -06:00
|
|
|
}
|
2013-02-04 19:12:31 -06:00
|
|
|
}
|
2013-01-16 05:59:37 -06:00
|
|
|
|
2013-03-12 15:00:50 -05:00
|
|
|
fn parse_reader(rdr: @io::Reader) -> Version {
|
2013-02-04 19:12:31 -06:00
|
|
|
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());
|
2013-01-16 05:59:37 -06:00
|
|
|
|
2013-02-04 19:12:31 -06:00
|
|
|
let mut pre = ~[];
|
|
|
|
let mut build = ~[];
|
2013-01-16 05:59:37 -06:00
|
|
|
|
2013-02-04 19:12:31 -06:00
|
|
|
let mut ch = ch;
|
|
|
|
if ch == '-' {
|
|
|
|
loop {
|
|
|
|
let (id, c) = take_ident(rdr, rdr.read_char());
|
|
|
|
pre.push(id);
|
|
|
|
ch = c;
|
|
|
|
if ch != '.' { break; }
|
|
|
|
}
|
2013-01-16 05:59:37 -06:00
|
|
|
}
|
|
|
|
|
2013-02-04 19:12:31 -06:00
|
|
|
if ch == '+' {
|
|
|
|
loop {
|
|
|
|
let (id, c) = take_ident(rdr, rdr.read_char());
|
|
|
|
build.push(id);
|
|
|
|
ch = c;
|
|
|
|
if ch != '.' { break; }
|
2013-01-16 05:59:37 -06:00
|
|
|
}
|
2013-02-04 19:12:31 -06:00
|
|
|
}
|
2013-01-16 05:59:37 -06:00
|
|
|
|
2013-02-04 19:12:31 -06:00
|
|
|
Version {
|
|
|
|
major: major,
|
|
|
|
minor: minor,
|
|
|
|
patch: patch,
|
|
|
|
pre: pre,
|
|
|
|
build: build,
|
|
|
|
}
|
2013-01-16 05:59:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-04 19:12:31 -06:00
|
|
|
pub fn parse(s: &str) -> Option<Version> {
|
|
|
|
if ! str::is_ascii(s) {
|
|
|
|
return None;
|
|
|
|
}
|
2013-03-21 17:02:27 -05:00
|
|
|
let s = s.trim();
|
2013-02-04 19:12:31 -06:00
|
|
|
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);
|
2013-03-21 16:59:33 -05:00
|
|
|
if bad || v.to_str() != s.to_owned() {
|
2013-01-16 05:59:37 -06:00
|
|
|
None
|
|
|
|
} else {
|
2013-02-04 19:12:31 -06:00
|
|
|
Some(v)
|
2013-01-16 05:59:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parse() {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(parse("") == None);
|
|
|
|
fail_unless!(parse(" ") == None);
|
|
|
|
fail_unless!(parse("1") == None);
|
|
|
|
fail_unless!(parse("1.2") == None);
|
|
|
|
fail_unless!(parse("1.2") == None);
|
|
|
|
fail_unless!(parse("1") == None);
|
|
|
|
fail_unless!(parse("1.2") == None);
|
|
|
|
fail_unless!(parse("1.2.3-") == None);
|
|
|
|
fail_unless!(parse("a.b.c") == None);
|
|
|
|
fail_unless!(parse("1.2.3 abc") == None);
|
|
|
|
|
|
|
|
fail_unless!(parse("1.2.3") == Some(Version {
|
2013-01-16 05:59:37 -06:00
|
|
|
major: 1u,
|
|
|
|
minor: 2u,
|
|
|
|
patch: 3u,
|
2013-02-04 19:12:31 -06:00
|
|
|
pre: ~[],
|
|
|
|
build: ~[],
|
2013-03-06 15:58:02 -06:00
|
|
|
}));
|
|
|
|
fail_unless!(parse(" 1.2.3 ") == Some(Version {
|
2013-01-16 05:59:37 -06:00
|
|
|
major: 1u,
|
|
|
|
minor: 2u,
|
|
|
|
patch: 3u,
|
2013-02-04 19:12:31 -06:00
|
|
|
pre: ~[],
|
|
|
|
build: ~[],
|
2013-03-06 15:58:02 -06:00
|
|
|
}));
|
|
|
|
fail_unless!(parse("1.2.3-alpha1") == Some(Version {
|
2013-01-16 05:59:37 -06:00
|
|
|
major: 1u,
|
|
|
|
minor: 2u,
|
|
|
|
patch: 3u,
|
2013-02-04 19:12:31 -06:00
|
|
|
pre: ~[AlphaNumeric(~"alpha1")],
|
|
|
|
build: ~[]
|
2013-03-06 15:58:02 -06:00
|
|
|
}));
|
|
|
|
fail_unless!(parse(" 1.2.3-alpha1 ") == Some(Version {
|
2013-01-16 05:59:37 -06:00
|
|
|
major: 1u,
|
|
|
|
minor: 2u,
|
|
|
|
patch: 3u,
|
2013-02-04 19:12:31 -06:00
|
|
|
pre: ~[AlphaNumeric(~"alpha1")],
|
|
|
|
build: ~[]
|
2013-03-06 15:58:02 -06:00
|
|
|
}));
|
|
|
|
fail_unless!(parse("1.2.3+build5") == Some(Version {
|
2013-02-04 19:12:31 -06:00
|
|
|
major: 1u,
|
|
|
|
minor: 2u,
|
|
|
|
patch: 3u,
|
|
|
|
pre: ~[],
|
|
|
|
build: ~[AlphaNumeric(~"build5")]
|
2013-03-06 15:58:02 -06:00
|
|
|
}));
|
|
|
|
fail_unless!(parse(" 1.2.3+build5 ") == Some(Version {
|
2013-02-04 19:12:31 -06:00
|
|
|
major: 1u,
|
|
|
|
minor: 2u,
|
|
|
|
patch: 3u,
|
|
|
|
pre: ~[],
|
|
|
|
build: ~[AlphaNumeric(~"build5")]
|
2013-03-06 15:58:02 -06:00
|
|
|
}));
|
|
|
|
fail_unless!(parse("1.2.3-alpha1+build5") == Some(Version {
|
2013-02-04 19:12:31 -06:00
|
|
|
major: 1u,
|
|
|
|
minor: 2u,
|
|
|
|
patch: 3u,
|
|
|
|
pre: ~[AlphaNumeric(~"alpha1")],
|
|
|
|
build: ~[AlphaNumeric(~"build5")]
|
2013-03-06 15:58:02 -06:00
|
|
|
}));
|
|
|
|
fail_unless!(parse(" 1.2.3-alpha1+build5 ") == Some(Version {
|
2013-02-04 19:12:31 -06:00
|
|
|
major: 1u,
|
|
|
|
minor: 2u,
|
|
|
|
patch: 3u,
|
|
|
|
pre: ~[AlphaNumeric(~"alpha1")],
|
|
|
|
build: ~[AlphaNumeric(~"build5")]
|
2013-03-06 15:58:02 -06:00
|
|
|
}));
|
|
|
|
fail_unless!(parse("1.2.3-1.alpha1.9+build5.7.3aedf ") == Some(Version {
|
2013-02-04 19:12:31 -06:00
|
|
|
major: 1u,
|
|
|
|
minor: 2u,
|
|
|
|
patch: 3u,
|
|
|
|
pre: ~[Numeric(1),AlphaNumeric(~"alpha1"),Numeric(9)],
|
|
|
|
build: ~[AlphaNumeric(~"build5"),
|
|
|
|
Numeric(7),
|
|
|
|
AlphaNumeric(~"3aedf")]
|
2013-03-06 15:58:02 -06:00
|
|
|
}));
|
2013-02-04 19:12:31 -06:00
|
|
|
|
2013-01-16 05:59:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_eq() {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(parse("1.2.3") == parse("1.2.3"));
|
|
|
|
fail_unless!(parse("1.2.3-alpha1") == parse("1.2.3-alpha1"));
|
2013-01-16 05:59:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ne() {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(parse("0.0.0") != parse("0.0.1"));
|
|
|
|
fail_unless!(parse("0.0.0") != parse("0.1.0"));
|
|
|
|
fail_unless!(parse("0.0.0") != parse("1.0.0"));
|
|
|
|
fail_unless!(parse("1.2.3-alpha") != parse("1.2.3-beta"));
|
2013-01-16 05:59:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_lt() {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(parse("0.0.0") < parse("1.2.3-alpha2"));
|
|
|
|
fail_unless!(parse("1.0.0") < parse("1.2.3-alpha2"));
|
|
|
|
fail_unless!(parse("1.2.0") < parse("1.2.3-alpha2"));
|
|
|
|
fail_unless!(parse("1.2.3-alpha1") < parse("1.2.3"));
|
|
|
|
fail_unless!(parse("1.2.3-alpha1") < parse("1.2.3-alpha2"));
|
|
|
|
fail_unless!(!(parse("1.2.3-alpha2") < parse("1.2.3-alpha2")));
|
2013-01-16 05:59:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_le() {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(parse("0.0.0") <= parse("1.2.3-alpha2"));
|
|
|
|
fail_unless!(parse("1.0.0") <= parse("1.2.3-alpha2"));
|
|
|
|
fail_unless!(parse("1.2.0") <= parse("1.2.3-alpha2"));
|
|
|
|
fail_unless!(parse("1.2.3-alpha1") <= parse("1.2.3-alpha2"));
|
|
|
|
fail_unless!(parse("1.2.3-alpha2") <= parse("1.2.3-alpha2"));
|
2013-01-16 05:59:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_gt() {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(parse("1.2.3-alpha2") > parse("0.0.0"));
|
|
|
|
fail_unless!(parse("1.2.3-alpha2") > parse("1.0.0"));
|
|
|
|
fail_unless!(parse("1.2.3-alpha2") > parse("1.2.0"));
|
|
|
|
fail_unless!(parse("1.2.3-alpha2") > parse("1.2.3-alpha1"));
|
|
|
|
fail_unless!(parse("1.2.3") > parse("1.2.3-alpha2"));
|
|
|
|
fail_unless!(!(parse("1.2.3-alpha2") > parse("1.2.3-alpha2")));
|
2013-01-16 05:59:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ge() {
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(parse("1.2.3-alpha2") >= parse("0.0.0"));
|
|
|
|
fail_unless!(parse("1.2.3-alpha2") >= parse("1.0.0"));
|
|
|
|
fail_unless!(parse("1.2.3-alpha2") >= parse("1.2.0"));
|
|
|
|
fail_unless!(parse("1.2.3-alpha2") >= parse("1.2.3-alpha1"));
|
|
|
|
fail_unless!(parse("1.2.3-alpha2") >= parse("1.2.3-alpha2"));
|
2013-01-16 05:59:37 -06:00
|
|
|
}
|
2013-02-04 19:12:31 -06:00
|
|
|
|
|
|
|
#[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();
|
2013-03-06 15:58:02 -06:00
|
|
|
fail_unless!(a < b);
|
2013-02-04 19:12:31 -06:00
|
|
|
i += 1;
|
|
|
|
}
|
2013-02-20 19:07:17 -06:00
|
|
|
}
|