-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from OffchainLabs/SolidityError
SolidityError derive macro
- Loading branch information
Showing
13 changed files
with
260 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2024, Offchain Labs, Inc. | ||
// For licensing, see https://github.com/OffchainLabs/stylus-sdk-rs/blob/stylus/licenses/COPYRIGHT.md | ||
|
||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{parse_macro_input, Fields, ItemEnum}; | ||
|
||
pub fn derive_solidity_error(input: TokenStream) -> TokenStream { | ||
let input = parse_macro_input!(input as ItemEnum); | ||
let name = &input.ident; | ||
let mut match_arms = quote!(); | ||
let mut errors = vec![]; | ||
for variant in input.variants { | ||
let variant_name = variant.ident; | ||
let error = match variant.fields { | ||
Fields::Unnamed(e) if variant.fields.len() == 1 => e.unnamed.first().unwrap().clone(), | ||
_ => error!(variant.fields, "Variant not a 1-tuple"), | ||
}; | ||
match_arms.extend(quote! { | ||
#name::#variant_name(e) => stylus_sdk::alloy_sol_types::SolError::encode(&e), | ||
}); | ||
errors.push(error); | ||
} | ||
let mut output = quote! { | ||
impl From<#name> for alloc::vec::Vec<u8> { | ||
fn from(err: #name) -> alloc::vec::Vec<u8> { | ||
match err { | ||
#match_arms | ||
} | ||
} | ||
} | ||
}; | ||
|
||
if cfg!(feature = "export-abi") { | ||
output.extend(quote! { | ||
impl stylus_sdk::abi::export::internal::InnerTypes for #name { | ||
fn inner_types() -> alloc::vec::Vec<stylus_sdk::abi::export::internal::InnerType> { | ||
use alloc::{format, vec}; | ||
use core::any::TypeId; | ||
use stylus_sdk::abi::export::internal::InnerType; | ||
use stylus_sdk::alloy_sol_types::SolError; | ||
|
||
vec![ | ||
#( | ||
InnerType { | ||
name: format!("error {};", <#errors as SolError>::SIGNATURE.replace(',', ", ")), | ||
id: TypeId::of::<#errors>(), | ||
} | ||
),* | ||
] | ||
} | ||
} | ||
}); | ||
} | ||
|
||
output.into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.