Skip to content

Commit

Permalink
fix: add more info on model deserialization fail (starkware-libs#1325)
Browse files Browse the repository at this point in the history
* feat: add more info on model deserialization fail

* fix: fix cairo tests

* fix: use trait path function call instead of prelude

* fix: fix cairo tests

* fix tests

* fix test
  • Loading branch information
glihm authored Jan 17, 2024
1 parent 66b16f8 commit 8fb9941
Show file tree
Hide file tree
Showing 3 changed files with 332 additions and 24 deletions.
17 changes: 12 additions & 5 deletions crates/dojo-lang/src/inline_macros/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,14 @@ impl InlineMacroExprPlugin for GetMacro {
}
let mut lookup_err_msg = format!("{} not found", model.to_string());
lookup_err_msg.truncate(CAIRO_ERR_MSG_LEN);
let mut deser_err_msg = format!("{} failed to deserialize", model.to_string());
deser_err_msg.truncate(CAIRO_ERR_MSG_LEN);
// Currently, the main reason to have a deserialization to fail is by having
// the user providing the wrong keys length, which causes an invalid offset
// in the model deserialization.
let deser_err_msg = format!(
"\"Model `{}`: deserialization failed. Ensure the length of the keys tuple is \
matching the number of #[key] fields in the model struct.\"",
model.to_string()
);

builder.add_str(&format!(
"\n let mut __{model}_layout__ = core::array::ArrayTrait::new();
Expand All @@ -122,10 +128,11 @@ impl InlineMacroExprPlugin for GetMacro {
core::array::serialize_array_helper(__{model}_values__, ref __{model}_model__);
let mut __{model}_model_span__ = \
core::array::ArrayTrait::span(@__{model}_model__);
let __{model} = \
core::option::OptionTrait::expect(core::serde::Serde::<{model}>::deserialize(
let __{model} = core::serde::Serde::<{model}>::deserialize(
ref __{model}_model_span__
), '{deser_err_msg}');\n",
); if core::option::OptionTrait::<{model}>::is_none(@__{model}) {{ \
panic!({deser_err_msg}); }}; let __{model} = \
core::option::OptionTrait::<{model}>::unwrap(__{model});\n",
world.as_syntax_node().get_text(db),
));
}
Expand Down
2 changes: 1 addition & 1 deletion crates/dojo-lang/src/manifest_test_data/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ test_manifest_file
{
"name": "dojo_examples::actions::actions",
"address": null,
"class_hash": "0x69c6bec7de74fc2404fe6b68ad8ece7be81ad6d861b38a8ba8fa583bfc3666b",
"class_hash": "0x352a6ab719469097c1f3f9db41552fc79a1bf09b83c8a0839052218461ecea9",
"abi": [
{
"type": "impl",
Expand Down
Loading

0 comments on commit 8fb9941

Please sign in to comment.