Enum Error
#[non_exhaustive]pub enum Error {
Show 27 variants
Parse(String),
ParseWithLocation {
message: String,
location: Location,
},
Serialize(String),
Deserialize(String),
DeserializeWithLocation {
message: String,
location: Location,
},
Io(Error),
Custom(String),
RecursionLimitExceeded {
depth: usize,
},
DuplicateKey(String),
RepetitionLimitExceeded,
Budget(BudgetBreach),
UnknownAnchor(String),
UnknownAnchorAt {
name: String,
location: Location,
suggestion: Option<(String, Location)>,
},
MissingField(String),
UnknownField(String),
ScalarInMergeElement,
SequenceInMergeElement,
TaggedInMerge,
Invalid(String),
TypeMismatch {
expected: &'static str,
found: String,
},
Shared(Arc<Error>),
EndOfStream,
MoreThanOneDocument,
ScalarInMerge,
EmptyTag,
FailedToParseNumber(String),
Message(String, Option<usize>),
}Expand description
§Examples
use noyalib::{from_str, Error, Value};
let err = from_str::<Value>("a: [unclosed").unwrap_err();
assert!(matches!(err, Error::Parse(_) | Error::ParseWithLocation { .. }));Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Parse(String)
ParseWithLocation
Error during YAML parsing with location information.
§Examples
use noyalib::{Error, Location};
let _e = Error::ParseWithLocation {
message: "bad token".into(),
location: Location::from_index("a: [", 3),
};Fields
message: StringThe error message.
§Examples
use noyalib::{Error, Location};
let e = Error::ParseWithLocation {
message: "bad".into(),
location: Location::default(),
};
if let Error::ParseWithLocation { message, .. } = e {
assert_eq!(message, "bad");
}location: LocationThe location in the source where the error occurred.
§Examples
use noyalib::{Error, Location};
let e = Error::ParseWithLocation {
message: "x".into(),
location: Location::from_index("abc", 1),
};
if let Error::ParseWithLocation { location, .. } = e {
assert_eq!(location.column(), 2);
}Serialize(String)
Deserialize(String)
Error during deserialization.
§Examples
let _e = noyalib::Error::Deserialize("type mismatch".into());DeserializeWithLocation
Error during deserialization with location information.
§Examples
use noyalib::{Error, Location};
let _e = Error::DeserializeWithLocation {
message: "expected int".into(),
location: Location::default(),
};Fields
message: StringThe error message.
§Examples
use noyalib::{Error, Location};
let e = Error::DeserializeWithLocation {
message: "m".into(),
location: Location::default(),
};
if let Error::DeserializeWithLocation { message, .. } = e {
assert_eq!(message, "m");
}location: LocationThe location in the source where the error occurred.
§Examples
use noyalib::{Error, Location};
let e = Error::DeserializeWithLocation {
message: "m".into(),
location: Location::from_index("ab", 1),
};
if let Error::DeserializeWithLocation { location, .. } = e {
assert_eq!(location.column(), 2);
}Io(Error)
I/O error (requires std feature).
§Examples
let ioe = std::io::Error::new(std::io::ErrorKind::Other, "nope");
let _e = noyalib::Error::Io(ioe);Custom(String)
RecursionLimitExceeded
Error when recursion depth limit is exceeded.
§Examples
let _e = noyalib::Error::RecursionLimitExceeded { depth: 64 };Fields
DuplicateKey(String)
Error when a duplicate key is encountered.
§Examples
let _e = noyalib::Error::DuplicateKey("name".into());RepetitionLimitExceeded
Repetition limit exceeded (security limit against billion-laughs).
§Examples
let _e = noyalib::Error::RepetitionLimitExceeded;Budget(BudgetBreach)
A configurable parser budget was exceeded.
Carries a [BudgetBreach] identifying which limit fired,
the configured cap, and (where meaningful) the observed
value at the moment the cap tripped. Distinct from the
older Error::RecursionLimitExceeded /
Error::RepetitionLimitExceeded variants — those stay
for backwards compatibility on the depth / alias-expansion
limits; new budgets in the v0.0.2 expansion (max_events,
max_nodes, max_total_scalar_bytes, max_documents,
max_merge_keys, alias_anchor_ratio) all flow through
Error::Budget.
§Examples
use noyalib::{BudgetBreach, Error};
let _e = Error::Budget(BudgetBreach::MaxDocuments {
limit: 1_000,
observed: 1_001,
});UnknownAnchor(String)
UnknownAnchorAt
Unknown anchor encountered at a specific location.
§Examples
use noyalib::{Error, Location};
let _e = Error::UnknownAnchorAt {
name: "x".into(),
location: Location::default(),
suggestion: None,
};Fields
name: StringThe anchor name.
§Examples
use noyalib::{Error, Location};
let e = Error::UnknownAnchorAt {
name: "x".into(),
location: Location::default(),
suggestion: None,
};
if let Error::UnknownAnchorAt { name, .. } = e {
assert_eq!(name, "x");
}location: LocationThe location where it was used.
§Examples
use noyalib::{Error, Location};
let e = Error::UnknownAnchorAt {
name: "x".into(),
location: Location::from_index("ab", 1),
suggestion: None,
};
if let Error::UnknownAnchorAt { location, .. } = e {
assert_eq!(location.column(), 2);
}suggestion: Option<(String, Location)>Optional suggestion for a similar anchor.
§Examples
use noyalib::{Error, Location};
let e = Error::UnknownAnchorAt {
name: "x".into(),
location: Location::default(),
suggestion: Some(("y".into(), Location::default())),
};
if let Error::UnknownAnchorAt { suggestion: Some((s, _)), .. } = e {
assert_eq!(s, "y");
}MissingField(String)
UnknownField(String)
Unknown field in a mapping (with deny_unknown_fields).
§Examples
let _e = noyalib::Error::UnknownField("extra".into());ScalarInMergeElement
Scalar encountered where a mapping was expected during merge.
§Examples
let _e = noyalib::Error::ScalarInMergeElement;SequenceInMergeElement
Sequence encountered where a mapping was expected during merge.
§Examples
let _e = noyalib::Error::SequenceInMergeElement;TaggedInMerge
Invalid(String)
Generic invalid construct error.
§Examples
let _e = noyalib::Error::Invalid("bad construct".into());TypeMismatch
A type mismatch error.
§Examples
let _e = noyalib::Error::TypeMismatch {
expected: "integer",
found: "string".into(),
};Fields
Shared error instance (Arc-wrapped for cloning).
§Examples
use std::sync::Arc;
let _e = noyalib::Error::Shared(Arc::new(noyalib::Error::EndOfStream));EndOfStream
MoreThanOneDocument
More than one document found where one was expected.
§Examples
let _e = noyalib::Error::MoreThanOneDocument;ScalarInMerge
EmptyTag
FailedToParseNumber(String)
Failed to parse a number.
§Examples
let _e = noyalib::Error::FailedToParseNumber("not-a-number".into());Message(String, Option<usize>)
A message error from Serde (compat variant).
§Examples
let _e = noyalib::Error::Message("oops".into(), Some(42));Implementations§
§impl Error
impl Error
pub fn render_with_formatter(&self, formatter: &dyn MessageFormatter) -> String
pub fn render_with_formatter(&self, formatter: &dyn MessageFormatter) -> String
Render this error via a custom [MessageFormatter].
Pairs with [DefaultFormatter] (developer-facing,
verbatim) and [UserFormatter] (user-facing, simplified).
Callers needing localisation or rich formatting plug in
their own MessageFormatter impl.
§Examples
use noyalib::i18n::UserFormatter;
use noyalib::{from_str, Value};
let err = from_str::<Value>("a: [unclosed").unwrap_err();
let msg = err.render_with_formatter(&UserFormatter);
assert!(msg.contains("syntax error"));§impl Error
impl Error
pub fn location(&self) -> Option<Location>
pub fn location(&self) -> Option<Location>
Get the location of the error, if any.
§Examples
use noyalib::{from_str, Value};
let err = from_str::<Value>("a: [unclosed").unwrap_err();
let _ = err.location();pub fn format_with_source(&self, source: &str) -> String
pub fn format_with_source(&self, source: &str) -> String
Format the error with source context. If the error carries a source
location and the line is in range, the output includes a
line <n>:<col> prefix, the offending line, and a caret (^)
pointing at the column. Out-of-range lines fall back to plain
Display.
For rustc-style multi-line context with surrounding lines, use
Self::format_with_source_radius.
§Examples
use noyalib::{from_str, Value};
let source = "a: [unclosed";
let err = from_str::<Value>(source).unwrap_err();
let formatted = err.format_with_source(source);
assert!(formatted.contains("error"));pub fn format_with_source_radius(&self, source: &str, radius: usize) -> String
pub fn format_with_source_radius(&self, source: &str, radius: usize) -> String
Format the error with radius lines of context above and
below the offending line — rustc-style. Each line gets a line
number on the left; the caret line under the offending column
is unnumbered. The output is byte-for-byte stable across
minor releases (no terminal escape codes, no
platform-conditional whitespace).
Out-of-range locations fall back to plain Display (no
snippet) — same contract as Self::format_with_source.
§Examples
use noyalib::{from_str, Value};
// Indentation-mismatch error — carries a concrete `(line,
// column)` location, so the snippet renderer engages.
let source = "\
header: ok
service:
nested: x
bad: y
trailer: ok
";
let e = from_str::<Value>(source).unwrap_err();
let formatted = e.format_with_source_radius(source, 1);
// Output includes the offending line plus a single line
// of context above and below.
assert!(formatted.contains("|"));
assert!(formatted.contains("bad: y"));pub fn format_with_source_truncated(
&self,
source: &str,
max_chars: usize,
) -> String
pub fn format_with_source_truncated( &self, source: &str, max_chars: usize, ) -> String
Format the error with source context, capped at max_chars
ASCII characters — the bridged-channel-friendly variant
of Self::format_with_source. Use when the diagnostic is
destined for a Slack message, a Sentry tag, a structured
log field, or any sink with a hard length budget.
§Truncation contract
- The output is plain ASCII (the renderer already emits no ANSI escapes, so this is a no-op for that axis).
- If the rendered string is
<= max_chars, returns it unchanged. - Otherwise truncates at a UTF-8 character boundary
<= max_chars - 3and appends an...ellipsis so the final length is at mostmax_chars. max_charssmaller than 3 keeps as much of the prefix as fits and drops the ellipsis (so amax_chars = 2yields exactly two characters of the message).
§Examples
use noyalib::{from_str, Value};
let source = "a: [unclosed";
let err = from_str::<Value>(source).unwrap_err();
let short = err.format_with_source_truncated(source, 60);
assert!(short.len() <= 60);
// Untrimmed output is the same as `format_with_source`:
let full = err.format_with_source(source);
let unbounded = err.format_with_source_truncated(source, full.len() + 100);
assert_eq!(unbounded, full);pub fn format_with_source_radius_truncated(
&self,
source: &str,
radius: usize,
max_chars: usize,
) -> String
pub fn format_with_source_radius_truncated( &self, source: &str, radius: usize, max_chars: usize, ) -> String
Format the error with multi-line radius context, capped
at max_chars. Same truncation contract as
Self::format_with_source_truncated.
§Examples
use noyalib::{from_str, Value};
let source = "a:\n b:\n c: [unclosed";
let err = from_str::<Value>(source).unwrap_err();
let s = err.format_with_source_radius_truncated(source, 1, 80);
assert!(s.len() <= 80);Convert the error into a shared Arc pointer. If the error is
already Error::Shared, the inner Arc is reused without
double-wrapping.
§Examples
let shared = noyalib::Error::EndOfStream.into_shared();
assert!(matches!(&*shared, noyalib::Error::EndOfStream));Check if the error is a shared error.
§Examples
use std::sync::Arc;
let e = noyalib::Error::Shared(Arc::new(noyalib::Error::EndOfStream));
assert!(e.is_shared());pub fn as_inner(&self) -> Option<&Error>
pub fn as_inner(&self) -> Option<&Error>
Access the inner error if this is a shared error.
§Examples
use std::sync::Arc;
let e = noyalib::Error::Shared(Arc::new(noyalib::Error::EndOfStream));
assert!(e.as_inner().is_some());pub fn parse_at(message: impl Into<String>, source: &str, index: usize) -> Error
pub fn parse_at(message: impl Into<String>, source: &str, index: usize) -> Error
Create a new parse error at the given index.
§Examples
let e = noyalib::Error::parse_at("bad", "a: x", 3);
assert!(matches!(e, noyalib::Error::ParseWithLocation { .. }));pub fn deserialize_at(
message: impl Into<String>,
source: &str,
index: usize,
) -> Error
pub fn deserialize_at( message: impl Into<String>, source: &str, index: usize, ) -> Error
Create a new deserialization error at the given index.
§Examples
let e = noyalib::Error::deserialize_at("bad", "a: x", 3);
assert!(matches!(e, noyalib::Error::DeserializeWithLocation { .. }));Create a new error from a shared error pointer.
§Examples
use std::sync::Arc;
let e = noyalib::Error::from_shared(Arc::new(noyalib::Error::EndOfStream));
assert!(e.is_shared());pub fn render(&self, source: &str) -> String
pub fn render(&self, source: &str) -> String
Render the error in rustc-style with default options.
Equivalent to
self.render_with_options(source, &RenderOptions::default()).
Issue #2 entry point — supersedes Self::format_with_source
for new code; that method is preserved for backwards
compatibility.
§Examples
use noyalib::{from_str, Value};
let source = "a:\n b: 1\n c: 2\n"; // misaligned indent
let err = from_str::<Value>(source).unwrap_err();
let rendered = err.render(source);
assert!(rendered.contains("error"));pub fn render_with_options(&self, source: &str, opts: &RenderOptions) -> String
pub fn render_with_options(&self, source: &str, opts: &RenderOptions) -> String
Render the error with caller-controlled options.
RenderOptions::crop_radius sets how many lines of context
surround the offending line; RenderOptions::color enables
terminal ANSI colour codes. The default
(RenderOptions::default()) is crop_radius = 2,
color = false.
§Examples
use noyalib::{from_str, RenderOptions, Value};
let source = "a: [unclosed";
let err = from_str::<Value>(source).unwrap_err();
let opts = RenderOptions { crop_radius: 1, color: false };
let rendered = err.render_with_options(source, &opts);
assert!(rendered.contains("error"));Trait Implementations§
§impl Error for Error
Available on crate feature std only.
impl Error for Error
std only.§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
§impl Error for Error
impl Error for Error
§fn custom<T>(msg: T) -> Errorwhere
T: Display,
fn custom<T>(msg: T) -> Errorwhere
T: Display,
§fn missing_field(field: &'static str) -> Error
fn missing_field(field: &'static str) -> Error
Deserialize struct type expected to receive a required
field with a particular name but that field was not present in the
input.§fn unknown_field(field: &str, _expected: &'static [&'static str]) -> Error
fn unknown_field(field: &str, _expected: &'static [&'static str]) -> Error
Deserialize struct type received a field with an
unrecognized name.Source§fn invalid_type(unexp: Unexpected<'_>, exp: &dyn Expected) -> Self
fn invalid_type(unexp: Unexpected<'_>, exp: &dyn Expected) -> Self
Deserialize receives a type different from what it was
expecting. Read moreSource§fn invalid_value(unexp: Unexpected<'_>, exp: &dyn Expected) -> Self
fn invalid_value(unexp: Unexpected<'_>, exp: &dyn Expected) -> Self
Deserialize receives a value of the right type but that
is wrong for some other reason. Read moreSource§fn invalid_length(len: usize, exp: &dyn Expected) -> Self
fn invalid_length(len: usize, exp: &dyn Expected) -> Self
Source§fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Self
fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Self
Deserialize enum type received a variant with an
unrecognized name.Source§fn duplicate_field(field: &'static str) -> Self
fn duplicate_field(field: &'static str) -> Self
Deserialize struct type received more than one of the
same field.