boxdot/serde-url-params-rs

`Option<SomeStruct>` yields MapError - with and without flatten annotations

drahnr opened this issue · 2 comments

Sometimes there are optional parameters which I tried to model as fields in a struct using Option<_>

// this struct just exists for structuring the data
#[derive(Serialize,Debug)]
pub struct ImaginaryNum {
     real: u64,
     imaginary: uu64,
}

#[derive(Serialize,Debug)]
pub struct MyParams {
     whatever: bool,
    x: u64,
    #[serde(flatten)]
    y : Option<ImaginaryNum>, // <<< error
    // #[serde(flatten)]
    // z: Vec<ImagninaryNum> would also be nice if that worked, just serializing all it's members
}

How can I make this work?

Flattened struct did not work, since I did not support any kind of mappings. For mapping serialization I needed to implement a simple serializer which always fails except when a string or a char is given to it. This is now done in #9. So, your example should work fine now. I also built it in as a test: b08e663#diff-b4aea3e418ccdb71239b96952d9cddb6R273

For you second example, z: Vec<ImagninaryNum>, actually you don't need my commit. It works already now, except that you cannot use #[serde(flatten)] on a Vec. You need to annotate the whole struct MyParams with #[serde(transparent)]. For more information, cf. serde-rs/serde#1378.

Thanks a lot @boxdot ! Much appreciated.