@@ -147,7 +147,7 @@ impl PartialOrd for VersionModifier {
147147}
148148
149149/// An exact, 3-number Semver version. With some possible extras.
150- #[ derive( Clone , Default , Deserialize , Eq ) ]
150+ #[ derive( Clone , Default , Debug , Deserialize , Eq ) ]
151151pub struct Version {
152152 pub major : Option < u32 > ,
153153 pub minor : Option < u32 > ,
@@ -318,45 +318,43 @@ impl FromStr for Version {
318318
319319impl Ord for Version {
320320 fn cmp ( & self , other : & Self ) -> cmp:: Ordering {
321- // None version modifiers should rank highest. Ie 17.0 > 17.0rc1
322- let self_mod = self . modifier . clone ( ) . unwrap_or ( ( VersionModifier :: Null , 0 ) ) ;
323- let other_mod = other. modifier . clone ( ) . unwrap_or ( ( VersionModifier :: Null , 0 ) ) ;
324- // Mirror the set field if we have a star present
325- let cmp_star = |obj : Option < u32 > , oth : Option < u32 > , star : bool | -> cmp:: Ordering {
326- let none_val = if star {
327- if obj. is_none ( ) && oth. is_none ( ) {
328- 0
329- } else if let Some ( x) = obj {
330- x
321+ // 1. Compare numeric components. Treat None as 0 for stability.
322+ let fields = [
323+ ( self . major , other. major ) ,
324+ ( self . minor , other. minor ) ,
325+ ( self . patch , other. patch ) ,
326+ ( self . extra_num , other. extra_num ) ,
327+ ] ;
328+
329+ for ( s, o) in fields {
330+ let res = s. unwrap_or ( 0 ) . cmp ( & o. unwrap_or ( 0 ) ) ;
331+ if res != cmp:: Ordering :: Equal {
332+ return res;
333+ }
334+ }
335+
336+ // 2. Handle Stars: If one is a wildcard and the other isn't,
337+ // define a stable rule (e.g., specific versions are "greater" than wildcards)
338+ if self . star != other. star {
339+ return self . star . cmp ( & other. star ) ;
340+ }
341+
342+ // 3. Handle Modifiers
343+ let self_mod = self . modifier . as_ref ( ) . map ( |( m, v) | ( m, * v) ) ;
344+ let other_mod = other. modifier . as_ref ( ) . map ( |( m, v) | ( m, * v) ) ;
345+
346+ match ( self_mod, other_mod) {
347+ ( None , Some ( _) ) => cmp:: Ordering :: Greater , // None is higher (e.g., 1.0 > 1.0-rc1)
348+ ( Some ( _) , None ) => cmp:: Ordering :: Less ,
349+ ( Some ( ( m1, v1) ) , Some ( ( m2, v2) ) ) => {
350+ let res = m1. cmp ( m2) ;
351+ if res == cmp:: Ordering :: Equal {
352+ v1. cmp ( & v2)
331353 } else {
332- oth . unwrap_or ( 0 )
354+ res
333355 }
334- } else {
335- 0
336- } ;
337- obj. unwrap_or ( none_val) . cmp ( & oth. unwrap_or ( none_val) )
338- } ;
339- let star = self . star || other. star ;
340- let maj = cmp_star ( self . major , other. major , star) ;
341- let min = cmp_star ( self . minor , other. minor , star) ;
342- let pat = cmp_star ( self . patch , other. patch , star) ;
343- let ext = cmp_star ( self . extra_num , other. extra_num , star) ;
344- if !matches ! ( maj, cmp:: Ordering :: Equal ) {
345- maj
346- } else if !matches ! ( min, cmp:: Ordering :: Equal ) {
347- min
348- } else if !matches ! ( pat, cmp:: Ordering :: Equal ) {
349- pat
350- } else if !matches ! ( ext, cmp:: Ordering :: Equal ) {
351- ext
352- } else if !star {
353- if self_mod. 0 == other_mod. 0 {
354- self_mod. 1 . cmp ( & other_mod. 1 )
355- } else {
356- self_mod. 0 . cmp ( & other_mod. 0 )
357356 }
358- } else {
359- cmp:: Ordering :: Equal
357+ ( None , None ) => cmp:: Ordering :: Equal ,
360358 }
361359 }
362360}
@@ -427,11 +425,11 @@ impl fmt::Display for Version {
427425 }
428426}
429427
430- impl fmt:: Debug for Version {
431- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
432- write ! ( f, "{}" , & self . to_string( ) )
433- }
434- }
428+ // impl fmt::Debug for Version {
429+ // fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
430+ // write!(f, "{}", &self.to_string())
431+ // }
432+ // }
435433
436434/// Specify the type of version requirement
437435#[ derive( Clone , Copy , Debug , Deserialize , PartialEq , Serialize ) ]
0 commit comments