Skip to content

Commit

Permalink
Use nom parser for parsing a VersionFilter
Browse files Browse the repository at this point in the history
The nom parser handles the ambiguity of a string with multiple post
release components versus multiple filters, as in:

    1.2.3+a.1,b.1,<2.0

This would fail to parse with the old code. Something like this could
appear in a `fromBuildEnv:` value.

Signed-off-by: J Robert Ray <jrray@jrray.org>
  • Loading branch information
jrray committed May 14, 2024
1 parent 51ada8e commit 4101815
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions crates/spk-schema/crates/foundation/src/version_range/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1344,21 +1344,18 @@ impl FromStr for VersionFilter {
type Err = Error;

fn from_str(range: &str) -> Result<Self> {
let mut out = VersionFilter::default();
if range.is_empty() {
return Ok(out);
}
for rule_str in range.split(VERSION_RANGE_SEP) {
if rule_str.is_empty() {
return Err(Error::String(format!(
"Empty segment not allowed in version range, got: {range}"
)));
}
let rule = VersionRange::from_str(rule_str)?;
out.rules.insert(rule);
}
use nom::combinator::all_consuming;
use nom::error::convert_error;

Ok(out)
all_consuming(parsing::version_range)(range)
.map(|(_, vr)| match vr {
VersionRange::Filter(f) => f,
vr => VersionFilter::single(vr),
})
.map_err(|err| match err {
nom::Err::Error(e) | nom::Err::Failure(e) => Error::String(convert_error(range, e)),
nom::Err::Incomplete(_) => unreachable!(),
})
}
}

Expand Down

0 comments on commit 4101815

Please sign in to comment.