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
extern crate proc_macro;
extern crate syn;
extern crate textwrap;
use std::fmt::Write;
use proc_macro::TokenStream;
use syn::parse_macro_input;
use syn::parse::Parse;
use syn::parse::ParseStream;
use syn::parse::Result as ParseResult;
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());
format!("{:?}", newstr).parse().unwrap()
}
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 = input.width.base10_parse().expect("could not parse number");
let newstr = textwrap::fill(&input.lit.value(), width);
format!("{:?}", newstr).parse().unwrap()
}
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());
format!("{:?}", newstr).parse().unwrap()
}
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 = input.width.base10_parse().expect("could not parse number");
let mut newstr = String::new();
newstr.push_str("&[");
for line in textwrap::wrap_iter(&input.lit.value(), width) {
write!(newstr, "{:?},", &line).unwrap();
}
newstr.push_str("]");
newstr.parse().unwrap()
}