Skip to content

Commit f7775a8

Browse files
Kaholazlpil
authored andcommitted
Upgrade pubgrub version.
1 parent dd025df commit f7775a8

File tree

4 files changed

+41
-144
lines changed

4 files changed

+41
-144
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ ring = "0.17"
3333
# PEM -> DER conversion
3434
x509-parser = "0.15"
3535
# Pubgrub dependency resolution algorithm
36-
pubgrub = "0.2"
36+
pubgrub = "0.3"
3737
# Basic auth HTTP helper
3838
http-auth-basic = "0.3"
3939
# base16 encoding

src/version.rs

Lines changed: 26 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ use serde::{
1010
de::{self, Deserializer},
1111
};
1212

13-
pub use pubgrub::report as pubgrub_report;
14-
1513
mod lexer;
1614
mod 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+
134151
impl<'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-
263182
impl<'a> TryFrom<&'a str> for Version {
264183
type Error = parser::Error;
265184

@@ -317,18 +236,18 @@ impl Identifier {
317236
#[derive(Clone, PartialEq, Eq)]
318237
pub struct Range {
319238
spec: String,
320-
range: pubgrub::range::Range<Version>,
239+
range: pubgrub::Range<Version>,
321240
}
322241

323242
impl 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

330249
impl 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-
347265
impl 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

src/version/parser.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use super::lexer::{self, Lexer, Token};
88
use crate::version::{Identifier, Version};
99
use thiserror::Error;
1010

11-
type PubgrubRange = pubgrub::range::Range<Version>;
11+
type PubgrubRange = pubgrub::Range<Version>;
1212

1313
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Error)]
1414
pub enum Error {
@@ -322,11 +322,11 @@ impl<'input> Parser<'input> {
322322
self.skip_whitespace()?;
323323
match self.peek() {
324324
None => break,
325-
Some(Numeric(_)) => range = and(range, PubgrubRange::exact(self.version()?)),
325+
Some(Numeric(_)) => range = and(range, PubgrubRange::singleton(self.version()?)),
326326

327327
Some(Eq) => {
328328
self.pop()?;
329-
range = and(range, PubgrubRange::exact(self.version()?));
329+
range = and(range, PubgrubRange::singleton(self.version()?));
330330
}
331331

332332
Some(NotEq) => {

src/version/tests.rs

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::cmp::Ordering::{Equal, Greater, Less};
22
use std::collections::HashMap;
33

44
use parser::Error;
5-
use pubgrub::version::Version as _;
65

76
use super::{
87
Identifier::{AlphaNumeric, Numeric},
@@ -208,17 +207,21 @@ fn v_(major: u32, minor: u32, patch: u32, pre: Vec<Identifier>, build: Option<St
208207
}
209208
}
210209

211-
type PubgrubRange = pubgrub::range::Range<Version>;
210+
type PubgrubRange = pubgrub::Range<Version>;
212211

213-
parse_range_test!(leading_space, " 1.2.3", PubgrubRange::exact(v(1, 2, 3)));
214-
parse_range_test!(trailing_space, "1.2.3 ", PubgrubRange::exact(v(1, 2, 3)));
212+
parse_range_test!(leading_space, " 1.2.3", PubgrubRange::singleton(v(1, 2, 3)));
213+
parse_range_test!(
214+
trailing_space,
215+
"1.2.3 ",
216+
PubgrubRange::singleton(v(1, 2, 3))
217+
);
215218

216-
parse_range_test!(eq_triplet, "== 1.2.3 ", PubgrubRange::exact(v(1, 2, 3)));
219+
parse_range_test!(eq_triplet, "== 1.2.3 ", PubgrubRange::singleton(v(1, 2, 3)));
217220

218221
parse_range_test!(
219222
eq_triplet_nospace,
220223
"==1.2.3 ",
221-
PubgrubRange::exact(v(1, 2, 3))
224+
PubgrubRange::singleton(v(1, 2, 3))
222225
);
223226

224227
parse_range_test!(
@@ -227,12 +230,12 @@ parse_range_test!(
227230
PubgrubRange::strictly_lower_than(v(1, 2, 3)).union(&PubgrubRange::higher_than(v(1, 2, 4)))
228231
);
229232

230-
parse_range_test!(implicit_eq, "2.2.3", PubgrubRange::exact(v(2, 2, 3)));
233+
parse_range_test!(implicit_eq, "2.2.3", PubgrubRange::singleton(v(2, 2, 3)));
231234

232235
parse_range_test!(
233236
range_pre_build,
234237
"1.2.3-thing+oop",
235-
PubgrubRange::exact(v_(
238+
PubgrubRange::singleton(v_(
236239
1,
237240
2,
238241
3,
@@ -388,30 +391,6 @@ assert_order!(ord_pre_smaller_than_zero_flip, "1.0.0-rc1", Less, "1.0.0");
388391

389392
assert_order!(ord_pre_rc1_2, "1.0.0-rc1", Less, "1.0.0-rc2");
390393

391-
#[test]
392-
fn test_pubgrub_bump_patch() {
393-
assert_eq!(
394-
Version::parse("1.0.0").unwrap().bump(),
395-
Version::parse("1.0.1").unwrap()
396-
);
397-
}
398-
399-
#[test]
400-
fn test_pubgrub_bump_prerelease_ending_with_a_number() {
401-
assert_eq!(
402-
Version::parse("1.0.0-rc2").unwrap().bump(),
403-
Version::parse("1.0.0-rc3").unwrap()
404-
);
405-
}
406-
407-
#[test]
408-
fn test_pubgrub_bump_prerelease_ending_with_a_letter() {
409-
assert_eq!(
410-
Version::parse("1.0.0-rc2a").unwrap().bump(),
411-
Version::parse("1.0.0-rc2a1").unwrap()
412-
);
413-
}
414-
415394
#[test]
416395
fn manifest_toml() {
417396
let manifest = toml::to_string(

0 commit comments

Comments
 (0)