pub struct DefaultContext<A, T, C>{ /* private fields */ }Expand description
The default context which uses an allocator to track the location of errors.
This is typically constructed using new and by default uses the
System allocator to allocate memory. To customized the allocator to use
new_in can be used during construction.
The default constructor is only available when the alloc feature is
enabled, and will use the System allocator.
Implementations§
Source§impl<A, T, C> DefaultContext<A, T, C>
impl<A, T, C> DefaultContext<A, T, C>
Sourcepub fn with_trace(self) -> DefaultContext<A, Trace, C>
pub fn with_trace(self) -> DefaultContext<A, Trace, C>
Sourcepub fn with_capture<E>(self) -> DefaultContext<A, T, Capture<E>>where
E: ContextError<A>,
pub fn with_capture<E>(self) -> DefaultContext<A, T, Capture<E>>where
E: ContextError<A>,
Capture the specified error type.
This gives access to the last captured error through
DefaultContext::unwrap and DefaultContext::result.
Capturing instead of forwarding the error might be beneficial if the error type is large.
§Examples
use musli::{Decode, Encode};
use musli::alloc::System;
use musli::context;
use musli::json::{Encoding, Error};
const ENCODING: Encoding = Encoding::new();
#[derive(Decode, Encode)]
struct Person {
name: String,
age: u32,
}
let cx = context::new().with_capture::<Error>();
let mut data = Vec::new();
ENCODING.encode_with(&cx, &mut data, &Person {
name: "Aristotle".to_string(),
age: 61,
})?;
assert!(cx.result().is_ok());
let _: Result<Person, _> = ENCODING.from_slice_with(&cx, &data[..data.len() - 2]);
assert!(cx.result().is_err());
Ok::<_, musli::context::ErrorMarker>(())Sourcepub fn with_error<E>(self) -> DefaultContext<A, T, Emit<E>>where
E: ContextError<A>,
pub fn with_error<E>(self) -> DefaultContext<A, T, Emit<E>>where
E: ContextError<A>,
Emit the specified error type E.
This causes the method receiving the context to return the specified
error type directly instead through Context::Error.
§Examples
use musli::{Decode, Encode};
use musli::alloc::System;
use musli::context;
use musli::json::{Encoding, Error};
const ENCODING: Encoding = Encoding::new();
#[derive(Decode, Encode)]
struct Person {
name: String,
age: u32,
}
let cx = context::new().with_error();
let mut data = Vec::new();
ENCODING.encode_with(&cx, &mut data, &Person {
name: "Aristotle".to_string(),
age: 61,
})?;
let person: Person = ENCODING.from_slice_with(&cx, &data[..])?;
assert_eq!(person.name, "Aristotle");
assert_eq!(person.age, 61);
Ok::<_, Error>(())Source§impl<A, C> DefaultContext<A, Trace, C>where
A: Allocator,
impl<A, C> DefaultContext<A, Trace, C>where
A: Allocator,
Sourcepub fn with_type(self) -> Self
pub fn with_type(self) -> Self
If tracing is enabled through DefaultContext::with_trace, this
configured the context to visualize type information, and not just
variant and fields.
§Examples
use musli::context;
let cx = context::new().with_trace().with_type();Sourcepub fn report(&self) -> Report<'_, A>
pub fn report(&self) -> Report<'_, A>
Generate a line-separated report of all reported errors.
This can be useful if you want a quick human-readable overview of errors. The line separator used will be platform dependent.
§Examples
use musli::context::{self, ErrorMarker};
use musli::value::Value;
use musli::json::Encoding;
const ENCODING: Encoding = Encoding::new();
let cx = context::new().with_trace();
let ErrorMarker = ENCODING.from_str_with::<_, Value<_>>(&cx, "not json").unwrap_err();
let report = cx.report();
println!("{report}");Sourcepub fn errors(&self) -> Errors<'_, A> ⓘ
pub fn errors(&self) -> Errors<'_, A> ⓘ
Iterate over all reported errors.
§Examples
use musli::context::{self, ErrorMarker};
use musli::value::Value;
use musli::json::Encoding;
const ENCODING: Encoding = Encoding::new();
let cx = context::new().with_trace();
let ErrorMarker = ENCODING.from_str_with::<_, Value<_>>(&cx, "not json").unwrap_err();
assert!(cx.errors().count() > 0);Trait Implementations§
Source§impl<A, T, C> Context for &DefaultContext<A, T, C>
impl<A, T, C> Context for &DefaultContext<A, T, C>
Source§fn custom<E>(self, message: E) -> Self::Error
fn custom<E>(self, message: E) -> Self::Error
Source§fn message<M>(self, message: M) -> Self::Errorwhere
M: Display,
fn message<M>(self, message: M) -> Self::Errorwhere
M: Display,
Source§fn message_at<M>(self, mark: &Self::Mark, message: M) -> Self::Errorwhere
M: Display,
fn message_at<M>(self, mark: &Self::Mark, message: M) -> Self::Errorwhere
M: Display,
Source§fn custom_at<E>(self, mark: &Self::Mark, message: E) -> Self::Error
fn custom_at<E>(self, mark: &Self::Mark, message: E) -> Self::Error
Source§fn mark(self) -> Self::Mark
fn mark(self) -> Self::Mark
Source§fn enter_named_field<F>(self, name: &'static str, field: F)where
F: Display,
fn enter_named_field<F>(self, name: &'static str, field: F)where
F: Display,
Source§fn enter_unnamed_field<F>(self, index: u32, name: F)where
F: Display,
fn enter_unnamed_field<F>(self, index: u32, name: F)where
F: Display,
Source§fn leave_field(self)
fn leave_field(self)
Source§fn enter_struct(self, name: &'static str)
fn enter_struct(self, name: &'static str)
name. Read moreSource§fn leave_struct(self)
fn leave_struct(self)
Source§fn enter_enum(self, name: &'static str)
fn enter_enum(self, name: &'static str)
name. Read more