logo
pub struct VersionReq { /* private fields */ }
Expand description

A VersionReq is a struct containing a list of predicates that can apply to ranges of version numbers. Matching operations can then be done with the VersionReq against a particular version to see if it satisfies some or all of the constraints.

Implementations

any() is a factory method which creates a VersionReq with no constraints. In other words, any version will match against it.

Examples
use semver::VersionReq;

let anything = VersionReq::any();

parse() is the main constructor of a VersionReq. It takes a string like "^1.2.3" and turns it into a VersionReq that matches that particular constraint.

A Result is returned which contains a ReqParseError if there was a problem parsing the VersionReq. ReqParseError: enum.ReqParseError.html

Examples
use semver::VersionReq;

let version = VersionReq::parse("=1.2.3");
let version = VersionReq::parse(">1.2.3");
let version = VersionReq::parse("<1.2.3");
let version = VersionReq::parse("~1.2.3");
let version = VersionReq::parse("^1.2.3");
let version = VersionReq::parse("1.2.3"); // synonym for ^1.2.3
let version = VersionReq::parse("<=1.2.3");
let version = VersionReq::parse(">=1.2.3");

This example demonstrates error handling, and will panic.

use semver::VersionReq;

let version = match VersionReq::parse("not a version") {
    Ok(version) => version,
    Err(e) => panic!("There was a problem parsing: {}", e),
};
Errors

Returns an error variant if the input could not be parsed as a semver requirement.

Examples of common error causes are as follows:

  • \0 - an invalid version requirement is used.
  • >= >= 1.2.3 - multiple operations are used. Only use one.
  • >== 1.2.3 - an invalid operation is used.
  • a.0.0 - version components are not numeric.
  • 1.2.3- - an invalid identifier is present.
  • >= - major version was not specified. At least a major version is required.
  • 0.2* - deprecated requirement syntax. Equivalent would be 0.2.*.

You may also encounter an UnimplementedVersionRequirement error, which indicates that a given requirement syntax is not yet implemented in this crate.

exact() is a factory method which creates a VersionReq with one exact constraint.

Examples
use semver::VersionReq;
use semver::Version;

let version = Version { major: 1, minor: 1, patch: 1, pre: vec![], build: vec![] };
let exact = VersionReq::exact(&version);

matches() matches a given Version against this VersionReq. Version: struct.Version.html

Examples
use semver::VersionReq;
use semver::Version;

let version = Version { major: 1, minor: 1, patch: 1, pre: vec![], build: vec![] };
let exact = VersionReq::exact(&version);

assert!(exact.matches(&version));

is_exact() returns true if there is exactly one version which could match this VersionReq. If false is returned, it is possible that there may still only be exactly one version which could match this VersionReq. This function is intended do allow short-circuiting more complex logic where being able to handle only the possibility of a single exact version may be cheaper.

Examples
use semver::ReqParseError;
use semver::VersionReq;

fn use_is_exact() -> Result<(), ReqParseError> {
  assert!(VersionReq::parse("=1.0.0")?.is_exact());
  assert!(!VersionReq::parse("=1.0")?.is_exact());
  assert!(!VersionReq::parse(">=1.0.0")?.is_exact());
  Ok(())
}

use_is_exact().unwrap();

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Deserialize this value from the given Serde deserializer. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

The associated error which can be returned from parsing.

Parses a string s to return a value of this type. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.