From 36e9e279e425fa53e0c20cf930a33c72da64336d Mon Sep 17 00:00:00 2001 From: Vidhan Bhatt Date: Sat, 31 Aug 2024 14:20:30 -0400 Subject: [PATCH] fix: doc comments --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- macros/src/lib.rs | 31 +++++++++++++++++++++++++------ 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3763fc5..87d0db7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1233,7 +1233,7 @@ dependencies = [ [[package]] name = "serenity-commands" -version = "0.5.0" +version = "0.5.1" dependencies = [ "serenity", "serenity-commands-macros", @@ -1243,7 +1243,7 @@ dependencies = [ [[package]] name = "serenity-commands-macros" -version = "0.5.0" +version = "0.5.1" dependencies = [ "darling", "heck", diff --git a/Cargo.toml b/Cargo.toml index 668f859..8b12648 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,7 +45,7 @@ members = [".", "macros"] license = "MIT" readme = "README.md" repository = "https://github.com/vidhanio/serenity-commands" - version = "0.5.0" + version = "0.5.1" [workspace.dependencies] serenity-commands-macros = { version = "0.5", path = "macros" } diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 5232189..cf897fe 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -548,9 +548,9 @@ fn documentation_string( spanned: &impl Spanned, acc: &mut Accumulator, ) -> LitStr { - let res = attrs + let mut doc_comments = attrs .iter() - .find_map(|attr| match &attr.meta { + .filter_map(|attr| match &attr.meta { Meta::NameValue(MetaNameValue { path, value: @@ -558,13 +558,32 @@ fn documentation_string( lit: Lit::Str(s), .. }), .. - }) if path.is_ident("doc") => Some(LitStr::new(s.value().trim(), s.span())), + }) if path.is_ident("doc") => Some((s.span(), s.value())), _ => None, }) - .ok_or_else(|| { + .peekable(); + + let res = if doc_comments.peek().is_none() { + Err( Error::custom("missing documentation comment (`///`) to use as description") - .with_span(spanned) - }); + .with_span(spanned), + ) + } else { + let (span, s) = doc_comments.fold( + (Span::call_site(), String::new()), + |(span, mut acc), (_, s)| { + if !acc.is_empty() { + acc.push(' '); + } + + acc.push_str(s.trim()); + + (span, acc) + }, + ); + + Ok(LitStr::new(&s, span)) + }; acc.handle(res) .unwrap_or_else(|| LitStr::new("", Span::call_site()))