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

fix: generate valid toml when outputting nested structs #936

Merged
merged 3 commits into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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