Skip to content

Commit

Permalink
fix: generate valid toml when outputting nested structs (#936)
Browse files Browse the repository at this point in the history
* chore: add regression test for #934

* fix: properly order nested toml tables

* chore: remove unnecessary `to_owned()`
  • Loading branch information
TomAFrench authored Mar 1, 2023
1 parent 156125b commit ba947a7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 16 deletions.
47 changes: 45 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ dirs = "4"
serde = { version = "1.0.136", features = ["derive"] }
smol_str = "0.1.17"
thiserror = "1.0.21"
toml = "0.5.8"
toml = "0.7.2"
url = "2.2.0"
wasm-bindgen = { version = "0.2.83", features = ["serde-serialize"] }
wasm-bindgen-test = "0.3.33"
2 changes: 2 additions & 0 deletions crates/nargo/tests/test_data/struct_inputs/Prover.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ array = [0, 1]
message = "helld"

[a]
baz = 0

[a.bar_struct]
val = "1"
array = [0, 1]
Expand Down
3 changes: 2 additions & 1 deletion crates/nargo/tests/test_data/struct_inputs/src/foo.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod bar;

struct fooStruct {
bar_struct: bar::barStruct
bar_struct: bar::barStruct,
baz: Field,
}
16 changes: 4 additions & 12 deletions crates/noirc_abi/src/input_parser/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,10 @@ pub(crate) fn parse_toml(
pub(crate) fn serialize_to_toml(
w_map: &BTreeMap<String, InputValue>,
) -> Result<String, InputParserError> {
// Toml requires that values be emitted before tables. Thus, we must reorder our map in case a TomlTypes::Table comes before any other values in the toml map
// BTreeMap orders by key and we need the name of the input as our key, so we must split our maps in case a table type has a name that is alphanumerically less
// than any other value type
let (tables_map, to_map): (BTreeMap<String, TomlTypes>, BTreeMap<String, TomlTypes>) = w_map
.iter()
.map(|(key, value)| (key.to_owned(), TomlTypes::from(value.clone())))
.partition(|(_, v)| matches!(v, TomlTypes::Table(_)));

let mut toml_string = toml::to_string(&to_map)?;
let toml_string_tables = toml::to_string(&tables_map)?;

toml_string.push_str(&toml_string_tables);
let to_map: BTreeMap<_, _> =
w_map.iter().map(|(key, value)| (key, TomlTypes::from(value.clone()))).collect();

let toml_string = toml::to_string(&to_map)?;

Ok(toml_string)
}
Expand Down

0 comments on commit ba947a7

Please sign in to comment.