forked from KasarLabs/ditto
-
Notifications
You must be signed in to change notification settings - Fork 0
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 KasarLabs#32 from Trantorian1/feat/macros
feat(macros): ✨ New `require` and `logging` macros
- Loading branch information
Showing
13 changed files
with
193 additions
and
62 deletions.
There are no files selected for viewing
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,80 @@ | ||
use macro_utils::{extract_u64_from_expr, get_block_number}; | ||
use proc_macro::TokenStream; | ||
use quote::{quote, ToTokens}; | ||
use syn::{ | ||
parse::{Parse, ParseStream}, | ||
parse_macro_input, parse_quote, ItemFn, MetaNameValue, Path, Token, | ||
}; | ||
|
||
#[proc_macro_attribute] | ||
pub fn logging(_: TokenStream, input: TokenStream) -> TokenStream { | ||
let mut input = parse_macro_input!(input as ItemFn); | ||
|
||
input.block.stmts.insert( | ||
0, | ||
syn::parse( | ||
quote! { | ||
env_logger::builder().is_test(true).try_init().err(); | ||
} | ||
.into(), | ||
) | ||
.unwrap(), | ||
); | ||
|
||
input.into_token_stream().into() | ||
} | ||
|
||
struct ArgsRequire { | ||
pub block_min: u64, | ||
pub block_max: u64, | ||
pub err: Result<(), Path>, | ||
} | ||
|
||
impl Parse for ArgsRequire { | ||
fn parse(input: ParseStream) -> syn::Result<Self> { | ||
let args = input.parse_terminated(MetaNameValue::parse, Token![,])?; | ||
|
||
let mut parsed_params = Self { | ||
block_min: 0, | ||
block_max: 0, | ||
err: Ok(()), | ||
}; | ||
|
||
for arg in args { | ||
match arg.path.get_ident() { | ||
Some(ident) => match ident.to_string().as_str() { | ||
"block_min" => { | ||
parsed_params.block_min = extract_u64_from_expr(arg.value).unwrap_or(0); | ||
} | ||
"block_max" => { | ||
parsed_params.block_max = | ||
extract_u64_from_expr(arg.value).unwrap_or(u64::MAX); | ||
} | ||
_ => { | ||
parsed_params.err = Err(arg.path); | ||
} | ||
}, | ||
None => todo!(), | ||
} | ||
} | ||
|
||
Ok(parsed_params) | ||
} | ||
} | ||
|
||
#[proc_macro_attribute] | ||
pub fn require(args: TokenStream, item: TokenStream) -> TokenStream { | ||
let bn = get_block_number(); | ||
let args_parsed = parse_macro_input!(args as ArgsRequire); | ||
|
||
if bn >= args_parsed.block_min && bn <= args_parsed.block_max { | ||
item | ||
} else { | ||
let mut func = parse_macro_input!(item as ItemFn); | ||
func.attrs.push( | ||
parse_quote!(#[ignore = "Deoxys node does not meet required specs to run this test"]), | ||
); | ||
|
||
quote!(#func).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "macro_utils" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
anyhow = "1.0.79" | ||
serde = "1.0.195" | ||
serde_json = "1.0.111" | ||
starknet = { git = "https://github.com/xJonathanLEI/starknet-rs.git", rev = "64ebc36", default-features = false } | ||
starknet-core = { git = "https://github.com/xJonathanLEI/starknet-rs.git", rev = "64ebc36", default-features = false } | ||
starknet-providers = { git = "https://github.com/xJonathanLEI/starknet-rs.git", rev = "64ebc36", default-features = false } | ||
url = "2.5.0" | ||
syn = "2.0.48" | ||
quote = "1.0.35" | ||
tokio = { version = "1", features = ["full"] } |
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,50 @@ | ||
use serde::Deserialize; | ||
use starknet_providers::{jsonrpc::HttpTransport, JsonRpcClient, Provider}; | ||
use std::{fs::File, io::Read}; | ||
use syn::{Expr, Lit}; | ||
use tokio::runtime; | ||
use url::Url; | ||
|
||
#[derive(PartialEq, Debug, Deserialize)] | ||
pub struct TestConfig { | ||
pub pathfinder: String, | ||
pub deoxys: String, | ||
} | ||
|
||
impl TestConfig { | ||
pub fn new(path: &str) -> anyhow::Result<Self> { | ||
let mut file = File::open(path)?; | ||
let mut content = String::new(); | ||
|
||
file.read_to_string(&mut content)?; | ||
|
||
let config: TestConfig = serde_json::from_str(&content) | ||
.expect("Could not deserialize test at {path} into Config"); | ||
|
||
Ok(config) | ||
} | ||
} | ||
|
||
pub fn get_block_number() -> u64 { | ||
let config = | ||
TestConfig::new("./secret.json").expect("'./secret.json' must contain correct node urls"); | ||
let deoxys = JsonRpcClient::new(HttpTransport::new( | ||
Url::parse(&config.deoxys).expect("Error parsing Deoxys node url"), | ||
)); | ||
|
||
let rt = runtime::Runtime::new().unwrap(); | ||
|
||
rt.block_on(async { deoxys.block_number().await.unwrap() }) | ||
} | ||
|
||
pub fn extract_u64_from_expr(expr: Expr) -> Result<u64, String> { | ||
match expr { | ||
Expr::Lit(expr_lit) => match expr_lit.lit { | ||
Lit::Int(lit_int) => lit_int | ||
.base10_parse::<u64>() | ||
.map_err(|_| "Failed to parse integer".to_string()), | ||
_ => Err("Not an integer literal".to_string()), | ||
}, | ||
_ => Err("Not a literal expression".to_string()), | ||
} | ||
} |
This file was deleted.
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
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