Skip to content

Commit dca0056

Browse files
committed
---
yaml --- r: 148957 b: refs/heads/try2 c: aa829c2 h: refs/heads/master i: 148955: 6af4918 v: v3
1 parent 771b7d7 commit dca0056

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 78cb1e2ab0b282654a39ad61549cf5baabaae316
8+
refs/heads/try2: aa829c290417a080f6d9bbcc08914618d7088d9c
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libsemver/lib.rs

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
use std::char;
3737
use std::cmp;
38+
use std::fmt;
3839
use std::option::{Option, Some, None};
3940
use std::to_str::ToStr;
4041

@@ -59,13 +60,20 @@ impl cmp::Ord for Identifier {
5960
}
6061
}
6162

63+
impl fmt::Show for Identifier {
64+
#[inline]
65+
fn fmt(version: &Identifier, f: &mut fmt::Formatter) -> fmt::Result {
66+
match *version {
67+
Numeric(ref n) => fmt::Show::fmt(n, f),
68+
AlphaNumeric(ref s) => fmt::Show::fmt(s, f)
69+
}
70+
}
71+
}
72+
6273
impl ToStr for Identifier {
6374
#[inline]
6475
fn to_str(&self) -> ~str {
65-
match self {
66-
&Numeric(n) => n.to_str(),
67-
&AlphaNumeric(ref s) => s.to_str()
68-
}
76+
format!("{}", *self)
6977
}
7078
}
7179

@@ -87,20 +95,32 @@ pub struct Version {
8795
build: ~[Identifier],
8896
}
8997

98+
impl fmt::Show for Version {
99+
#[inline]
100+
fn fmt(version: &Version, f: &mut fmt::Formatter) -> fmt::Result {
101+
if_ok!(write!(f.buf, "{}.{}.{}", version.major, version.minor, version.patch))
102+
if !version.pre.is_empty() {
103+
if_ok!(write!(f.buf, "-"));
104+
for (i, x) in version.pre.iter().enumerate() {
105+
if i != 0 { if_ok!(write!(f.buf, ".")) };
106+
if_ok!(fmt::Show::fmt(x, f));
107+
}
108+
}
109+
if !version.build.is_empty() {
110+
if_ok!(write!(f.buf, "+"));
111+
for (i, x) in version.build.iter().enumerate() {
112+
if i != 0 { if_ok!(write!(f.buf, ".")) };
113+
if_ok!(fmt::Show::fmt(x, f));
114+
}
115+
}
116+
Ok(())
117+
}
118+
}
119+
90120
impl ToStr for Version {
91121
#[inline]
92122
fn to_str(&self) -> ~str {
93-
let s = format!("{}.{}.{}", self.major, self.minor, self.patch);
94-
let s = if self.pre.is_empty() {
95-
s
96-
} else {
97-
format!("{}-{}", s, self.pre.map(|i| i.to_str()).connect("."))
98-
};
99-
if self.build.is_empty() {
100-
s
101-
} else {
102-
format!("{}+{}", s, self.build.map(|i| i.to_str()).connect("."))
103-
}
123+
format!("{}", *self)
104124
}
105125
}
106126

@@ -365,6 +385,22 @@ fn test_ne() {
365385
assert!(parse("1.2.3+23") != parse("1.2.3+42"));
366386
}
367387
388+
#[test]
389+
fn test_show() {
390+
assert_eq!(format!("{}", parse("1.2.3").unwrap()), ~"1.2.3");
391+
assert_eq!(format!("{}", parse("1.2.3-alpha1").unwrap()), ~"1.2.3-alpha1");
392+
assert_eq!(format!("{}", parse("1.2.3+build.42").unwrap()), ~"1.2.3+build.42");
393+
assert_eq!(format!("{}", parse("1.2.3-alpha1+42").unwrap()), ~"1.2.3-alpha1+42");
394+
}
395+
396+
#[test]
397+
fn test_to_str() {
398+
assert_eq!(parse("1.2.3").unwrap().to_str(), ~"1.2.3");
399+
assert_eq!(parse("1.2.3-alpha1").unwrap().to_str(), ~"1.2.3-alpha1");
400+
assert_eq!(parse("1.2.3+build.42").unwrap().to_str(), ~"1.2.3+build.42");
401+
assert_eq!(parse("1.2.3-alpha1+42").unwrap().to_str(), ~"1.2.3-alpha1+42");
402+
}
403+
368404
#[test]
369405
fn test_lt() {
370406
assert!(parse("0.0.0") < parse("1.2.3-alpha2"));

0 commit comments

Comments
 (0)