toby / serde-bencode

Serde backed Bencode encoding/decoding library for Rust.
MIT License
65 stars 16 forks source link

Allow serialization of unit variant as string #43

Open ngoquang2708 opened 3 months ago

ngoquang2708 commented 3 months ago

Currently I can deserialize HashMap with key as an enum as example bellow:

#[derive(PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
enum Extension {
    #[serde(rename = "ut_metadata")]
    Metadata,
}
serde_bencode::from_str::<std::collections::HashMap<Extension, u8>>("d11:ut_metadatai1ee").unwrap();

But I cannot serialize a HashMap to a string:

serde_bencode::to_string(&std::collections::HashMap::from([(Extension::Metadata, 1)])).unwrap();

If I modifiy the serialize_newtype_struct() to allow serialize unit variant as a string the above code work. I don't know if it will break anything else so please consider.