Beautify cal_on_store_by_handle!{} macro syntax

This commit is contained in:
Matthias Beyer 2017-01-21 19:53:32 +01:00
parent 04363035c2
commit c2b146aad8
2 changed files with 65 additions and 47 deletions

View file

@ -90,40 +90,44 @@ macro_rules! call_on_fle_from_store {
($itself:ident ($wrapper:ident) -> $name:ident -> $operation:block) => {{ ($itself:ident ($wrapper:ident) -> $name:ident -> $operation:block) => {{
let handle = $itself.get_data(&*$wrapper); let handle = $itself.get_data(&*$wrapper);
let store_handle = handle.store_handle(); let store_handle = handle.store_handle();
call_on_store_by_handle!(store_handle -> store -> { call_on_store_by_handle! {
match store.get(handle.fle_handle().clone()) { store_handle named store inside {
Ok(Some(mut $name)) => { match store.get(handle.fle_handle().clone()) {
$operation Ok(Some(mut $name)) => {
}, $operation
Ok(None) => { },
VM::raise(Class::from_existing("RuntimeError"), "Obj does not exist"); Ok(None) => {
NilClass::new().to_any_object() VM::raise(Class::from_existing("RuntimeError"), "Obj does not exist");
}, NilClass::new().to_any_object()
Err(e) => { },
VM::raise(Class::from_existing("RuntimeError"), e.description()); Err(e) => {
NilClass::new().to_any_object() VM::raise(Class::from_existing("RuntimeError"), e.description());
}, NilClass::new().to_any_object()
},
}
} }
}) }
}}; }};
($itself:ident ($wrapper:ident) -> $name:ident -> $operation: block on fail return $ex:expr) => {{ ($itself:ident ($wrapper:ident) -> $name:ident -> $operation: block on fail return $ex:expr) => {{
let handle = $itself.get_data(&*$wrapper); let handle = $itself.get_data(&*$wrapper);
let store_handle = handle.store_handle(); let store_handle = handle.store_handle();
call_on_store_by_handle!(store_handle -> store -> { call_on_store_by_handle! {
match store.get(handle.fle_handle().clone()) { store_handle named store inside {
Ok(Some(mut $name)) => { match store.get(handle.fle_handle().clone()) {
$operation Ok(Some(mut $name)) => {
}, $operation
Ok(None) => { },
VM::raise(Class::from_existing("RuntimeError"), "Obj does not exist"); Ok(None) => {
$ex VM::raise(Class::from_existing("RuntimeError"), "Obj does not exist");
}, $ex
Err(e) => { },
VM::raise(Class::from_existing("RuntimeError"), e.description()); Err(e) => {
$ex VM::raise(Class::from_existing("RuntimeError"), e.description());
}, $ex
},
}
} }
}) }
}}; }};
} }

View file

@ -41,11 +41,20 @@ impl_unwrap!(RStore, StoreHandle, STORE_WRAPPER);
impl_verified_object!(RStore); impl_verified_object!(RStore);
macro_rules! call_on_store_by_handle { macro_rules! call_on_store_by_handle {
($store_handle:ident -> $name:ident -> $operation:block) => {{ {
call_on_store_by_handle!($store_handle -> $name -> $operation on fail return NilClass::new().to_any_object()) $store_handle:ident named $name:ident inside $operation:block
}=> {{
call_on_store_by_handle! {
$store_handle
named $name
inside $operation
on fail return NilClass::new().to_any_object()
}
}}; }};
($store_handle:ident -> $name:ident -> $operation:block on fail return $ex:expr) => {{ {
$store_handle:ident named $name:ident inside $operation:block on fail return $ex:expr
} => {{
use cache::RUBY_STORE_CACHE; use cache::RUBY_STORE_CACHE;
let arc = RUBY_STORE_CACHE.clone(); let arc = RUBY_STORE_CACHE.clone();
@ -92,20 +101,23 @@ macro_rules! call_on_store {
on fail return $fail_expr:expr on fail return $fail_expr:expr
} => { } => {
let handle = $itself.get_data(&*$wrapper); let handle = $itself.get_data(&*$wrapper);
call_on_store_by_handle!(handle -> $store_name -> { call_on_store_by_handle! {
let $fle_name = match $store_name.get($fle_handle_name) { handle named $store_name inside {
Ok(Some(fle)) => fle, let $fle_name = match $store_name.get($fle_handle_name) {
Ok(None) => { Ok(Some(fle)) => fle,
VM::raise(Class::from_existing("RuntimeError"), "Obj does not exist"); Ok(None) => {
return $fail_expr VM::raise(Class::from_existing("RuntimeError"), "Obj does not exist");
}, return $fail_expr
Err(e) => { },
VM::raise(Class::from_existing("RuntimeError"), e.description()); Err(e) => {
return $fail_expr VM::raise(Class::from_existing("RuntimeError"), e.description());
}, return $fail_expr
}; },
$operation };
} on fail return $fail_expr) $operation
}
on fail return $fail_expr
}
}; };
{ {
@ -114,7 +126,9 @@ macro_rules! call_on_store {
on fail return $fail_expr:expr on fail return $fail_expr:expr
} => { } => {
let handle = $itself.get_data(&*$wrapper); let handle = $itself.get_data(&*$wrapper);
call_on_store_by_handle!(handle -> $store_name -> $operation on fail return $fail_expr) call_on_store_by_handle! {
handle named $store_name inside $operation on fail return $fail_expr
}
}; };
{ {
@ -122,7 +136,7 @@ macro_rules! call_on_store {
operation $block operation $block
} => { } => {
let handle = $itself.get_data(&*$wrapper); let handle = $itself.get_data(&*$wrapper);
call_on_store_by_handle!(handle -> $name -> $operation) call_on_store_by_handle! { handle named $name inside $operation }
}; };
} }