A Rust crate that provides an enum with three states: zero, one, or too many values.
Zotm is intended to ergonomically handle these kinds of situations:
- You expect exactly one value, or you expect zero-to-one values.
- However, it's possible that you will get two or more values as an error case.
- If you encounter two or more values, it doesn't matter what those values are or exactly how many there are.
The crate-level documentation provides more examples.
use zotm::Zotm;
// Pretend `values` is a call to some external function.
let values = vec![1, 2, 3];
// In theory, only one value should be present.
// In practice, anything can happen!
let result: Zotm<u32> = values.into_iter().collect();
match result {
Zotm::Zero => eprintln!("Too few!"),
Zotm::TooMany => eprintln!("Too many!"),
Zotm::One(x) => println!("Just right: {x}"),
}Build a Zotm<T> by calling accept or extend:
use zotm::Zotm;
let mut x: Zotm<u32> = Zotm::Zero;
assert!(x.is_zero());
x.accept(42);
assert!(x.is_one());
x.extend(vec![1, 2, 3]);
assert!(x.is_too_many());Convert a Zotm<T> to an Option<T> with .one():
use zotm::Zotm;
let x: Option<u32> = Zotm::Zero.one();
assert!(x.is_none());
let x: Option<u32> = Zotm::One(42).one();
assert!(x.is_some());
let x: Option<u32> = Zotm::TooMany.one();
assert!(x.is_none());Convert an Option<T> to Zotm<T>:
use zotm::Zotm;
let x: Zotm<u32> = None.into();
assert!(x.is_zero());
let x: Zotm<u32> = Some(42).into();
assert!(x.is_one());Please see the crate-level documentation for more information and usage instructions.