kdl-org / kdl-rs

Rust parser for KDL
https://docs.rs/kdl
Other
303 stars 28 forks source link

Serde Support #71

Open scott-wilson opened 6 months ago

scott-wilson commented 6 months ago

This is another crack at Serde support (linking #17 for historical reference).

The basic API that I'm thinking of would look something like this (borrowing from Serde JSON):

#[derive(Serialize, Deserialize)]
struct Config {
    root_dir: PathBuf,
    iterations: u32,
}

// Using KDL v1 syntax
let data = r#"
config {
    root_dir "/path/to/project"
    iterations: 3
}
"#;

// Deserialize from string
let conf: Config = kdl::from_str(data).unwrap();

// Deserialize from file
let mut file = std::fs::File::open("config.kdl").unwrap();
let conf2: Config = kdl::read(&mut file).unwrap()

// Serialize to string
let roundtrip_data = kdl::to_string(&conf).unwrap();

// Serialize to file
let mut file = std::fs::File::create("config.kdl").unwrap();
kdl::write(&mut file, &conf).unwrap();

If no one has an issue, I'd be happy to take this on.

zkat commented 6 months ago

Two things:

  1. we probably want to look at what the serde xml parsers do here, because children can mean different things and it needs to be configurable
  2. We should wait until #70 lands to do this, since that’s going to break a lot of stuff
scott-wilson commented 6 months ago

Two things:

  1. we probably want to look at what the serde xml parsers do here, because children can mean different things and it needs to be configurable
  2. We should wait until #70 lands to do this, since that’s going to break a lot of stuff
  1. Sounds good, I'll do some research.
  2. Sounds good. I may do a POC anyways to get a feel for how to get it done. Worst case, it gets scraped and I use the knowledge to do something better.
zkat commented 6 months ago

That seems fine by me. I expect it to be a bit of a challenge tbh but that might be because I’ve never implemented a serde thing before.

scott-wilson commented 6 months ago

Same, haven't implemented serde before. But, that's the fun! We'll learn how to implement it together!

zkat commented 6 months ago

Another useful reference might be Kaydle: https://github.com/Lucretiel/kaydle, which is the last attempt I know of of someone trying to serdeify kdl, and they did some interesting things, just never finished it (it's a big project, it turns out!)

zkat commented 6 months ago

although part of the reason Kaydle was big was because it wrote its own parser and it was being fancy about it. With kdl-rs you'd only really need to convert the KdlDocument struct through serde.

zkat commented 1 week ago

sigh. I think this is going to need a completely new parser. Can I just catch a break lmao.