rune_macros/quote/
builder.rs
1use crate::quote::inner::ToTokens;
2use proc_macro2::{Span, TokenStream};
3
4#[derive(Debug, Clone)]
5pub(crate) struct Builder {
6 stream: TokenStream,
7 span: Option<Span>,
8}
9
10impl Builder {
11 pub(crate) fn new() -> Self {
12 Self {
13 stream: TokenStream::new(),
14 span: None,
15 }
16 }
17
18 pub(crate) fn into_stream(self) -> TokenStream {
19 self.stream
20 }
21
22 pub(crate) fn push<T>(&mut self, tokens: T)
23 where
24 T: ToTokens,
25 {
26 let span = self.span.unwrap_or_else(Span::call_site);
27 tokens.to_tokens(&mut self.stream, span);
28 }
29
30 pub(crate) fn push_spanned<T>(&mut self, span: Span, tokens: T)
31 where
32 T: ToTokens,
33 {
34 tokens.to_tokens(&mut self.stream, span);
35 }
36}
37
38impl ToTokens for Builder {
39 fn to_tokens(self, stream: &mut TokenStream, _: Span) {
40 stream.extend(self.stream);
41 }
42}
43
44impl From<TokenStream> for Builder {
45 fn from(stream: TokenStream) -> Self {
46 Self { stream, span: None }
47 }
48}