bunt_macros/
lib.rs

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
4use proc_macro::TokenStream as TokenStream1;
5use proc_macro2::TokenStream;
6
7#[macro_use]
8mod err;
9mod gen;
10mod ir;
11mod parse;
12
13#[cfg(test)]
14mod tests;
15
16use crate::{
17    err::Error,
18    ir::{Style, WriteInput},
19};
20
21
22// Docs are in the `bunt` reexport.
23#[proc_macro]
24pub 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]
33pub 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]
39pub 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.
49fn 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}