/
alloc_context.rs
240 lines (208 loc) · 5.69 KB
/
alloc_context.rs
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
use core::fmt;
use core::marker::PhantomData;
use core::ops::Range;
use core::ptr;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use musli::de;
use musli::error::Error;
use musli::Context;
use crate::context::rich_error::{RichError, Step};
/// Buffer used in combination with [`AllocContext`].
///
/// This can be safely re-used.
#[derive(Default)]
pub struct AllocBuf {
string: String,
}
impl AllocBuf {
/// De-allocate the underlying buffer, this can be useful if you're dealing
/// with infrequent decodings which uses large keys and you don't want the
/// memory to keep being reserved.
pub fn free(&mut self) {
self.string = String::new();
}
}
/// A rich context which uses allocations and tracks the exact location of every
/// error.
pub struct AllocContext<'buf, E> {
mark: usize,
buf: ptr::NonNull<AllocBuf>,
errors: Vec<(Vec<Step<String>>, Range<usize>, E)>,
path: Vec<Step<String>>,
include_type: bool,
_marker: PhantomData<(&'buf mut AllocBuf, E)>,
}
impl<'buf, E> AllocContext<'buf, E> {
/// Construct a new context which uses allocations to store arbitrary
/// amounts of diagnostics about decoding.
///
/// Or at least until we run out of memory.
pub fn new(buf: &'buf mut AllocBuf) -> Self {
Self {
mark: 0,
buf: buf.into(),
errors: Vec::new(),
path: Vec::new(),
include_type: false,
_marker: PhantomData,
}
}
/// Configure the context to visualize type information, and not just
/// variant and fields.
pub fn include_type(&mut self) -> &mut Self {
self.include_type = true;
self
}
/// Iterate over all collected errors.
pub fn iter(&self) -> impl Iterator<Item = RichError<'_, String, E>> {
self.errors
.iter()
.map(|(path, range, error)| RichError::new(path, 0, range.clone(), error))
}
}
impl<'buf, E> Context<'buf> for AllocContext<'buf, E>
where
E: Error,
{
type Input = E;
type Error = de::Error;
type Mark = usize;
#[inline(always)]
fn report<T>(&mut self, error: T) -> Self::Error
where
E: From<T>,
{
self.errors
.push((self.path.clone(), self.mark..self.mark, E::from(error)));
de::Error
}
#[inline]
fn marked_report<T>(&mut self, mark: Self::Mark, message: T) -> Self::Error
where
E: From<T>,
{
self.errors
.push((self.path.clone(), mark..self.mark, E::from(message)));
de::Error
}
#[inline(always)]
fn custom<T>(&mut self, message: T) -> Self::Error
where
T: 'static + Send + Sync + fmt::Display + fmt::Debug,
{
self.errors
.push((self.path.clone(), self.mark..self.mark, E::custom(message)));
de::Error
}
#[inline(always)]
fn message<T>(&mut self, message: T) -> Self::Error
where
T: fmt::Display,
{
self.errors
.push((self.path.clone(), self.mark..self.mark, E::message(message)));
de::Error
}
#[inline]
fn marked_message<T>(&mut self, mark: Self::Mark, message: T) -> Self::Error
where
T: fmt::Display,
{
self.errors
.push((self.path.clone(), mark..self.mark, E::message(message)));
de::Error
}
#[inline]
fn mark(&mut self) -> Self::Mark {
self.mark
}
#[inline]
fn advance(&mut self, n: usize) {
self.mark = self.mark.wrapping_add(n);
}
#[inline(always)]
fn store_string(&mut self, s: &str) {
// SAFETY: we're holding onto a mutable reference to the string so it
// must be live for the duration of the context.
let buf = unsafe { self.buf.as_mut() };
buf.string.clear();
buf.string.push_str(s);
}
#[inline(always)]
fn get_string<'a>(&self) -> Option<&'buf str> {
// SAFETY: we're holding onto a mutable reference to the string so it
// must be live for the duration of the context.
let buf = unsafe { self.buf.as_ref() };
Some(&buf.string)
}
#[inline]
fn enter_named_field<T>(&mut self, name: &'static str, _: T)
where
T: fmt::Display,
{
self.path.push(Step::Named(name));
}
#[inline]
fn enter_unnamed_field<T>(&mut self, index: u32, _: T)
where
T: fmt::Display,
{
self.path.push(Step::Unnamed(index));
}
#[inline]
fn leave_field(&mut self) {
self.path.pop();
}
#[inline]
fn enter_struct(&mut self, name: &'static str) {
if self.include_type {
self.path.push(Step::Struct(name));
}
}
#[inline]
fn leave_struct(&mut self) {
if self.include_type {
self.path.pop();
}
}
#[inline]
fn enter_enum(&mut self, name: &'static str) {
if self.include_type {
self.path.push(Step::Enum(name));
}
}
#[inline]
fn leave_enum(&mut self) {
if self.include_type {
self.path.pop();
}
}
#[inline]
fn enter_variant<T>(&mut self, name: &'static str, _: T) {
self.path.push(Step::Variant(name));
}
#[inline]
fn leave_variant(&mut self) {
self.path.pop();
}
#[inline]
fn enter_sequence_index(&mut self, index: usize) {
self.path.push(Step::Index(index));
}
#[inline]
fn leave_sequence_index(&mut self) {
self.path.pop();
}
#[inline]
fn enter_map_key<T>(&mut self, field: T)
where
T: fmt::Display,
{
self.path.push(Step::Key(field.to_string()));
}
#[inline]
fn leave_map_key(&mut self) {
self.path.pop();
}
}