1
- use crate :: JsResult ;
2
1
use rattler_conda_types:: { Version , VersionBumpType } ;
3
2
use std:: { cmp:: Ordering , str:: FromStr } ;
4
3
use wasm_bindgen:: prelude:: wasm_bindgen;
4
+ use wasm_bindgen:: JsValue ;
5
+
6
+ use crate :: JsResult ;
5
7
6
- #[ wasm_bindgen]
8
+ /// This class implements an order relation between version strings. Version
9
+ /// strings can contain the usual alphanumeric characters (A-Za-z0-9), separated
10
+ /// into segments by dots and underscores. Empty segments (i.e. two consecutive
11
+ /// dots, a leading/trailing underscore) are not permitted. An optional epoch
12
+ /// number - an integer followed by '!' - can precede the actual version string
13
+ /// (this is useful to indicate a change in the versioning scheme itself).
14
+ /// Version comparison is case-insensitive.
15
+ ///
16
+ /// @public
17
+ #[ wasm_bindgen( js_name = "Version" ) ]
7
18
#[ repr( transparent) ]
8
19
#[ derive( Eq , PartialEq ) ]
9
20
pub struct JsVersion {
@@ -28,110 +39,198 @@ impl AsRef<Version> for JsVersion {
28
39
}
29
40
}
30
41
31
- #[ wasm_bindgen]
32
- pub struct MajorMinor ( pub usize , pub usize ) ;
33
-
34
- #[ wasm_bindgen]
42
+ #[ wasm_bindgen( js_class = "Version" ) ]
35
43
impl JsVersion {
44
+ /// Constructs a new Version object from a string representation.
36
45
#[ wasm_bindgen( constructor) ]
37
- pub fn new ( version : & str ) -> JsResult < Self > {
46
+ pub fn new (
47
+ #[ wasm_bindgen( param_description = "The string representation of the version." ) ]
48
+ version : & str ,
49
+ ) -> JsResult < Self > {
38
50
let version = Version :: from_str ( version) ?;
39
51
Ok ( version. into ( ) )
40
52
}
41
53
54
+ /// Returns the string representation of the version.
55
+ ///
56
+ /// An attempt is made to return the version in the same format as the input
57
+ /// string but this is not guaranteed.
58
+ #[ wasm_bindgen( js_name = "toString" ) ]
42
59
pub fn as_str ( & self ) -> String {
43
60
format ! ( "{}" , self . as_ref( ) )
44
61
}
45
62
63
+ /// The epoch part of the version. E.g. `1` in `1!2.3`.
46
64
#[ wasm_bindgen( getter) ]
47
65
pub fn epoch ( & self ) -> Option < usize > {
48
66
self . as_ref ( ) . epoch_opt ( ) . map ( |v| v as usize )
49
67
}
50
68
51
- #[ wasm_bindgen( getter) ]
69
+ /// `true` if the version has a local part. E.g. `2.3` in `1+2.3`.
70
+ #[ wasm_bindgen( getter, js_name = "hasLocal" ) ]
52
71
pub fn has_local ( & self ) -> bool {
53
72
self . as_ref ( ) . has_local ( )
54
73
}
55
74
56
- #[ wasm_bindgen( getter) ]
75
+ /// `true` if the version is considered a development version.
76
+ ///
77
+ /// A development version is a version that contains the string `dev` in the
78
+ /// version string.
79
+ #[ wasm_bindgen( getter, js_name = "isDev" ) ]
57
80
pub fn is_dev ( & self ) -> bool {
58
81
self . as_ref ( ) . is_dev ( )
59
82
}
60
83
61
- pub fn as_major_minor ( & self ) -> Option < MajorMinor > {
84
+ /// Returns the major and minor part of the version if the version does not
85
+ /// represent a typical major minor version. If any of the parts are not a
86
+ /// single number, undefined is returned.
87
+ // TODO: Simplify when https://github.com/rustwasm/wasm-bindgen/issues/122 is fixed
88
+ #[ wasm_bindgen(
89
+ js_name = "asMajorMinor" ,
90
+ unchecked_return_type = "[number, number] | undefined"
91
+ ) ]
92
+ pub fn as_major_minor ( & self ) -> Option < Vec < JsValue > > {
62
93
let ( major, minor) = self . as_ref ( ) . as_major_minor ( ) ?;
63
- Some ( MajorMinor ( major as _ , minor as _ ) )
64
- }
65
-
66
- pub fn starts_with ( & self , other : & Self ) -> bool {
94
+ Some ( vec ! [
95
+ JsValue :: from( major as usize ) ,
96
+ JsValue :: from( minor as usize ) ,
97
+ ] )
98
+ }
99
+
100
+ /// Returns true if this version starts with the other version. This is
101
+ /// defined as the other version being a prefix of this version.
102
+ #[ wasm_bindgen( js_name = "startsWith" ) ]
103
+ pub fn starts_with (
104
+ & self ,
105
+ #[ wasm_bindgen( param_description = "The version to use for the comparison" ) ] other : & Self ,
106
+ ) -> bool {
67
107
self . as_ref ( ) . starts_with ( other. as_ref ( ) )
68
108
}
69
109
70
- pub fn compatible_with ( & self , other : & Self ) -> bool {
110
+ /// Returns true if this version is compatible with the other version.
111
+ #[ wasm_bindgen( js_name = "compatibleWith" ) ]
112
+ pub fn compatible_with (
113
+ & self ,
114
+ #[ wasm_bindgen( param_description = "The version to use for the comparison" ) ] other : & Self ,
115
+ ) -> bool {
71
116
self . as_ref ( ) . compatible_with ( other. as_ref ( ) )
72
117
}
73
118
74
- pub fn pop_segments ( & self , n : usize ) -> Option < Self > {
119
+ /// Pop the last `n` segments from the version.
120
+ #[ wasm_bindgen( js_name = "popSegments" ) ]
121
+ pub fn pop_segments (
122
+ & self ,
123
+ #[ wasm_bindgen( param_description = "The number of segments to pop" ) ] n : usize ,
124
+ ) -> Option < Self > {
75
125
Some ( self . as_ref ( ) . pop_segments ( n) ?. into ( ) )
76
126
}
77
127
78
- pub fn extend_to_length ( & self , length : usize ) -> JsResult < Self > {
128
+ /// Extend the version to the given length by adding zeros. If the version
129
+ /// is already at the specified length or longer the original version
130
+ /// will be returned.
131
+ #[ wasm_bindgen( js_name = "extendToLength" ) ]
132
+ pub fn extend_to_length (
133
+ & self ,
134
+ #[ wasm_bindgen( param_description = "The length to extend to" ) ] length : usize ,
135
+ ) -> JsResult < Self > {
79
136
Ok ( self . as_ref ( ) . extend_to_length ( length) ?. into_owned ( ) . into ( ) )
80
137
}
81
138
82
- pub fn with_segments ( & self , start : usize , stop : usize ) -> Option < Self > {
83
- let range = start..stop;
139
+ /// Returns a new version with the segments from start to end (exclusive).
140
+ ///
141
+ /// Returns undefined if the start or end index is out of bounds.
142
+ #[ wasm_bindgen( js_name = "withSegments" ) ]
143
+ pub fn with_segments (
144
+ & self ,
145
+ #[ wasm_bindgen( param_description = "The start index" ) ] start : usize ,
146
+ #[ wasm_bindgen( param_description = "The end index" ) ] end : usize ,
147
+ ) -> Option < Self > {
148
+ let range = start..end;
84
149
Some ( self . as_ref ( ) . with_segments ( range) ?. into ( ) )
85
150
}
86
151
152
+ /// The number of segments in the version.
87
153
#[ wasm_bindgen( getter) ]
88
154
pub fn length ( & self ) -> usize {
89
155
self . as_ref ( ) . segment_count ( )
90
156
}
91
157
92
- /// Create a new version with local segment stripped.
158
+ /// Returns the version without the local part. E.g. `1+2.3` becomes `1`.
159
+ #[ wasm_bindgen( js_name = "stripLocal" ) ]
93
160
pub fn strip_local ( & self ) -> Self {
94
161
self . as_ref ( ) . strip_local ( ) . into_owned ( ) . into ( )
95
162
}
96
163
97
- /// Returns a new version where the major segment of this version has been bumped.
164
+ /// Returns a new version where the major segment of this version has been
165
+ /// bumped.
166
+ #[ wasm_bindgen( js_name = "bumpMajor" ) ]
98
167
pub fn bump_major ( & self ) -> JsResult < Self > {
99
168
Ok ( self . as_ref ( ) . bump ( VersionBumpType :: Major ) . map ( Into :: into) ?)
100
169
}
101
170
102
- /// Returns a new version where the minor segment of this version has been bumped.
171
+ /// Returns a new version where the minor segment of this version has been
172
+ /// bumped.
173
+ #[ wasm_bindgen( js_name = "bumpMinor" ) ]
103
174
pub fn bump_minor ( & self ) -> JsResult < Self > {
104
175
Ok ( self . as_ref ( ) . bump ( VersionBumpType :: Minor ) . map ( Into :: into) ?)
105
176
}
106
177
107
- /// Returns a new version where the patch segment of this version has been bumped.
178
+ /// Returns a new version where the patch segment of this version has been
179
+ /// bumped.
180
+ #[ wasm_bindgen( js_name = "bumpPatch" ) ]
108
181
pub fn bump_patch ( & self ) -> JsResult < Self > {
109
182
Ok ( self . as_ref ( ) . bump ( VersionBumpType :: Patch ) . map ( Into :: into) ?)
110
183
}
111
184
112
- /// Returns a new version where the last segment of this version has been bumped.
185
+ /// Returns a new version where the last segment of this version has been
186
+ /// bumped.
187
+ #[ wasm_bindgen( js_name = "bumpLast" ) ]
113
188
pub fn bump_last ( & self ) -> JsResult < Self > {
114
189
Ok ( self . as_ref ( ) . bump ( VersionBumpType :: Last ) . map ( Into :: into) ?)
115
190
}
116
191
117
- /// Returns a new version where the given segment of this version has been bumped.
118
- pub fn bump_segment ( & self , index : i32 ) -> JsResult < Self > {
192
+ /// Returns a new version where the given segment of this version has been
193
+ /// bumped.
194
+ #[ wasm_bindgen( js_name = "bumpSegment" ) ]
195
+ pub fn bump_segment (
196
+ & self ,
197
+ #[ wasm_bindgen( param_description = "The index of the segment to bump" ) ] index : i32 ,
198
+ ) -> JsResult < Self > {
119
199
Ok ( self
120
200
. as_ref ( )
121
201
. bump ( VersionBumpType :: Segment ( index) )
122
202
. map ( Into :: into) ?)
123
203
}
124
204
125
- /// Returns a new version where the last segment is an "alpha" segment (ie. `.0a0`)
205
+ /// Returns a new version where the last segment is an "alpha" segment (ie.
206
+ /// `.0a0`)
207
+ #[ wasm_bindgen( js_name = "withAlpha" ) ]
126
208
pub fn with_alpha ( & self ) -> Self {
127
209
self . as_ref ( ) . with_alpha ( ) . into_owned ( ) . into ( )
128
210
}
129
211
130
- pub fn equals ( & self , other : & Self ) -> bool {
212
+ /// Compares this version with another version. Returns `true` if the
213
+ /// versions are considered equal.
214
+ ///
215
+ /// Note that two version strings can be considered equal even if they are
216
+ /// not exactly the same. For example, `1.0` and `1` are considered equal.
217
+ #[ wasm_bindgen( js_name = "equals" ) ]
218
+ pub fn equals (
219
+ & self ,
220
+ #[ wasm_bindgen( param_description = "The version to compare with" ) ] other : & Self ,
221
+ ) -> bool {
131
222
self . as_ref ( ) == other. as_ref ( )
132
223
}
133
224
134
- pub fn compare ( & self , other : & Self ) -> i8 {
225
+ /// Compare two versions.
226
+ ///
227
+ /// Returns `-1` if this instance should be ordered before `other`, `0` if
228
+ /// this version and `other` are considered equal, `1` if this version
229
+ /// should be ordered after `other`.
230
+ pub fn compare (
231
+ & self ,
232
+ #[ wasm_bindgen( param_description = "The version to compare with" ) ] other : & Self ,
233
+ ) -> i8 {
135
234
match self . as_ref ( ) . cmp ( other. as_ref ( ) ) {
136
235
Ordering :: Less => -1 ,
137
236
Ordering :: Equal => 0 ,
0 commit comments