1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
extern crate proc_macro;
extern crate syn;
extern crate textwrap;

use proc_macro::TokenStream;
use quote::ToTokens;
use syn::parse::Parse;
use syn::parse::ParseStream;
use syn::parse::Result as ParseResult;
use syn::parse_macro_input;

// ---------------------------------------------------------------------------

struct DedentInput {
    lit: syn::LitStr,
}

impl Parse for DedentInput {
    fn parse(input: ParseStream) -> ParseResult<Self> {
        Ok(Self {
            lit: input.parse()?,
        })
    }
}

#[proc_macro_hack::proc_macro_hack]
pub fn dedent(tokens: TokenStream) -> TokenStream {
    let input = parse_macro_input!(tokens as DedentInput);
    let newstr = textwrap::dedent(&input.lit.value());

    syn::LitStr::new(&newstr, input.lit.span())
        .into_token_stream()
        .into()
}

// ---------------------------------------------------------------------------

struct FillInput {
    lit: syn::LitStr,
    width: syn::LitInt,
}

impl Parse for FillInput {
    fn parse(input: ParseStream) -> ParseResult<Self> {
        let lit = input.parse()?;
        input.parse::<syn::Token![,]>()?;
        let width = input.parse()?;
        Ok(Self { lit, width })
    }
}

#[proc_macro_hack::proc_macro_hack]
pub fn fill(tokens: TokenStream) -> TokenStream {
    let input = parse_macro_input!(tokens as FillInput);
    let width: usize = input.width.base10_parse().expect("could not parse number");
    let newstr = textwrap::fill(&input.lit.value(), width);

    syn::LitStr::new(&newstr, input.lit.span())
        .into_token_stream()
        .into()
}

// ---------------------------------------------------------------------------

struct IndentInput {
    lit: syn::LitStr,
    prefix: syn::LitStr,
}

impl Parse for IndentInput {
    fn parse(input: ParseStream) -> ParseResult<Self> {
        let lit = input.parse()?;
        input.parse::<syn::Token![,]>()?;
        let prefix = input.parse()?;
        Ok(Self { lit, prefix })
    }
}

#[proc_macro_hack::proc_macro_hack]
pub fn indent(tokens: TokenStream) -> TokenStream {
    let input = parse_macro_input!(tokens as IndentInput);
    let newstr = textwrap::indent(&input.lit.value(), &input.prefix.value());

    syn::LitStr::new(&newstr, input.lit.span())
        .into_token_stream()
        .into()
}

// ---------------------------------------------------------------------------

struct WrapInput {
    lit: syn::LitStr,
    width: syn::LitInt,
}

impl Parse for WrapInput {
    fn parse(input: ParseStream) -> ParseResult<Self> {
        let lit = input.parse()?;
        input.parse::<syn::Token![,]>()?;
        let width = input.parse()?;
        Ok(Self { lit, width })
    }
}

#[proc_macro_hack::proc_macro_hack]
pub fn wrap(tokens: TokenStream) -> TokenStream {
    let input = parse_macro_input!(tokens as WrapInput);
    let width: usize = input.width.base10_parse().expect("could not parse number");

    let elems = textwrap::wrap(&input.lit.value(), width)
        .iter()
        .map(|s| syn::Lit::from(syn::LitStr::new(&s, input.lit.span())))
        .map(|lit| {
            syn::Expr::Lit(syn::ExprLit {
                lit,
                attrs: Vec::new(),
            })
        })
        .collect();
    let array = syn::ExprArray {
        elems,
        attrs: Vec::new(),
        bracket_token: Default::default(),
    };
    let expr = syn::ExprReference {
        attrs: Vec::new(),
        and_token: Default::default(),
        raw: Default::default(),
        mutability: None,
        expr: Box::new(syn::Expr::Array(array)),
    };

    expr.into_token_stream().into()
}