Skip to content

Commit e3a0786

Browse files
committed
---
yaml --- r: 103935 b: refs/heads/try c: aa829c2 h: refs/heads/master i: 103933: b0aedbd 103931: 05b6fa2 103927: 6d47f94 103919: 7426dcf 103903: 41ccbaf 103871: ae1dcbc 103807: 4e6a450 103679: 2e343f2 103423: ee081bc v: v3
1 parent 1c898dd commit e3a0786

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
@@ -2,7 +2,7 @@
22
refs/heads/master: 62f1d68439dcfd509eaca29887afa97f22938373
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 6e7f170fedd3c526a643c0b2d13863acd982be02
5-
refs/heads/try: 78cb1e2ab0b282654a39ad61549cf5baabaae316
5+
refs/heads/try: aa829c290417a080f6d9bbcc08914618d7088d9c
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/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)