From b4cad360dbf019cd325e22609e90e62a549edd59 Mon Sep 17 00:00:00 2001 From: Pia Date: Mon, 9 Sep 2024 09:48:21 +0900 Subject: [PATCH] feat: starknet-types-macros --- Cargo.toml | 1 + crates/starknet-types-macros/Cargo.toml | 21 ++++++++++++++++ crates/starknet-types-macros/src/lib.rs | 24 +++++++++++++++++++ crates/starknet-types-rpc/Cargo.toml | 5 +++- .../starknet-types-rpc/src/custom/block_id.rs | 6 +++-- 5 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 crates/starknet-types-macros/Cargo.toml create mode 100644 crates/starknet-types-macros/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 6277ccb..0d8d559 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "crates/starknet-types-core", "crates/starknet-types-rpc", + "crates/starknet-types-macros", ] exclude = ["ensure_no_std", "fuzz"] diff --git a/crates/starknet-types-macros/Cargo.toml b/crates/starknet-types-macros/Cargo.toml new file mode 100644 index 0000000..f0063e7 --- /dev/null +++ b/crates/starknet-types-macros/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "starknet-types-macros" +version = "0.1.5" +edition = "2021" +license = "MIT" +homepage = "https://github.com/starknet-io/types-rs" +repository = "https://github.com/starknet-io/types-rs" +categories = ["mathematics", "cryptography"] +keywords = ["stark", "zkp", "cairo"] +description = "Macros types representation for Starknet" +readme = "README.md" + +[lib] +proc-macro = true + +[dependencies] +starknet-types-core = { path = "../starknet-types-core", default-features = false, features = [ + "serde", +] } +syn = { version = "0.15", features = ["full", "extra-traits"] } +quote = "0.6" diff --git a/crates/starknet-types-macros/src/lib.rs b/crates/starknet-types-macros/src/lib.rs new file mode 100644 index 0000000..16fb1f5 --- /dev/null +++ b/crates/starknet-types-macros/src/lib.rs @@ -0,0 +1,24 @@ +use proc_macro::TokenStream; +use quote::quote; +use starknet_types_core::felt::Felt; +use syn::{parse_macro_input, LitStr}; + +#[proc_macro] +pub fn felt(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as LitStr); + + let str_value = input.value(); + + let felt_value = if str_value.starts_with("0x") { + Felt::from_hex(&str_value).expect("invalid Felt value") + } else { + Felt::from_dec_str(&str_value).expect("invalid Felt value") + }; + + let felt_bytes = felt_value.to_bytes_be(); + + quote! { + Felt::from_bytes_be(&[#(#felt_bytes),*]) + } + .into() +} diff --git a/crates/starknet-types-rpc/Cargo.toml b/crates/starknet-types-rpc/Cargo.toml index 03d5407..cac7dbc 100644 --- a/crates/starknet-types-rpc/Cargo.toml +++ b/crates/starknet-types-rpc/Cargo.toml @@ -22,7 +22,10 @@ default = ["std"] std = ["serde/std", "starknet-types-core/std"] [dependencies] -starknet-types-core = { path = "../starknet-types-core", default-features = false, features = ["serde"] } +starknet-types-core = { path = "../starknet-types-core", default-features = false, features = [ + "serde", +] } +starknet-types-macros = { path = "../starknet-types-macros" } serde = { version = "1", default-features = false, features = ["derive"] } [dev-dependencies] diff --git a/crates/starknet-types-rpc/src/custom/block_id.rs b/crates/starknet-types-rpc/src/custom/block_id.rs index e5b92eb..a4e26c5 100644 --- a/crates/starknet-types-rpc/src/custom/block_id.rs +++ b/crates/starknet-types-rpc/src/custom/block_id.rs @@ -69,10 +69,11 @@ impl<'de, F: Deserialize<'de>> serde::Deserialize<'de> for BlockId { #[test] fn block_id_from_hash() { pub use starknet_types_core::felt::Felt; + use starknet_types_macros::felt; let s = "{\"block_hash\":\"0x123\"}"; let block_id: BlockId = serde_json::from_str(s).unwrap(); - assert_eq!(block_id, BlockId::Hash(Felt::from_hex("0x123").unwrap())); + assert_eq!(block_id, BlockId::Hash(felt!("0x123"))); } #[test] @@ -106,8 +107,9 @@ fn block_id_from_pending() { #[test] fn block_id_to_hash() { pub use starknet_types_core::felt::Felt; + use starknet_types_macros::felt; - let block_id = BlockId::Hash(Felt::from_hex("0x123").unwrap()); + let block_id = BlockId::Hash(felt!("0x123")); let s = serde_json::to_string(&block_id).unwrap(); assert_eq!(s, "{\"block_hash\":\"0x123\"}"); }