Skip to content

benjaminhottell/zotm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zotm

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.

Quickstart

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

Usage

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.

About

Rust library for handling zero, one, or "too many" values. Similar usage and ergonomics to Option/Result.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages