Crates.io | later_operator |
lib.rs | later_operator |
version | 0.4.1 |
source | src |
created_at | 2023-02-10 03:30:30.4787 |
updated_at | 2024-11-05 03:36:18.659915 |
description | Parsable, storable, printable comparison operators, w/ optional serde support. |
homepage | |
repository | https://github.com/Blobfolio/later_operator |
max_upload_size | |
id | 781601 |
size | 22,265 |
This library provides a ComparisonOperator
enum that can be used to parse, store, evaluate, and/or print Rust's relational operators: !=
, <
, <=
, ==
, >=
, >
.
use later_operator::ComparisonOperator;
// Parse from a string, then compare two arbitrary values.
let op = ComparisonOperator::try_from("<=").unwrap();
assert!(op.compare(&3_u8, &u8::MAX)); // 3 <= 255
// Re-stringify the operator for printing or whatever.
assert_eq!(
format!("3 {op} 255"),
"3 <= 255",
);
// Leading/trailing whitespace is ignored when parsing.
assert_eq!(
ComparisonOperator::try_from("==").unwrap(),
ComparisonOperator::try_from(" ==\n").unwrap(),
);
// But the value has to make sense or it will fail.
assert!(ComparisonOperator::try_from("~").is_err());
When the optional serde
crate feature is enabled, ComparisonOperator
can be de/serialized as a string too:
use later_operator::ComparisonOperator;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize, Serialize)]
struct AffectedVersion {
operator: ComparisonOperator,
version: String,
}
Add later_operator
to your dependencies
in Cargo.toml
, like:
[dependencies]
later_operator = "0.4.*"