from-str-sequential

Crates.iofrom-str-sequential
lib.rsfrom-str-sequential
version0.1.0
sourcesrc
created_at2022-11-26 20:55:43.078689
updated_at2022-11-26 20:55:43.078689
descriptionA FromStr-like trait for enums, matching the first compatible variant
homepage
repositoryhttps://github.com/kraktus/from-str-sequential
max_upload_size
id723456
size38,727
(kraktus)

documentation

README

from-str-sequential

crates.io docs.rs

A utility crate which implement a simple FromStrSequential trait similar to FromStr. Used on enums with unit and un-named variants, and try to convert the string to each variant sequentially (from top to bottom variant). For unit variant, the string must be the variant name (case-insentive). For un-named variants, the string must match the FromStr implementation of the un-named type.

This crate was initially released to allow multiple input formats for clap::Command

Example

use from_str_sequential::FromStrSequential;

#[derive(Debug, FromStrSequential, PartialEq, Eq)]
enum Foo {
	Bar,
	Baz(usize),
}

assert_eq!(Foo::Bar, Foo::from_str_sequential("bar").unwrap());
assert_eq!(Foo::Bar, Foo::from_str_sequential("BaR").unwrap());
assert_eq!(Foo::Baz(100), Foo::from_str_sequential("100").unwrap());
Commit count: 15

cargo fmt