3535
3636use std:: char;
3737use std:: cmp;
38- use std:: fmt;
3938use std:: option:: { Option , Some , None } ;
4039use std:: to_str:: ToStr ;
4140
@@ -60,20 +59,13 @@ impl cmp::Ord for Identifier {
6059 }
6160}
6261
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-
7362impl ToStr for Identifier {
7463 #[ inline]
7564 fn to_str ( & self ) -> ~str {
76- format ! ( "{}" , * self )
65+ match self {
66+ & Numeric ( n) => n. to_str ( ) ,
67+ & AlphaNumeric ( ref s) => s. to_str ( )
68+ }
7769 }
7870}
7971
@@ -82,45 +74,33 @@ impl ToStr for Identifier {
8274#[ deriving( Clone , Eq ) ]
8375pub struct Version {
8476 /// The major version, to be incremented on incompatible changes.
85- major : uint ,
77+ priv major : uint ,
8678 /// The minor version, to be incremented when functionality is added in a
8779 /// backwards-compatible manner.
88- minor : uint ,
80+ priv minor : uint ,
8981 /// The patch version, to be incremented when backwards-compatible bug
9082 /// fixes are made.
91- patch : uint ,
83+ priv patch : uint ,
9284 /// The pre-release version identifier, if one exists.
93- pre : ~[ Identifier ] ,
85+ priv pre: ~[ Identifier ] ,
9486 /// The build metadata, ignored when determining version precedence.
95- build : ~[ Identifier ] ,
96- }
97-
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- }
87+ priv build : ~[ Identifier ] ,
11888}
11989
12090impl ToStr for Version {
12191 #[ inline]
12292 fn to_str ( & self ) -> ~str {
123- format ! ( "{}" , * self )
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+ }
124104 }
125105}
126106
@@ -385,22 +365,6 @@ fn test_ne() {
385365 assert!(parse(" 1.2 . 3 +23 ") != parse(" 1.2 . 3 +42 "));
386366}
387367
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-
404368#[test]
405369fn test_lt() {
406370 assert!(parse(" 0.0 . 0 ") < parse(" 1.2 . 3 -alpha2"));
0 commit comments