mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 22:46:39 +01:00
Possibility of using more than one attribute for the description
This commit is contained in:
parent
c0fd07184b
commit
0d02c48afd
1 changed files with 36 additions and 14 deletions
|
@ -50,24 +50,23 @@ impl CommandAttrs {
|
||||||
pub fn from_attributes(attributes: &[Attribute]) -> Result<Self> {
|
pub fn from_attributes(attributes: &[Attribute]) -> Result<Self> {
|
||||||
use CommandAttrKind::*;
|
use CommandAttrKind::*;
|
||||||
|
|
||||||
let docs = attributes
|
let docs = attributes.iter().filter_map(|attr| {
|
||||||
.iter()
|
parse_doc_comment(attr)
|
||||||
.filter_map(|attr| parse_doc_comment(attr).map(|doc| (doc, attr.span())));
|
.or_else(|| parse_command_description(attr))
|
||||||
|
.map(|doc| (doc, attr.span()))
|
||||||
|
});
|
||||||
|
|
||||||
let mut attributes = attributes.to_vec();
|
let mut attributes = attributes.to_vec();
|
||||||
if docs.clone().count() != 0 {
|
if docs.clone().count() != 0 {
|
||||||
if let Some(des_sp) = get_descripation_span(&attributes) {
|
// Remove all command description attributes, to avoid duplication
|
||||||
return Err(compile_error_at(
|
attributes.retain(|attr| !is_command_description(attr));
|
||||||
"You cannot use doc comments and #[command(description = \"...\")] \
|
|
||||||
simultaneously\nYou can use only one of them",
|
|
||||||
des_sp,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
let description = docs.clone().map(|(doc, _)| doc).collect::<Vec<_>>().join("\n");
|
let description = docs.clone().map(|(doc, _)| doc).collect::<Vec<_>>().join("\n");
|
||||||
let sp = docs
|
let sp = docs
|
||||||
.map(|(_, sp)| sp)
|
.map(|(_, sp)| sp)
|
||||||
.reduce(|acc, sp| acc.join(sp).expect("The spans are in the same file"))
|
.reduce(|acc, sp| acc.join(sp).expect("The spans are in the same file"))
|
||||||
.expect("There is at least one doc comment");
|
.expect("There is at least one doc comment");
|
||||||
|
// Insert a new command description attribute, with all descriptions and doc
|
||||||
|
// comments
|
||||||
let attr = Attribute::parse_outer
|
let attr = Attribute::parse_outer
|
||||||
.parse2(quote_spanned! { sp => #[command(description = #description)] })?;
|
.parse2(quote_spanned! { sp => #[command(description = #description)] })?;
|
||||||
attributes.push(attr.into_iter().next().unwrap());
|
attributes.push(attr.into_iter().next().unwrap());
|
||||||
|
@ -149,21 +148,44 @@ fn is_command_attribute(a: &Attribute) -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the span of the first attribute with a description meta item.
|
fn is_command_description(attr: &Attribute) -> bool {
|
||||||
fn get_descripation_span(attrs: &[Attribute]) -> Option<Span> {
|
for token in attr.tokens.clone() {
|
||||||
for attr in attrs {
|
if let TokenTree::Group(group) = token {
|
||||||
|
for token in group.stream() {
|
||||||
|
if let TokenTree::Ident(ident) = token {
|
||||||
|
if ident == "description" {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_command_description(attr: &Attribute) -> Option<String> {
|
||||||
|
if is_command_attribute(attr) {
|
||||||
for token in attr.tokens.clone() {
|
for token in attr.tokens.clone() {
|
||||||
if let TokenTree::Group(group) = token {
|
if let TokenTree::Group(group) = token {
|
||||||
for token in group.stream() {
|
for token in group.stream() {
|
||||||
if let TokenTree::Ident(ident) = token {
|
if let TokenTree::Ident(ident) = token {
|
||||||
if ident == "description" {
|
if ident == "description" {
|
||||||
return Some(group.span());
|
for token in group.stream() {
|
||||||
|
if let TokenTree::Literal(lit) = token {
|
||||||
|
let description = lit.to_string();
|
||||||
|
return Some(
|
||||||
|
lit.to_string().trim()[1..description.len() - 1]
|
||||||
|
.replace(r"\n", "\n"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue