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
use proc_macro::TokenStream;
use syn::{
    parse::{Parse, ParseStream},
    parse_macro_input,
    punctuated::Punctuated,
    Error, Expr, Ident, LitStr, Result, Token,
};

mod colors;

#[allow(dead_code)]
#[derive(Debug)]
struct WithFormatString {
    fstring: LitStr,
    sep: Token![,],
    rest: TokenStream,
}

#[allow(dead_code)]
#[derive(Debug)]
struct ColorizeAll {
    ident: Ident,
    tok: Token![=>],
    rest: TokenStream,
}

#[allow(dead_code)]
#[derive(Debug)]
pub(crate) struct ColorizeItem {
    pub ident: Ident,
    pub sep: Token![->],
    pub msg: Expr,
}

#[derive(Debug)]
pub(crate) enum Args {
    Item(ColorizeItem),
    Expr(Expr),
}

impl Parse for WithFormatString {
    fn parse(input: ParseStream) -> Result<Self> {
        Ok(Self {
            fstring: input.parse()?,
            sep: input.parse()?,
            rest: input.parse::<proc_macro2::TokenStream>()?.into(),
        })
    }
}

impl Parse for ColorizeAll {
    fn parse(input: ParseStream) -> Result<Self> {
        Ok(Self {
            ident: input.parse()?,
            tok: input.parse()?,
            rest: input.parse::<proc_macro2::TokenStream>()?.into(),
        })
    }
}

impl Parse for ColorizeItem {
    fn parse(input: ParseStream) -> Result<Self> {
        Ok(Self {
            ident: input.parse()?,
            sep: input.parse()?,
            msg: input.parse()?,
        })
    }
}

impl Parse for Args {
    fn parse(input: ParseStream) -> Result<Self> {
        if input.peek(Ident) && input.peek2(Token![->]) {
            input.parse().map(Args::Item)
        } else {
            input.parse().map(Args::Expr)
        }
    }
}

fn valid_color_all(tag: &Ident) -> Result<()> {
    let str_tag = tag.to_string();
    let mut it = str_tag.chars().peekable();

    while let Some(t) = it.next() {
        match t {
            'b' | 'i' | 'u' | 'N' => continue,
            'F' => {
                if let Some(n) = it.peek() {
                    match n {
                        'k' | 'r' | 'g' | 'y' | 'b' | 'm' | 'c' | 'w' => continue,
                        e => {
                            return Err(Error::new(
                                tag.span(),
                                format!("'F{e}' Invalid foreground option - '{e}'"),
                            ))
                        }
                    }
                } else {
                    return Err(Error::new(
                        tag.span(),
                        "Forground option must be followed by a valid identifier",
                    ));
                }
            }
            'B' => {
                if let Some(n) = it.peek() {
                    match n {
                        'k' | 'r' | 'g' | 'y' | 'b' | 'm' | 'c' | 'w' => continue,
                        e => {
                            return Err(Error::new(
                                tag.span(),
                                format!("'B{e}' Invalid background option - '{e}'"),
                            ))
                        }
                    }
                } else {
                    return Err(Error::new(
                        tag.span(),
                        "Background option must be followed by a valid identifier",
                    ));
                }
            }
            _ => {
                return Err(Error::new(
                    tag.span(),
                    format!("Invalid format identifier '{t}'"),
                ))
            }
        }
    }
    Ok(())
}

#[proc_macro]
/// Adds ANSI color escape sequences to inputs
///
/// ## Usage
///
/// `colorize!` takes a series of inputs, with or without tokens, and converts the inputs into a `String` with ANSI escape sequences added in.
///
/// The returned `String` is primarily useful for printing out to a terminal which is capable of showing color.
/// However, if all you want to do is print, and want to cut out the extra code, use `print_color` instead.
///
/// ## Valid inputs
/// `colorize!` uses the same formatting style as [`format!`](std::format!) so it follows the same
/// argument rules.
///
/// ```
/// # use colorize_proc_macro as colorize;
/// use std::path::PathBuf;
/// use colorize::colorize;
///
/// let user_path = PathBuf::from("/home/color/my_new_file.txt");
/// let pretty_path = colorize!("{:?}", Fgu->user_path.clone());
///
/// assert_eq!(
///     String::from("\x1b[32;4m\"/home/color/my_new_file.txt\"\x1b[0m"),
///     pretty_path
/// );
/// ```
///
/// ## Tokens
/// Tokens can change color or font styling depending on their order and usage.
///
/// #### Styling
/// 1. b -> bold
/// 2. u -> underline
/// 3. i -> italic
///
/// #### Color
/// Color tokens start with an `F` (for foreground) or `B` (for background)
///
/// 1. Fb/Bb -> blue
/// 2. Fr/Br -> red
/// 3. Fg/Bg -> green
/// 4. Fy/By -> yellow
/// 5. Fm/By -> magenta
/// 6. Fc/Bc -> cyan
/// 7. Fw/Bw -> white
/// 8. Fk/Bk -> black
///
/// #### Special Newline Token
/// If you want to add a newline  within the string, include a `N` token at the start
/// of the word(s) you wish to be on the newline. This is the same as just adding '\n' to the
/// string, so it's up to you to use it or not.
///
///
/// Example -
/// ```
/// # use colorize_proc_macro as colorize;
/// use colorize::colorize;
///
/// let color_string = colorize!(
///     "{} {}",
///     b->"Hello", // First line
///     Nb->"world, it's me!" // "world..." will be on the new line
/// );
///
/// let same_color_string = colorize!(
///    "{} \n{}",
///    b->"Hello",
///    b->"world, it's me!"
/// );
///
/// assert_eq!(color_string, same_color_string);
/// ```
///
/// #### Format Multiple Inputs
/// You also have the ability to apply a token to multiple inputs by using `=>` at the beginning of the call.
///
/// ```
/// # use colorize_proc_macro as colorize;
/// use colorize::colorize;
///
/// let color_string = colorize!("{} {}", b => Fg->"Hello", By->"world");
/// ```
/// In the above example, "Hello" will have a green foreground, and "world" will have a yellow background. The preceeding `b =>` applies bold formatting to both.
///
/// ### Examples
/// ```
/// # use colorize_proc_macro as colorize;
/// use colorize::colorize;
///
/// // Returns "Hello" in bold green
/// let color_string = colorize!("{}", Fgb->"Hello");
/// assert_eq!(String::from("\x1b[32;1mHello\x1b[0m"), color_string);
///
/// // Returns "Hello" in italic blue and "World" underlined in magenta
/// // ", it's me" will be unformatted
/// let color_string = colorize!("{} {} {}", iFb->"Hello", Fmu->"world", ", it's me!");
/// assert_eq!(String::from("\x1b[3;34mHello\x1b[0m \x1b[35;4mworld\x1b[0m , it's me!"), color_string);
/// ```
pub fn colorize(input: TokenStream) -> TokenStream {
    let inp = input.clone();

    let (fstring, inp) = match syn::parse::<WithFormatString>(inp) {
        Ok(r) => (r.fstring, r.rest),
        Err(e) => return e.into_compile_error().into(),
    };

    let (args, id) = match syn::parse::<ColorizeAll>(inp.clone()) {
        Ok(r) => {
            if let Err(e) = valid_color_all(&r.ident) {
                e.into_compile_error();
            }
            let rem = r.rest;
            let a = parse_macro_input!(rem with Punctuated::<Args, Token![,]>::parse_terminated);

            (a, Some(r.ident))
        }
        Err(_) => {
            let a = parse_macro_input!(inp with Punctuated::<Args, Token![,]>::parse_terminated);
            (a, None)
        }
    };

    let res = match crate::colors::parse_fstring(&fstring, args, id) {
        Ok(r) => r,
        Err(e) => return e.into_compile_error().into(),
    };

    res.into()
}