fix: command: commas

This commit is contained in:
🪞👃🪞 2025-05-08 17:25:45 +03:00
parent 046be9a9e1
commit e3bfae8897

View file

@ -57,7 +57,7 @@ impl ToTokens for CommandDef {
fn to_tokens (&self, out: &mut TokenStream2) {
let Self(CommandMeta(target), CommandImpl(block, exposed)) = self;
let enumeration = &block.self_ty;
let variants = exposed.values().map(|x|CommandArm::to_enum_variant(x, true, true, false));
let variants = exposed.values().map(|x|x.to_enum_variant_def());
let matchers = exposed.values().map(CommandArm::to_matcher);
let implementations = exposed.values().map(CommandArm::to_implementation);
write_quote_to(out, quote! {
@ -86,11 +86,15 @@ impl ToTokens for CommandDef {
}
}
});
if exposed.len() > 0 {
//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> {
@ -105,7 +109,7 @@ impl CommandArm {
fn ident_to_enum_variant (ident: &Ident) -> Arc<str> {
format!("{}", AsUpperCamelCase(format!("{ident}"))).into()
}
fn to_enum_variant (&self, with_types: bool, trailing_comma: bool, with_values: bool) -> TokenStream2 {
fn to_enum_variant_def (&self) -> TokenStream2 {
let mut out = TokenStream2::new();
out.append(self.to_enum_variant_ident());
let ident = &self.0;
@ -114,22 +118,7 @@ impl CommandArm {
let mut out = TokenStream2::new();
for arg in self.1.iter().skip(2) {
if let FnArg::Typed(PatType { attrs, pat, colon_token, ty }) = arg {
write_quote_to(&mut out, quote! { #pat });
if with_types && with_values {
unreachable!();
}
if with_types {
write_quote_to(&mut out, quote! { : #ty });
}
if with_values {
let take_err = LitStr::new(&format!("{}: missing argument \"{}\" ({})",
quote!{#ident}, quote!{#pat}, quote!{#ty}), Span::call_site());
let give_err = LitStr::new(&format!("{}: missing value \"{}\" ({})",
quote!{#ident}, quote!{#pat}, quote!{#ty}), Span::call_site());
write_quote_to(&mut out, quote! {
: Context::get(state, &iter.next().expect(#take_err).value)
.expect(#give_err) });
}
write_quote_to(&mut out, quote! { #pat : #ty , });
} else {
unreachable!("only typed args should be present at this position")
}
@ -137,8 +126,51 @@ impl CommandArm {
out
}));
}
if trailing_comma {
out.append(Punct::new(',', Alone));
out
}
fn to_enum_variant_bind (&self) -> TokenStream2 {
let mut out = TokenStream2::new();
out.append(self.to_enum_variant_ident());
let ident = &self.0;
if self.1.len() > 2 {
out.append(Group::new(Delimiter::Brace, {
let mut out = TokenStream2::new();
for arg in self.1.iter().skip(2) {
if let FnArg::Typed(PatType { attrs, pat, colon_token, ty }) = arg {
let take_err = LitStr::new(&format!("{}: missing argument \"{}\" ({})",
quote!{#ident}, quote!{#pat}, quote!{#ty}), Span::call_site());
let give_err = LitStr::new(&format!("{}: missing value \"{}\" ({})",
quote!{#ident}, quote!{#pat}, quote!{#ty}), Span::call_site());
write_quote_to(&mut out, quote! {
#pat : Context::get(state, &iter.next().expect(#take_err).value)
.expect(#give_err) ,
});
} else {
unreachable!("only typed args should be present at this position")
}
}
out
}));
}
out
}
fn to_enum_variant_unbind (&self) -> TokenStream2 {
let mut out = TokenStream2::new();
out.append(self.to_enum_variant_ident());
let ident = &self.0;
if self.1.len() > 2 {
out.append(Group::new(Delimiter::Brace, {
let mut out = TokenStream2::new();
for arg in self.1.iter().skip(2) {
if let FnArg::Typed(PatType { attrs, pat, colon_token, ty }) = arg {
write_quote_to(&mut out, quote! { #pat , });
} else {
unreachable!("only typed args should be present at this position")
}
}
out
}));
}
out
}
@ -161,7 +193,7 @@ impl CommandArm {
}
}).collect::<Vec<_>>();
let variant = Self::ident_to_enum_variant(&self.0);
let variant = self.to_enum_variant(false, false, true);
let variant = self.to_enum_variant_bind();
write_quote(quote! {
Some(::tengri::dsl::Token { value: ::tengri::dsl::Value::Key(#key), .. }) => {
let mut iter = iter.clone();
@ -173,8 +205,8 @@ impl CommandArm {
}
fn to_implementation (&self) -> TokenStream2 {
let ident = &self.0;
let variant = self.to_enum_variant(false, false, false);
let mut give_rest = write_quote(quote! { });
let variant = self.to_enum_variant_unbind();
let mut give_rest = write_quote(quote! { /*TODO*/ });
let give_args = self.1.iter().skip(2).map(|arg|{
if let FnArg::Typed(PatType { attrs, pat, colon_token, ty }) = arg {
//let give_err = LitStr::new(&format!("{}: missing value \"{}\" ({})",