toml-rs / toml

Rust TOML Parser
https://docs.rs/toml
Apache License 2.0
730 stars 107 forks source link

`serde::Serialize` does not work with valid utf-8 encoded `OsString` #784

Closed hn275 closed 2 months ago

hn275 commented 2 months ago

I know OsString may or may not be utf-8, which could be a violation to toml format from the spec.

I can serialize it with valid utf-8 characters, but the output is not what I expected.

use std::{ffi::OsString, str::FromStr};

#[derive(serde::Serialize)]
struct FooOsString {
    bar: OsString,
}

fn main() {
    let foo_os = FooOsString {
        bar: OsString::from_str("foo").expect("invalid encoding"),
    };
    let encoding_os = toml::to_string(&foo_os).expect("failed to serialize to toml string");

    println!("{}", encoding_os);

    /* output:

    [bar]
    Unix = [102, 111, 111]

    */
}

and here is my dependency list in Cargo.toml

[dependencies]
serde = { version = "1.0.210", features = ["derive"] }
toml = "0.8.19"
epage commented 2 months ago

This is an implementation detail of serde, see https://docs.rs/serde/latest/src/serde/de/impls.rs.html#1920-1949

You'll need to override serde's behavior to change this.\

As there is nothing to do within toml, I'm closing