Hi all!
I'm trying to deserialize a field that may be either a string or a struct; so far, so good – there's even a chunk of the Serde docs about exactly that.
However, I'm trying to deserialize an internally tagged enum, and that seems to be where stuff goes off the rails.
(A reduced version of my current attempt is on the playground.)
If I make use of an untagged enum, the string_or_struct
example function works perfectly; however, I was running into issues where my enum variants would just report "unmatched variant at $line, $column", and I was hoping that switching to a tagged enum would provide me better error messages.
#[derive(Debug, PartialEq, Deserialize, Serialize, Clone)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum Rule {
Course(course::Rule),
Requirement(requirement::Rule),
}
With the above code for my internally-tagged enum, and the messy stuff that I adopted from the serde.rs string_or_struct
example, I wind up with:
thread 'main' has overflowed its stack
fatal runtime error: stack overflow
… which is, er, not quite what I'm looking for.
I would like strings to be passed to Visitor::visit_str
, where I can handle them, and maps/structs to be passed to Visitor::visit_map
, where I would like to let serde's default Deserialize
implementation handle them.
I'm not sure how to achieve the "let the default behavior happen" bit without going into a recursive loop, and I don't yet know enough Rust to make a educated guess as to what I need.
(I can throw around words, though – maybe I need another wrapper struct w/#[derive(Deserialize)]
? or … okay, that's about my only idea.)
TL;DR: I want
-
the string
"ART 101"
to be deserialized asRule::Course(course::Rule {name: "ART 101"})
-
the struct
{type: course, name: "ART 101"}
to be deserialized asRule::Course(course::Rule {name: "ART 101"})
-
the struct
{type: requirement, name: "Intro"}
to be deserialized asRule::Requirement(requirement::Rule {name: "Intro"})
If I can get those three working, I'm pretty sure I can get my other variants working, since I had them all working with Serde's untyped enum variants.
(Apologies if this should go on StackOverflow or somewhere else instead.)