rust/src/libextra/semver.rs

397 lines
10 KiB
Rust
Raw Normal View History

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
#[allow(missing_doc)];
use std::char;
use std::cmp;
use std::io::{ReaderUtil};
use std::io;
use std::option::{Option, Some, None};
use std::to_str::ToStr;
use std::uint;
2013-01-16 05:59:37 -06:00
#[deriving(Eq)]
2013-02-04 19:12:31 -06:00
pub enum Identifier {
Numeric(uint),
AlphaNumeric(~str)
}
impl cmp::Ord for Identifier {
#[inline]
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]
fn le(&self, other: &Identifier) -> bool {
2013-02-04 19:12:31 -06:00
! (other < self)
}
#[inline]
fn gt(&self, other: &Identifier) -> bool {
2013-02-04 19:12:31 -06:00
other < self
}
#[inline]
fn ge(&self, other: &Identifier) -> bool {
2013-02-04 19:12:31 -06:00
! (self < other)
}
}
impl ToStr for Identifier {
#[inline]
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()
}
}
}
#[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 {
#[inline]
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 {
fmt!("%s-%s", s, self.pre.map(|i| i.to_str()).connect("."))
2013-01-16 05:59:37 -06:00
};
2013-02-04 19:12:31 -06:00
if self.build.is_empty() {
s
} else {
fmt!("%s+%s", s, self.build.map(|i| i.to_str()).connect("."))
2013-02-04 19:12:31 -06:00
}
2013-01-16 05:59:37 -06:00
}
}
2013-02-04 19:12:31 -06:00
impl cmp::Ord for Version {
#[inline]
fn lt(&self, other: &Version) -> bool {
2013-02-04 19:12:31 -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-02-04 19:12:31 -06:00
#[inline]
fn le(&self, other: &Version) -> bool {
2013-02-04 19:12:31 -06:00
! (other < self)
}
#[inline]
fn gt(&self, other: &Version) -> bool {
2013-02-04 19:12:31 -06:00
other < self
}
#[inline]
fn ge(&self, other: &Version) -> bool {
2013-02-04 19:12:31 -06:00
! (self < other)
}
}
2013-02-04 19:12:31 -06:00
condition! {
bad_parse: () -> ();
}
2013-01-16 05:59:37 -06:00
fn take_nonempty_prefix(rdr: @io::Reader,
2013-02-04 19:12:31 -06:00
ch: char,
pred: &fn(char) -> bool) -> (~str, char) {
2013-02-04 19:12:31 -06:00
let mut buf = ~"";
let mut ch = ch;
while pred(ch) {
buf.push_char(ch);
2013-02-04 19:12:31 -06:00
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
}
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
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.iter().all(char::is_digit) {
2013-02-04 19:12:31 -06:00
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
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 !s.is_ascii() {
2013-02-04 19:12:31 -06:00
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);
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() {
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);
2013-03-28 20:39:09 -05:00
assert!(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-28 20:39:09 -05:00
assert!(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-28 20:39:09 -05:00
assert!(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-28 20:39:09 -05:00
assert!(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-28 20:39:09 -05:00
assert!(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-28 20:39:09 -05:00
assert!(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-28 20:39:09 -05:00
assert!(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-28 20:39:09 -05:00
assert!(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-28 20:39:09 -05:00
assert!(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-02-04 19:12:31 -06:00
2013-01-16 05:59:37 -06:00
}
#[test]
fn test_eq() {
assert_eq!(parse("1.2.3"), parse("1.2.3"));
assert_eq!(parse("1.2.3-alpha1"), parse("1.2.3-alpha1"));
2013-01-16 05:59:37 -06:00
}
#[test]
fn test_ne() {
2013-03-28 20:39:09 -05:00
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"));
2013-01-16 05:59:37 -06:00
}
#[test]
fn test_lt() {
2013-03-28 20:39:09 -05:00
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")));
2013-01-16 05:59:37 -06:00
}
#[test]
fn test_le() {
2013-03-28 20:39:09 -05:00
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"));
2013-01-16 05:59:37 -06:00
}
#[test]
fn test_gt() {
2013-03-28 20:39:09 -05:00
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")));
2013-01-16 05:59:37 -06:00
}
#[test]
fn test_ge() {
2013-03-28 20:39:09 -05:00
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"));
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-28 20:39:09 -05:00
assert!(a < b);
2013-02-04 19:12:31 -06:00
i += 1;
}
}