Struct 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>

Source

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?
examples/example.rs (line 30)
12fn main() {
13    let a = "1.2";
14    let b = "1.5.1";
15
16    // The following comparison operators are used:
17    // - Cmp::Eq -> Equal
18    // - Cmp::Ne -> Not equal
19    // - Cmp::Lt -> Less than
20    // - Cmp::Le -> Less than or equal
21    // - Cmp::Ge -> Greater than or equal
22    // - Cmp::Gt -> Greater than
23
24    // Easily compare version strings
25    assert_eq!(compare(a, b), Ok(Cmp::Lt));
26    assert_eq!(compare_to(a, b, Cmp::Le), Ok(true));
27    assert_eq!(compare_to(a, b, Cmp::Gt), Ok(false));
28
29    // Parse and wrap version strings as a Version
30    let a = Version::from(a).unwrap();
31    let b = Version::from(b).unwrap();
32
33    // The Version can easily be compared with
34    assert_eq!(a < b, true);
35    assert_eq!(a <= b, true);
36    assert_eq!(a > b, false);
37    assert_eq!(a != b, true);
38    assert_eq!(a.compare(&b), Cmp::Lt);
39    assert_eq!(a.compare_to(&b, Cmp::Lt), true);
40
41    // Or match the comparison operators
42    match a.compare(b) {
43        Cmp::Lt => println!("Version a is less than b"),
44        Cmp::Eq => println!("Version a is equal to b"),
45        Cmp::Gt => println!("Version a is greater than b"),
46        _ => unreachable!(),
47    }
48}
Source

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)]);
Source

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);
Source

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");
}
Source

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");
}
Source

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));
Source

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");
Source

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)));
Source

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)
]);
Source

pub fn compare<V>(&self, other: V) -> Cmp
where V: Borrow<Version<'a>>,

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?
examples/example.rs (line 38)
12fn main() {
13    let a = "1.2";
14    let b = "1.5.1";
15
16    // The following comparison operators are used:
17    // - Cmp::Eq -> Equal
18    // - Cmp::Ne -> Not equal
19    // - Cmp::Lt -> Less than
20    // - Cmp::Le -> Less than or equal
21    // - Cmp::Ge -> Greater than or equal
22    // - Cmp::Gt -> Greater than
23
24    // Easily compare version strings
25    assert_eq!(compare(a, b), Ok(Cmp::Lt));
26    assert_eq!(compare_to(a, b, Cmp::Le), Ok(true));
27    assert_eq!(compare_to(a, b, Cmp::Gt), Ok(false));
28
29    // Parse and wrap version strings as a Version
30    let a = Version::from(a).unwrap();
31    let b = Version::from(b).unwrap();
32
33    // The Version can easily be compared with
34    assert_eq!(a < b, true);
35    assert_eq!(a <= b, true);
36    assert_eq!(a > b, false);
37    assert_eq!(a != b, true);
38    assert_eq!(a.compare(&b), Cmp::Lt);
39    assert_eq!(a.compare_to(&b, Cmp::Lt), true);
40
41    // Or match the comparison operators
42    match a.compare(b) {
43        Cmp::Lt => println!("Version a is less than b"),
44        Cmp::Eq => println!("Version a is equal to b"),
45        Cmp::Gt => println!("Version a is greater than b"),
46        _ => unreachable!(),
47    }
48}
Source

pub fn compare_to<V>(&self, other: V, operator: Cmp) -> bool
where V: Borrow<Version<'a>>,

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?
examples/example.rs (line 39)
12fn main() {
13    let a = "1.2";
14    let b = "1.5.1";
15
16    // The following comparison operators are used:
17    // - Cmp::Eq -> Equal
18    // - Cmp::Ne -> Not equal
19    // - Cmp::Lt -> Less than
20    // - Cmp::Le -> Less than or equal
21    // - Cmp::Ge -> Greater than or equal
22    // - Cmp::Gt -> Greater than
23
24    // Easily compare version strings
25    assert_eq!(compare(a, b), Ok(Cmp::Lt));
26    assert_eq!(compare_to(a, b, Cmp::Le), Ok(true));
27    assert_eq!(compare_to(a, b, Cmp::Gt), Ok(false));
28
29    // Parse and wrap version strings as a Version
30    let a = Version::from(a).unwrap();
31    let b = Version::from(b).unwrap();
32
33    // The Version can easily be compared with
34    assert_eq!(a < b, true);
35    assert_eq!(a <= b, true);
36    assert_eq!(a > b, false);
37    assert_eq!(a != b, true);
38    assert_eq!(a.compare(&b), Cmp::Lt);
39    assert_eq!(a.compare_to(&b, Cmp::Lt), true);
40
41    // Or match the comparison operators
42    match a.compare(b) {
43        Cmp::Lt => println!("Version a is less than b"),
44        Cmp::Eq => println!("Version a is equal to b"),
45        Cmp::Gt => println!("Version a is greater than b"),
46        _ => unreachable!(),
47    }
48}

Trait Implementations§

Source§

impl<'a> Clone for Version<'a>

Source§

fn clone(&self) -> Version<'a>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Version<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Display for Version<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> PartialEq for Version<'a>

Implement the partial equality trait for the version struct, to easily allow version comparison.

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> PartialOrd for Version<'a>

Implement the partial ordering trait for the version struct, to easily allow version comparison.

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

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

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a> Eq for Version<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Version<'a>

§

impl<'a> RefUnwindSafe for Version<'a>

§

impl<'a> Send for Version<'a>

§

impl<'a> Sync for Version<'a>

§

impl<'a> Unpin for Version<'a>

§

impl<'a> UnwindSafe for Version<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.