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
#![warn(clippy::pedantic, rust_2018_idioms)]

use std::array;

use bytestring::ByteString;
use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote};
use syn::{punctuated::Punctuated, Error, Expr, Ident, Result, Token};

fn ident_to_expr(ident: Ident) -> syn::Expr {
    syn::Expr::Path(syn::ExprPath {
        attrs: Vec::new(),
        qself: None,
        path: syn::Path {
            leading_colon: None,
            segments: Punctuated::from_iter([syn::PathSegment {
                arguments: syn::PathArguments::None,
                ident,
            }]),
        },
    })
}

enum Piece {
    Literal(ByteString),
    Argument { expr: Expr, ident: Ident },
}

impl Piece {
    fn as_ident(&self) -> Option<&Ident> {
        if let Self::Argument { ident, .. } = self {
            Some(ident)
        } else {
            None
        }
    }

    fn as_expr(&self) -> Option<&Expr> {
        if let Self::Argument { expr, .. } = self {
            Some(expr)
        } else {
            None
        }
    }
}

impl quote::ToTokens for Piece {
    fn to_tokens(&self, tokens: &mut TokenStream) {
        match self {
            Self::Literal(str) if str.is_empty() => {}
            Self::Literal(str) => {
                let str: &str = str;
                quote!(out.push_str(#str);).to_tokens(tokens)
            }
            Self::Argument { ident, .. } => {
                quote!(out.push_str(#ident.as_str());).to_tokens(tokens)
            }
        }
    }
}

struct Arguments {
    str_base_len: usize,
    pieces: Vec<Piece>,
}

impl syn::parse::Parse for Arguments {
    fn parse(input: syn::parse::ParseStream<'_>) -> Result<Self> {
        let format_str = input.parse::<syn::LitStr>()?;
        input.parse::<Option<Token![,]>>()?;

        let create_err = |msg| Error::new(format_str.span(), msg);
        let unterminated_fmt = move || create_err("Unterminated format argument");

        let format_string = ByteString::from(format_str.value());
        let mut current: &str = &*format_string;

        let mut str_base_len = 0;
        let mut pieces = Vec::new();

        for arg_num in 0_u8.. {
            let Some((text, rest)) = current.split_once('{') else {
                str_base_len += current.len();
                pieces.push(Piece::Literal(format_string.slice_ref(current)));
                break;
            };

            let (arg_name, rest) = rest.split_once('}').ok_or_else(unterminated_fmt)?;

            let arg_expr = if arg_name.is_empty() {
                if input.is_empty() {
                    return Err(create_err("Not enough arguments for format string"));
                }

                let argument = input.parse::<syn::Expr>()?;
                if input.parse::<Option<Token![,]>>()?.is_none() && !input.is_empty() {
                    return Err(Error::new(input.span(), "Missing argument seperator (`,`)"));
                };

                argument
            } else {
                ident_to_expr(syn::Ident::new(arg_name, format_str.span()))
            };

            current = rest;

            str_base_len += text.len();
            pieces.push(Piece::Literal(format_string.slice_ref(text)));
            pieces.push(Piece::Argument {
                expr: arg_expr,
                ident: syn::Ident::new(&format!("arg_{arg_num}"), Span::call_site()),
            });
        }

        Ok(Self {
            str_base_len,
            pieces,
        })
    }
}

struct FormatIntoArguments {
    write_into: syn::Ident,
    arguments: Arguments,
}

impl syn::parse::Parse for FormatIntoArguments {
    fn parse(input: syn::parse::ParseStream<'_>) -> Result<Self> {
        let write_into = input.parse()?;
        input.parse::<Token![,]>()?;

        let arguments = Arguments::parse(input)?;

        Ok(Self {
            write_into,
            arguments,
        })
    }
}

/// A no-alloc version of [`format!`], producing an [`ArrayString`].
///
/// ## Usage
/// Usage is similar to `format!`, although there are multiple limitations:
/// - No support for formatting flags, as we are not reinventing all of `format!`.
/// - No support for `format!("{name}", name=username)` syntax, may be lifted in future.
///
// Workaround for a rustdoc bug.
/// [`format!`]: https://doc.rust-lang.org/stable/std/macro.format.html
/// [`ArrayString`]: https://docs.rs/arrayvec/latest/arrayvec/struct.ArrayString.html
#[proc_macro]
pub fn aformat(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let Arguments {
        str_base_len,
        pieces,
    } = match syn::parse(tokens) {
        Ok(args) => args,
        Err(err) => return err.into_compile_error().into(),
    };

    let caller_arguments = pieces.iter().filter_map(Piece::as_expr);
    let [arguments_iter_1, arguments_iter_2] =
        array::from_fn(|_| pieces.iter().filter_map(Piece::as_ident));

    let argument_count = arguments_iter_1.count();
    let [const_args_1, const_args_2, const_args_3, const_args_4, const_args_5] =
        array::from_fn(|_| (0..argument_count).map(|i| format_ident!("N{i}")));

    let return_adder = const_args_1.fold(
        quote!(StrBaseLen),
        |current, ident| quote!(RunAdd<#current, U<#ident>>),
    );

    let return_close_angle_braces = (0..argument_count).map(|_| <Token![>]>::default());

    quote!({
        use ::aformat::{ArrayString, ToArrayString, __internal::*};

        #[allow(clippy::too_many_arguments)]
        fn aformat_inner<StrBaseLen, #(const #const_args_2: usize),*>(
            #(#arguments_iter_2: ArrayString<#const_args_3>),*
        ) -> RunTypeToArrayString<#return_adder>
        where
            Const<#str_base_len>: ToUInt<Output = StrBaseLen>,
            #(Const<#const_args_4>: ToUInt,)*
            StrBaseLen: #(Add<U<#const_args_5>, Output: )* TypeNumToArrayString #(#return_close_angle_braces)*
        {
            let mut out = ArrayStringLike::new();
            // Fixes type inferrence
            if false { return out; }

            #(#pieces)*
            out
        }

        aformat_inner(#(ToArrayString::to_arraystring(#caller_arguments)),*)
    })
    .into()
}

/// [`aformat!`], but you provide your own [`ArrayString`].
///
/// The length of the [`ArrayString`] is checked at compile-time to fit all the arguments, although is not checked to be optimal.
///
/// ## Usage
/// The first argument should be the identifier of the [`ArrayString`], then the normal [`aformat!`] arguments follow.
///
/// ## Examples
/// ```
/// let mut out_buf = ArrayString::<32>::new();
///
/// let age = 18_u8;
/// aformat_into!(out_buf, "You are {} years old!", age);
///
/// assert_eq!(out_buf.as_str(), "You are 18 years old!");
/// ```
///
/// ```compile_fail
/// // Buffer is too small, so compile failure!
/// let mut out_buf = ArrayString::<4>::new();
///
/// let age = 18_u8;
/// aformat_into!(out_buf, "You are {} years old!", age);
/// ```
///
// Workaround for a rustdoc bug.
/// [`ArrayString`]: https://docs.rs/arrayvec/latest/arrayvec/struct.ArrayString.html
#[proc_macro]
pub fn aformat_into(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let FormatIntoArguments {
        arguments: Arguments {
            str_base_len,
            pieces,
        },
        write_into,
    } = match syn::parse(tokens) {
        Ok(args) => args,
        Err(err) => return err.into_compile_error().into(),
    };

    let caller_arguments = pieces.iter().filter_map(Piece::as_expr);
    let [arguments_iter_1, arguments_iter_2] =
        array::from_fn(|_| pieces.iter().filter_map(Piece::as_ident));

    let argument_count = arguments_iter_1.clone().count();
    let [const_args_1, const_args_2, const_args_3, const_args_4] =
        array::from_fn(|_| (0..argument_count).map(|i| format_ident!("N{i}")));

    let return_close_angle_braces = (0..argument_count).map(|_| <Token![>]>::default());

    quote!({
        use ::aformat::{ArrayString, ToArrayString, __internal::*};

        #[allow(clippy::too_many_arguments)]
        fn aformat_into_inner<StrBaseLen, const OUT: usize, #(const #const_args_2: usize),*>(
            out: &mut ArrayString<OUT>,
            #(#arguments_iter_2: ArrayString<#const_args_3>),*
        )
        where
            Const<#str_base_len>: ToUInt<Output = StrBaseLen>,
            Const<OUT>: ToUInt,
            #(Const<#const_args_4>: ToUInt,)*

            StrBaseLen: #(Add<U<#const_args_1>, Output: )* IsLessOrEqual<U<OUT>, Output: BufferFits> #(#return_close_angle_braces)*
        {
            #(#pieces)*
        }

        aformat_into_inner(&mut #write_into, #(ToArrayString::to_arraystring(#caller_arguments)),*)
    })
    .into()
}