pub struct VersRange { /* private fields */ }Expand description
A parsed vers range.
Experimental: may change in a 0.x minor release.
use libtea_core::vers::VersRange;
let range = VersRange::parse("vers:npm/>=1.0.0|!=1.2.0|<2.0.0").unwrap();
let v = |s: &str| semver::Version::parse(s).unwrap();
assert_eq!(range.contains_semver(&v("1.5.0")), Ok(true));
assert_eq!(range.contains_semver(&v("1.2.0")), Ok(false));
assert_eq!(range.contains_semver(&v("2.0.0")), Ok(false));
assert_eq!(range.contains_semver(&v("2.0.0-rc.1")), Ok(true));Implementations§
Source§impl VersRange
impl VersRange
Sourcepub fn parse(text: &str) -> Result<Self, VersError>
pub fn parse(text: &str) -> Result<Self, VersError>
Parse vers:<type>/<constraints> as the specification’s
“How to parse” describes. Ordering and comparator rules depend on how
the type compares versions, so they are checked by
Self::contains_semver, not here.
§Errors
Returns VersError for a missing or wrong scheme or type, empty
constraints or constraint segments, whitespace, a leading =, an empty
version, a misplaced *, invalid percent-encoding, or a string longer
than MAX_VERS_BYTES.
Sourcepub fn version_type(&self) -> &str
pub fn version_type(&self) -> &str
The vers type, such as npm or semver.
Sourcepub fn constraints(&self) -> &[Constraint]
pub fn constraints(&self) -> &[Constraint]
The constraints in the order written. Empty for *.
Sourcepub fn is_semver_type(&self) -> bool
pub fn is_semver_type(&self) -> bool
Whether this type’s versions are compared as SemVer 2.0 here:
semver, and npm (node-semver versions).
Sourcepub fn contains_semver(&self, version: &Version) -> Result<bool, VersError>
pub fn contains_semver(&self, version: &Version) -> Result<bool, VersError>
Whether version is in the range, comparing as SemVer 2.0 (build
metadata ignored, pre-releases ordered before their release).
The range is validated first: constraint versions must be SemVer, sorted, unique, and in a valid comparator sequence.
§Errors
VersError::UnsupportedType when Self::is_semver_type is false,
VersError::NotSemver for a constraint version that does not parse,
and VersError::Order or VersError::Sequence for a range the
specification calls invalid.
Sourcepub fn is_pypi_type(&self) -> bool
pub fn is_pypi_type(&self) -> bool
Whether this range’s versions are compared as PEP 440 versions here:
the pypi type.
Sourcepub fn contains_pypi(&self, version: &Pep440Version) -> Result<bool, VersError>
pub fn contains_pypi(&self, version: &Pep440Version) -> Result<bool, VersError>
Whether version is in the range, comparing with PEP 440 ordering
(crate::pep440): 1.0 == 1.0.0, and
1.0.dev1 < 1.0a1 < 1.0 < 1.0.post1 < 1.0.1.
Only the ordering is used, as vers asks: PEP 440’s own specifier
rules (such as <1.0 excluding 1.0a1) do not apply, so
vers:pypi/<1.0 contains 1.0a1. The range is validated first, as
for Self::contains_semver.
use libtea_core::pep440::Pep440Version;
use libtea_core::vers::VersRange;
let range = VersRange::parse("vers:pypi/>=1.0|<2.0").unwrap();
let v = |s: &str| Pep440Version::parse(s).unwrap();
assert_eq!(range.contains_pypi(&v("1.5.post1")), Ok(true));
assert_eq!(range.contains_pypi(&v("1.0.0")), Ok(true));
assert_eq!(range.contains_pypi(&v("2.0rc1")), Ok(true));
assert_eq!(range.contains_pypi(&v("2.0")), Ok(false));§Errors
VersError::UnsupportedType when Self::is_pypi_type is false,
VersError::NotPep440 for a constraint version that does not parse,
and VersError::Order or VersError::Sequence for a range the
specification calls invalid (including 1.0|1.0.0, which repeats a
version).