From 19a16d21eb5c0bcf08eb8f53613e75d8d4dec943 Mon Sep 17 00:00:00 2001 From: TheAwiteb Date: Tue, 29 Aug 2023 01:15:22 +0300 Subject: [PATCH 1/4] Make `AttrValue::span` instance function public --- crates/teloxide-macros/src/attr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/teloxide-macros/src/attr.rs b/crates/teloxide-macros/src/attr.rs index 9f872946..7768cad5 100644 --- a/crates/teloxide-macros/src/attr.rs +++ b/crates/teloxide-macros/src/attr.rs @@ -124,7 +124,7 @@ impl AttrValue { /// #[blahblah(key = "puff", value = 12, nope )] /// ^^^^^^ ^^ ^ /// ``` - fn span(&self) -> Span { + pub fn span(&self) -> Span { match self { Self::Path(p) => p.span(), Self::Lit(l) => l.span(), From 7881a1cb784a8c6b7d22ecc68e3c1409ec3cdd3d Mon Sep 17 00:00:00 2001 From: TheAwiteb Date: Tue, 29 Aug 2023 01:15:56 +0300 Subject: [PATCH 2/4] Fix https://github.com/teloxide/teloxide/issues/922 --- crates/teloxide-macros/src/command_attr.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/crates/teloxide-macros/src/command_attr.rs b/crates/teloxide-macros/src/command_attr.rs index 5c916a7b..203b92ea 100644 --- a/crates/teloxide-macros/src/command_attr.rs +++ b/crates/teloxide-macros/src/command_attr.rs @@ -1,5 +1,5 @@ use crate::{ - attr::{fold_attrs, Attr}, + attr::{fold_attrs, Attr, AttrValue}, error::compile_error_at, fields_parse::ParserType, rename_rules::RenameRule, @@ -104,7 +104,16 @@ impl CommandAttr { "rename" => Rename(value.expect_string()?), "parse_with" => ParseWith(ParserType::parse(value)?), "separator" => Separator(value.expect_string()?), - "hide" => Hide, + "hide" => { + if let AttrValue::None(_) = value { + Hide + } else { + return Err(compile_error_at( + "The hide attribute should not have a value, remove it ", + value.span(), + )); + } + } _ => { return Err(compile_error_at( "unexpected attribute name (expected one of `prefix`, `description`, \ From 547f5889d2f33fc51e60002d5c662730458f069e Mon Sep 17 00:00:00 2001 From: TheAwiteb Date: Thu, 7 Sep 2023 08:02:17 +0300 Subject: [PATCH 3/4] Add `expect_none` method function to `AttrValue` This method function will return a error if the option not a flag option --- crates/teloxide-macros/src/attr.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/crates/teloxide-macros/src/attr.rs b/crates/teloxide-macros/src/attr.rs index 7768cad5..a52aff01 100644 --- a/crates/teloxide-macros/src/attr.rs +++ b/crates/teloxide-macros/src/attr.rs @@ -87,6 +87,17 @@ impl AttrValue { }) } + /// Unwraps this value if it's a nothing. + pub fn expect_none(self, option_name: &str) -> Result<()> { + match self { + AttrValue::None(_) => Ok(()), + _ => Err(compile_error_at( + &format!("The {option_name} option should not have a value, remove it"), + self.span(), + )), + } + } + // /// Unwraps this value if it's a path. // pub fn expect_path(self) -> Result { // self.expect("a path", |this| match this { From 42fd9b869305cea3f8648d635409f850a77cb903 Mon Sep 17 00:00:00 2001 From: TheAwiteb Date: Thu, 7 Sep 2023 08:03:08 +0300 Subject: [PATCH 4/4] Use `AttrValue::expect_none` with `hide` option --- crates/teloxide-macros/src/command_attr.rs | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/crates/teloxide-macros/src/command_attr.rs b/crates/teloxide-macros/src/command_attr.rs index 203b92ea..1e01aed1 100644 --- a/crates/teloxide-macros/src/command_attr.rs +++ b/crates/teloxide-macros/src/command_attr.rs @@ -1,5 +1,5 @@ use crate::{ - attr::{fold_attrs, Attr, AttrValue}, + attr::{fold_attrs, Attr}, error::compile_error_at, fields_parse::ParserType, rename_rules::RenameRule, @@ -104,16 +104,7 @@ impl CommandAttr { "rename" => Rename(value.expect_string()?), "parse_with" => ParseWith(ParserType::parse(value)?), "separator" => Separator(value.expect_string()?), - "hide" => { - if let AttrValue::None(_) = value { - Hide - } else { - return Err(compile_error_at( - "The hide attribute should not have a value, remove it ", - value.span(), - )); - } - } + "hide" => value.expect_none("hide").map(|_| Hide)?, _ => { return Err(compile_error_at( "unexpected attribute name (expected one of `prefix`, `description`, \