Skip to content
This repository has been archived by the owner on May 12, 2024. It is now read-only.

Support ValueLike macro on plain enum #52

Open
chipsenkbeil opened this issue Mar 14, 2021 · 0 comments
Open

Support ValueLike macro on plain enum #52

chipsenkbeil opened this issue Mar 14, 2021 · 0 comments
Labels
enhancement New feature or request
Milestone

Comments

@chipsenkbeil
Copy link
Owner

For enums like

pub enum MyValueEnum {
    One,
    Two,
    Three,
}

should be able to derive a ValueLike impl of

impl ValueLike for MyValueEnum {
    fn into_value(self) -> Value {
        match self {
            Self::One => Value::Text(String::from("one")),
            Self::Two => Value::Text(String::from("two")),
            Self::Three => Value::Text(String::from("three")),
        }
    }

    fn try_from_value(value: Value) -> Result<Self, Value> {
        match value {
            Value::Text(x) => match x.as_str() {
                "one" => Ok(Self::One),
                "two" => Ok(Self::Two),
                "three" => Ok(Self::Three),
                _ => Err(Value::Text(x)),
            },
            x => Err(x),
        }
    }
}
@chipsenkbeil chipsenkbeil added the enhancement New feature or request label Mar 14, 2021
@chipsenkbeil chipsenkbeil added this to the 0.3 milestone Mar 14, 2021
@chipsenkbeil chipsenkbeil modified the milestones: 0.3, 0.4 Apr 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant