mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
proc: working command, expose
This commit is contained in:
parent
751e01a41e
commit
046be9a9e1
2 changed files with 24 additions and 13 deletions
|
|
@ -70,23 +70,31 @@ impl ToTokens for ExposeImpl {
|
|||
out.append(token);
|
||||
}
|
||||
for (t, variants) in exposed.iter() {
|
||||
let predefined = match format!("{}", quote! { #t }).as_str() {
|
||||
"bool" => vec![
|
||||
quote! { ::tengri::dsl::Value::Sym(":true") => true },
|
||||
quote! { ::tengri::dsl::Value::Sym(":false") => false },
|
||||
],
|
||||
let formatted_type = format!("{}", quote! { #t });
|
||||
let predefined = match formatted_type.as_str() {
|
||||
"bool" => quote! {
|
||||
::tengri::dsl::Value::Sym(":true") => true,
|
||||
::tengri::dsl::Value::Sym(":false") => false,
|
||||
},
|
||||
"u8" | "u16" | "u32" | "u64" | "usize" |
|
||||
"i8" | "i16" | "i32" | "i64" | "isize" => vec![
|
||||
quote! { ::tengri::dsl::Value::Num(n) => *n },
|
||||
],
|
||||
_ => vec![],
|
||||
"i8" | "i16" | "i32" | "i64" | "isize" => {
|
||||
let num_err = LitStr::new(
|
||||
&format!("{{n}}: failed to convert to {formatted_type}"),
|
||||
Span::call_site()
|
||||
);
|
||||
quote! {
|
||||
::tengri::dsl::Value::Num(n) => TryInto::<#t>::try_into(*n)
|
||||
.unwrap_or_else(|_|panic!(#num_err)),
|
||||
}
|
||||
},
|
||||
_ => quote! {},
|
||||
};
|
||||
let values = variants.iter().map(|(k, v)|ExposeArm(k.clone(), v.clone()));
|
||||
let trait_impl = quote! {
|
||||
impl ::tengri::dsl::Context<#t> for #target {
|
||||
fn get (&self, dsl: &::tengri::dsl::Value) -> Option<#t> {
|
||||
Some(match dsl {
|
||||
#(#predefined,)*
|
||||
#predefined
|
||||
#(#values,)*
|
||||
_ => return None
|
||||
})
|
||||
|
|
@ -97,9 +105,9 @@ impl ToTokens for ExposeImpl {
|
|||
out.append(token);
|
||||
}
|
||||
}
|
||||
//if exposed.len() > 0 {
|
||||
if exposed.len() > 0 {
|
||||
//panic!("{}", quote! {#out});
|
||||
//}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue