tengri/proc/src/proc_command.rs
unspeaker cbd28a5934
Some checks are pending
/ build (push) Waiting to run
dsl: auto-impl the obvious one (hopefully)
re foreign trait constraints
2025-05-23 21:52:08 +03:00

215 lines
7.7 KiB
Rust

use crate::*;
#[derive(Debug, Clone)]
pub(crate) struct CommandDef(pub(crate) CommandMeta, pub(crate) CommandImpl);
#[derive(Debug, Clone)]
pub(crate) struct CommandMeta(TypePath);
#[derive(Debug, Clone)]
pub(crate) struct CommandImpl(ItemImpl, BTreeMap<Arc<str>, CommandArm>);
#[derive(Debug, Clone)]
struct CommandArm(Ident, Vec<FnArg>, #[allow(unused)] ReturnType);
impl Parse for CommandMeta {
fn parse (input: ParseStream) -> Result<Self> {
Ok(Self(input.parse()?))
}
}
impl Parse for CommandImpl {
fn parse (input: ParseStream) -> Result<Self> {
let block = input.parse::<ItemImpl>()?;
let exposed = Self::collect(&block.items).map_err(|e|input.error(e))?;
Ok(Self(block, exposed))
}
}
impl CommandImpl {
fn collect (items: &Vec<ImplItem>)
-> std::result::Result<BTreeMap<Arc<str>, CommandArm>, String>
{
let mut exposed: BTreeMap<Arc<str>, CommandArm> = Default::default();
for item in items.iter() {
if let ImplItem::Fn(
ImplItemFn { sig: Signature { ident, inputs, output, .. }, .. }
) = item {
let key = CommandArm::ident_to_key(&ident);
if exposed.contains_key(&key) {
return Err(format!("already defined: {ident}"));
}
exposed.insert(key, CommandArm(
ident.clone(),
inputs.iter().map(|x|x.clone()).collect(),
output.clone(),
));
}
}
Ok(exposed)
}
}
impl ToTokens for CommandDef {
fn to_tokens (&self, out: &mut TokenStream2) {
let Self(CommandMeta(state), CommandImpl(block, exposed)) = self;
let command_enum = &block.self_ty;
let variants = exposed.values().map(|arm|{
let mut out = TokenStream2::new();
out.append(arm.to_enum_variant_ident());
//let ident = &arm.0;
if arm.has_args() {
out.append(Group::new(Delimiter::Brace, {
let mut out = TokenStream2::new();
for (arg, ty) in arm.args() {
write_quote_to(&mut out, quote! { #arg : #ty , });
}
out
}));
}
out.append(Punct::new(',', Alone));
out
});
let matchers = exposed.values().map(|arm|{
let key = LitStr::new(&arm.to_key(), Span::call_site());
let variant = {
let mut out = TokenStream2::new();
out.append(arm.to_enum_variant_ident());
let ident = &arm.0;
if arm.has_args() {
out.append(Group::new(Delimiter::Brace, {
let mut out = TokenStream2::new();
for (arg, ty) in arm.args() {
write_quote_to(&mut out, quote! {
#arg: Take::take_or_fail(self, words)?,
});
}
out
}));
}
out
};
write_quote(quote! {
Some(::tengri::dsl::Token { value: ::tengri::dsl::Value::Key(#key), .. }) => {
let mut words = words.clone();
Some(#command_enum::#variant)
},
//Some(::tengri::dsl::Token {
//value: ::tengri::dsl::Value::Key(#key), ..
//}) => {
//let mut iter = iter.clone(); Some(#command_enum::#variant)
//},
})
});
let implementations = exposed.values().map(|arm|{
let ident = &arm.0;
let variant = {
let mut out = TokenStream2::new();
out.append(arm.to_enum_variant_ident());
//let ident = &arm.0;
if arm.has_args() {
out.append(Group::new(Delimiter::Brace, {
let mut out = TokenStream2::new();
for (arg, _ty) in arm.args() {
write_quote_to(&mut out, quote! { #arg , });
}
out
}));
}
out
};
let give_rest = write_quote(quote! { /*TODO*/ });
let give_args = arm.args()
.map(|(arg, _ty)|write_quote(quote! { #arg, }))
.collect::<Vec<_>>();
write_quote(quote! {
#command_enum::#variant =>
#command_enum::#ident(state, #(#give_args)* #give_rest),
})
});
write_quote_to(out, quote! {
/// Generated by [tengri_proc].
#[derive(Clone, Debug)] pub enum #command_enum { #(#variants)* }
/// Not generated by [tengri_proc].
#block
/// Generated by [tengri_proc::command].
///
/// Means [#command_enum] is now a [Command] over [#state].
/// Instances of [#command_enum] can be consumed by a
/// mutable pointer to [#state], invoking predefined operations
/// and optionally returning undo history data.
impl ::tengri::input::Command<#state> for #command_enum {
fn execute (self, state: &mut #state) -> Perhaps<Self> {
match self { #(#implementations)* }
}
}
/// Generated by [tengri_proc::command].
impl<'n> ::tengri::dsl::Take<'n, #state> for #command_enum {
fn take <'source> (
state: &#state,
words: &mut ::tengri::dsl::TokenIter<'source>
) -> Perhaps<Self> {
let mut words = words.clone();
let token = words.next();
todo!()//Ok(match token { #(#matchers)* _ => None })
}
}
});
//if exposed.len() > 0 {
//panic!("{:#?}", block.self_ty);
//if let Type::Path(ref path) = *block.self_ty {
//if path.path.segments.get(0).unwrap().ident == "TekCommand" {
//panic!("\n{}", quote! {#out});
//}
//}
}
}
impl CommandArm {
fn to_key (&self) -> Arc<str> {
Self::ident_to_key(&self.0)
}
fn to_enum_variant_ident (&self) -> Ident {
Ident::new(&Self::ident_to_enum_variant(&self.0), Span::call_site())
}
fn ident_to_key (ident: &Ident) -> Arc<str> {
format!("{}", AsKebabCase(format!("{ident}"))).into()
}
fn ident_to_enum_variant (ident: &Ident) -> Arc<str> {
format!("{}", AsUpperCamelCase(format!("{ident}"))).into()
}
fn has_args (&self) -> bool {
self.1.len() > 1
}
fn args (&self) -> impl Iterator<Item = (&Ident, &Box<Type>)> {
self.1.iter().skip(1).filter_map(|arg|if let FnArg::Typed(PatType {
ty, pat: box Pat::Ident(PatIdent { ident: arg, .. }), ..
}) = arg {
Some((arg, ty))
} else {
unreachable!("only typed args should be present at this position");
})
}
fn to_enum_variant_def (&self) -> TokenStream2 {
let mut out = TokenStream2::new();
out.append(self.to_enum_variant_ident());
//let ident = &self.0;
if self.has_args() {
out.append(Group::new(Delimiter::Brace, {
let mut out = TokenStream2::new();
for (arg, ty) in self.args() {
write_quote_to(&mut out, quote! { #arg : #ty , });
}
out
}));
}
out.append(Punct::new(',', Alone));
out
}
}