9
9
// except according to those terms.
10
10
11
11
pub use package_path:: { RemotePath , LocalPath , normalize, hash} ;
12
- use extra:: semver;
13
12
use 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} ;
18
14
19
15
/// Path-fragment identifier of a package such as
20
16
/// 'github.com/graydon/test'; path must be a relative
@@ -39,6 +35,21 @@ impl PkgId {
39
35
pub fn new ( s : & str ) -> PkgId {
40
36
use conditions:: bad_pkg_id:: cond;
41
37
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
+
42
53
let p = Path ( s) ;
43
54
if p. is_absolute {
44
55
return cond. raise ( ( p, ~"absolute pkgid") ) ;
@@ -49,11 +60,20 @@ impl PkgId {
49
60
let remote_path = RemotePath ( p) ;
50
61
let local_path = normalize ( copy remote_path) ;
51
62
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
+
52
72
PkgId {
53
73
local_path : local_path,
54
74
remote_path : remote_path,
55
75
short_name : short_name,
56
- version : default_version ( )
76
+ version : version
57
77
}
58
78
}
59
79
@@ -64,69 +84,17 @@ impl PkgId {
64
84
}
65
85
66
86
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( ) )
68
88
}
69
89
}
70
90
71
91
impl ToStr for PkgId {
72
92
fn to_str ( & self ) -> ~str {
93
+ let maybe_dash = match self . version {
94
+ NoVersion => "" ,
95
+ _ => "-"
96
+ } ;
73
97
// 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( ) )
131
99
}
132
100
}
0 commit comments