Expand description
Semantic version parsing and comparison (semver). The implementation of this crate is based on the node-semver npm package. The tests are taken directly from node-semver’s repo. This should make this crate equally good at parsing semver expressions as the node package manager.
§Examples
§Comparing two versions:
use semver_rs::Version;
// by constructing version instances manually
let ver1 = Version::new("2.0.0").parse()?;
let ver2 = Version::new("1.2.3").parse()?;
assert!(ver1 > ver2);
// by using the exported helper function
use semver_rs::compare;
use std::cmp::Ordering;
assert!(compare("2.0.0", "1.2.3", None)? == Ordering::Greater);
§Checking whether a version is in a range
use semver_rs::{Range, Version};
// by constructing version instances manually
let range = Range::new(">=1.2.3").parse()?;
let ver = Version::new("1.2.4").parse()?;
assert!(range.test(&ver));
// by using the exported helper function
use semver_rs::satisfies;
assert!(satisfies("1.2.4", ">=1.2.4", None)?);
§Parsing with specific options
use semver_rs::{Version, Range, Options};
let opts = Options::builder().loose(true).include_prerelease(true).build();
let range = Range::new(">=1.2.3").with_options(opts).parse()?;
let ver = Version::new("1.2.4-pre1").with_options(opts).parse()?;
assert!(range.test(&ver));
§Serialisation with Serde
use semver_rs::{Range, Options};
let opts = Options::builder().loose(true).include_prerelease(true).build();
let range = Range::new(">=1.2.3").with_options(opts).parse()?;
let _ = serde_json::to_string(&opts)?;
Structs§
- Builder
- A Builder that helps create instances of Version and Range by also optionally supplying Options.
- Options
- Allows to configure the parsing of semver strings, same as the node-semver package. All options are false by default.
- Options
Builder - Allows building an Options instance.
- Range
- A
version range
is a set ofcomparators
which specify versions that satisfy therange
. A comparator is composed of an operator and a version. The set of primitive operators is: - Version
- A
version
is described by thev2.0.0
specification found at semver.