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
|
use crate::parser::Segment;
use proc_macro2::TokenStream;
use quote::quote;
use syn::Expr;
use syn::punctuated::Punctuated;
/// Returns true if the string contains any format placeholders (`{}`, `{name}`, `{0}`, `{:<10}`, etc.)
/// but not escaped braces `{{` or `}}`.
fn has_placeholders(s: &str) -> bool {
let mut chars = s.chars().peekable();
while let Some(ch) = chars.next() {
if ch == '{' {
match chars.peek() {
Some('{') => {
chars.next(); // skip escaped
}
_ => return true,
}
} else if ch == '}' && chars.peek() == Some(&'}') {
chars.next(); // skip escaped
}
}
false
}
/// Count implicit positional placeholders (`{}` and `{:spec}`) in a format string.
/// Named (`{name}`) and numbered (`{0}`) placeholders are NOT counted
/// since they don't consume positional arguments.
fn count_positional_placeholders(s: &str) -> usize {
let mut count = 0;
let mut chars = s.chars().peekable();
while let Some(ch) = chars.next() {
if ch == '{' {
match chars.peek() {
Some('{') => {
chars.next(); // escaped
}
Some('}') => {
// `{}` — implicit positional
count += 1;
chars.next();
}
Some(':') => {
// `{:spec}` — implicit positional with format spec
count += 1;
for c in chars.by_ref() {
if c == '}' {
break;
}
}
}
Some(c) if c.is_ascii_digit() => {
// `{0}`, `{0:spec}` — explicit positional, skip
for c in chars.by_ref() {
if c == '}' {
break;
}
}
}
_ => {
// `{name}` or `{name:spec}` — named, skip
for c in chars.by_ref() {
if c == '}' {
break;
}
}
}
}
} else if ch == '}' && chars.peek() == Some(&'}') {
chars.next();
}
}
count
}
pub fn generate(
segments: &[Segment],
extra_args: &Punctuated<Expr, syn::Token![,]>,
span: proc_macro2::Span,
) -> TokenStream {
// Single segment: pass all extra args
if segments.len() == 1 {
return generate_single(&segments[0], extra_args, span);
}
// Multiple segments: distribute positional args across segments
let mut pos = 0usize;
let mut seg_bindings = Vec::new();
let mut seg_idents = Vec::new();
for (i, segment) in segments.iter().enumerate() {
let content = segment_content(segment);
let n = count_positional_placeholders(content);
let end = (pos + n).min(extra_args.len());
let slice: Punctuated<Expr, syn::Token![,]> = extra_args
.iter()
.skip(pos)
.take(end - pos)
.cloned()
.collect();
pos = end;
let ident = quote::format_ident!("__seg{}", i);
let expr = generate_single(segment, &slice, span);
seg_bindings.push(quote! { let #ident = #expr; });
seg_idents.push(ident);
}
// Build a format string with one `{}` per segment
let fmt_str = seg_idents.iter().map(|_| "{}").collect::<Vec<_>>().join("");
quote! {
{
#(#seg_bindings)*
::std::format!(#fmt_str, #(#seg_idents),*)
}
}
}
fn segment_content(segment: &Segment) -> &str {
match segment {
Segment::Plain(s) => s,
Segment::Tagged { content, .. } => content,
}
}
fn spanned_lit(s: &str, span: proc_macro2::Span) -> proc_macro2::Literal {
let mut lit = proc_macro2::Literal::string(s);
lit.set_span(span);
lit
}
fn generate_single(
segment: &Segment,
args: &Punctuated<Expr, syn::Token![,]>,
span: proc_macro2::Span,
) -> TokenStream {
match segment {
Segment::Plain(text) => {
if has_placeholders(text) {
let lit = spanned_lit(text, span);
quote! { ::std::format!(#lit, #args) }
} else {
quote! { ::std::string::String::from(#text) }
}
}
Segment::Tagged { tag, content } => {
let func = quote::format_ident!("__format_{}_message", tag);
if has_placeholders(content) {
let lit = spanned_lit(content, span);
quote! {
::std::string::ToString::to_string(
&::mozart_core::console::#func(&::std::format!(#lit, #args))
)
}
} else {
quote! {
::std::string::ToString::to_string(
&::mozart_core::console::#func(#content)
)
}
}
}
}
}
|