Struct version_compare::Version
source · pub struct Version<'a> { /* private fields */ }
Expand description
Version struct, wrapping a string, providing useful comparison functions.
A version in string format can be parsed using methods like Version::from("1.2.3");
,
returning a Result
with the parse result.
The original version string can be accessed using version.as_str()
. A Version
that isn’t
derrived from a version string returns a generated string.
The struct provides many methods for easy comparison and probing.
§Examples
use version_compare::{Version};
let ver = Version::from("1.2.3").unwrap();
Implementations§
source§impl<'a> Version<'a>
impl<'a> Version<'a>
sourcepub fn from(version: &'a str) -> Option<Self>
pub fn from(version: &'a str) -> Option<Self>
Create a Version
instance from a version string.
The version string should be passed to the version
parameter.
§Examples
use version_compare::{Cmp, Version};
let a = Version::from("1.2.3").unwrap();
let b = Version::from("1.3.0").unwrap();
assert_eq!(a.compare(b), Cmp::Lt);
Examples found in repository?
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
fn main() {
let a = "1.2";
let b = "1.5.1";
// The following comparison operators are used:
// - Cmp::Eq -> Equal
// - Cmp::Ne -> Not equal
// - Cmp::Lt -> Less than
// - Cmp::Le -> Less than or equal
// - Cmp::Ge -> Greater than or equal
// - Cmp::Gt -> Greater than
// Easily compare version strings
assert_eq!(compare(a, b), Ok(Cmp::Lt));
assert_eq!(compare_to(a, b, Cmp::Le), Ok(true));
assert_eq!(compare_to(a, b, Cmp::Gt), Ok(false));
// Parse and wrap version strings as a Version
let a = Version::from(a).unwrap();
let b = Version::from(b).unwrap();
// The Version can easily be compared with
assert_eq!(a < b, true);
assert_eq!(a <= b, true);
assert_eq!(a > b, false);
assert_eq!(a != b, true);
assert_eq!(a.compare(&b), Cmp::Lt);
assert_eq!(a.compare_to(&b, Cmp::Lt), true);
// Or match the comparison operators
match a.compare(b) {
Cmp::Lt => println!("Version a is less than b"),
Cmp::Eq => println!("Version a is equal to b"),
Cmp::Gt => println!("Version a is greater than b"),
_ => unreachable!(),
}
}
sourcepub fn from_parts(version: &'a str, parts: Vec<Part<'a>>) -> Self
pub fn from_parts(version: &'a str, parts: Vec<Part<'a>>) -> Self
Create a Version
instance from already existing parts
§Examples
use version_compare::{Cmp, Version, Part};
let ver = Version::from_parts("1.0", vec![Part::Number(1), Part::Number(0)]);
sourcepub fn from_manifest(version: &'a str, manifest: &'a Manifest) -> Option<Self>
pub fn from_manifest(version: &'a str, manifest: &'a Manifest) -> Option<Self>
Create a Version
instance from a version string with the given manifest
.
The version string should be passed to the version
parameter.
§Examples
use version_compare::{Cmp, Version, Manifest};
let manifest = Manifest::default();
let ver = Version::from_manifest("1.2.3", &manifest).unwrap();
assert_eq!(ver.compare(Version::from("1.2.3").unwrap()), Cmp::Eq);
sourcepub fn manifest(&self) -> Option<&Manifest>
pub fn manifest(&self) -> Option<&Manifest>
Get the version manifest, if available.
§Examples
use version_compare::Version;
let version = Version::from("1.2.3").unwrap();
if version.has_manifest() {
println!(
"Maximum version part depth is {} for this version",
version.manifest().unwrap().max_depth.unwrap_or(0),
);
} else {
println!("Version has no manifest");
}
sourcepub fn has_manifest(&self) -> bool
pub fn has_manifest(&self) -> bool
Check whether this version has a manifest.
§Examples
use version_compare::Version;
let version = Version::from("1.2.3").unwrap();
if version.has_manifest() {
println!("This version does have a manifest");
} else {
println!("This version does not have a manifest");
}
sourcepub fn set_manifest(&mut self, manifest: Option<&'a Manifest>)
pub fn set_manifest(&mut self, manifest: Option<&'a Manifest>)
Set the version manifest.
§Examples
use version_compare::{Version, Manifest};
let manifest = Manifest::default();
let mut version = Version::from("1.2.3").unwrap();
version.set_manifest(Some(&manifest));
sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Get the original version string.
§Examples
use version_compare::Version;
let ver = Version::from("1.2.3").unwrap();
assert_eq!(ver.as_str(), "1.2.3");
sourcepub fn part(&self, index: usize) -> Result<Part<'a>, ()>
pub fn part(&self, index: usize) -> Result<Part<'a>, ()>
Get a specific version part by it’s index
.
An error is returned if the given index is out of bound.
§Examples
use version_compare::{Version, Part};
let ver = Version::from("1.2.3").unwrap();
assert_eq!(ver.part(0), Ok(Part::Number(1)));
assert_eq!(ver.part(1), Ok(Part::Number(2)));
assert_eq!(ver.part(2), Ok(Part::Number(3)));
sourcepub fn parts(&self) -> &[Part<'a>]
pub fn parts(&self) -> &[Part<'a>]
Get a vector of all version parts.
§Examples
use version_compare::{Version, Part};
let ver = Version::from("1.2.3").unwrap();
assert_eq!(ver.parts(), [
Part::Number(1),
Part::Number(2),
Part::Number(3)
]);
sourcepub fn compare<V>(&self, other: V) -> Cmp
pub fn compare<V>(&self, other: V) -> Cmp
Compare this version to the given other
version using the default Manifest
.
This method returns one of the following comparison operators:
Lt
Eq
Gt
Other comparison operators can be used when comparing, but aren’t returned by this method.
§Examples:
use version_compare::{Cmp, Version};
let a = Version::from("1.2").unwrap();
let b = Version::from("1.3.2").unwrap();
assert_eq!(a.compare(&b), Cmp::Lt);
assert_eq!(b.compare(&a), Cmp::Gt);
assert_eq!(a.compare(&a), Cmp::Eq);
Examples found in repository?
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
fn main() {
let a = "1.2";
let b = "1.5.1";
// The following comparison operators are used:
// - Cmp::Eq -> Equal
// - Cmp::Ne -> Not equal
// - Cmp::Lt -> Less than
// - Cmp::Le -> Less than or equal
// - Cmp::Ge -> Greater than or equal
// - Cmp::Gt -> Greater than
// Easily compare version strings
assert_eq!(compare(a, b), Ok(Cmp::Lt));
assert_eq!(compare_to(a, b, Cmp::Le), Ok(true));
assert_eq!(compare_to(a, b, Cmp::Gt), Ok(false));
// Parse and wrap version strings as a Version
let a = Version::from(a).unwrap();
let b = Version::from(b).unwrap();
// The Version can easily be compared with
assert_eq!(a < b, true);
assert_eq!(a <= b, true);
assert_eq!(a > b, false);
assert_eq!(a != b, true);
assert_eq!(a.compare(&b), Cmp::Lt);
assert_eq!(a.compare_to(&b, Cmp::Lt), true);
// Or match the comparison operators
match a.compare(b) {
Cmp::Lt => println!("Version a is less than b"),
Cmp::Eq => println!("Version a is equal to b"),
Cmp::Gt => println!("Version a is greater than b"),
_ => unreachable!(),
}
}
sourcepub fn compare_to<V>(&self, other: V, operator: Cmp) -> bool
pub fn compare_to<V>(&self, other: V, operator: Cmp) -> bool
Compare this version to the given other
version,
and check whether the given comparison operator is valid using the default Manifest
.
All comparison operators can be used.
§Examples:
use version_compare::{Cmp, Version};
let a = Version::from("1.2").unwrap();
let b = Version::from("1.3.2").unwrap();
assert!(a.compare_to(&b, Cmp::Lt));
assert!(a.compare_to(&b, Cmp::Le));
assert!(a.compare_to(&a, Cmp::Eq));
assert!(a.compare_to(&a, Cmp::Le));
Examples found in repository?
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
fn main() {
let a = "1.2";
let b = "1.5.1";
// The following comparison operators are used:
// - Cmp::Eq -> Equal
// - Cmp::Ne -> Not equal
// - Cmp::Lt -> Less than
// - Cmp::Le -> Less than or equal
// - Cmp::Ge -> Greater than or equal
// - Cmp::Gt -> Greater than
// Easily compare version strings
assert_eq!(compare(a, b), Ok(Cmp::Lt));
assert_eq!(compare_to(a, b, Cmp::Le), Ok(true));
assert_eq!(compare_to(a, b, Cmp::Gt), Ok(false));
// Parse and wrap version strings as a Version
let a = Version::from(a).unwrap();
let b = Version::from(b).unwrap();
// The Version can easily be compared with
assert_eq!(a < b, true);
assert_eq!(a <= b, true);
assert_eq!(a > b, false);
assert_eq!(a != b, true);
assert_eq!(a.compare(&b), Cmp::Lt);
assert_eq!(a.compare_to(&b, Cmp::Lt), true);
// Or match the comparison operators
match a.compare(b) {
Cmp::Lt => println!("Version a is less than b"),
Cmp::Eq => println!("Version a is equal to b"),
Cmp::Gt => println!("Version a is greater than b"),
_ => unreachable!(),
}
}
Trait Implementations§
source§impl<'a> PartialEq for Version<'a>
impl<'a> PartialEq for Version<'a>
Implement the partial equality trait for the version struct, to easily allow version comparison.
source§impl<'a> PartialOrd for Version<'a>
impl<'a> PartialOrd for Version<'a>
Implement the partial ordering trait for the version struct, to easily allow version comparison.
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more