1use proc_macro2::TokenStream; 2use quote::{quote, ToTokens}; 3use syn::LitStr; 4 5pub struct Doc { 6 pub(crate) hidden: bool, 7 fragments: Vec<LitStr>, 8} 9 10impl Doc { 11 pub fn new() -> Self { 12 Doc { 13 hidden: false, 14 fragments: Vec::new(), 15 } 16 } 17 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 23 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 28 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 38impl ToTokens for Doc { 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