#operator #comparison #relational #string-parser

later_operator

Parsable, storable, printable comparison operators, w/ optional serde support

9 unstable releases (3 breaking)

0.4.2 Nov 28, 2024
0.4.0 Sep 5, 2024
0.3.0 Jul 29, 2024
0.2.0 Feb 8, 2024
0.1.1 Mar 9, 2023

#445 in Encoding

Download history 139/week @ 2024-09-01 6/week @ 2024-09-08 14/week @ 2024-09-15 19/week @ 2024-09-22 5/week @ 2024-09-29 2/week @ 2024-10-06 2/week @ 2024-10-13 127/week @ 2024-11-03 8/week @ 2024-11-10 3/week @ 2024-11-17 137/week @ 2024-11-24 22/week @ 2024-12-01

174 downloads per month

WTFPL license

20KB
327 lines

Later Operator

docs.rs changelog
crates.io ci deps.rs
license contributions welcome

This library provides a ComparisonOperator enum that can be used to parse, store, evaluate, and/or print Rust's relational operators: !=, <, <=, ==, >=, >.

Examples

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

Installation

Add later_operator to your dependencies in Cargo.toml, like:

[dependencies]
later_operator = "0.4.*"

Dependencies

~160KB