Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vector of vectors produces invalid toml with trailing comma #387

Closed
anthrotype opened this issue Dec 5, 2022 · 2 comments · Fixed by #471
Closed

vector of vectors produces invalid toml with trailing comma #387

anthrotype opened this issue Dec 5, 2022 · 2 comments · Fixed by #471
Labels
A-serde Area: Serde integration C-bug Category: Things not working as expected

Comments

@anthrotype
Copy link

anthrotype commented Dec 5, 2022

I would like to use toml to serialise/deserialize a struct that contains a Vec<Vec>, however when I try that I get toml that is invalid and cannot be parsed back into a struct. See the code below, how the toml string gets serialized without errors however when I attempt to parse it it fails with

Compiling playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 6.85s
     Running `target/debug/playground`
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error { inner: ErrorInner { kind: Wanted { expected: "a table key", found: "a comma" }, line: Some(49), col: 0, at: Some(485), message: "", key: [] } }', src/main.rs:51:40
note: [run with `RUST_BACKTRACE=1` environment variable to display a backtrace](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a4fdfa8f7a9fe94e94c4662e975f971b#)
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, Debug)]
struct Glyph {
    components: Vec<Component>,
    // contours is a vector of vectors of points
    contours: Vec<Contour>,
}

#[derive(Deserialize, Serialize, Debug)]
struct Point {
    x: f64,
    y: f64,
    pt_type: String,
}

type Contour = Vec<Point>;

#[derive(Deserialize, Serialize, Debug)]
struct Component {
    base: String,
    transform: (f64, f64, f64, f64, f64, f64),
}

fn main() {
    let comp1 = Component { base: "b".to_string(), transform:  (1.0, 0.0, 0.0, 1.0, 0.0, 0.0) };
    let comp2 = Component { base: "c".to_string(), transform:  (1.0, 0.0, 0.0, 1.0, 0.0, 0.0) };
    let components = vec![comp1, comp2];
    
    let contours = vec![
        vec![
            Point { x: 3.0, y: 4.0, pt_type: "line".to_string() },
            Point { x: 5.0, y: 6.0, pt_type: "line".to_string() },
        ],
        vec![
            Point { x: 0.0, y: 0.0, pt_type: "move".to_string() },
            Point { x: 7.0, y: 9.0, pt_type: "offcurve".to_string() },
            Point { x: 8.0, y: 10.0, pt_type: "offcurve".to_string() },
            Point { x: 11.0, y: 12.0, pt_type: "curve".to_string() },
        ],
    ];
    let g1 = Glyph { contours, components };

    let s = toml::to_string_pretty(&g1).unwrap();
    // the vector of vectors contains a stray trailing comma at the end, making it
    // invalid toml
    println!("{}", s);
    // in fact, toml fails with inner error, { expected: "a table key", found: "a comma" }
    // at line 49, col 0
    let g2: Glyph = toml::from_str(&s).unwrap();
    println!("{:?}", g2);

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=57b8b8d804328548b40d24dd4a5b8ae7

Am I doing something wrong? Is it possible to have an array of arrays of tables in toml, like you can say in json or yaml?

@epage
Copy link
Member

epage commented Dec 5, 2022

toml does not support arbitrary structures like json or yaml; I do not think Vec<Vec<Table>> is supported for standard tables but would be for inline tables.

What results do you get using toml_edit::easy (cargo add toml_edit -F easy)? We expect to move tom;s parser/generator to use toml_edit (see #340). If toml_edit::easy works for you, that could be a workaround until we make that change.

@anthrotype
Copy link
Author

thanks! I tried to replace it with toml_edit::easy and indeed it works, i.e. gives me array of arrays of inline tables for the Points. I'll try use that for now.

@epage epage added C-bug Category: Things not working as expected A-serde Area: Serde integration labels Jan 18, 2023
epage added a commit to epage/toml_edit that referenced this issue Jan 19, 2023
With toml-rs#470, we more reliably generate valid output.

Fixes toml-rs#387
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-serde Area: Serde integration C-bug Category: Things not working as expected
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants