| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 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 { |
| 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 | |
| 20 | pub fn to_string(&self) -> String { |
| 21 | let mut doc = String::new(); |
| 22 | for lit in &self.fragments { |
| 23 | doc += &lit.value(); |
| 24 | doc.push('\n'); |
| 25 | } |
| 26 | doc |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | impl ToTokens for Doc { |
| 31 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 32 | let fragments = &self.fragments; |
| 33 | tokens.extend(quote! { |
| 34 | #(#[doc = #fragments])* |
| 35 | }); |
| 36 | } |
| 37 | } |