blob: cd764facb1d39a055d032343b211d175a0cc12cc [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use proc_macro2::TokenStream;
2use quote::{quote, ToTokens};
3use syn::LitStr;
4
5pub struct Doc {
6 fragments: Vec<LitStr>,
7}
8
9impl Doc {
10 pub fn new() -> Self {
11 Doc {
12 fragments: Vec::new(),
13 }
14 }
15
16 pub fn push(&mut self, lit: LitStr) {
17 self.fragments.push(lit);
18 }
19
David Tolnay65b83382021-04-13 20:23:19 -070020 pub fn is_empty(&self) -> bool {
21 self.fragments.is_empty()
22 }
23
David Tolnay7db73692019-10-20 14:51:12 -040024 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
34impl ToTokens for Doc {
35 fn to_tokens(&self, tokens: &mut TokenStream) {
36 let fragments = &self.fragments;
37 tokens.extend(quote! {
38 #(#[doc = #fragments])*
39 });
40 }
41}