1 //! These are the docs for the crate `bunt-macros`. This is just implementation
2 //! detail, please see the crate `bunt` for the real docs.
3
4 use proc_macro::TokenStream as TokenStream1;
5 use proc_macro2::TokenStream;
6
7 #[macro_use]
8 mod err;
9 mod gen;
10 mod ir;
11 mod parse;
12
13 #[cfg(test)]
14 mod tests;
15
16 use crate::{
17 err::Error,
18 ir::{Style, WriteInput},
19 };
20
21
22 // Docs are in the `bunt` reexport.
23 #[proc_macro]
style(input: TokenStream1) -> TokenStream124 pub fn style(input: TokenStream1) -> TokenStream1 {
25 run(input, |input| {
26 let style = Style::parse_from_tokens(input)?;
27 Ok(style.to_tokens())
28 })
29 }
30
31 // Docs are in the `bunt` reexport.
32 #[proc_macro]
write(input: TokenStream1) -> TokenStream133 pub fn write(input: TokenStream1) -> TokenStream1 {
34 run(input, |input| parse::parse(input, WriteInput::parse)?.gen_output())
35 }
36
37 // Docs are in the `bunt` reexport.
38 #[proc_macro]
writeln(input: TokenStream1) -> TokenStream139 pub fn writeln(input: TokenStream1) -> TokenStream1 {
40 run(input, |input| {
41 let mut input = parse::parse(input, WriteInput::parse)?;
42 input.format_str.add_newline();
43 input.gen_output()
44 })
45 }
46
47 /// Performs the conversion from and to `proc_macro::TokenStream` and converts
48 /// `Error`s into `compile_error!` tokens.
run( input: TokenStream1, f: impl FnOnce(TokenStream) -> Result<TokenStream, Error>, ) -> TokenStream149 fn run(
50 input: TokenStream1,
51 f: impl FnOnce(TokenStream) -> Result<TokenStream, Error>,
52 ) -> TokenStream1 {
53 f(input.into())
54 .unwrap_or_else(|e| e.to_compile_error())
55 .into()
56 }
57