@@ -10,8 +10,6 @@ use serde::{
1010 de:: { self , Deserializer } ,
1111} ;
1212
13- pub use pubgrub:: report as pubgrub_report;
14-
1513mod lexer;
1614mod parser;
1715#[ cfg( test) ]
@@ -101,7 +99,7 @@ impl Version {
10199 }
102100
103101 /// Parse a Hex compatible version range. i.e. `> 1 and < 2 or == 4.5.2`.
104- fn parse_range ( input : & str ) -> Result < pubgrub:: range :: Range < Version > , parser:: Error > {
102+ fn parse_range ( input : & str ) -> Result < pubgrub:: Range < Version > , parser:: Error > {
105103 let mut parser = Parser :: new ( input) ?;
106104 let version = parser. range ( ) ?;
107105 if !parser. is_eof ( ) {
@@ -117,6 +115,10 @@ impl Version {
117115 Ok ( version)
118116 }
119117
118+ pub fn lowest ( ) -> Self {
119+ Self :: new ( 0 , 0 , 0 )
120+ }
121+
120122 fn tuple ( & self ) -> ( u32 , u32 , u32 , PreOrder < ' _ > ) {
121123 (
122124 self . major ,
@@ -131,6 +133,21 @@ impl Version {
131133 }
132134}
133135
136+ pub trait LowestVersion {
137+ fn lowest_version ( & self ) -> Option < Version > ;
138+ }
139+ impl LowestVersion for pubgrub:: Range < Version > {
140+ fn lowest_version ( & self ) -> Option < Version > {
141+ self . iter ( )
142+ . flat_map ( |( lower, _higher) | match lower {
143+ std:: ops:: Bound :: Included ( v) => Some ( v. clone ( ) ) ,
144+ std:: ops:: Bound :: Excluded ( _) => None ,
145+ std:: ops:: Bound :: Unbounded => Some ( Version :: lowest ( ) ) ,
146+ } )
147+ . min ( )
148+ }
149+ }
150+
134151impl < ' de > Deserialize < ' de > for Version {
135152 fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
136153 where
@@ -162,104 +179,6 @@ impl std::cmp::Ord for Version {
162179 }
163180}
164181
165- impl pubgrub:: version:: Version for Version {
166- fn lowest ( ) -> Self {
167- Self :: new ( 0 , 0 , 0 )
168- }
169-
170- fn bump ( & self ) -> Self {
171- if self . is_pre ( ) {
172- let mut pre = self . pre . clone ( ) ;
173- let last_component = pre
174- . last_mut ( )
175- // This `.expect` is safe, as we know there to be at least
176- // one pre-release component.
177- . expect ( "no pre-release components" ) ;
178-
179- match last_component {
180- Identifier :: Numeric ( pre) => * pre += 1 ,
181- Identifier :: AlphaNumeric ( pre) => {
182- let mut segments = split_alphanumeric ( pre) ;
183- let last_segment = segments. last_mut ( ) . unwrap ( ) ;
184-
185- match last_segment {
186- AlphaOrNumeric :: Numeric ( n) => * n += 1 ,
187- AlphaOrNumeric :: Alpha ( alpha) => {
188- // We should potentially be smarter about this (for instance, pick the next letter in the
189- // alphabetic sequence), however, this seems like it could be quite a bit more complex.
190- alpha. push ( '1' )
191- }
192- }
193-
194- * pre = segments
195- . into_iter ( )
196- . map ( |segment| match segment {
197- AlphaOrNumeric :: Alpha ( segment) => segment,
198- AlphaOrNumeric :: Numeric ( segment) => segment. to_string ( ) ,
199- } )
200- . collect :: < Vec < _ > > ( )
201- . join ( "" ) ;
202- }
203- }
204-
205- Self {
206- major : self . major ,
207- minor : self . minor ,
208- patch : self . patch ,
209- pre,
210- build : None ,
211- }
212- } else {
213- self . bump_patch ( )
214- }
215- }
216- }
217-
218- enum AlphaOrNumeric {
219- Alpha ( String ) ,
220- Numeric ( u32 ) ,
221- }
222-
223- /// Splits the given string into alphabetic and numeric segments.
224- fn split_alphanumeric ( str : & str ) -> Vec < AlphaOrNumeric > {
225- let mut segments = Vec :: new ( ) ;
226- let mut current_segment = String :: new ( ) ;
227- let mut previous_char_was_numeric = None ;
228-
229- for char in str. chars ( ) {
230- let is_numeric = char. is_ascii_digit ( ) ;
231- match previous_char_was_numeric {
232- Some ( previous_char_was_numeric) if previous_char_was_numeric == is_numeric => {
233- current_segment. push ( char)
234- }
235- _ => {
236- if !current_segment. is_empty ( ) {
237- if current_segment. chars ( ) . any ( |char| char. is_ascii_digit ( ) ) {
238- segments. push ( AlphaOrNumeric :: Numeric ( current_segment. parse ( ) . unwrap ( ) ) ) ;
239- } else {
240- segments. push ( AlphaOrNumeric :: Alpha ( current_segment) ) ;
241- }
242-
243- current_segment = String :: new ( ) ;
244- }
245-
246- current_segment. push ( char) ;
247- previous_char_was_numeric = Some ( is_numeric) ;
248- }
249- }
250- }
251-
252- if !current_segment. is_empty ( ) {
253- if current_segment. chars ( ) . any ( |char| char. is_ascii_digit ( ) ) {
254- segments. push ( AlphaOrNumeric :: Numeric ( current_segment. parse ( ) . unwrap ( ) ) ) ;
255- } else {
256- segments. push ( AlphaOrNumeric :: Alpha ( current_segment) ) ;
257- }
258- }
259-
260- segments
261- }
262-
263182impl < ' a > TryFrom < & ' a str > for Version {
264183 type Error = parser:: Error ;
265184
@@ -317,18 +236,18 @@ impl Identifier {
317236#[ derive( Clone , PartialEq , Eq ) ]
318237pub struct Range {
319238 spec : String ,
320- range : pubgrub:: range :: Range < Version > ,
239+ range : pubgrub:: Range < Version > ,
321240}
322241
323242impl Range {
324243 pub fn new ( spec : String ) -> Result < Self , parser:: Error > {
325- let range = Version :: parse_range ( spec. as_str ( ) ) ?;
244+ let range = Version :: parse_range ( & spec) ?;
326245 Ok ( Self { spec, range } )
327246 }
328247}
329248
330249impl Range {
331- pub fn to_pubgrub ( & self ) -> & pubgrub:: range :: Range < Version > {
250+ pub fn to_pubgrub ( & self ) -> & pubgrub:: Range < Version > {
332251 & self . range
333252 }
334253
@@ -337,16 +256,15 @@ impl Range {
337256 }
338257}
339258
340- impl From < pubgrub:: range :: Range < Version > > for Range {
341- fn from ( range : pubgrub:: range :: Range < Version > ) -> Self {
259+ impl From < pubgrub:: Range < Version > > for Range {
260+ fn from ( range : pubgrub:: Range < Version > ) -> Self {
342261 let spec = range. to_string ( ) ;
343262 Self { spec, range }
344263 }
345264}
346-
347265impl From < Version > for Range {
348266 fn from ( version : Version ) -> Self {
349- pubgrub:: range :: Range :: exact ( version) . into ( )
267+ pubgrub:: Range :: singleton ( version) . into ( )
350268 }
351269}
352270
0 commit comments