arcnmx / serde-ini

Windows INI file {de,}serialization
MIT License
19 stars 17 forks source link

ser::Error::OrphanValue #10

Open arcnmx opened 5 years ago

arcnmx commented 5 years ago

An INI file can only contain top-level keys at the beginning of the file, before the first section starts. This presents a problem if we serialize a section (map/struct type), and then later on encounter another non-section key, so unfortunately depends on the order in which the fields are serialized (generally, this is declaration order). Currently the Serializer will error when this happens with OrphanValue. A simple demonstration:

#[derive(Serialize)]
struct TopLevel {
    data: i32, // a top-level key
    section: HashMap<String, String>, // a [section]
    orphan: i32, // oh no, this needs to be placed above `section`
}

The obvious approach to this I can see would be to avoid writing any sections until the entire type is serialized. Yay allocation, and in the most common case probably means not writing anything out and buffering the whole ini file until the very end. Possibly make this an option when constructing the Serializer?

Maybe with specialization (or code duplication whatever) this could be handled okay when serializing to a buffer (Vec<u8>) where you can just seek+memmove+insert when orphans are encountered. I don't see that working well with files though :(

arcnmx commented 5 years ago

It's worth noting that the toml format has the same limitations, so the serde serializer has the same issue, and provides a workaround that needs to be explicitly used via serialize_with. Seems kind of unfortunate that toml didn't address this, but at least it offers prior art for how to work around it.