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
use core::fmt;
use crate::{parse::ParseSegment, FormatArgs, FormatError, FormatKey, ToFormatParser};
pub struct ParsedFmt<'a> {
segments: tinyvec::TinyVec<[ParseSegment<'a>; 8]>,
}
impl<'a> ToFormatParser<'a> for ParsedFmt<'a> {
type Parser = std::iter::Copied<std::slice::Iter<'a, ParseSegment<'a>>>;
fn to_parser(&'a self) -> Self::Parser {
self.segments.iter().copied()
}
fn unparsed(_: Self::Parser) -> &'a str {
""
}
}
impl fmt::Debug for ParsedFmt<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CompiledFormatter")
.field("segments", &self.segments)
.finish()
}
}
impl<'a> ParsedFmt<'a> {
pub fn new(s: &'a str) -> Result<Self, FormatError<'a>> {
let mut segments = s.to_parser();
let this = Self {
segments: segments.by_ref().collect(),
};
if !segments.s.is_empty() {
Err(FormatError::Parse(segments.s))
} else {
Ok(this)
}
}
pub fn keys(&self) -> impl Iterator<Item = &'_ str> {
self.segments.iter().filter_map(|segment| match segment {
ParseSegment::Literal(_) => None,
ParseSegment::Key(key) => Some(*key),
})
}
pub fn with_args<'fs, 'fk, F: FormatKey>(
&'fs self,
fmt: &'fk F,
) -> FormatArgs<'fs, 'fk, Self, F> {
FormatArgs::new(self, fmt)
}
}
impl<'a> TryFrom<&'a str> for ParsedFmt<'a> {
type Error = FormatError<'a>;
fn try_from(value: &'a str) -> Result<Self, Self::Error> {
Self::new(value)
}
}
#[cfg(test)]
mod tests {
use core::fmt;
use crate::{FormatError, FormatKey, FormatKeyError};
use super::ParsedFmt;
struct Message;
impl FormatKey for Message {
fn fmt(&self, key: &str, f: &mut fmt::Formatter<'_>) -> Result<(), FormatKeyError> {
match key {
"recipient" => f.write_str("World").map_err(FormatKeyError::Fmt),
"time_descriptor" => f.write_str("morning").map_err(FormatKeyError::Fmt),
_ => Err(FormatKeyError::UnknownKey),
}
}
}
#[test]
fn compiled_happy_path() {
let formatter =
ParsedFmt::new("Hello, {recipient}. Hope you are having a nice {time_descriptor}.")
.unwrap();
let expected = "Hello, World. Hope you are having a nice morning.";
assert_eq!(formatter.with_args(&Message).to_string(), expected);
}
#[test]
fn compiled_failed_parsing() {
let err =
ParsedFmt::new("Hello, {recipient}. Hope you are having a nice {time_descriptor.")
.unwrap_err();
assert_eq!(err, FormatError::Parse("time_descriptor."));
}
#[test]
fn compiled_keys() {
let parsed =
ParsedFmt::new("Hello, {recipient}. Hope you are having a nice {time_descriptr}.")
.unwrap();
let keys: Vec<_> = parsed.keys().collect();
assert_eq!(keys, ["recipient", "time_descriptr"]);
}
}