Skip to content

satisfies

Whether the version version satisfies the requirement requirement, both given as text.

A shortcut for Version::parse_lenient, VersionReq::parse and VersionReq::matches: use those directly to check many versions against one requirement.

use helpers4::version::satisfies;

Cargo feature version (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features version

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.6", default-features = false, features = ["version"] }
pub fn satisfies(version: &str, requirement: &str) -> Result<bool, ParseVersionError>
ParameterTypeDescription
version&strThe version to check, such as "1.4.2".
requirement&strThe requirement, such as "^1.2" (see [VersionReq] for the syntax).

Result<bool, ParseVersionError>Ok on success, otherwise an Err: see Errors.

The ParseVersionError of whichever string does not parse.

use helpers4::version::satisfies;

assert!(satisfies("1.4.2", "^1.2")?);
assert!(!satisfies("2.0.0", "^1.2")?);
assert!(satisfies("0.3.1", ">=0.3, <0.4")?);

Why a version or a version requirement could not be parsed.

use helpers4::version::ParseVersionError;

#[non_exhaustive]
pub enum ParseVersionError {
    /// The text is empty or only whitespace.
    Empty,
    /// A part is missing: `"1.2"` has no patch.
    MissingComponent {
        /// `"minor"` or `"patch"`.
        component: &'static str,
    },
    /// A part is not a number (or does not fit a `u64`).
    InvalidNumber {
        /// `"major"`, `"minor"` or `"patch"`.
        component: &'static str,
    },
    /// A number has a leading zero (`"01"`), which semver forbids.
    LeadingZero {
        /// `"major"`, `"minor"`, `"patch"` or `"prerelease"` (a numeric identifier).
        component: &'static str,
    },
    /// A pre-release or build identifier is empty or has a character outside `[0-9A-Za-z-]`.
    InvalidIdentifier,
    /// A requirement comparator is malformed: unknown operator, a wildcard where it is not
    /// allowed, a pre-release on a partial version, or build metadata.
    InvalidComparator,
}

src/version/satisfies.rs