musli_core/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 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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
//! Things related to working with contexts.
use core::error::Error;
use core::fmt;
use core::str;
use crate::alloc::Allocator;
use crate::de::{DecodeBytes, DecodeUnsized, DecodeUnsizedBytes};
use crate::{Decode, Decoder};
/// Provides ergonomic access to the serialization context.
///
/// This is used to among other things report diagnostics.
pub trait Context {
/// Mode of the context.
type Mode: 'static;
/// Error produced by context.
type Error: 'static;
/// A mark during processing.
type Mark: Copy + Default;
/// The allocator associated with the context.
type Allocator: ?Sized + Allocator;
/// An allocated buffer containing a valid string.
type String<'this>: AsRef<str>
where
Self: 'this;
/// Clear the state of the context, allowing it to be re-used.
fn clear(&self);
/// Decode the given input using the associated mode.
#[inline]
fn decode<'de, T, D>(&self, decoder: D) -> Result<T, Self::Error>
where
T: Decode<'de, Self::Mode>,
D: Decoder<'de, Cx = Self, Mode = Self::Mode, Error = Self::Error>,
{
T::decode(self, decoder)
}
/// Decode the given unsized value using the associated mode.
#[inline]
fn decode_unsized<'de, T, D, F, O>(&self, decoder: D, f: F) -> Result<O, Self::Error>
where
T: ?Sized + DecodeUnsized<'de, Self::Mode>,
D: Decoder<'de, Cx = Self, Mode = Self::Mode, Error = Self::Error>,
F: FnOnce(&T) -> Result<O, D::Error>,
{
T::decode_unsized(self, decoder, f)
}
/// Decode the given input as bytes using the associated mode.
fn decode_bytes<'de, T, D>(&self, decoder: D) -> Result<T, Self::Error>
where
T: DecodeBytes<'de, Self::Mode>,
D: Decoder<'de, Cx = Self, Mode = Self::Mode, Error = Self::Error>,
{
T::decode_bytes(self, decoder)
}
/// Decode the given unsized value as bytes using the associated mode.
#[inline]
fn decode_unsized_bytes<'de, T, D, F, O>(&self, decoder: D, f: F) -> Result<O, Self::Error>
where
T: ?Sized + DecodeUnsizedBytes<'de, Self::Mode>,
D: Decoder<'de, Cx = Self, Mode = Self::Mode, Error = Self::Error>,
F: FnOnce(&T) -> Result<O, D::Error>,
{
T::decode_unsized_bytes(self, decoder, f)
}
/// Access the underlying allocator.
fn alloc(&self) -> &Self::Allocator;
/// Collect and allocate a string from a [`Display`] implementation.
///
/// [`Display`]: fmt::Display
fn collect_string<T>(&self, value: &T) -> Result<Self::String<'_>, Self::Error>
where
T: ?Sized + fmt::Display;
/// Generate a map function which maps an error using the `custom` function.
#[inline]
fn map<T>(&self) -> impl FnOnce(T) -> Self::Error + '_
where
T: 'static + Send + Sync + Error,
{
move |error| self.custom(error)
}
/// Report a custom error, which is not encapsulated by the error type
/// expected by the context. This is essentially a type-erased way of
/// reporting error-like things out from the context.
fn custom<T>(&self, error: T) -> Self::Error
where
T: 'static + Send + Sync + Error;
/// Generate a map function which maps an error using the `message` function.
#[inline]
fn map_message<T>(&self) -> impl FnOnce(T) -> Self::Error + '_
where
T: fmt::Display,
{
move |error| self.message(error)
}
/// Report a message as an error.
///
/// This is made available to format custom error messages in `no_std`
/// environments. The error message is to be collected by formatting `T`.
fn message<T>(&self, message: T) -> Self::Error
where
T: fmt::Display;
/// Report an error based on a mark.
///
/// A mark is generated using [Context::mark] and indicates a prior state.
#[allow(unused_variables)]
#[inline(always)]
fn marked_message<T>(&self, mark: Self::Mark, message: T) -> Self::Error
where
T: fmt::Display,
{
self.message(message)
}
/// Report an error based on a mark.
///
/// A mark is generated using [Context::mark] and indicates a prior state.
#[allow(unused_variables)]
#[inline(always)]
fn marked_custom<T>(&self, mark: Self::Mark, message: T) -> Self::Error
where
T: 'static + Send + Sync + Error,
{
self.custom(message)
}
/// Advance the context by `n` bytes of input.
///
/// This is typically used to move the mark forward as produced by
/// [Context::mark].
#[allow(unused_variables)]
#[inline(always)]
fn advance(&self, n: usize) {}
/// Return a mark which acts as a checkpoint at the current encoding state.
///
/// The context is in a privileged state in that it sees everything, so a
/// mark can be quite useful for determining the context of an error.
///
/// This typically indicates a byte offset, and is used by
/// [`marked_message`][Context::marked_message] to report a spanned error.
#[inline(always)]
fn mark(&self) -> Self::Mark {
Self::Mark::default()
}
/// Report that an invalid variant tag was encountered.
#[inline(always)]
fn invalid_variant_tag<T>(&self, _: &'static str, tag: &T) -> Self::Error
where
T: ?Sized + fmt::Debug,
{
self.message(format_args!("Invalid variant tag {tag:?}"))
}
/// The value for the given tag could not be collected.
#[inline(always)]
fn expected_tag<T>(&self, _: &'static str, tag: &T) -> Self::Error
where
T: ?Sized + fmt::Debug,
{
self.message(format_args!("Expected tag: {tag:?}"))
}
/// Trying to decode an uninhabitable type.
#[inline(always)]
fn uninhabitable(&self, _: &'static str) -> Self::Error {
self.message(format_args!("Cannot decode uninhabitable types"))
}
/// Encountered an unsupported field tag.
#[inline(always)]
fn invalid_field_tag<T>(&self, _: &'static str, tag: &T) -> Self::Error
where
T: ?Sized + fmt::Debug,
{
self.message(format_args!("Invalid field tag {tag:?}"))
}
/// Expected another field to decode.
#[inline(always)]
fn expected_field_adjacent<T, C>(&self, _: &'static str, tag: &T, content: &C) -> Self::Error
where
T: ?Sized + fmt::Debug,
C: ?Sized + fmt::Debug,
{
self.message(format_args!(
"Expected adjacent field {tag:?} or {content:?}"
))
}
/// Missing adjacent tag when decoding.
#[inline(always)]
fn missing_adjacent_tag<T>(&self, _: &'static str, tag: &T) -> Self::Error
where
T: ?Sized + fmt::Debug,
{
self.message(format_args!("Missing adjacent tag {tag:?}"))
}
/// Encountered an unsupported field tag.
#[inline(always)]
fn invalid_field_string_tag(&self, _: &'static str, field: Self::String<'_>) -> Self::Error {
let field = field.as_ref();
self.message(format_args!("Invalid field tag `{field}`"))
}
/// Missing variant field required to decode.
#[allow(unused_variables)]
#[inline(always)]
fn missing_variant_field<T>(&self, name: &'static str, tag: &T) -> Self::Error
where
T: ?Sized + fmt::Debug,
{
self.message(format_args!("Missing variant field: {tag:?}"))
}
/// Indicate that a variant tag could not be determined.
#[allow(unused_variables)]
#[inline(always)]
fn missing_variant_tag(&self, name: &'static str) -> Self::Error {
self.message(format_args!("Missing variant tag"))
}
/// Encountered an unsupported variant field.
#[allow(unused_variables)]
#[inline(always)]
fn invalid_variant_field_tag<V, T>(
&self,
name: &'static str,
variant: &V,
tag: &T,
) -> Self::Error
where
V: ?Sized + fmt::Debug,
T: ?Sized + fmt::Debug,
{
self.message(format_args!(
"Invalid variant field tag `{tag:?}` for variant `{variant:?}`",
))
}
/// Missing variant field required to decode.
#[allow(unused_variables)]
#[inline(always)]
fn alloc_failed(&self) -> Self::Error {
self.message("Failed to allocate")
}
/// Indicate that we've entered a struct with the given `name`.
///
/// The `name` variable corresponds to the identifiers of the struct.
///
/// This will be matched with a corresponding call to [`leave_struct`].
///
/// [`leave_struct`]: Context::leave_struct
#[allow(unused_variables)]
#[inline(always)]
fn enter_struct(&self, name: &'static str) {}
/// Trace that we've left the last struct that was entered.
#[inline(always)]
fn leave_struct(&self) {}
/// Indicate that we've entered an enum with the given `name`.
///
/// The `name` variable corresponds to the identifiers of the enum.
///
/// This will be matched with a corresponding call to [`leave_enum`].
///
/// [`leave_enum`]: Context::leave_enum
#[allow(unused_variables)]
#[inline(always)]
fn enter_enum(&self, name: &'static str) {}
/// Trace that we've left the last enum that was entered.
#[inline(always)]
fn leave_enum(&self) {}
/// Trace that we've entered the given named field.
///
/// A named field is part of a regular struct, where the literal field name
/// is the `name` argument below, and the musli tag being used for the field
/// is the second argument.
///
/// This will be matched with a corresponding call to [`leave_field`].
///
/// Here `name` is `"field"` and `tag` is `"string"`.
///
/// ```
/// use musli::{Decode, Encode};
///
/// #[derive(Decode, Encode)]
/// #[musli(name_all = "name")]
/// struct Struct {
/// #[musli(name = "string")]
/// field: String,
/// }
/// ```
///
/// [`leave_field`]: Context::leave_field
#[allow(unused_variables)]
#[inline(always)]
fn enter_named_field<T>(&self, name: &'static str, tag: &T)
where
T: ?Sized + fmt::Display,
{
}
/// Trace that we've entered the given unnamed field.
///
/// An unnamed field is part of a tuple struct, where the field index is the
/// `index` argument below, and the musli tag being used for the field is
/// the second argument.
///
/// This will be matched with a corresponding call to [`leave_field`].
///
/// Here `index` is `0` and `name` is `"string"`.
///
/// ```
/// use musli::{Decode, Encode};
///
/// #[derive(Decode, Encode)]
/// #[musli(name_all = "name")]
/// struct Struct(#[musli(name = "string")] String);
/// ```
///
/// [`leave_field`]: Context::leave_field
#[allow(unused_variables)]
#[inline(always)]
fn enter_unnamed_field<T>(&self, index: u32, name: &T)
where
T: ?Sized + fmt::Display,
{
}
/// Trace that we've left the last field that was entered.
///
/// The `marker` argument will be the same as the one returned from
/// [`enter_named_field`] or [`enter_unnamed_field`].
///
/// [`enter_named_field`]: Context::enter_named_field
/// [`enter_unnamed_field`]: Context::enter_unnamed_field
#[allow(unused_variables)]
#[inline(always)]
fn leave_field(&self) {}
/// Trace that we've entered the given variant in an enum.
///
/// A named variant is part of an enum, where the literal variant name is
/// the `name` argument below, and the musli tag being used to decode the
/// variant is the second argument.
///
/// This will be matched with a corresponding call to
/// [`leave_variant`] with the same marker provided as an argument as
/// the one returned here.
///
/// Here `name` is `"field"` and `tag` is `"string"`.
///
/// ```
/// use musli::{Decode, Encode};
///
/// #[derive(Decode, Encode)]
/// #[musli(name_all = "name")]
/// struct Struct {
/// #[musli(name = "string")]
/// field: String,
/// }
/// ```
///
/// [`leave_variant`]: Context::leave_variant
#[allow(unused_variables)]
#[inline(always)]
fn enter_variant<T>(&self, name: &'static str, tag: T)
where
T: fmt::Display,
{
}
/// Trace that we've left the last variant that was entered.
///
/// The `marker` argument will be the same as the one returned from
/// [`enter_variant`].
///
/// [`enter_variant`]: Context::enter_variant
#[allow(unused_variables)]
#[inline(always)]
fn leave_variant(&self) {}
/// Trace a that a map key has been entered.
#[allow(unused_variables)]
#[inline(always)]
fn enter_map_key<T>(&self, field: T)
where
T: fmt::Display,
{
}
/// Trace that we've left the last map field that was entered.
///
/// The `marker` argument will be the same as the one returned from
/// [`enter_map_key`].
///
/// [`enter_map_key`]: Context::enter_map_key
#[allow(unused_variables)]
#[inline(always)]
fn leave_map_key(&self) {}
/// Trace a sequence field.
#[allow(unused_variables)]
#[inline(always)]
fn enter_sequence_index(&self, index: usize) {}
/// Trace that we've left the last sequence index that was entered.
///
/// The `marker` argument will be the same as the one returned from
/// [`enter_sequence_index`].
///
/// [`enter_sequence_index`]: Context::enter_sequence_index
#[allow(unused_variables)]
#[inline(always)]
fn leave_sequence_index(&self) {}
}