Skip to content

Commit aa829c2

Browse files
committed
Implement std::fmt::Show for semver::{Identifier, Version}
1 parent 78cb1e2 commit aa829c2

File tree

1 file changed

+51
-15
lines changed

1 file changed

+51
-15
lines changed

src/libsemver/lib.rs

+51-15
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)