about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBrendan Zabarauskas <bjzaba@yahoo.com.au>2014-02-07 17:20:35 +1100
committerBrendan Zabarauskas <bjzaba@yahoo.com.au>2014-02-07 18:03:06 +1100
commitaa829c290417a080f6d9bbcc08914618d7088d9c (patch)
treee9d12978554aa53da581572e3fb67b416e98c760
parent78cb1e2ab0b282654a39ad61549cf5baabaae316 (diff)
downloadrust-aa829c290417a080f6d9bbcc08914618d7088d9c.tar.gz
rust-aa829c290417a080f6d9bbcc08914618d7088d9c.zip
Implement std::fmt::Show for semver::{Identifier, Version}
-rw-r--r--src/libsemver/lib.rs66
1 files changed, 51 insertions, 15 deletions
diff --git a/src/libsemver/lib.rs b/src/libsemver/lib.rs
index 8a485eb8014..9c5dd656d42 100644
--- a/src/libsemver/lib.rs
+++ b/src/libsemver/lib.rs
@@ -35,6 +35,7 @@
 
 use std::char;
 use std::cmp;
+use std::fmt;
 use std::option::{Option, Some, None};
 use std::to_str::ToStr;
 
@@ -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)
     }
 }
 
@@ -87,20 +95,32 @@ pub struct Version {
     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)
     }
 }
 
@@ -366,6 +386,22 @@ fn test_ne() {
 }
 
 #[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"));
     assert!(parse("1.0.0")        < parse("1.2.3-alpha2"));