1//! This module contains the code for analyzing the input Rust code and extracting the necessary information from it.
2
3pub mod crate_;
4pub mod enum_;
5pub mod function;
6pub mod module;
7pub mod struct_;
8pub mod type_;
9
10pub use self::crate_::analyze_crate;
11
12/// Extracts the docstring from an object's attributes
13///
14/// An initial whitespace character is stripped from the start of each line.
15///
16/// :param attrs: The attributes of the object
17///
18// TODO also extract an optional docstring type from the attributes?
19pub(super) fn docstring_from_attrs(attrs: &[syn::Attribute]) -> String {
20 attrs
21 .iter()
22 .filter_map(|attr| {
23 if attr.path().is_ident("doc") {
24 match &attr.meta {
25 syn::Meta::NameValue(value) => {
26 if let syn::Expr::Lit(value) = &value.value {
27 if let syn::Lit::Str(value) = &value.lit {
28 let string = value.value();
29 match string.strip_prefix(' ') {
30 Some(string) => Some(string.to_string()),
31 None => Some(string),
32 }
33 } else {
34 None
35 }
36 } else {
37 None
38 }
39 }
40 _ => None,
41 }
42 } else {
43 None
44 }
45 })
46 .collect::<Vec<_>>()
47 .join("\n")
48}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_docstring_from_attrs() {
56 let attrs: Vec<syn::Attribute> = vec![
57 syn::parse_quote! { #[doc = "This is a docstring"] },
58 syn::parse_quote! { #[doc = "Another docstring"] },
59 syn::parse_quote! { #[other_attr] },
60 ];
61 let result = docstring_from_attrs(&attrs);
62 assert_eq!(result, "This is a docstring\nAnother docstring");
63 }
64}