pub trait TryScribeStaticStr {
    // Required method
    fn try_scribe(&self) -> Option<&'static str>;
}
Expand description

Trait for converting an enum to a static string slice, or None if the conversion fails.

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

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

You may also annotate a variant with #[enumscribe(ignore)], in which case attempting to convert the variant to a string will always result in None.

This trait can only be used if none of the enum’s variants use other. If you have variants that use other, use TryScribeCowStr instead.

use enumscribe::TryScribeStaticStr;

#[derive(TryScribeStaticStr, PartialEq, Eq, Debug)]
enum Airport {
    #[enumscribe(str = "LHR")]
    Heathrow,
    #[enumscribe(ignore)]
    Gatwick,
    UnnamedAirport,
}

assert_eq!(Airport::Heathrow.try_scribe(), Some("LHR"));
assert_eq!(Airport::Gatwick.try_scribe(), None);
assert_eq!(Airport::UnnamedAirport.try_scribe(), Some("UnnamedAirport"));

Required Methods§

source

fn try_scribe(&self) -> Option<&'static str>

Converts this enum to a Option<&'static str>.

Calling this method on a variant marked with #[enumscribe(ignore)] will return None. Calling it on any other variant will return Some("..."), where "..." is the string specified by the #[enumscribe(str = "...")] attribute.

Implementors§