Skip to content

Commit

Permalink
feat: error handling (#52)
Browse files Browse the repository at this point in the history
* add proc-macro-error for better error feedback to user

* add trybuild for testing test

* ci: remove nightly tests

---------

Co-authored-by: glihm <[email protected]>
  • Loading branch information
greged93 and glihm authored Aug 26, 2024
1 parent fb91215 commit 299e7ca
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
toolchain: [stable, nightly]
toolchain: [stable]

steps:
- name: Checkout source code
Expand Down
114 changes: 112 additions & 2 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion crates/rs-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ proc-macro = true
[dependencies]
anyhow.workspace = true
starknet.workspace = true
cainome-cairo-serde.workspace = true
cainome-parser.workspace = true
cainome-rs.workspace = true
proc-macro2 = "1.0"
proc-macro-error = "1.0.4"
quote = "1.0"
syn = "2.0.15"
serde_json = "1.0.74"
thiserror.workspace = true
cainome-cairo-serde.workspace = true

[dev-dependencies]
trybuild = "1.0.99"
3 changes: 3 additions & 0 deletions crates/rs-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cainome_parser::{AbiParser, AbiParserLegacy};
use cainome_rs::{self};
use proc_macro::TokenStream;
use proc_macro_error::proc_macro_error;
use quote::quote;

mod macro_inputs;
Expand All @@ -10,11 +11,13 @@ mod spanned;
use crate::macro_inputs::ContractAbi;
use crate::macro_inputs_legacy::ContractAbiLegacy;

#[proc_macro_error]
#[proc_macro]
pub fn abigen(input: TokenStream) -> TokenStream {
abigen_internal(input)
}

#[proc_macro_error]
#[proc_macro]
pub fn abigen_legacy(input: TokenStream) -> TokenStream {
abigen_internal_legacy(input)
Expand Down
13 changes: 10 additions & 3 deletions crates/rs-macro/src/macro_inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//!
//! TODO: support the full artifact JSON to be able to
//! deploy contracts from abigen.
use proc_macro_error::emit_error;
use quote::ToTokens;
use starknet::core::types::contract::{AbiEntry, SierraClass};
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -114,10 +115,16 @@ impl Parse for ContractAbi {

for type_alias in parsed {
if !abi_types.insert(type_alias.abi.clone()) {
panic!("{} duplicate abi type", type_alias.abi)
emit_error!(
type_alias.span(),
format!("{} duplicate abi type", type_alias.abi)
);
}
if !aliases.insert(type_alias.alias.clone()) {
panic!("{} duplicate alias name", type_alias.alias)
emit_error!(
type_alias.span(),
format!("{} duplicate alias name", type_alias.alias)
);
}

let ta = type_alias.into_inner();
Expand Down Expand Up @@ -146,7 +153,7 @@ impl Parse for ContractAbi {
derives.push(derive.to_token_stream().to_string());
}
}
_ => panic!("unexpected named parameter `{}`", name),
_ => emit_error!(name.span(), format!("unexpected named parameter `{name}`")),
}
}

Expand Down
13 changes: 10 additions & 3 deletions crates/rs-macro/src/macro_inputs_legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//!
//! TODO: support the full artifact JSON to be able to
//! deploy contracts from abigen.
use proc_macro_error::emit_error;
use quote::ToTokens;
use starknet::core::types::contract::legacy::{LegacyContractClass, RawLegacyAbiEntry};
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -109,10 +110,16 @@ impl Parse for ContractAbiLegacy {

for type_alias in parsed {
if !abi_types.insert(type_alias.abi.clone()) {
panic!("{} duplicate abi type", type_alias.abi)
emit_error!(
type_alias.span(),
format!("{} duplicate abi type", type_alias.abi)
);
}
if !aliases.insert(type_alias.alias.clone()) {
panic!("{} duplicate alias name", type_alias.alias)
emit_error!(
type_alias.span(),
format!("{} duplicate alias name", type_alias.alias)
);
}

let ta = type_alias.into_inner();
Expand All @@ -124,7 +131,7 @@ impl Parse for ContractAbiLegacy {
parenthesized!(content in input);
output_path = Some(content.parse::<LitStr>()?.value());
}
_ => panic!("unexpected named parameter `{}`", name),
_ => emit_error!(name.span(), format!("unexpected named parameter `{name}`")),
}
}

Expand Down
26 changes: 26 additions & 0 deletions crates/rs-macro/tests/abigen/duplicate_abi_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#![no_main]
use cainome_rs_macro::abigen;

abigen!(
MyContract,
r#"[
{
"type": "struct",
"name": "core::integer::u256",
"members": [
{
"name": "low",
"type": "core::integer::u128"
},
{
"name": "high",
"type": "core::integer::u128"
}
]
}
]"#,
type_aliases {
core::integer::u256 as MyStruct1;
core::integer::u256 as MyStruct2;
}
);
5 changes: 5 additions & 0 deletions crates/rs-macro/tests/abigen/duplicate_abi_type.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: core::integer::u256 duplicate abi type
--> tests/abigen/duplicate_abi_type.rs:24:9
|
24 | core::integer::u256 as MyStruct2;
| ^^^^
36 changes: 36 additions & 0 deletions crates/rs-macro/tests/abigen/duplicate_alias_name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#![no_main]
use cainome_rs_macro::abigen;

abigen!(
MyStruct,
r#"[
{
"type": "struct",
"name": "core::integer::u256",
"members": [
{
"name": "low",
"type": "core::integer::u128"
},
{
"name": "high",
"type": "core::integer::u128"
}
]
},
{
"type": "struct",
"name": "core::starknet::eth_address::EthAddress",
"members": [
{
"name": "address",
"type": "core::felt252"
}
]
}
]"#,
type_aliases {
core::integer::u256 as MyStruct;
core::starknet::eth_address::EthAddress as MyStruct;
}
);
5 changes: 5 additions & 0 deletions crates/rs-macro/tests/abigen/duplicate_alias_name.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: MyStruct duplicate alias name
--> tests/abigen/duplicate_alias_name.rs:34:9
|
34 | core::starknet::eth_address::EthAddress as MyStruct;
| ^^^^
Loading

0 comments on commit 299e7ca

Please sign in to comment.