mirror of https://github.com/vacp2p/zerokit.git
Macro to call functions with an error message (#140)
abstract out calls --------- Co-authored-by: tyshkor <tyshko1@gmail.com>
This commit is contained in:
parent
1f983bb232
commit
6ff4eeb237
|
@ -8,6 +8,7 @@ rln/pmtree_db
|
||||||
# will have compiled files and executables
|
# will have compiled files and executables
|
||||||
debug/
|
debug/
|
||||||
target/
|
target/
|
||||||
|
wabt/
|
||||||
|
|
||||||
# These are backup files generated by rustfmt
|
# These are backup files generated by rustfmt
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
|
|
|
@ -20,6 +20,64 @@ pub struct RLNWrapper {
|
||||||
instance: RLN<'static>,
|
instance: RLN<'static>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Macro to call_with_error_msg methods with arbitrary amount of arguments,
|
||||||
|
// First argument to the macro is context,
|
||||||
|
// second is the actual method on `RLNWrapper`
|
||||||
|
// rest are all other arguments to the method
|
||||||
|
macro_rules! call_with_error_msg {
|
||||||
|
($instance:expr, $method:ident, $error_msg:expr $(, $arg:expr)*) => {
|
||||||
|
{
|
||||||
|
let new_instance: &mut RLNWrapper = $instance.process();
|
||||||
|
if let Err(err) = new_instance.instance.$method($($arg.process()),*) {
|
||||||
|
Err(format!("Msg: {:#?}, Error: {:#?}", $error_msg, err))
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
trait ProcessArg {
|
||||||
|
type ReturnType;
|
||||||
|
fn process(self) -> Self::ReturnType;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ProcessArg for usize {
|
||||||
|
type ReturnType = usize;
|
||||||
|
fn process(self) -> Self::ReturnType {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> ProcessArg for *const RLN<'a> {
|
||||||
|
type ReturnType = &'a RLN<'a>;
|
||||||
|
fn process(self) -> Self::ReturnType {
|
||||||
|
unsafe { &*self }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
trait ProcessArgRef {
|
||||||
|
type ReturnType;
|
||||||
|
fn process(self) -> Self::ReturnType;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ProcessArgRef for *mut RLNWrapper {
|
||||||
|
type ReturnType = &'static mut RLNWrapper;
|
||||||
|
|
||||||
|
fn process(self) -> Self::ReturnType {
|
||||||
|
unsafe { &mut *self }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> ProcessArgRef for &'a [u8] {
|
||||||
|
type ReturnType = &'a [u8];
|
||||||
|
|
||||||
|
fn process(self) -> Self::ReturnType {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||||
#[wasm_bindgen(js_name = newRLN)]
|
#[wasm_bindgen(js_name = newRLN)]
|
||||||
pub fn wasm_new(
|
pub fn wasm_new(
|
||||||
|
@ -51,12 +109,12 @@ pub fn wasm_get_serialized_rln_witness(
|
||||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||||
#[wasm_bindgen(js_name = insertMember)]
|
#[wasm_bindgen(js_name = insertMember)]
|
||||||
pub fn wasm_set_next_leaf(ctx: *mut RLNWrapper, input: Uint8Array) -> Result<(), String> {
|
pub fn wasm_set_next_leaf(ctx: *mut RLNWrapper, input: Uint8Array) -> Result<(), String> {
|
||||||
let wrapper = unsafe { &mut *ctx };
|
call_with_error_msg!(
|
||||||
if wrapper.instance.set_next_leaf(&input.to_vec()[..]).is_ok() {
|
ctx,
|
||||||
Ok(())
|
set_next_leaf,
|
||||||
} else {
|
"could not insert member into merkle tree".to_string(),
|
||||||
Err("could not insert member into merkle tree".into())
|
&input.to_vec()[..]
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||||
|
@ -66,31 +124,24 @@ pub fn wasm_set_leaves_from(
|
||||||
index: usize,
|
index: usize,
|
||||||
input: Uint8Array,
|
input: Uint8Array,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
let wrapper = unsafe { &mut *ctx };
|
call_with_error_msg!(
|
||||||
if wrapper
|
ctx,
|
||||||
.instance
|
set_leaves_from,
|
||||||
.set_leaves_from(index as usize, &input.to_vec()[..])
|
"could not set multiple leaves".to_string(),
|
||||||
.is_ok()
|
index,
|
||||||
{
|
&*input.to_vec()
|
||||||
Ok(())
|
)
|
||||||
} else {
|
|
||||||
Err("could not set multiple leaves".into())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||||
#[wasm_bindgen(js_name = initTreeWithLeaves)]
|
#[wasm_bindgen(js_name = initTreeWithLeaves)]
|
||||||
pub fn wasm_init_tree_with_leaves(ctx: *mut RLNWrapper, input: Uint8Array) -> Result<(), String> {
|
pub fn wasm_init_tree_with_leaves(ctx: *mut RLNWrapper, input: Uint8Array) -> Result<(), String> {
|
||||||
let wrapper = unsafe { &mut *ctx };
|
call_with_error_msg!(
|
||||||
if wrapper
|
ctx,
|
||||||
.instance
|
init_tree_with_leaves,
|
||||||
.init_tree_with_leaves(&input.to_vec()[..])
|
"could not init merkle tree".to_string(),
|
||||||
.is_ok()
|
&*input.to_vec()
|
||||||
{
|
)
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err("could not init merkle tree".into())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||||
|
|
Loading…
Reference in New Issue