Skip to content

Commit

Permalink
feat: add hidden attribute to query/update calls, to hide from the …
Browse files Browse the repository at this point in the history
…candidfile (#20)
  • Loading branch information
ozwaldorf authored Sep 1, 2022
2 parents 842eb63 + 1fef981 commit 27cfbe7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 33 deletions.
63 changes: 38 additions & 25 deletions ic-kit-macros/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl EntryPoint {
struct Config {
name: Option<String>,
guard: Option<String>,
hidden: Option<bool>,
}

/// Process a rust syntax and generate the code for processing it.
Expand Down Expand Up @@ -94,20 +95,6 @@ pub fn gen_entry_point_code(
));
}

if entry_point.is_lifecycle() && !entry_point.is_inspect_message() && return_length > 0 {
return Err(Error::new(
signature.output.span(),
format!("#[{}] function cannot have a return value.", entry_point),
));
}

if entry_point.is_lifecycle() && attrs.name.is_some() {
return Err(Error::new(
Span::call_site(),
format!("#[{}] function cannot be renamed.", entry_point),
));
}

if entry_point.is_inspect_message() && return_length != 1 {
return Err(Error::new(
Span::call_site(),
Expand All @@ -118,18 +105,42 @@ pub fn gen_entry_point_code(
));
}

if entry_point.is_lifecycle() && attrs.guard.is_some() {
return Err(Error::new(
Span::call_site(),
format!("#[{}] function cannot have a guard", entry_point),
));
}
// Lifecycle functions have some restrictions
if entry_point.is_lifecycle() {
if !entry_point.is_inspect_message() && return_length > 0 {
return Err(Error::new(
signature.output.span(),
format!("#[{}] function cannot have a return value.", entry_point),
));
}

if entry_point.is_lifecycle() && is_async {
return Err(Error::new(
Span::call_site(),
format!("#[{}] function cannot be async.", entry_point),
));
if attrs.name.is_some() {
return Err(Error::new(
Span::call_site(),
format!("#[{}] function cannot be renamed.", entry_point),
));
}

if attrs.hidden.is_some() {
return Err(Error::new(
Span::call_site(),
format!("#[{}] function cannot be hidden.", entry_point),
));
}

if attrs.guard.is_some() {
return Err(Error::new(
Span::call_site(),
format!("#[{}] function cannot have a guard", entry_point),
));
}

if is_async {
return Err(Error::new(
Span::call_site(),
format!("#[{}] function cannot be async.", entry_point),
));
}
}

let outer_function_ident = Ident::new(
Expand Down Expand Up @@ -262,10 +273,12 @@ pub fn gen_entry_point_code(
}
};

// only declare candid if hide is false
declare(
entry_point,
name.clone(),
candid_name,
attrs.hidden.unwrap_or(false),
can_args,
can_types,
&signature.output,
Expand Down
24 changes: 16 additions & 8 deletions ic-kit-macros/src/export_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::sync::Mutex;
use syn::{DeriveInput, Error};

struct Method {
hidden: bool,
mode: EntryPoint,
rust_name: String,
_arg_names: Vec<String>,
Expand All @@ -23,6 +24,7 @@ pub(crate) fn declare(
entry_point: EntryPoint,
rust_name: Ident,
name: String,
hidden: bool,
can_args: Vec<Ident>,
can_types: Vec<syn::Type>,
rt: &syn::ReturnType,
Expand All @@ -41,6 +43,7 @@ pub(crate) fn declare(
};

let method = Method {
hidden,
mode: entry_point,
rust_name: rust_name.to_string(),
_arg_names: can_args.iter().map(|i| i.to_string()).collect(),
Expand Down Expand Up @@ -113,6 +116,7 @@ pub fn export_service(input: DeriveInput, save_candid_path: Option<syn::LitStr>)
arg_types,
rets,
mode,
hidden,
..
},
)| {
Expand All @@ -134,15 +138,19 @@ pub fn export_service(input: DeriveInput, save_candid_path: Option<syn::LitStr>)
_ => unreachable!(),
};

quote! {
{
let mut args = Vec::new();
#(#args)*
let mut rets = Vec::new();
#(#rets)*
let func = Function { args, rets, modes: #modes };
service.push((#name.to_string(), Type::Func(func)));
if !hidden {
quote! {
{
let mut args = Vec::new();
#(#args)*
let mut rets = Vec::new();
#(#rets)*
let func = Function { args, rets, modes: #modes };
service.push((#name.to_string(), Type::Func(func)));
}
}
} else {
quote! {}
}
},
);
Expand Down

0 comments on commit 27cfbe7

Please sign in to comment.