pub trait TryScribeString {
    // Required method
    fn try_scribe(&self) -> Option<String>;
}
Expand description

Trait for converting an enum to an allocated string, or None if the conversion fails. Generally, TryScribeCowStr should be preferred over this trait because it avoids unnecessary allocations.

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

use enumscribe::TryScribeString;

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

assert_eq!(Airport::Heathrow.try_scribe(), None);
assert_eq!(Airport::Gatwick.try_scribe(), Some("LGW".to_owned()));
assert_eq!(Airport::Other("STN".to_owned()).try_scribe(), Some("STN".to_owned()));

Required Methods§

source

fn try_scribe(&self) -> Option<String>

Converts this enum to an allocated String.

Calling this method on a variant marked with #[enumscribe(ignore)] will return None.

When called on a variant marked with #[enumscribe(other)], the variant’s field will be returned. For other variants, the string returned is determined by the #[enumscribe(str = "...")] attribute, or the name of the variant if the attribute is omitted.

Implementors§