savvy_bindgen/utils.rs
1pub fn extract_docs(attrs: &[syn::Attribute]) -> Vec<String> {
2 attrs
3 .iter()
4 .filter_map(|attr| {
5 match &attr.meta {
6 syn::Meta::NameValue(nv) => {
7 // Doc omments are transformed into the form of `#[doc =
8 // r"comment"]` before macros are expanded.
9 // cf., https://docs.rs/syn/latest/syn/struct.Attribute.html#doc-comments
10 if nv.path.is_ident("doc") {
11 match &nv.value {
12 syn::Expr::Lit(syn::ExprLit {
13 lit: syn::Lit::Str(doc),
14 ..
15 }) => Some(doc.value()),
16 _ => None,
17 }
18 } else {
19 None
20 }
21 }
22 _ => None,
23 }
24 })
25 .collect()
26}
27
28pub(crate) fn add_indent(x: &str, indent: usize) -> String {
29 x.lines()
30 .map(|x| format!("{:indent$}{x}", "", indent = indent))
31 .collect::<Vec<String>>()
32 .join("\n")
33}