From e3bfae889792e70fa88a02fcad90e531f41059ec Mon Sep 17 00:00:00 2001 From: unspeaker Date: Thu, 8 May 2025 17:25:45 +0300 Subject: [PATCH] fix: command: commas --- proc/src/proc_command.rs | 82 ++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 25 deletions(-) diff --git a/proc/src/proc_command.rs b/proc/src/proc_command.rs index 2573d0f..3be238e 100644 --- a/proc/src/proc_command.rs +++ b/proc/src/proc_command.rs @@ -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,8 +86,12 @@ impl ToTokens for CommandDef { } } }); - if exposed.len() > 0 { - //panic!("\n{}", quote! {#out}); + //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}); + } } } } @@ -105,7 +109,7 @@ impl CommandArm { fn ident_to_enum_variant (ident: &Ident) -> Arc { 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.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::>(); 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 \"{}\" ({})",