Trait Unscribe

Source
pub trait Unscribe: Sized {
    // Required method
    fn unscribe(to_unscribe: &str) -> Self;
}
Expand description

Trait for converting from a string to an enum.

Like all of the traits provided by enumscribe, this should not be implemented manually; use #[derive(Unscribe)] provided by the enumscribe_derive crate instead.

When deriving this trait, you may specify the string that should map to a particular variant by annotating it with #[enumscribe(str = "foo")]. If this is omitted, the name of the variant will be used instead.

Annotating a variant with #[enumscribe(case_insensitive)] will cause case insensitive matching to be used for that variant. If it is omitted, matching will be case sensitive.

For this trait to be derived, there must be a variant marked with #[enumscribe(other)]. This variant will be used to store strings that could not be matched to any other variant. It must have a single field, which should have type String. If you do not have such a variant, try deriving TryUnscribe instead.

use enumscribe::Unscribe;

#[derive(Unscribe, PartialEq, Eq, Debug)]
enum Airport {
    #[enumscribe(str = "LHR", case_insensitive)]
    Heathrow,
    #[enumscribe(str = "LGW")]
    Gatwick,
    #[enumscribe(other)]
    Other(String),
}

assert_eq!(Airport::unscribe("LHR"), Airport::Heathrow);
assert_eq!(Airport::unscribe("lhr"), Airport::Heathrow);

assert_eq!(Airport::unscribe("LGW"), Airport::Gatwick);
assert_eq!(Airport::unscribe("lgw"), Airport::Other("lgw".to_owned()));

assert_eq!(Airport::unscribe("STN"), Airport::Other("STN".to_owned()));
assert_eq!(Airport::unscribe("stn"), Airport::Other("stn".to_owned()));

Required Methods§

Source

fn unscribe(to_unscribe: &str) -> Self

Converts the given string to an enum variant.

The given string is matched against the #[enumscribe(str = "...")] attribute for each variant to determine which variant to return. If there was no successful match, the variant marked with #[enumscribe(other)] will be returned instead.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§