1 use proc_macro2::TokenStream; 2 use quote::{quote, ToTokens}; 3 use syn::LitStr; 4 5 pub struct Doc { 6 pub(crate) hidden: bool, 7 fragments: Vec<LitStr>, 8 } 9 10 impl Doc { new() -> Self11 pub fn new() -> Self { 12 Doc { 13 hidden: false, 14 fragments: Vec::new(), 15 } 16 } 17 push(&mut self, lit: LitStr)18 pub fn push(&mut self, lit: LitStr) { 19 self.fragments.push(lit); 20 } 21 22 #[allow(dead_code)] // only used by cxx-build, not cxxbridge-macro is_empty(&self) -> bool23 pub fn is_empty(&self) -> bool { 24 self.fragments.is_empty() 25 } 26 27 #[allow(dead_code)] // only used by cxx-build, not cxxbridge-macro to_string(&self) -> String28 pub fn to_string(&self) -> String { 29 let mut doc = String::new(); 30 for lit in &self.fragments { 31 doc += &lit.value(); 32 doc.push('\n'); 33 } 34 doc 35 } 36 } 37 38 impl ToTokens for Doc { to_tokens(&self, tokens: &mut TokenStream)39 fn to_tokens(&self, tokens: &mut TokenStream) { 40 let fragments = &self.fragments; 41 tokens.extend(quote! { #(#[doc = #fragments])* }); 42 if self.hidden { 43 tokens.extend(quote! { #[doc(hidden)] }); 44 } 45 } 46 } 47