mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2025-12-06 11:46:42 +01:00
fix: command: commas
This commit is contained in:
parent
046be9a9e1
commit
e3bfae8897
1 changed files with 57 additions and 25 deletions
|
|
@ -57,7 +57,7 @@ impl ToTokens for CommandDef {
|
||||||
fn to_tokens (&self, out: &mut TokenStream2) {
|
fn to_tokens (&self, out: &mut TokenStream2) {
|
||||||
let Self(CommandMeta(target), CommandImpl(block, exposed)) = self;
|
let Self(CommandMeta(target), CommandImpl(block, exposed)) = self;
|
||||||
let enumeration = &block.self_ty;
|
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 matchers = exposed.values().map(CommandArm::to_matcher);
|
||||||
let implementations = exposed.values().map(CommandArm::to_implementation);
|
let implementations = exposed.values().map(CommandArm::to_implementation);
|
||||||
write_quote_to(out, quote! {
|
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});
|
//panic!("\n{}", quote! {#out});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl CommandArm {
|
impl CommandArm {
|
||||||
fn to_key (&self) -> Arc<str> {
|
fn to_key (&self) -> Arc<str> {
|
||||||
|
|
@ -105,7 +109,7 @@ impl CommandArm {
|
||||||
fn ident_to_enum_variant (ident: &Ident) -> Arc<str> {
|
fn ident_to_enum_variant (ident: &Ident) -> Arc<str> {
|
||||||
format!("{}", AsUpperCamelCase(format!("{ident}"))).into()
|
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();
|
let mut out = TokenStream2::new();
|
||||||
out.append(self.to_enum_variant_ident());
|
out.append(self.to_enum_variant_ident());
|
||||||
let ident = &self.0;
|
let ident = &self.0;
|
||||||
|
|
@ -114,22 +118,7 @@ impl CommandArm {
|
||||||
let mut out = TokenStream2::new();
|
let mut out = TokenStream2::new();
|
||||||
for arg in self.1.iter().skip(2) {
|
for arg in self.1.iter().skip(2) {
|
||||||
if let FnArg::Typed(PatType { attrs, pat, colon_token, ty }) = arg {
|
if let FnArg::Typed(PatType { attrs, pat, colon_token, ty }) = arg {
|
||||||
write_quote_to(&mut out, quote! { #pat });
|
write_quote_to(&mut out, quote! { #pat : #ty , });
|
||||||
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) });
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
unreachable!("only typed args should be present at this position")
|
unreachable!("only typed args should be present at this position")
|
||||||
}
|
}
|
||||||
|
|
@ -137,8 +126,51 @@ impl CommandArm {
|
||||||
out
|
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
|
out
|
||||||
}
|
}
|
||||||
|
|
@ -161,7 +193,7 @@ impl CommandArm {
|
||||||
}
|
}
|
||||||
}).collect::<Vec<_>>();
|
}).collect::<Vec<_>>();
|
||||||
let variant = Self::ident_to_enum_variant(&self.0);
|
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! {
|
write_quote(quote! {
|
||||||
Some(::tengri::dsl::Token { value: ::tengri::dsl::Value::Key(#key), .. }) => {
|
Some(::tengri::dsl::Token { value: ::tengri::dsl::Value::Key(#key), .. }) => {
|
||||||
let mut iter = iter.clone();
|
let mut iter = iter.clone();
|
||||||
|
|
@ -173,8 +205,8 @@ impl CommandArm {
|
||||||
}
|
}
|
||||||
fn to_implementation (&self) -> TokenStream2 {
|
fn to_implementation (&self) -> TokenStream2 {
|
||||||
let ident = &self.0;
|
let ident = &self.0;
|
||||||
let variant = self.to_enum_variant(false, false, false);
|
let variant = self.to_enum_variant_unbind();
|
||||||
let mut give_rest = write_quote(quote! { });
|
let mut give_rest = write_quote(quote! { /*TODO*/ });
|
||||||
let give_args = self.1.iter().skip(2).map(|arg|{
|
let give_args = self.1.iter().skip(2).map(|arg|{
|
||||||
if let FnArg::Typed(PatType { attrs, pat, colon_token, ty }) = arg {
|
if let FnArg::Typed(PatType { attrs, pat, colon_token, ty }) = arg {
|
||||||
//let give_err = LitStr::new(&format!("{}: missing value \"{}\" ({})",
|
//let give_err = LitStr::new(&format!("{}: missing value \"{}\" ({})",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue