Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

rust - how can I flatten an enum to a special case when the explicit cases don't match

I'd like it so when the case is unknown, it will be associated with the last case

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(untagged)]
pub enum Action {
    Action1,
    Action2,
    Action3,
    Other(String), // when not known it should be here
}

I've tried using the directive

#[serde(untagged)]

but then it doesn't serialize properly

let b = Action::Action1;
let s = serde_json::to_string(&b);
let ss = s.unwrap();
println!("ss {:#?}", &ss);
let val = serde_json::to_value(b);
println!("ss {:#?}", &val);

results in

ss "null"
ss Ok(
    Null,
)

Playground link

question from:https://stackoverflow.com/questions/66046461/how-can-i-flatten-an-enum-to-a-special-case-when-the-explicit-cases-dont-match

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I can think of two options that build off each other.

First use From to turn this into a string every time you serialize and then From to turn it back into your own type. This requires you to convert every time you serialize and deserialize but will accomplish your goal.

If you want to make the API a little cleaner at the cost of doing more work you can implement serialize and deserialize yourself. Here are some references on how to do that:

  1. Custom Serialization
  2. Implementing Serialize
  3. Implementing Deserialize

As a second option you can offload the custom serialization and deserialization if your willing to add another dependency of serde_with.

According to its docs:

De/Serializing a type using the Display and FromStr traits, e.g., for u8, url::Url, or mime::Mime. Check DisplayFromStr or serde_with::rust::display_fromstr for details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...