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
|
use crate::attr::Display;
use proc_macro2::TokenStream;
use quote::quote_spanned;
use syn::{Ident, LitStr};
macro_rules! peek_next {
($read:ident) => {
match $read.chars().next() {
Some(next) => next,
None => return,
}
};
}
impl Display {
// Transform `"error {var}"` to `"error {}", var`.
pub(crate) fn expand_shorthand(&mut self) {
let span = self.fmt.span();
let fmt = self.fmt.value();
let mut read = fmt.as_str();
let mut out = String::new();
let mut args = TokenStream::new();
while let Some(brace) = read.find('{') {
out += &read[..=brace];
read = &read[brace + 1..];
// skip cases where we find a {{
if read.starts_with('{') {
out.push('{');
read = &read[1..];
continue;
}
let next = peek_next!(read);
let var = match next {
'0'..='9' => take_int(&mut read),
'a'..='z' | 'A'..='Z' | '_' => take_ident(&mut read),
_ => return,
};
let ident = Ident::new(&var, span);
let next = peek_next!(read);
let arg = if cfg!(feature = "std") && next == '}' {
quote_spanned!(span=> , #ident.__displaydoc_display())
} else {
quote_spanned!(span=> , #ident)
};
args.extend(arg);
}
out += read;
self.fmt = LitStr::new(&out, self.fmt.span());
self.args = args;
}
}
fn take_int(read: &mut &str) -> String {
let mut int = String::new();
int.push('_');
for (i, ch) in read.char_indices() {
match ch {
'0'..='9' => int.push(ch),
_ => {
*read = &read[i..];
break;
}
}
}
int
}
fn take_ident(read: &mut &str) -> String {
let mut ident = String::new();
for (i, ch) in read.char_indices() {
match ch {
'a'..='z' | 'A'..='Z' | '0'..='9' | '_' => ident.push(ch),
_ => {
*read = &read[i..];
break;
}
}
}
ident
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
use proc_macro2::Span;
fn assert(input: &str, fmt: &str, args: &str) {
let mut display = Display {
fmt: LitStr::new(input, Span::call_site()),
args: TokenStream::new(),
};
display.expand_shorthand();
assert_eq!(fmt, display.fmt.value());
assert_eq!(args, display.args.to_string());
}
#[test]
fn test_expand() {
assert("fn main() {{ }}", "fn main() {{ }}", "");
}
#[test]
#[cfg_attr(not(feature = "std"), ignore)]
fn test_std_expand() {
assert(
"{v} {v:?} {0} {0:?}",
"{} {:?} {} {:?}",
", v . __displaydoc_display () , v , _0 . __displaydoc_display () , _0",
);
assert("error {var}", "error {}", ", var . __displaydoc_display ()");
assert(
"error {var1}",
"error {}",
", var1 . __displaydoc_display ()",
);
assert(
"error {var1var}",
"error {}",
", var1var . __displaydoc_display ()",
);
assert(
"The path {0}",
"The path {}",
", _0 . __displaydoc_display ()",
);
assert("The path {0:?}", "The path {:?}", ", _0");
}
#[test]
#[cfg_attr(feature = "std", ignore)]
fn test_nostd_expand() {
assert(
"{v} {v:?} {0} {0:?}",
"{} {:?} {} {:?}",
", v , v , _0 , _0",
);
assert("error {var}", "error {}", ", var");
assert("The path {0}", "The path {}", ", _0");
assert("The path {0:?}", "The path {:?}", ", _0");
assert("error {var1}", "error {}", ", var1");
assert("error {var1var}", "error {}", ", var1var");
}
}
|