You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)]structGlyph{components:Vec<Component>,// contours is a vector of vectors of pointscontours:Vec<Contour>,}#[derive(Deserialize,Serialize,Debug)]structPoint{x:f64,y:f64,pt_type:String,}typeContour = Vec<Point>;#[derive(Deserialize,Serialize,Debug)]structComponent{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 tomlprintln!("{}", s);// in fact, toml fails with inner error, { expected: "a table key", found: "a comma" }// at line 49, col 0let g2:Glyph = toml::from_str(&s).unwrap();println!("{:?}", g2);
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.
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.
anthrotype
added a commit
to googlefonts/fontc
that referenced
this issue
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
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?
The text was updated successfully, but these errors were encountered: