spraypaint-macros 0.1.0

Procedural macros for the spraypaint terminal styling crate
Documentation
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! Procedural macros for the `spraypaint` crate.
//!
//! This crate is not intended to be used directly; import from `spraypaint` instead.

use proc_macro::TokenStream;
use proc_macro2::Span;
use quote::quote;
use syn::{
    parse::{Parse, ParseStream},
    parse_macro_input, LitStr, Token,
};

// ── Public macro entrypoints ──────────────────────────────────────────────────

/// Print styled text to stdout with a trailing newline.
///
/// # Syntax
/// ```text
/// paint!("template string")
/// paint!(inline, "template string")   // no trailing newline
/// paint!(stderr, "template string")   // print to stderr with newline
/// ```
///
/// # Template Format
/// ```text
/// paint!("{red.bold Error:} something went wrong");
/// paint!("Hello {green.italic world}!");
/// paint!("{blue Welcome to {bold.underline spraypaint}}");
///
/// let name = "world";
/// paint!("Hello {green.bold {name}}!");
/// ```
#[proc_macro]
pub fn paint(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as PaintInput);
    let parts = match parse_template(&input.template, input.template_span) {
        Ok(p) => p,
        Err(e) => return e.into_compile_error().into(),
    };
    let exprs = parts_to_exprs(&parts);

    match input.mode {
        PrintMode::Stdout => quote! {
            {
                use ::std::io::Write as _;
                #( ::std::print!("{}", #exprs); )*
                ::std::println!();
            }
        },
        PrintMode::Inline => quote! {
            {
                use ::std::io::Write as _;
                #( ::std::print!("{}", #exprs); )*
                let _ = ::std::io::stdout().flush();
            }
        },
        PrintMode::Stderr => quote! {
            {
                #( ::std::eprint!("{}", #exprs); )*
                ::std::eprintln!();
            }
        },
    }
    .into()
}

/// Return an owned `String` with ANSI styling applied (does not print).
///
/// Use this when you need to pass styled text to a logger, format string, etc.
///
/// # Example
/// ```rust,ignore
/// use spraypaint::styled;
///
/// let msg = styled!("{red.bold Error:} something went wrong");
/// eprintln!("{msg}");
/// ```
#[proc_macro]
pub fn styled(input: TokenStream) -> TokenStream {
    let lit = parse_macro_input!(input as LitStr);
    let span = lit.span();
    let template = lit.value();

    let parts = match parse_template(&template, span) {
        Ok(p) => p,
        Err(e) => return e.into_compile_error().into(),
    };
    let exprs = parts_to_exprs(&parts);

    quote! {
        {
            use ::std::fmt::Write as _;
            let mut __buf = ::std::string::String::new();
            #( ::std::write!(__buf, "{}", #exprs)
                .expect("fmt::Write to String is infallible"); )*
            __buf
        }
    }
    .into()
}

// ── Input structs ─────────────────────────────────────────────────────────────

enum PrintMode {
    Stdout,
    Inline,
    Stderr,
}

struct PaintInput {
    mode: PrintMode,
    template: String,
    template_span: Span,
}

impl Parse for PaintInput {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        if input.peek(syn::Ident) && !input.peek2(Token![,]) {
            // Single ident with no comma: must be the template (treat as error below
            // if it's not a string literal). Fall through.
        }
        if input.peek(syn::Ident) {
            let ident: syn::Ident = input.parse()?;
            let _: Token![,] = input.parse()?;
            let lit: LitStr = input.parse()?;
            let mode = match ident.to_string().as_str() {
                "inline" => PrintMode::Inline,
                "stderr" => PrintMode::Stderr,
                other => {
                    return Err(syn::Error::new(
                        ident.span(),
                        format!("unknown paint! mode `{other}`; expected `inline` or `stderr`"),
                    ))
                }
            };
            return Ok(PaintInput {
                mode,
                template: lit.value(),
                template_span: lit.span(),
            });
        }
        let lit: LitStr = input.parse()?;
        Ok(PaintInput {
            mode: PrintMode::Stdout,
            template: lit.value(),
            template_span: lit.span(),
        })
    }
}

// ── Template AST ──────────────────────────────────────────────────────────────

#[derive(Debug)]
enum Part {
    /// A plain string literal fragment.
    Literal(String),
    /// A `{style1.style2 content}` block.
    Styled {
        styles: Vec<String>,
        inner: Vec<Part>,
    },
    /// A `{expr}` interpolation (arbitrary Rust expression).
    Expr(String),
}

// ── Template parser ───────────────────────────────────────────────────────────

fn parse_template(template: &str, span: Span) -> syn::Result<Vec<Part>> {
    let chars: Vec<char> = template.chars().collect();
    let mut parser = Parser {
        chars,
        pos: 0,
        span,
    };
    parser.parse_sequence(false)
}

struct Parser {
    chars: Vec<char>,
    pos: usize,
    span: Span,
}

impl Parser {
    fn peek(&self) -> Option<char> {
        self.chars.get(self.pos).copied()
    }

    fn advance(&mut self) -> Option<char> {
        let c = self.chars.get(self.pos).copied();
        if c.is_some() {
            self.pos += 1;
        }
        c
    }

    /// Parse a sequence of parts until EOF or (if `stop=true`) a closing `}`.
    fn parse_sequence(&mut self, stop_at_close: bool) -> syn::Result<Vec<Part>> {
        let mut parts = Vec::new();
        let mut literal = String::new();

        loop {
            match self.peek() {
                None => break,
                Some('}') if stop_at_close => {
                    self.advance();
                    break;
                }
                Some('{') => {
                    if !literal.is_empty() {
                        parts.push(Part::Literal(std::mem::take(&mut literal)));
                    }
                    self.advance(); // consume '{'
                    parts.push(self.parse_brace()?);
                }
                Some(c) => {
                    literal.push(c);
                    self.advance();
                }
            }
        }

        if !literal.is_empty() {
            parts.push(Part::Literal(literal));
        }
        Ok(parts)
    }

    /// Parse the content after an opening `{`.
    fn parse_brace(&mut self) -> syn::Result<Part> {
        // Peek ahead to determine whether this is a style spec or a raw expression.
        // Strategy: read the first "word" (up to whitespace or end of brace group).
        let save = self.pos;
        let first_word = self.read_word();

        if !first_word.is_empty() && looks_like_style_spec(&first_word) {
            // Parse style spec tokens.
            let styles = parse_style_spec(&first_word, self.span)?;
            // Skip optional single space between spec and content.
            if self.peek() == Some(' ') {
                self.advance();
            }
            let inner = self.parse_sequence(true)?;
            Ok(Part::Styled { styles, inner })
        } else {
            // Raw Rust expression: restore position and consume until matching `}`.
            self.pos = save;
            let expr = self.read_until_close_brace()?;
            Ok(Part::Expr(expr.trim().to_string()))
        }
    }

    /// Read contiguous non-whitespace, non-brace characters (the style spec).
    fn read_word(&mut self) -> String {
        let mut word = String::new();
        while let Some(c) = self.peek() {
            if c.is_whitespace() || c == '{' || c == '}' {
                break;
            }
            word.push(c);
            self.advance();
        }
        word
    }

    /// Consume until the matching `}` (handling nested braces), return the content.
    fn read_until_close_brace(&mut self) -> syn::Result<String> {
        let mut s = String::new();
        let mut depth = 1usize;
        loop {
            match self.advance() {
                None => {
                    return Err(syn::Error::new(
                        self.span,
                        "unclosed `{` in paint! template",
                    ));
                }
                Some('{') => {
                    depth += 1;
                    s.push('{');
                }
                Some('}') => {
                    depth -= 1;
                    if depth == 0 {
                        break;
                    }
                    s.push('}');
                }
                Some(c) => s.push(c),
            }
        }
        Ok(s)
    }
}

// ── Style spec helpers ────────────────────────────────────────────────────────

/// Return true if every dot-separated token in `spec` is a known style name.
fn looks_like_style_spec(spec: &str) -> bool {
    spec.split('.').all(|t| {
        let t = t.trim();
        is_known_style(t) || t.starts_with("rgb(") || t.starts_with("hex(")
    })
}

