sourcegen_cli/
tokens.rs

1use proc_macro2::{Ident, Span, TokenStream, TokenTree};
2
3pub(crate) const MAGIC_COMMENT_IDENT: &str = "__SOURCEGEN_MAGIC_COMMENT__";
4
5/// Token used to generate plain Rust comments in the output. Used as a marker in front of the
6/// string literal to generate a plain comment. Usage:
7///
8/// ```rust
9/// use sourcegen_cli::tokens::PlainComment;
10/// let _output = quote::quote! {
11///     #PlainComment "GeneratedComment"
12///     struct Test;
13/// };
14/// ```
15///
16/// Generated output will contain a plain comment:
17/// ```
18/// // Generated comment
19/// struct Test;
20/// ```
21pub struct PlainComment;
22
23impl quote::ToTokens for PlainComment {
24    fn to_tokens(&self, tokens: &mut TokenStream) {
25        tokens.extend(std::iter::once(TokenTree::Ident(Ident::new(
26            MAGIC_COMMENT_IDENT,
27            Span::call_site(),
28        ))));
29    }
30}
31
32pub(crate) const MAGIC_NEWLINE_IDENT: &str = "__SOURCEGEN_MAGIC_NEWLINE__";
33
34/// Token used to generate a newline in the output. Used as a marker. Usage:
35///
36/// ```rust
37/// use sourcegen_cli::tokens::NewLine;
38/// let _output = quote::quote! {
39///     struct Frist;
40///     #NewLine
41///     struct Second;
42/// };
43/// ```
44///
45/// Generated output will contain a plain comment:
46/// ```
47/// struct First;
48///
49/// struct Second;
50/// ```
51pub struct NewLine;
52
53impl quote::ToTokens for NewLine {
54    fn to_tokens(&self, tokens: &mut TokenStream) {
55        tokens.extend(std::iter::once(TokenTree::Ident(Ident::new(
56            MAGIC_NEWLINE_IDENT,
57            Span::call_site(),
58        ))));
59    }
60}