Skip to main content

Version

Enum Version 

Source
pub enum Version {
    Base(BaseVersion),
    Full(FullVersion),
}
Expand description

A numbered version which is a two-component major.minor version number, or a three-component major.minor.patch version number.

Variants§

§

Base(BaseVersion)

A two-component major.minor version.

§

Full(FullVersion)

A three-component major.minor.patch version.

Implementations§

Source§

impl Version

Source

pub fn parse(input: &str) -> Result<Self, Error>

Parse a two- or three-component, major.minor or major.minor.patch respectively, version number from a given input.

Returns a Error::ParserError if it fails to parse.

Source

pub const fn new_base_version(major: u64, minor: u64) -> Self

Create a new two-component major.minor version number.

Source

pub const fn new_full_version(major: u64, minor: u64, patch: u64) -> Self

Create a new three-component major.minor.patch version number.

Source

pub fn major(&self) -> u64

Returns the major version component.

Both the two- and three-component version number variants have a major version. This is the leading component.

Source

pub fn minor(&self) -> u64

Returns the minor version component.

Both the two- and three-component version number variants have a minor version. This is the middle component.

Source

pub fn patch(&self) -> Option<u64>

Returns the patch version component, if any.

A three component major.minor.patch version will return a Some(<version>), while a two component major.minor version will return None instead.

If it exists, it is the last component.

Source

pub fn to_full_version_lossy(&self) -> FullVersion

Convert this version to a three-component FullVersion.

A two-component version gets a patch of 0, so 1.2 becomes 1.2.0.

Source

pub fn is_compatible_with(&self, other: &FullVersion) -> Ordering

Compare self with a FullVersion, using only the components which self has.

A two-component version ignores the patch of other, so 1.2 is equal to both 1.2.0 and 1.2.9, while 1.2.0 is only equal to 1.2.0.

§Example
use std::cmp::Ordering;
use version_number::{FullVersion, Version};

let base = Version::new_base_version(1, 2);
let full = Version::new_full_version(1, 2, 0);

assert_eq!(base.is_compatible_with(&FullVersion::new(1, 2, 9)), Ordering::Equal);
assert_eq!(full.is_compatible_with(&FullVersion::new(1, 2, 9)), Ordering::Less);
assert_eq!(base.is_compatible_with(&FullVersion::new(1, 1, 9)), Ordering::Greater);
Source

pub fn matches(&self, other: &FullVersion) -> bool

Check whether other matches self, using only the components which self has.

See Version::is_compatible_with for how the versions are compared.

§Example
use version_number::{FullVersion, Version};

let base = Version::new_base_version(1, 2);

assert!(base.matches(&FullVersion::new(1, 2, 0)));
assert!(base.matches(&FullVersion::new(1, 2, 9)));
assert!(!base.matches(&FullVersion::new(1, 3, 0)));
Source

pub fn is(&self, variant: Variant) -> bool

Check of which variant self is.

Source

pub fn map<U, F>(self, fun: F) -> U
where F: FnOnce(Self) -> U,

Map a Version to U.

§Example
use version_number::{BaseVersion, FullVersion, Version};

// 🧑‍🔬
fn invert_version(v: Version) -> Version {
    match v {
        Version::Base(base) => Version::Base(BaseVersion::new(base.minor, base.major)),
        Version::Full(full) => Version::Full(FullVersion::new(full.patch, full.minor, full.major))
    }
}

let base_example = Version::Base(BaseVersion::new(1, 2));
let full_example = Version::Full(FullVersion::new(1, 2, 3));

assert_eq!(base_example.map(invert_version), Version::Base(BaseVersion::new(2, 1)));
assert_eq!(full_example.map(invert_version), Version::Full(FullVersion::new(3, 2, 1)));
Source

pub fn map_major<F>(self, fun: F) -> Self
where F: FnOnce(u64) -> u64,

Map over the major version component of the Version.

§Example
use version_number::{BaseVersion, FullVersion, Version};

let base_example = Version::Base(BaseVersion::new(0, 0));
let full_example = Version::Full(FullVersion::new(0, 0, 0));

assert_eq!(base_example.map_major(|a| a + 1), Version::Base(BaseVersion::new(1, 0)));
assert_eq!(full_example.map_major(|a| a + 1), Version::Full(FullVersion::new(1, 0, 0)));
Source

pub fn map_minor<F>(self, fun: F) -> Self
where F: FnOnce(u64) -> u64,

Map over the minor version component of the Version.

§Example
use version_number::{BaseVersion, FullVersion, Version};

let base_example = Version::Base(BaseVersion::new(0, 0));
let full_example = Version::Full(FullVersion::new(0, 0, 0));

assert_eq!(base_example.map_minor(|a| a + 1), Version::Base(BaseVersion::new(0, 1)));
assert_eq!(full_example.map_minor(|a| a + 1), Version::Full(FullVersion::new(0, 1, 0)));
Source

pub fn map_patch<F>(self, fun: F) -> Self
where F: FnOnce(u64) -> u64,

Map over the patch version component of the Version. If no patch version exists (in case the Version consists of two components), then the original version is returned.

§Example
use version_number::{BaseVersion, FullVersion, Version};

let base_example = Version::Base(BaseVersion::new(0, 0));
let full_example = Version::Full(FullVersion::new(0, 0, 0));

assert_eq!(base_example.map_patch(|a| a + 1), Version::Base(BaseVersion::new(0, 0)));
assert_eq!(full_example.map_patch(|a| a + 1), Version::Full(FullVersion::new(0, 0, 1)));

Trait Implementations§

Source§

impl Clone for Version

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Version

Source§

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

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

impl Display for Version

Source§

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

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

impl Eq for Version

Source§

impl From<(u64, u64)> for Version

Source§

fn from(tuple: (u64, u64)) -> Self

Converts to this type from the input type.
Source§

impl From<(u64, u64, u64)> for Version

Source§

fn from(tuple: (u64, u64, u64)) -> Self

Converts to this type from the input type.
Source§

impl From<BaseVersion> for Version

Source§

fn from(version: BaseVersion) -> Self

Converts to this type from the input type.
Source§

impl From<FullVersion> for Version

Source§

fn from(version: FullVersion) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Version

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(input: &str) -> Result<Self, Error>

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

impl Hash for Version

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

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

impl PartialEq for Version

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Version

Auto Trait Implementations§

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.