-
Notifications
You must be signed in to change notification settings - Fork 11
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 #232 from bitfinity-network/maxim/export_candid
feature: Add `export_candid` attribute macro
- Loading branch information
Showing
17 changed files
with
181 additions
and
2 deletions.
There are no files selected for viewing
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,63 @@ | ||
use proc_macro::TokenStream; | ||
use quote::{quote, ToTokens}; | ||
use serde::Deserialize; | ||
use syn::parse::{Parse, ParseStream}; | ||
use syn::spanned::Spanned; | ||
use syn::{parse_macro_input, ItemFn, ReturnType}; | ||
|
||
#[derive(Default, Deserialize, Debug)] | ||
struct ExportCandidAttr {} | ||
|
||
impl Parse for ExportCandidAttr { | ||
fn parse(input: ParseStream) -> syn::Result<Self> { | ||
if input.is_empty() { | ||
Ok(Self {}) | ||
} else { | ||
Err(syn::Error::new( | ||
input.span(), | ||
"unexpected attribute argument", | ||
)) | ||
} | ||
} | ||
} | ||
|
||
pub(crate) fn export_candid(attr: TokenStream, input: TokenStream) -> TokenStream { | ||
let _ = parse_macro_input!(attr as ExportCandidAttr); | ||
|
||
let input_fn = parse_macro_input!(input as ItemFn); | ||
let input_fn_name = input_fn.sig.ident.clone(); | ||
|
||
match input_fn.sig.output { | ||
ReturnType::Default => { | ||
return syn::Error::new( | ||
input_fn.sig.span(), | ||
"`#[export_candid]` function must return `String`", | ||
) | ||
.to_compile_error() | ||
.into(); | ||
} | ||
ReturnType::Type(_, ref ty) => { | ||
if ty.to_token_stream().to_string() != "String" { | ||
return syn::Error::new( | ||
ty.span(), | ||
"`#[export_candid]` function must return `String`", | ||
) | ||
.to_compile_error() | ||
.into(); | ||
} | ||
} | ||
}; | ||
|
||
let result = quote! { | ||
#input_fn | ||
|
||
#[no_mangle] | ||
pub fn get_candid_pointer() -> *mut std::os::raw::c_char { | ||
let candid_string: String = #input_fn_name(); | ||
let c_string = std::ffi::CString::new(candid_string).unwrap(); | ||
c_string.into_raw() | ||
} | ||
}; | ||
|
||
result.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
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,11 @@ | ||
#[test] | ||
fn not_expended() { | ||
let tests = trybuild::TestCases::new(); | ||
tests.compile_fail("tests/export_candid/not_expended/*.rs"); | ||
} | ||
|
||
#[test] | ||
fn expended() { | ||
let tests = trybuild::TestCases::new(); | ||
tests.pass("tests/export_candid/expended/*.rs"); | ||
} |
6 changes: 6 additions & 0 deletions
6
ic-canister/ic-canister-macros/tests/export_candid/expended/empty_attr_arguments.rs
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,6 @@ | ||
fn main() {} | ||
|
||
#[ic_canister_macros::export_candid()] | ||
fn did() -> String { | ||
panic!() | ||
} |
6 changes: 6 additions & 0 deletions
6
ic-canister/ic-canister-macros/tests/export_candid/expended/valid_return_type.rs
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,6 @@ | ||
fn main() {} | ||
|
||
#[ic_canister_macros::export_candid] | ||
fn did() -> String { | ||
panic!() | ||
} |
6 changes: 6 additions & 0 deletions
6
ic-canister/ic-canister-macros/tests/export_candid/not_expended/invalid_attr_arguments.rs
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,6 @@ | ||
fn main() {} | ||
|
||
#[ic_canister_macros::export_candid(attribute)] | ||
fn did() -> String { | ||
panic!() | ||
} |
5 changes: 5 additions & 0 deletions
5
...anister/ic-canister-macros/tests/export_candid/not_expended/invalid_attr_arguments.stderr
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,5 @@ | ||
error: unexpected attribute argument | ||
--> tests/export_candid/not_expended/invalid_attr_arguments.rs:3:37 | ||
| | ||
3 | #[ic_canister_macros::export_candid(attribute)] | ||
| ^^^^^^^^^ |
4 changes: 4 additions & 0 deletions
4
ic-canister/ic-canister-macros/tests/export_candid/not_expended/invalid_item_type.rs
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,4 @@ | ||
fn main() {} | ||
|
||
#[ic_canister_macros::export_candid] | ||
struct Did {} |
5 changes: 5 additions & 0 deletions
5
ic-canister/ic-canister-macros/tests/export_candid/not_expended/invalid_item_type.stderr
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,5 @@ | ||
error: expected `fn` | ||
--> tests/export_candid/not_expended/invalid_item_type.rs:4:1 | ||
| | ||
4 | struct Did {} | ||
| ^^^^^^ |
4 changes: 4 additions & 0 deletions
4
ic-canister/ic-canister-macros/tests/export_candid/not_expended/invalid_return_type.rs
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,4 @@ | ||
fn main() {} | ||
|
||
#[ic_canister_macros::export_candid] | ||
fn did() -> () {} |
5 changes: 5 additions & 0 deletions
5
ic-canister/ic-canister-macros/tests/export_candid/not_expended/invalid_return_type.stderr
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,5 @@ | ||
error: `#[export_candid]` function must return `String` | ||
--> tests/export_candid/not_expended/invalid_return_type.rs:4:13 | ||
| | ||
4 | fn did() -> () {} | ||
| ^^ |
4 changes: 4 additions & 0 deletions
4
ic-canister/ic-canister-macros/tests/export_candid/not_expended/no_return_type.rs
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,4 @@ | ||
fn main() {} | ||
|
||
#[ic_canister_macros::export_candid] | ||
fn did() {} |
5 changes: 5 additions & 0 deletions
5
ic-canister/ic-canister-macros/tests/export_candid/not_expended/no_return_type.stderr
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,5 @@ | ||
error: `#[export_candid]` function must return `String` | ||
--> tests/export_candid/not_expended/no_return_type.rs:4:1 | ||
| | ||
4 | fn did() {} | ||
| ^^ |
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