Add a flag to description represent if the description contain doc comment or not

This commit is contained in:
TheAwiteb 2023-09-12 18:20:49 +03:00
parent b8148e0bc9
commit 7e6198925f
No known key found for this signature in database
GPG key ID: ABF818BD15DC2D34
4 changed files with 31 additions and 26 deletions

View file

@ -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! {},
};

View file

@ -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<Span> {
self.description.as_ref().filter(|(d, _)| d == "off").map(|&(_, span)| span)
self.description.as_ref().filter(|(d, ..)| d == "off").map(|&(.., span)| span)
}
}

View file

@ -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))?)
}

View file

@ -5,7 +5,8 @@ use crate::{
pub(crate) struct CommandEnum {
pub prefix: String,
pub description: Option<String>,
/// 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,
})