Enum version_compare::Cmp

source ·
pub enum Cmp {
    Eq,
    Ne,
    Lt,
    Le,
    Ge,
    Gt,
}
Expand description

Comparison operators enum.

Variants§

§

Eq

Equal (==, =). When version A is equal to B.

§

Ne

Not equal (!=, !, <>). When version A is not equal to B.

§

Lt

Less than (<). When version A is less than B but not equal.

§

Le

Less or equal (<=). When version A is less than or equal to B.

§

Ge

Greater or equal (>=). When version A is greater than or equal to B.

§

Gt

Greater than (>). When version A is greater than B but not equal.

Implementations§

source§

impl Cmp

source

pub fn from_sign<S: AsRef<str>>(sign: S) -> Result<Cmp, ()>

Get a comparison operator by it’s sign. Whitespaces are stripped from the sign string. An error is returned if the sign isn’t recognized.

The following signs are supported:

  • == or = -> Eq
  • != or ! or <> -> Ne
  • < -> Lt
  • <= -> Le
  • >= -> Ge
  • > -> Gt
§Examples
use version_compare::Cmp;

assert_eq!(Cmp::from_sign("=="), Ok(Cmp::Eq));
assert_eq!(Cmp::from_sign("<"), Ok(Cmp::Lt));
assert_eq!(Cmp::from_sign("  >=   "), Ok(Cmp::Ge));
assert!(Cmp::from_sign("*").is_err());
source

pub fn from_name<S: AsRef<str>>(sign: S) -> Result<Cmp, ()>

Get a comparison operator by it’s name. Names are case-insensitive, and whitespaces are stripped from the string. An error is returned if the name isn’t recognized.

§Examples
use version_compare::Cmp;

assert_eq!(Cmp::from_name("eq"), Ok(Cmp::Eq));
assert_eq!(Cmp::from_name("lt"), Ok(Cmp::Lt));
assert_eq!(Cmp::from_name("  Ge   "), Ok(Cmp::Ge));
assert!(Cmp::from_name("abc").is_err());
source

pub fn from_ord(ord: Ordering) -> Cmp

👎Deprecated since 0.2.0: use Cmp::from(ord) instead

Get the comparison operator from Rusts Ordering enum.

The following comparison operators are returned:

  • Ordering::Less -> Lt
  • Ordering::Equal -> Eq
  • Ordering::Greater -> Gt
source

pub fn name<'a>(self) -> &'a str

Get the name of this comparison operator.

§Examples
use version_compare::Cmp;

assert_eq!(Cmp::Eq.name(), "eq");
assert_eq!(Cmp::Lt.name(), "lt");
assert_eq!(Cmp::Ge.name(), "ge");
source

pub fn invert(self) -> Self

Get the inverted comparison operator.

This uses the following bidirectional rules:

  • Eq <-> Ne
  • Lt <-> Ge
  • Le <-> Gt
§Examples
use version_compare::Cmp;

assert_eq!(Cmp::Eq.invert(), Cmp::Ne);
assert_eq!(Cmp::Lt.invert(), Cmp::Ge);
assert_eq!(Cmp::Gt.invert(), Cmp::Le);
source

pub fn opposite(self) -> Self

Get the opposite comparison operator.

This uses the following bidirectional rules:

  • Eq <-> Ne
  • Lt <-> Gt
  • Le <-> Ge
§Examples
use version_compare::Cmp;

assert_eq!(Cmp::Eq.opposite(), Cmp::Ne);
assert_eq!(Cmp::Lt.opposite(), Cmp::Gt);
assert_eq!(Cmp::Ge.opposite(), Cmp::Le);
source

pub fn flip(self) -> Self

Get the flipped comparison operator.

This uses the following bidirectional rules:

  • Lt <-> Gt
  • Le <-> Ge
  • Other operators are returned as is.
§Examples
use version_compare::Cmp;

assert_eq!(Cmp::Eq.flip(), Cmp::Eq);
assert_eq!(Cmp::Lt.flip(), Cmp::Gt);
assert_eq!(Cmp::Ge.flip(), Cmp::Le);
source

pub fn sign(self) -> &'static str

Get the sign for this comparison operator.

The following signs are returned:

  • Eq -> ==
  • Ne -> !=
  • Lt -> <
  • Le -> <=
  • Ge -> >=
  • Gt -> >

Note: Some comparison operators also support other signs, such as = for Eq and ! for Ne, these are never returned by this method however as the table above is used.

§Examples
use version_compare::Cmp;

assert_eq!(Cmp::Eq.sign(), "==");
assert_eq!(Cmp::Lt.sign(), "<");
assert_eq!(Cmp::Ge.flip().sign(), "<=");
source

pub fn factor(self) -> i8

Get a factor (number) for this comparison operator. These factors can be useful for quick calculations.

The following factor numbers are returned:

  • Eq or Ne -> 0
  • Lt or Le -> -1
  • Gt or Ge -> 1
§Examples
use version_compare::Version;

let a = Version::from("1.2.3").unwrap();
let b = Version::from("1.3").unwrap();

assert_eq!(a.compare(&b).factor(), -1);
assert_eq!(10 * b.compare(a).factor(), 10);
source

pub fn ord(self) -> Option<Ordering>

Get Rust’s ordering for this comparison operator.

The following comparison operators are supported:

  • Eq -> Ordering::Equal
  • Lt -> Ordering::Less
  • Gt -> Ordering::Greater

For other comparison operators None is returned.

§Examples
use std::cmp::Ordering;
use version_compare::Version;

let a = Version::from("1.2.3").unwrap();
let b = Version::from("1.3").unwrap();

assert_eq!(a.compare(b).ord().unwrap(), Ordering::Less);

Trait Implementations§

source§

impl Clone for Cmp

source§

fn clone(&self) -> Cmp

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 Debug for Cmp

source§

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

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

impl From<Ordering> for Cmp

source§

fn from(ord: Ordering) -> Self

Get the comparison operator from Rusts Ordering enum.

The following comparison operators are returned:

  • Ordering::Less -> Lt
  • Ordering::Equal -> Eq
  • Ordering::Greater -> Gt
source§

impl Hash for Cmp

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 Cmp

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for Cmp

source§

impl Eq for Cmp

source§

impl StructuralPartialEq for Cmp

Auto Trait Implementations§

§

impl Freeze for Cmp

§

impl RefUnwindSafe for Cmp

§

impl Send for Cmp

§

impl Sync for Cmp

§

impl Unpin for Cmp

§

impl UnwindSafe for Cmp

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

§

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, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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.