dtolnay / serde-yaml

Strongly typed YAML library for Rust
Apache License 2.0
964 stars 164 forks source link

Serde deserializer is not implemented #247

Closed FlashyReese closed 2 years ago

FlashyReese commented 2 years ago

The deserializer does not implement the Serde deserializer. Meanwhile, the serializer does implement the Serde serializer.

Code snippet:

fn main() {
    println!("{}", convert_yaml_to_toml("toml: 0.5.9").unwrap())
}

pub fn convert_toml_to_yaml(input: &str) -> Result<String, String> {
    let mut buf = std::io::BufWriter::new(Vec::new());
    let mut deserializer = toml::Deserializer::new(input);
    let mut serializer = serde_yaml::Serializer::new(&mut buf);
    serde_transcode::transcode(&mut deserializer, &mut serializer).map_err(|err| err.to_string())?;
    Ok(String::from_utf8(buf.into_inner().unwrap()).unwrap())
}

pub fn convert_yaml_to_toml(input: &str) -> Result<String, String> {
    let mut toml = String::new();
    let mut deserializer = serde_yaml::Deserializer::from_str(input);
    let mut serializer = toml::Serializer::pretty(&mut toml);
    serde_transcode::transcode(&mut deserializer, &mut serializer).map_err(|err| err.to_string())?;
    Ok(toml)
}

Build Output: image

dtolnay commented 2 years ago

The Deserializer impl is for serde_yaml::Deserializer, not &mut serde_yaml::Deserializer.

- serde_transcode::transcode(&mut deserializer, &mut serializer).map_err(|err| err.to_string())?;
+ serde_transcode::transcode(deserializer, &mut serializer).map_err(|err| err.to_string())?;