Skip to content

Commit

Permalink
feat(zink): introduce proc-macro assert
Browse files Browse the repository at this point in the history
  • Loading branch information
clearloop committed Nov 30, 2024
1 parent eda8027 commit ee9aa49
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
7 changes: 5 additions & 2 deletions examples/revert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ fn main() {}

/// check if the passing address is owner
#[zink::external]
pub fn run_revert() {
pub fn revert() {
zink::revert!("revert works")
}

/// check if the passing address is owner
#[zink::external]
pub fn run_assert() {
pub fn assert() {
zink::assert!(false, "assert works");
}

Expand All @@ -26,5 +26,8 @@ fn test_revert() -> anyhow::Result<()> {

let info = contract.execute(["revert()".as_bytes()])?;
assert_eq!(info.revert, Some("revert works".into()));

let info = contract.execute(["assert()".as_bytes()])?;
assert_eq!(info.revert, Some("assert works".into()));
Ok(())
}
12 changes: 5 additions & 7 deletions zink/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
extern crate proc_macro;

use proc_macro::TokenStream;
use syn::{parse_macro_input, Attribute, DeriveInput, ItemFn, ItemStruct, LitStr};
use proc_macro2::Span;
use quote::ToTokens;
use syn::{parse_macro_input, Attribute, DeriveInput, Expr, ItemFn, ItemStruct, LitStr};

mod event;
mod revert;
Expand All @@ -27,12 +29,8 @@ pub fn revert(input: TokenStream) -> TokenStream {
/// message only support raw string.
#[proc_macro]
pub fn assert(input: TokenStream) -> TokenStream {
todo!(
r#"
1. split input into condition and message then do the following
2. if not cond then revert!(message);
"#
)
let input = parse_macro_input!(input as revert::AssertInput);
revert::parse_assert(input)
}

/// Event logging interface
Expand Down
43 changes: 42 additions & 1 deletion zink/codegen/src/revert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
use proc_macro::TokenStream;
use proc_macro2::{Literal, Span};
use quote::{quote, ToTokens};
use syn::{Ident, LitStr};
use syn::{
parse::{Parse, ParseStream},
parse2, Expr, Ident, LitStr, Token,
};

/// Revert with message
pub fn parse(input: LitStr) -> TokenStream {
Expand Down Expand Up @@ -34,3 +37,41 @@ pub fn parse(input: LitStr) -> TokenStream {
}
.into()
}

/// Parse assert macro
pub fn parse_assert(input: AssertInput) -> TokenStream {
let cond = input.cond;
let revert: Expr = syn::parse2(
parse(
input
.message
.unwrap_or(LitStr::new("unknown error", Span::call_site())),
)
.into(),
)
.expect("Invalid revert message");

quote! {
if !#cond {
#revert
}
}
.into()
}

/// Assert input
pub struct AssertInput {
pub cond: Expr,
pub comma: Token![,],
pub message: Option<LitStr>,
}

impl Parse for AssertInput {
fn parse(input: ParseStream) -> syn::Result<Self> {
Ok(AssertInput {
cond: input.parse()?,
comma: input.parse()?,
message: input.parse()?,
})
}
}

0 comments on commit ee9aa49

Please sign in to comment.