有什么方法可以将String转换为带有字段的枚举(命名为struct枚举)?

时间:2019-11-12 01:35:53

标签: generics enums rust

我有一个HashMap<String, serde_json::Value>,我想将其转换为HashMap<String, Attribute>。我想根据名称(带有值)将字段转换为Attribute

pub struct Name {
    pub first_name: String,
    pub last_name: String,
}

pub enum Attribute {
    __Noop,
    Name(Name),
    Age(i32),
    Id(String),
}

impl Attribute {
    fn new<T>(field_name: &str, attrs: T) -> Attribute {
        if field_name == "name" {
            Attribute::Name(attrs)
        } else if field_name == "age" {
            Attribute::Age(attrs)
        } else if field_name == "id" {
            Attribute::Id(attrs)
        } else {
            Attribute::__Noop
        }
    }
}

fn main() {}

0 个答案:

没有答案