Skip to content

Commit

Permalink
auto merge of #12083 : bjz/rust/semver, r=huonw
Browse files Browse the repository at this point in the history
  • Loading branch information
bors committed Feb 7, 2014
2 parents 36f1b38 + aa829c2 commit 14cb4be
Showing 1 changed file with 56 additions and 20 deletions.
76 changes: 56 additions & 20 deletions src/libsemver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

use std::char;
use std::cmp;
use std::fmt;
use std::option::{Option, Some, None};
use std::to_str::ToStr;

Expand All @@ -59,13 +60,20 @@ impl cmp::Ord for Identifier {
}
}

impl fmt::Show for Identifier {
#[inline]
fn fmt(version: &Identifier, f: &mut fmt::Formatter) -> fmt::Result {
match *version {
Numeric(ref n) => fmt::Show::fmt(n, f),
AlphaNumeric(ref s) => fmt::Show::fmt(s, f)
}
}
}

impl ToStr for Identifier {
#[inline]
fn to_str(&self) -> ~str {
match self {
&Numeric(n) => n.to_str(),
&AlphaNumeric(ref s) => s.to_str()
}
format!("{}", *self)
}
}

Expand All @@ -74,33 +82,45 @@ impl ToStr for Identifier {
#[deriving(Clone, Eq)]
pub struct Version {
/// The major version, to be incremented on incompatible changes.
priv major: uint,
major: uint,
/// The minor version, to be incremented when functionality is added in a
/// backwards-compatible manner.
priv minor: uint,
minor: uint,
/// The patch version, to be incremented when backwards-compatible bug
/// fixes are made.
priv patch: uint,
patch: uint,
/// The pre-release version identifier, if one exists.
priv pre: ~[Identifier],
pre: ~[Identifier],
/// The build metadata, ignored when determining version precedence.
priv build: ~[Identifier],
build: ~[Identifier],
}

impl fmt::Show for Version {
#[inline]
fn fmt(version: &Version, f: &mut fmt::Formatter) -> fmt::Result {
if_ok!(write!(f.buf, "{}.{}.{}", version.major, version.minor, version.patch))
if !version.pre.is_empty() {
if_ok!(write!(f.buf, "-"));
for (i, x) in version.pre.iter().enumerate() {
if i != 0 { if_ok!(write!(f.buf, ".")) };
if_ok!(fmt::Show::fmt(x, f));
}
}
if !version.build.is_empty() {
if_ok!(write!(f.buf, "+"));
for (i, x) in version.build.iter().enumerate() {
if i != 0 { if_ok!(write!(f.buf, ".")) };
if_ok!(fmt::Show::fmt(x, f));
}
}
Ok(())
}
}

impl ToStr for Version {
#[inline]
fn to_str(&self) -> ~str {
let s = format!("{}.{}.{}", self.major, self.minor, self.patch);
let s = if self.pre.is_empty() {
s
} else {
format!("{}-{}", s, self.pre.map(|i| i.to_str()).connect("."))
};
if self.build.is_empty() {
s
} else {
format!("{}+{}", s, self.build.map(|i| i.to_str()).connect("."))
}
format!("{}", *self)
}
}

Expand Down Expand Up @@ -365,6 +385,22 @@ fn test_ne() {
assert!(parse("1.2.3+23") != parse("1.2.3+42"));
}
#[test]
fn test_show() {
assert_eq!(format!("{}", parse("1.2.3").unwrap()), ~"1.2.3");
assert_eq!(format!("{}", parse("1.2.3-alpha1").unwrap()), ~"1.2.3-alpha1");
assert_eq!(format!("{}", parse("1.2.3+build.42").unwrap()), ~"1.2.3+build.42");
assert_eq!(format!("{}", parse("1.2.3-alpha1+42").unwrap()), ~"1.2.3-alpha1+42");
}
#[test]
fn test_to_str() {
assert_eq!(parse("1.2.3").unwrap().to_str(), ~"1.2.3");
assert_eq!(parse("1.2.3-alpha1").unwrap().to_str(), ~"1.2.3-alpha1");
assert_eq!(parse("1.2.3+build.42").unwrap().to_str(), ~"1.2.3+build.42");
assert_eq!(parse("1.2.3-alpha1+42").unwrap().to_str(), ~"1.2.3-alpha1+42");
}
#[test]
fn test_lt() {
assert!(parse("0.0.0") < parse("1.2.3-alpha2"));
Expand Down

0 comments on commit 14cb4be

Please sign in to comment.