Enum version_compare::Cmp[][src]

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

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

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

Get the comparison operator from Rusts Ordering enum.

The following comparison operators are returned:

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

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

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

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

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

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(), "<=");

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

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Feeds this value into the given Hasher. Read more

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

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.