Skip to content

Commit

Permalink
adds parsing @ ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
jhheider committed Jan 4, 2025
1 parent 241658b commit 454ad14
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
16 changes: 15 additions & 1 deletion lib/src/range/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ lazy_static! {
static ref RANGE_REGEX: Regex = Regex::new(r"\s*(,|\|\|)\s*").unwrap();
static ref CONSTRAINT_REGEX_RANGE: Regex =
Regex::new(r"^>=((\d+\.)*\d+)\s*(<((\d+\.)*\d+))?$").unwrap();
static ref CONSTRAINT_REGEX_SIMPLE: Regex = Regex::new(r"^([~=<^])(.+)$").unwrap();
static ref CONSTRAINT_REGEX_SIMPLE: Regex = Regex::new(r"^([~=<^@])(.+)$").unwrap();
}

impl Range {
Expand Down Expand Up @@ -91,6 +91,20 @@ impl Constraint {
let v2 = Semver::parse(cap.get(2).context("invalid description")?.as_str())?;
Ok(Constraint::Contiguous(v1, v2))
}
"@" => {
let v1 = Semver::parse(cap.get(2).context("invalid description")?.as_str())?;
let mut parts = v1.components.clone();
let last = parts.last_mut().context("version too short")?;
*last += 1;
let v2 = Semver::parse(
&parts
.iter()
.map(|c| c.to_string())
.collect::<Vec<_>>()
.join("."),
)?;
Ok(Constraint::Contiguous(v1, v2))
}
"=" => Ok(Constraint::Single(Semver::parse(
cap.get(2).context("invalid description")?.as_str(),
)?)),
Expand Down
36 changes: 22 additions & 14 deletions lib/src/tests/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ fn test_parse() -> Result<()> {
let j = Range::parse("Your mom");
let k = Range::parse("");
let l = Range::parse(">=12");
let m = Range::parse("@1");
let n = Range::parse("@1.1");
let o = Range::parse("@1.1.1");
let p = Range::parse("@1.1.1.1");

assert!(a.is_ok());
assert!(b.is_ok());
Expand All @@ -29,6 +33,10 @@ fn test_parse() -> Result<()> {
assert!(j.is_err());
assert!(k.is_err());
assert!(l.is_ok());
assert!(m.is_ok());
assert!(n.is_ok());
assert!(o.is_ok());
assert!(p.is_ok());

assert_eq!(f?.set.len(), 5);

Expand Down Expand Up @@ -207,40 +215,40 @@ fn test_intersect() -> Result<()> {
#[test]
fn test_at() -> Result<()> {
let ra = Range::parse(">=1.0<1.1")?;

assert_eq!(format!("{ra}"), "~1");

let rb = Range::parse("=1.1")?;

assert_eq!(format!("{ra}"), "~1");
assert_eq!(format!("{rb}"), "=1.1");

let rc = Range::parse(">=1.1.0<1.1.1")?;

assert_eq!(format!("{rc}"), "@1.1.0");

let rd = Range::parse("=1.1.1")?;

assert_eq!(format!("{rc}"), "@1.1.0");
assert_eq!(format!("{rd}"), "=1.1.1");

let re = Range::parse(">=1.1.1.0<1.1.1.1")?;

assert_eq!(format!("{re}"), "@1.1.1.0");

let rf = Range::parse("=1.1.1.0")?;

assert_eq!(format!("{re}"), "@1.1.1.0");
assert_eq!(format!("{rf}"), "=1.1.1.0");

let rg = Range::parse(">=1.1<1.1.1.1.1")?;

assert_eq!(format!("{rg}"), ">=1.1<1.1.1.1.1");

let rh = Range::parse(">=1.1.1<1.1.3")?;
let ri = Range::parse(">=1.1.1<1.2.2")?;

assert_eq!(format!("{rg}"), ">=1.1<1.1.1.1.1");
assert_eq!(format!("{rh}"), ">=1.1.1<1.1.3");
assert_eq!(format!("{ri}"), ">=1.1.1<1.2.2");

let ri = Range::parse(">=1.1.1<1.2.2")?;
let rj = Range::parse("@1")?;
let rk = Range::parse("@1.1")?;
let rl = Range::parse("@1.1.1")?;
let rm = Range::parse("@1.1.1.1")?;

assert_eq!(format!("{ri}"), ">=1.1.1<1.2.2");
assert_eq!(format!("{rj}"), "^1");
assert_eq!(format!("{rk}"), "~1.1");
assert_eq!(format!("{rl}"), "@1.1.1");
assert_eq!(format!("{rm}"), "@1.1.1.1");

Ok(())
}
Expand Down

0 comments on commit 454ad14

Please sign in to comment.