aboutsummaryrefslogtreecommitdiff
path: root/syntax/doc.rs
blob: 5de824f3ac3ad01fd3a56942523fa6b75e995ac3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::LitStr;

pub struct Doc {
    pub(crate) hidden: bool,
    fragments: Vec<LitStr>,
}

impl Doc {
    pub fn new() -> Self {
        Doc {
            hidden: false,
            fragments: Vec::new(),
        }
    }

    pub fn push(&mut self, lit: LitStr) {
        self.fragments.push(lit);
    }

    #[allow(dead_code)] // only used by cxx-build, not cxxbridge-macro
    pub fn is_empty(&self) -> bool {
        self.fragments.is_empty()
    }

    #[allow(dead_code)] // only used by cxx-build, not cxxbridge-macro
    pub fn to_string(&self) -> String {
        let mut doc = String::new();
        for lit in &self.fragments {
            doc += &lit.value();
            doc.push('\n');
        }
        doc
    }
}

impl ToTokens for Doc {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        let fragments = &self.fragments;
        tokens.extend(quote! { #(#[doc = #fragments])* });
        if self.hidden {
            tokens.extend(quote! { #[doc(hidden)] });
        }
    }
}