99// except according to those terms.
1010
1111pub use package_path:: { RemotePath , LocalPath , normalize, hash} ;
12- use extra:: semver;
1312use core:: prelude:: * ;
14- use core:: result;
15-
16- /// Placeholder
17- pub fn default_version ( ) -> Version { ExactRevision ( 0.1 ) }
13+ use version:: { try_getting_version, Version , NoVersion , split_version} ;
1814
1915/// Path-fragment identifier of a package such as
2016/// 'github.com/graydon/test'; path must be a relative
@@ -39,6 +35,21 @@ impl PkgId {
3935 pub fn new ( s : & str ) -> PkgId {
4036 use conditions:: bad_pkg_id:: cond;
4137
38+ let mut given_version = None ;
39+
40+ // Did the user request a specific version?
41+ let s = match split_version ( s) {
42+ Some ( ( path, v) ) => {
43+ debug ! ( "s = %s, path = %s, v = %s" , s, path, v. to_str( ) ) ;
44+ given_version = Some ( v) ;
45+ path
46+ }
47+ None => {
48+ debug ! ( "%s has no explicit version" , s) ;
49+ s
50+ }
51+ } ;
52+
4253 let p = Path ( s) ;
4354 if p. is_absolute {
4455 return cond. raise ( ( p, ~"absolute pkgid") ) ;
@@ -49,11 +60,20 @@ impl PkgId {
4960 let remote_path = RemotePath ( p) ;
5061 let local_path = normalize ( copy remote_path) ;
5162 let short_name = ( copy local_path) . filestem ( ) . expect ( fmt ! ( "Strange path! %s" , s) ) ;
63+
64+ let version = match given_version {
65+ Some ( v) => v,
66+ None => match try_getting_version ( & remote_path) {
67+ Some ( v) => v,
68+ None => NoVersion
69+ }
70+ } ;
71+
5272 PkgId {
5373 local_path : local_path,
5474 remote_path : remote_path,
5575 short_name : short_name,
56- version : default_version ( )
76+ version : version
5777 }
5878 }
5979
@@ -64,69 +84,17 @@ impl PkgId {
6484 }
6585
6686 pub fn short_name_with_version ( & self ) -> ~str {
67- fmt ! ( "%s- %s" , self . short_name, self . version. to_str( ) )
87+ fmt ! ( "%s%s" , self . short_name, self . version. to_str( ) )
6888 }
6989}
7090
7191impl ToStr for PkgId {
7292 fn to_str ( & self ) -> ~str {
93+ let maybe_dash = match self . version {
94+ NoVersion => "" ,
95+ _ => "-"
96+ } ;
7397 // should probably use the filestem and not the whole path
74- fmt ! ( "%s-%s" , self . local_path. to_str( ) , self . version. to_str( ) )
75- }
76- }
77-
78- /// A version is either an exact revision,
79- /// or a semantic version
80- pub enum Version {
81- ExactRevision ( float ) ,
82- SemVersion ( semver:: Version )
83- }
84-
85-
86- impl Ord for Version {
87- fn lt ( & self , other : & Version ) -> bool {
88- match ( self , other) {
89- ( & ExactRevision ( f1) , & ExactRevision ( f2) ) => f1 < f2,
90- ( & SemVersion ( ref v1) , & SemVersion ( ref v2) ) => v1 < v2,
91- _ => false // incomparable, really
92- }
93- }
94- fn le ( & self , other : & Version ) -> bool {
95- match ( self , other) {
96- ( & ExactRevision ( f1) , & ExactRevision ( f2) ) => f1 <= f2,
97- ( & SemVersion ( ref v1) , & SemVersion ( ref v2) ) => v1 <= v2,
98- _ => false // incomparable, really
99- }
100- }
101- fn ge ( & self , other : & Version ) -> bool {
102- match ( self , other) {
103- ( & ExactRevision ( f1) , & ExactRevision ( f2) ) => f1 > f2,
104- ( & SemVersion ( ref v1) , & SemVersion ( ref v2) ) => v1 > v2,
105- _ => false // incomparable, really
106- }
107- }
108- fn gt ( & self , other : & Version ) -> bool {
109- match ( self , other) {
110- ( & ExactRevision ( f1) , & ExactRevision ( f2) ) => f1 >= f2,
111- ( & SemVersion ( ref v1) , & SemVersion ( ref v2) ) => v1 >= v2,
112- _ => false // incomparable, really
113- }
114- }
115-
116- }
117-
118- impl ToStr for Version {
119- fn to_str ( & self ) -> ~str {
120- match * self {
121- ExactRevision ( ref n) => n. to_str ( ) ,
122- SemVersion ( ref v) => v. to_str ( )
123- }
124- }
125- }
126-
127- pub fn parse_vers ( vers : ~str ) -> result:: Result < semver:: Version , ~str > {
128- match semver:: parse ( vers) {
129- Some ( vers) => result:: Ok ( vers) ,
130- None => result:: Err ( ~"could not parse version: invalid")
98+ fmt ! ( "%s%s%s" , self . local_path. to_str( ) , maybe_dash, self . version. to_str( ) )
13199 }
132100}
0 commit comments