From 7e6198925f0514ff8454a289e3c0cec0e3fb39ed Mon Sep 17 00:00:00 2001 From: TheAwiteb Date: Tue, 12 Sep 2023 18:20:49 +0300 Subject: [PATCH] Add a flag to description represent if the description contain doc comment or not --- crates/teloxide-macros/src/bot_commands.rs | 2 +- crates/teloxide-macros/src/command.rs | 7 ++-- crates/teloxide-macros/src/command_attr.rs | 43 ++++++++++++---------- crates/teloxide-macros/src/command_enum.rs | 5 ++- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/crates/teloxide-macros/src/bot_commands.rs b/crates/teloxide-macros/src/bot_commands.rs index 960184f6..e081d680 100644 --- a/crates/teloxide-macros/src/bot_commands.rs +++ b/crates/teloxide-macros/src/bot_commands.rs @@ -76,7 +76,7 @@ fn impl_descriptions(infos: &[Command], global: &CommandEnum) -> proc_macro2::To } }); - let global_description = match global.description.as_deref() { + let global_description = match global.description.as_ref().map(|(d, _)| d) { Some(gd) => quote! { .global_description(#gd) }, None => quote! {}, }; diff --git a/crates/teloxide-macros/src/command.rs b/crates/teloxide-macros/src/command.rs index d26c85f7..0bc13b83 100644 --- a/crates/teloxide-macros/src/command.rs +++ b/crates/teloxide-macros/src/command.rs @@ -9,7 +9,8 @@ pub(crate) struct Command { /// Prefix of this command, for example "/". pub prefix: String, /// Description for the command. - pub description: Option<(String, Span)>, + /// The bool is true if the description contains a doc comment. + pub description: Option<(String, bool, Span)>, /// Name of the command, with all renames already applied. pub name: String, /// Parser for arguments of this command. @@ -61,7 +62,7 @@ impl Command { } pub fn description(&self) -> Option<&str> { - self.description.as_ref().map(|(d, _span)| &**d) + self.description.as_ref().map(|(d, ..)| &**d) } pub(crate) fn description_is_enabled(&self) -> bool { @@ -70,6 +71,6 @@ impl Command { } pub(crate) fn deprecated_description_off_span(&self) -> Option { - self.description.as_ref().filter(|(d, _)| d == "off").map(|&(_, span)| span) + self.description.as_ref().filter(|(d, ..)| d == "off").map(|&(.., span)| span) } } diff --git a/crates/teloxide-macros/src/command_attr.rs b/crates/teloxide-macros/src/command_attr.rs index fed0ba27..3e7a6592 100644 --- a/crates/teloxide-macros/src/command_attr.rs +++ b/crates/teloxide-macros/src/command_attr.rs @@ -16,7 +16,8 @@ use syn::{ /// All attributes that can be used for `derive(BotCommands)` pub(crate) struct CommandAttrs { pub prefix: Option<(String, Span)>, - pub description: Option<(String, Span)>, + /// The bool is true if the description contains a doc comment + pub description: Option<(String, bool, Span)>, pub rename_rule: Option<(RenameRule, Span)>, pub rename: Option<(String, Span)>, pub parser: Option<(ParserType, Span)>, @@ -41,7 +42,8 @@ struct CommandAttr { /// Kind of [`CommandAttr`]. enum CommandAttrKind { Prefix(String), - Description(String), + /// Description of the command. and if its doc comment or not + Description(String, bool), RenameRule(RenameRule), Rename(String), ParseWith(ParserType), @@ -77,31 +79,33 @@ impl CommandAttrs { } } - fn join_string( - opt: &mut Option<(String, Span)>, - new_str: &str, - sp: Span, - ) -> Result<()> { + fn join_string(opt: &mut Option<(String, bool, Span)>, new_str: &str, sp: Span) { match opt { slot @ None => { - *slot = Some((new_str.to_owned(), sp)); - Ok(()) + *slot = Some((new_str.to_owned(), false, sp)); } - Some((old_str, _)) => { + Some((old_str, ..)) => { *old_str = format!("{old_str}\n{new_str}"); - Ok(()) } } } match attr.kind { Prefix(p) => insert(&mut this.prefix, p, attr.sp), - Description(d) => join_string( - &mut this.description, - // Sometimes doc comments include a space before them, this removes it - d.strip_prefix(' ').unwrap_or(&d), - attr.sp, - ), + Description(d, is_doc) => { + join_string( + &mut this.description, + // Sometimes doc comments include a space before them, this removes it + d.strip_prefix(' ').unwrap_or(&d), + attr.sp, + ); + if is_doc { + if let Some((_, is_doc, _)) = &mut this.description { + *is_doc = true; + } + } + Ok(()) + } RenameRule(r) => insert(&mut this.rename_rule, r, attr.sp), Rename(r) => insert(&mut this.rename, r, attr.sp), ParseWith(p) => insert(&mut this.parser, p, attr.sp), @@ -133,8 +137,7 @@ impl CommandAttr { )); } - // FIXME(awiteb): flag here that this is a doc comment - Description(value.expect_string()?) + Description(value.expect_string()?, true) } "command" => { @@ -155,7 +158,7 @@ impl CommandAttr { match &*attr.to_string() { "prefix" => Prefix(value.expect_string()?), - "description" => Description(value.expect_string()?), + "description" => Description(value.expect_string()?, false), "rename_rule" => { RenameRule(value.expect_string().and_then(|r| self::RenameRule::parse(&r))?) } diff --git a/crates/teloxide-macros/src/command_enum.rs b/crates/teloxide-macros/src/command_enum.rs index 3648d3ae..d17247cc 100644 --- a/crates/teloxide-macros/src/command_enum.rs +++ b/crates/teloxide-macros/src/command_enum.rs @@ -5,7 +5,8 @@ use crate::{ pub(crate) struct CommandEnum { pub prefix: String, - pub description: Option, + /// The bool is true if the description contains a doc comment + pub description: Option<(String, bool)>, pub rename_rule: RenameRule, pub parser_type: ParserType, } @@ -37,7 +38,7 @@ impl CommandEnum { Ok(Self { prefix: prefix.map(|(p, _)| p).unwrap_or_else(|| "/".to_owned()), - description: description.map(|(d, _)| d), + description: description.map(|(d, is_doc, _)| (d, is_doc)), rename_rule: rename_rule.map(|(rr, _)| rr).unwrap_or(RenameRule::Identity), parser_type: parser, })