1 use proc_macro2::TokenStream; 2 use quote::{quote, ToTokens}; 3 use syn::LitStr; 4 5 pub struct Doc { 6 fragments: Vec<LitStr>, 7 } 8 9 impl Doc { new() -> Self10 pub fn new() -> Self { 11 Doc { 12 fragments: Vec::new(), 13 } 14 } 15 push(&mut self, lit: LitStr)16 pub fn push(&mut self, lit: LitStr) { 17 self.fragments.push(lit); 18 } 19 is_empty(&self) -> bool20 pub fn is_empty(&self) -> bool { 21 self.fragments.is_empty() 22 } 23 to_string(&self) -> String24 pub fn to_string(&self) -> String { 25 let mut doc = String::new(); 26 for lit in &self.fragments { 27 doc += &lit.value(); 28 doc.push('\n'); 29 } 30 doc 31 } 32 } 33 34 impl ToTokens for Doc { to_tokens(&self, tokens: &mut TokenStream)35 fn to_tokens(&self, tokens: &mut TokenStream) { 36 let fragments = &self.fragments; 37 tokens.extend(quote! { 38 #(#[doc = #fragments])* 39 }); 40 } 41 } 42