fn is_known_style(token: &str) -> bool {
    matches!(
        token,
        "black"
            | "red"
            | "green"
            | "yellow"
            | "blue"
            | "magenta"
            | "cyan"
            | "white"
            | "bright_black"
            | "bright_red"
            | "bright_green"
            | "bright_yellow"
            | "bright_blue"
            | "bright_magenta"
            | "bright_cyan"
            | "bright_white"
            | "on_black"
            | "on_red"
            | "on_green"
            | "on_yellow"
            | "on_blue"
            | "on_magenta"
            | "on_cyan"
            | "on_white"
            | "on_bright_black"
            | "on_bright_red"
            | "on_bright_green"
            | "on_bright_yellow"
            | "on_bright_blue"
            | "on_bright_magenta"
            | "on_bright_cyan"
            | "on_bright_white"
            | "bold"
            | "dim"
            | "italic"
            | "underline"
            | "blink"
            | "blink_fast"
            | "reverse"
            | "hidden"
            | "strikethrough"
    )
}

fn parse_style_spec(spec: &str, span: Span) -> syn::Result<Vec<String>> {
    let mut result = Vec::new();
    for token in spec.split('.') {
        let token = token.trim();
        if token.is_empty() {
            continue;
        }
        if is_known_style(token) || token.starts_with("rgb(") || token.starts_with("hex(") {
            result.push(token.to_string());
        } else {
            return Err(syn::Error::new(
                span,
                format!(
                    "unknown style `{token}` in paint! template\n\
                     hint: valid colors are red, green, blue, yellow, magenta, cyan, white, black\n\
                     hint: valid attributes are bold, dim, italic, underline, strikethrough, reverse"
                ),
            ));
        }
    }
    Ok(result)
}

// ── Code generation ───────────────────────────────────────────────────────────

fn parts_to_exprs(parts: &[Part]) -> Vec<proc_macro2::TokenStream> {
    parts.iter().map(part_to_expr).collect()
}

fn part_to_expr(part: &Part) -> proc_macro2::TokenStream {
    match part {
        Part::Literal(s) => {
            quote! { #s }
        }

        Part::Expr(e) => {
            let tokens: proc_macro2::TokenStream = match e.parse() {
                Ok(t) => t,
                Err(lex_err) => {
                    let msg =
                        format!("invalid expression `{e}` in paint!/styled! template: {lex_err}");
                    return quote! { { compile_error!(#msg); "" } };
                }
            };
            quote! { &::std::format!("{}", #tokens) }
        }

        Part::Styled { styles, inner } => {
            let inner_exprs = parts_to_exprs(inner);
            let method_chain = build_method_chain(styles);

            // Collect inner parts into an owned String, then apply the style chain.
            // `String` implements `Colorize`, so chaining methods returns `Styled<String>`,
            // which implements `Display` -- safe to pass to `print!`.
            quote! {
                {
                    use ::std::fmt::Write as _;
                    use ::spraypaint::Colorize as _;
                    let mut __inner = ::std::string::String::new();
                    #( ::std::write!(__inner, "{}", #inner_exprs)
                        .expect("fmt::Write to String is infallible"); )*
                    __inner #method_chain
                }
            }
        }
    }
}

/// Build a dot-chained method call sequence, e.g. `.red().bold().italic()`.
fn build_method_chain(styles: &[String]) -> proc_macro2::TokenStream {
    let mut chain = quote! {};
    for style in styles {
        if let Some(inner) = style.strip_prefix("rgb(").and_then(|s| s.strip_suffix(')')) {
            let parts: Vec<&str> = inner.split(',').collect();
            if parts.len() == 3 {
                if let (Ok(r), Ok(g), Ok(b)) = (
                    parts[0].trim().parse::<u8>(),
                    parts[1].trim().parse::<u8>(),
                    parts[2].trim().parse::<u8>(),
                ) {
                    chain = quote! { #chain .rgb(#r, #g, #b) };
                    continue;
                }
            }
        }
        if let Some(inner) = style.strip_prefix("hex(").and_then(|s| s.strip_suffix(')')) {
            chain = quote! { #chain .hex(#inner) };
            continue;
        }
        let ident = proc_macro2::Ident::new(style, Span::call_site());
        chain = quote! { #chain .#ident() };
    }
    chain
}