47 lines
1.4 KiB
Rust
47 lines
1.4 KiB
Rust
use orchid_extension::gen_expr::new_atom;
|
|
use orchid_extension::tree::{GenMember, fun, prefix};
|
|
use orchid_extension::{Expr, TAtom, exec};
|
|
|
|
use crate::macros::match_macros::{MatcherAtom, match_one};
|
|
use crate::macros::utils::{build_macro, mactree, mactreev};
|
|
use crate::{OrcOpt, Tpl};
|
|
|
|
pub async fn gen_option_macro_lib() -> Vec<GenMember> {
|
|
prefix("std::option", [
|
|
fun(false, "is_some_body", |sub: Expr, val: OrcOpt<Expr>| {
|
|
exec(async move |mut h| {
|
|
let Some(sub_val) = val.0 else { return Ok(OrcOpt(None)) };
|
|
match_one(&mut h, sub, sub_val).await
|
|
})
|
|
}),
|
|
fun(
|
|
false,
|
|
"is_none_body",
|
|
async |val: OrcOpt<Expr>| {
|
|
if val.0.is_none() { OrcOpt(Some(Tpl(()))) } else { OrcOpt(None) }
|
|
},
|
|
),
|
|
build_macro(None, ["some", "none"])
|
|
.rule(
|
|
mactreev!("pattern::match_rule" ( "std::option::some" "...$" sub_pattern 0)),
|
|
async |mut cx, [sub]| {
|
|
let sub: TAtom<MatcherAtom> =
|
|
cx.recur_call(mactree!("pattern::match_rule" "push" sub;)).await?;
|
|
let sub = sub.own().await;
|
|
let matcher = new_atom(MatcherAtom {
|
|
keys: sub.keys,
|
|
matcher: mactree!("std::option::is_some_body" "push" sub.matcher),
|
|
});
|
|
Ok(mactree!("Val" matcher))
|
|
},
|
|
)
|
|
.rule(mactreev!("pattern::match_rule"("std::option::none")), async |_cx, []| {
|
|
Ok(mactree!("Val" new_atom(MatcherAtom {
|
|
keys: vec![],
|
|
matcher: mactree!("std::option::is_none_body"),
|
|
})))
|
|
})
|
|
.finish(),
|
|
])
|
|
}
|