birkenfeld/serde-pickle

Isssue with deserializing nested Enums/Stucts

zachcp opened this issue · 2 comments

Hi @birkenfeld ,

Thank you for this great crate. I wanted to use it to deserialise and process Pymol PSE files. I've made quite a bit of progress building a nice set of structs to serialize molecules but now I want to add support for other types of objects stored in these files.

  • I have a struct PyObjectMolecule and I can deserialize a test file completely.
  • in anticipation of needing to add additional deserialization, I want to move this to an Enum
  • as soon as I move the Struct into an Enum I get errors that are difficult to troubleshoot.
  • Syntax(Structure("data did not match any variant of untagged enum PymolSessionObjectData"))

So I'm hoping you might be able to suggest a way forward - either directly or stategically. Thank you! ( I can post the full code somewhere if that would be helpful.

Struct-Based Parsing

// works great
#[derive(Debug, Deserialize, Serialize)]
struct ToplevelStruct {
    data: PyObjectMolecule,  //<- this is a deeply nested struct
}

Enum + Struct

// fails no matter what I do 
#[derive(Debug, Deserialize, Serialize)]
#[serde(untagged)]
enum PymolSessionObjectData {
    Molecue(PyObjectMolecule),
}

#[derive(Debug, Deserialize, Serialize)]
struct ToplevelStruct {
    data: PymolSessionObjectData,  //<- Use the Enum to match. I thought this would be straightforward.....
}

Repo:

  • clone this repo
  • cargo test : works!
  • toggle these two lines
  • fails :(

Insight/pointers very much appreciated.

I ended up writing some custom Deserialization functions. I'm still not really famillar with Rust so it feels like I'm banging around until I get the thing to work .... but its working and passing tests.