Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] - toml is not correctly using custom serialize impl #647

Closed
oknozor opened this issue Nov 2, 2023 · 1 comment
Closed

[BUG] - toml is not correctly using custom serialize impl #647

oknozor opened this issue Nov 2, 2023 · 1 comment

Comments

@oknozor
Copy link

oknozor commented Nov 2, 2023

Seems related to #603, #636

When serializing a struct containing Option with a custom serializer, we get an UnsupportedType(None) error.
Note that this work as expected using toml < 0.5 and panic starting from 0.6.

Here is a minimal reproducer:

use std::fmt;
use std::fmt::Formatter;
use serde::{Serialize, Serializer};

struct Person {
    name: String,
    age: Option<u16>
}

impl Serialize for Person {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
    {
        serializer.serialize_str(&self.to_string())
    }
}

impl fmt::Display for Person {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.name)
    }
}

fn main() {
    // Panics on toml = ^0.6
    // Works with toml = 0.5
    toml::to_string(&Person {
        name: "tom".to_string(),
        age: None,
    }).unwrap();
}
@epage
Copy link
Member

epage commented Nov 2, 2023

This is serializing a string as a top-level TOML document which is invalid TOML syntax. If you want to deserialize a TOML value, you can use ValueSerailizer. As this is working as expected, I'm closing this, If there is a reason for us to re-evaluate this, let us know!

@epage epage closed this as not planned Won't fix, can't repro, duplicate, stale Nov 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants