-
Notifications
You must be signed in to change notification settings - Fork 65
feat: add macro #179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: add macro #179
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
735b912
feat: add macro
tdelabro ae50e10
chore: move macro to own file
tdelabro 9465192
typos
tdelabro fa8c332
extern crate alloc import in test
tdelabro ef5bf3a
unsigned int const expressions OK
tdelabro 4805529
more types handled
tdelabro f71eec8
spaned errors
tdelabro 75ef55d
alloc in tests
tdelabro 80a40e9
review
tdelabro cc61bf3
improve error message
tdelabro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,14 @@ | ||
| name: Typo Check | ||
|
|
||
| on: | ||
| push: | ||
| branches: [ main ] | ||
| pull_request: | ||
| branches: [ main ] | ||
|
|
||
| jobs: | ||
| typos: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: crate-ci/typos@master |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| [workspace] | ||
| members = [ | ||
| "crates/felt-macro", | ||
| "crates/starknet-types-core", | ||
| ] | ||
|
|
||
|
|
||
This file contains hidden or 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,12 @@ | ||
| [package] | ||
| name = "felt-macro" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
|
|
||
| [lib] | ||
| proc-macro = true | ||
|
|
||
| [dependencies] | ||
| syn = { version = "2.0", features = ["full"] } | ||
| quote = "1.0" | ||
| proc-macro2 = "1.0" |
This file contains hidden or 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,101 @@ | ||
| use proc_macro::TokenStream; | ||
| use quote::quote; | ||
| use syn::{Expr, Lit, parse_macro_input}; | ||
|
|
||
| #[proc_macro] | ||
| pub fn felt(input: TokenStream) -> TokenStream { | ||
| let expr = parse_macro_input!(input as Expr); | ||
|
|
||
| match &expr { | ||
| Expr::Lit(expr_lit) => match &expr_lit.lit { | ||
| Lit::Str(lit_str) => { | ||
| let value = lit_str.value(); | ||
|
|
||
| // Check if it's a hex string (starts with 0x or 0X) | ||
| if value.starts_with("0x") || value.starts_with("0X") { | ||
| // Hex string: use const fn for compile-time validation | ||
| quote! { | ||
| { | ||
| const __FELT_VALUE: Felt = Felt::from_hex_unwrap(#lit_str); | ||
| __FELT_VALUE | ||
| } | ||
| } | ||
| .into() | ||
| } else { | ||
| // Check for valid decimal format (optional leading minus, then digits) | ||
| let is_valid = if let Some(stripped) = value.strip_prefix('-') { | ||
| !stripped.is_empty() && stripped.chars().all(|c| c.is_ascii_digit()) | ||
| } else { | ||
| !value.is_empty() && value.chars().all(|c| c.is_ascii_digit()) | ||
| }; | ||
|
|
||
| if !is_valid { | ||
| return syn::Error::new_spanned( | ||
| lit_str, | ||
| format!("Invalid Felt decimal string literal: '{}'. Expected decimal digits (0-9), optionally prefixed with '-'.", value) | ||
| ) | ||
| .to_compile_error() | ||
| .into(); | ||
| } | ||
|
|
||
| // Valid format, generate runtime parsing code | ||
| quote! { | ||
| match <Felt as ::core::str::FromStr>::from_str(#lit_str) { | ||
| Ok(f) => f, | ||
| Err(_) => panic!(concat!("Invalid Felt decimal string literal: ", #lit_str)), | ||
| } | ||
| } | ||
| .into() | ||
| } | ||
| } | ||
|
|
||
| Lit::Bool(lit_bool) => quote! { | ||
| match #lit_bool { | ||
| true => Felt::ONE, | ||
| false => Felt::ZERO, | ||
| } | ||
| } | ||
| .into(), | ||
|
|
||
| Lit::Int(lit_int) => quote! { | ||
| Felt::from(#lit_int) | ||
| } | ||
| .into(), | ||
|
|
||
| _ => panic!("Unsupported literal type for felt! macro"), | ||
| }, | ||
|
|
||
| // Handle negative integer literals: -42, -123, etc. | ||
| Expr::Unary(expr_unary) if matches!(expr_unary.op, syn::UnOp::Neg(_)) => { | ||
| if let Expr::Lit(syn::ExprLit { | ||
| lit: Lit::Int(lit_int), | ||
| .. | ||
| }) = &*expr_unary.expr | ||
| { | ||
| // Negative integer literal | ||
| quote! { | ||
| Felt::from(-#lit_int) | ||
| } | ||
| .into() | ||
| } else { | ||
| // Some other unary negation, treat as expression | ||
| quote! { | ||
| match <Felt as ::core::str::FromStr>::from_str(&#expr) { | ||
| Ok(f) => f, | ||
| Err(_) => panic!("Invalid Felt value"), | ||
| } | ||
| } | ||
| .into() | ||
| } | ||
| } | ||
|
|
||
| // Anything else is handled as a string and will fail if it is not one | ||
| _ => quote! { | ||
| match Felt::try_from(#expr) { | ||
| Ok(f) => f, | ||
| Err(_) => panic!("Invalid Felt value"), | ||
| } | ||
| } | ||
| .into(), | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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,76 @@ | ||
| /// Handy macro to initialize `Felt` | ||
| // | ||
| /// Accepts: | ||
| /// - booleans | ||
| /// - positive and negative number literals (eg. `5`, `12u8`, `-77`, `-2007i32`) | ||
| /// - positive and negative decimal string literals (eg. `"5"`, `"-12"`) | ||
| /// - positive hexadecimal string literal (eg. `"0x0"`, `"0x42"`) | ||
| /// - variables of any type that implements `TryFrom<T> for Felt` (eg. `u32`, `i128`, `&str`, `String`) | ||
| /// - functions and closure which return type implements `TryFrom<T> for Felt` (eg. `|x| x * 42`, `fn ret42() -> u32 { 42 }` ) | ||
| /// - code block (eg. `{40 + 2}`) and more generally any expression that returns as types that implements `TryFrom<T> for Felt` | ||
| /// | ||
| /// Use in `const` expression is only possible using literal `bool` and literal hex string | ||
| /// because the other types rely on non-`const` function for conversion (eg. `From::from` for numbers). | ||
| #[macro_export] | ||
| macro_rules! felt { | ||
| ($($tt:tt)*) => {{ | ||
| let felt: $crate::felt::Felt = felt_macro::felt!($($tt)*); | ||
| felt | ||
| }}; | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| #[cfg(feature = "alloc")] | ||
| pub extern crate alloc; | ||
|
|
||
| use crate::felt::Felt; | ||
|
|
||
| #[test] | ||
| fn felt_macro() { | ||
| // Bools | ||
| assert_eq!(felt!(false), Felt::ZERO); | ||
| assert_eq!(felt!(true), Felt::ONE); | ||
|
|
||
| // Primitive numbers | ||
| assert_eq!(felt!(42), Felt::from(42)); | ||
| assert_eq!(felt!(42u8), Felt::from(42)); | ||
| assert_eq!(felt!(42i8), Felt::from(42)); | ||
| assert_eq!(felt!(42u128), Felt::from(42)); | ||
| assert_eq!(felt!(-42), Felt::ZERO - Felt::from(42)); | ||
| assert_eq!(felt!(-42i8), Felt::ZERO - Felt::from(42)); | ||
|
|
||
| // Static &str | ||
| assert_eq!(felt!("42"), Felt::from(42)); | ||
| assert_eq!(felt!("-42"), Felt::ZERO - Felt::from(42)); | ||
| assert_eq!(felt!("0x42"), Felt::from_hex_unwrap("0x42")); | ||
|
|
||
| // Variables | ||
| let x = true; | ||
| assert_eq!(felt!(x), Felt::ONE); | ||
| let x = "42"; | ||
| assert_eq!(felt!(x), Felt::from(42)); | ||
| let x = alloc::string::String::from("42"); | ||
| assert_eq!(felt!(x), Felt::from(42)); | ||
| let x = 42u32; | ||
| assert_eq!(felt!(x), Felt::from(42)); | ||
| let x = -42; | ||
| assert_eq!(felt!(x), Felt::ZERO - Felt::from(42)); | ||
|
|
||
| // Expressions | ||
| let double_closure = |x| x * 2; | ||
| assert_eq!(felt!(double_closure(5)), Felt::from(10)); | ||
| assert_eq!(felt!({ 40 + 2 }), Felt::from(42)); | ||
|
|
||
| // Constants | ||
| const X: &str = "42"; | ||
| assert_eq!(felt!(X), Felt::from(42)); | ||
| const Y: u32 = 42; | ||
| assert_eq!(felt!(Y), Felt::from(42)); | ||
|
|
||
| // Use in const expressions | ||
| const _: Felt = felt!("0x42"); | ||
| const _: Felt = felt!(true); | ||
| const _: Felt = felt!(false); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| [toolchain] | ||
| channel = "1.87.0" | ||
| channel = "1.89.0" | ||
tdelabro marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| components = ["rustfmt", "clippy", "rust-analyzer"] | ||
| profile = "minimal" | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.