Skip to content

Commit

Permalink
feat: allow imported FieldElement type in macros (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
xJonathanLEI authored Jan 21, 2023
1 parent 54d2890 commit e3f3e4f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
4 changes: 4 additions & 0 deletions starknet-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ proc-macro = true
[dependencies]
starknet-core = { version = "0.2.0", path = "../starknet-core" }
syn = "1.0.96"

[features]
default = []
use_imported_type = []
50 changes: 40 additions & 10 deletions starknet-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ pub fn selector(input: TokenStream) -> TokenStream {
let selector_raw = selector_value.into_mont();

format!(
"::starknet::core::types::FieldElement::from_mont([{}, {}, {}, {}])",
selector_raw[0], selector_raw[1], selector_raw[2], selector_raw[3],
"{}::from_mont([{}, {}, {}, {}])",
field_element_path(),
selector_raw[0],
selector_raw[1],
selector_raw[2],
selector_raw[3],
)
.parse()
.unwrap()
Expand All @@ -32,8 +36,12 @@ pub fn short_string(input: TokenStream) -> TokenStream {
let felt_raw = felt_value.into_mont();

format!(
"::starknet::core::types::FieldElement::from_mont([{}, {}, {}, {}])",
felt_raw[0], felt_raw[1], felt_raw[2], felt_raw[3],
"{}::from_mont([{}, {}, {}, {}])",
field_element_path(),
felt_raw[0],
felt_raw[1],
felt_raw[2],
felt_raw[3],
)
.parse()
.unwrap()
Expand All @@ -54,8 +62,12 @@ pub fn felt(input: TokenStream) -> TokenStream {
let felt_raw = felt_value.into_mont();

format!(
"::starknet::core::types::FieldElement::from_mont([{}, {}, {}, {}])",
felt_raw[0], felt_raw[1], felt_raw[2], felt_raw[3],
"{}::from_mont([{}, {}, {}, {}])",
field_element_path(),
felt_raw[0],
felt_raw[1],
felt_raw[2],
felt_raw[3],
)
.parse()
.unwrap()
Expand All @@ -71,8 +83,12 @@ pub fn felt_dec(input: TokenStream) -> TokenStream {
let felt_raw = felt_value.into_mont();

format!(
"::starknet::core::types::FieldElement::from_mont([{}, {}, {}, {}])",
felt_raw[0], felt_raw[1], felt_raw[2], felt_raw[3],
"{}::from_mont([{}, {}, {}, {}])",
field_element_path(),
felt_raw[0],
felt_raw[1],
felt_raw[2],
felt_raw[3],
)
.parse()
.unwrap()
Expand All @@ -88,9 +104,23 @@ pub fn felt_hex(input: TokenStream) -> TokenStream {
let felt_raw = felt_value.into_mont();

format!(
"::starknet::core::types::FieldElement::from_mont([{}, {}, {}, {}])",
felt_raw[0], felt_raw[1], felt_raw[2], felt_raw[3],
"{}::from_mont([{}, {}, {}, {}])",
field_element_path(),
felt_raw[0],
felt_raw[1],
felt_raw[2],
felt_raw[3],
)
.parse()
.unwrap()
}

#[cfg(feature = "use_imported_type")]
fn field_element_path() -> &'static str {
"FieldElement"
}

#[cfg(not(feature = "use_imported_type"))]
fn field_element_path() -> &'static str {
"::starknet::core::types::FieldElement"
}

0 comments on commit e3f3e4f

Please sign in to comment.