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
impl Cmp
sourcepub fn from_sign<S: AsRef<str>>(sign: S) -> Result<Cmp, ()>
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());
sourcepub fn from_name<S: AsRef<str>>(sign: S) -> Result<Cmp, ()>
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());
sourcepub fn from_ord(ord: Ordering) -> Cmp
👎Deprecated since 0.2.0: use Cmp::from(ord) instead
pub fn from_ord(ord: Ordering) -> Cmp
Get the comparison operator from Rusts Ordering
enum.
The following comparison operators are returned:
Ordering::Less
->Lt
Ordering::Equal
->Eq
Ordering::Greater
->Gt
sourcepub fn name<'a>(self) -> &'a str
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");
sourcepub fn invert(self) -> Self
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);
sourcepub fn opposite(self) -> Self
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);
sourcepub fn flip(self) -> Self
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);
sourcepub fn sign(self) -> &'static str
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(), "<=");
sourcepub fn factor(self) -> i8
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
orNe
->0
Lt
orLe
->-1
Gt
orGe
->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);
sourcepub fn ord(self) -> Option<Ordering>
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);