1- use core:: { fmt, ops :: Bound } ;
1+ use core:: fmt;
22
33#[ cfg( feature = "serde" ) ]
44use miden_assembly_syntax:: debuginfo:: SourceId ;
@@ -71,12 +71,15 @@ impl PartialEq for VersionRequirement {
7171 }
7272}
7373
74+ #[ cfg( false ) ]
7475impl PartialOrd for VersionRequirement {
7576 fn partial_cmp ( & self , other : & Self ) -> Option < core:: cmp:: Ordering > {
7677 Some ( self . cmp ( other) )
7778 }
7879}
7980
81+ /// Ordering version requirements is used
82+ #[ cfg( false ) ]
8083impl Ord for VersionRequirement {
8184 fn cmp ( & self , other : & Self ) -> core:: cmp:: Ordering {
8285 use core:: { cmp:: Ordering , ops:: Bound :: * } ;
@@ -98,13 +101,33 @@ impl Ord for VersionRequirement {
98101 } ,
99102 ( Included ( l) , Excluded ( r) ) => match l. cmp ( & r) {
100103 Ordering :: Equal | Ordering :: Less => return Ordering :: Less ,
101- Ordering :: Greater => todo ! ( ) ,
104+ Ordering :: Greater => return Ordering :: Greater ,
102105 } ,
103106 ( Excluded ( l) , Included ( r) ) => match l. cmp ( & r) {
104107 Ordering :: Equal | Ordering :: Greater => return Ordering :: Greater ,
105- Ordering :: Less => todo ! ( ) ,
108+ Ordering :: Less => {
109+ // While `l` is less than `r`, `l` is excluded from the range, so if
110+ // the next possible version after `l` is still in its bounding range,
111+ // and is still less than `r`, then the left-hand requirement is lesser,
112+ // otherwise we consider it greater than `r`.
113+ let next_patch_version = SemVer {
114+ patch : l. patch . saturating_add ( 1 ) ,
115+ ..l. clone ( )
116+ } ;
117+ if next_patch_version < r
118+ && ( l_max == Unbounded
119+ || matches ! ( l_max, Included ( lmax) if lmax >= next_patch_version) )
120+ {
121+ return Ordering :: Less ;
122+ } else {
123+ return Ordering :: Greater ;
124+ }
125+ } ,
106126 } ,
107- }
127+ } ;
128+
129+ // The lower bounds are equal/unbounded, so the upper bounds determine which range
130+ // should
108131 match ( l_max, r_max) {
109132 ( Unbounded , Unbounded ) => Ordering :: Equal ,
110133 ( Unbounded , _) => Ordering :: Less ,
@@ -144,259 +167,3 @@ impl From<Word> for VersionRequirement {
144167 Self :: Digest ( Span :: unknown ( digest) )
145168 }
146169}
147-
148- /// Convert a [VersionReq] to a set of concrete [SemVer] bounds, for used with [BTreeMap::range].
149- ///
150- /// This takes the semantic versioning constraints of `req` and finds the minimum and maximum
151- /// versions that can satisfy `req` based on the rules for satisfying those constraints.
152- ///
153- /// To determine if the resulting range is empty or not, use `Range::contains` on the resulting
154- /// tuple.
155- pub ( crate ) fn bounding_range ( req : & VersionReq ) -> ( Bound < SemVer > , Bound < SemVer > ) {
156- use Bound :: * ;
157-
158- use crate :: semver:: { BuildMetadata , Op , Prerelease } ;
159-
160- let mut min = None ;
161- let mut max = None ;
162- let mut min_inclusive = true ;
163- let mut max_inclusive = false ;
164- for comparator in & req. comparators {
165- let major = comparator. major ;
166- let minor = comparator. minor . unwrap_or ( 0 ) ;
167- let patch = comparator. patch . unwrap_or ( 0 ) ;
168- let pre = if comparator. patch . is_some ( ) {
169- comparator. pre . clone ( )
170- } else {
171- Prerelease :: EMPTY
172- } ;
173- match comparator. op {
174- Op :: Exact | Op :: Wildcard => {
175- match ( comparator. minor . is_some ( ) , comparator. patch . is_some ( ) ) {
176- ( true , true ) => {
177- min = Some ( SemVer {
178- major,
179- minor,
180- patch,
181- pre,
182- build : BuildMetadata :: EMPTY ,
183- } ) ;
184- max = min. clone ( ) ;
185- max_inclusive = true ;
186- } ,
187- ( true , false ) => {
188- let min_version = SemVer {
189- major,
190- minor,
191- patch,
192- pre,
193- build : BuildMetadata :: EMPTY ,
194- } ;
195- let max_version = SemVer { minor : minor + 1 , ..min_version. clone ( ) } ;
196- min = Some ( min_version) ;
197- max = Some ( max_version) ;
198- } ,
199- ( false , false ) => {
200- let min_version = SemVer {
201- major,
202- minor,
203- patch,
204- pre,
205- build : BuildMetadata :: EMPTY ,
206- } ;
207- let max_version = SemVer { major : major + 1 , ..min_version. clone ( ) } ;
208- min = Some ( min_version) ;
209- max = Some ( max_version) ;
210- } ,
211- _ => unreachable ! ( ) ,
212- }
213- } ,
214- Op :: Caret => {
215- max_inclusive = false ;
216- match ( comparator. minor . is_some ( ) , comparator. patch . is_some ( ) ) {
217- ( true , true ) if major > 0 => {
218- min = Some ( SemVer {
219- major,
220- minor,
221- patch,
222- pre,
223- build : BuildMetadata :: EMPTY ,
224- } ) ;
225- max = Some ( SemVer {
226- major : major + 1 ,
227- minor : 0 ,
228- patch : 0 ,
229- pre : Prerelease :: EMPTY ,
230- build : BuildMetadata :: EMPTY ,
231- } ) ;
232- } ,
233- ( true , true ) if minor > 0 => {
234- min = Some ( SemVer {
235- major,
236- minor,
237- patch,
238- pre,
239- build : BuildMetadata :: EMPTY ,
240- } ) ;
241- max = Some ( SemVer {
242- major,
243- minor : minor + 1 ,
244- patch : 0 ,
245- pre : Prerelease :: EMPTY ,
246- build : BuildMetadata :: EMPTY ,
247- } ) ;
248- } ,
249- ( true , true ) => {
250- max_inclusive = true ;
251- min = Some ( SemVer {
252- major,
253- minor,
254- patch,
255- pre,
256- build : BuildMetadata :: EMPTY ,
257- } ) ;
258- max = min. clone ( ) ;
259- } ,
260- ( true , false ) if major > 0 || minor > 0 => {
261- min = Some ( SemVer {
262- major,
263- minor,
264- patch,
265- pre,
266- build : BuildMetadata :: EMPTY ,
267- } ) ;
268- max = Some ( SemVer {
269- major : major + 1 ,
270- minor : 0 ,
271- patch : 0 ,
272- pre : Prerelease :: EMPTY ,
273- build : BuildMetadata :: EMPTY ,
274- } ) ;
275- } ,
276- ( true , false ) => {
277- min = Some ( SemVer {
278- major,
279- minor,
280- patch,
281- pre,
282- build : BuildMetadata :: EMPTY ,
283- } ) ;
284- max = Some ( SemVer {
285- major,
286- minor : minor + 1 ,
287- patch : 0 ,
288- pre : Prerelease :: EMPTY ,
289- build : BuildMetadata :: EMPTY ,
290- } ) ;
291- } ,
292- ( false , false ) => {
293- min = Some ( SemVer {
294- major,
295- minor,
296- patch,
297- pre,
298- build : BuildMetadata :: EMPTY ,
299- } ) ;
300- max = Some ( SemVer {
301- major : major + 1 ,
302- minor : 0 ,
303- patch : 0 ,
304- pre : Prerelease :: EMPTY ,
305- build : BuildMetadata :: EMPTY ,
306- } ) ;
307- } ,
308- _ => unreachable ! ( ) ,
309- }
310- } ,
311- Op :: Tilde => {
312- max_inclusive = false ;
313-
314- min = Some ( SemVer {
315- major,
316- minor,
317- patch,
318- pre,
319- build : BuildMetadata :: EMPTY ,
320- } ) ;
321- if comparator. minor . is_some ( ) {
322- max = Some ( SemVer {
323- major,
324- minor : minor + 1 ,
325- patch : 0 ,
326- pre : Prerelease :: EMPTY ,
327- build : BuildMetadata :: EMPTY ,
328- } ) ;
329- } else {
330- max = Some ( SemVer {
331- major : major + 1 ,
332- minor : 0 ,
333- patch : 0 ,
334- pre : Prerelease :: EMPTY ,
335- build : BuildMetadata :: EMPTY ,
336- } ) ;
337- }
338- } ,
339- Op :: Greater => {
340- min_inclusive = false ;
341- let mut min_version = SemVer {
342- major,
343- minor,
344- patch,
345- pre,
346- build : BuildMetadata :: EMPTY ,
347- } ;
348- if comparator. minor . is_none ( ) {
349- min_version. major += 1 ;
350- } else if comparator. patch . is_none ( ) {
351- min_version. minor += 1 ;
352- }
353- min = Some ( min_version) ;
354- } ,
355- Op :: GreaterEq => {
356- min = Some ( SemVer {
357- major,
358- minor,
359- patch,
360- pre,
361- build : BuildMetadata :: EMPTY ,
362- } ) ;
363- } ,
364- Op :: Less => {
365- max = Some ( SemVer {
366- major,
367- minor,
368- patch,
369- pre,
370- build : BuildMetadata :: EMPTY ,
371- } ) ;
372- } ,
373- Op :: LessEq => {
374- max_inclusive = true ;
375- let minor = if comparator. patch . is_some ( ) { minor } else { minor + 1 } ;
376- let major = if comparator. minor . is_some ( ) { major } else { major + 1 } ;
377- max = Some ( SemVer {
378- major,
379- minor,
380- patch,
381- pre,
382- build : BuildMetadata :: EMPTY ,
383- } ) ;
384- } ,
385- op => panic ! ( "unhandled semantic versioning operator: {op:#?}" ) ,
386- }
387- }
388-
389- let min = if let Some ( min) = min {
390- if min_inclusive { Included ( min) } else { Excluded ( min) }
391- } else {
392- Unbounded
393- } ;
394-
395- let max = if let Some ( max) = max {
396- if max_inclusive { Included ( max) } else { Excluded ( max) }
397- } else {
398- Unbounded
399- } ;
400-
401- ( min, max)
402- }
0 commit comments