pub struct DecodeError(/* private fields */);Expand description
Why a typed value could not be read: what went wrong, at which byte, and where in the document.
The path is built as the error travels out through the containers it
was found in, so it costs nothing until something fails.
pointer gives it as an RFC 6901 JSON Pointer, which is
what Display shows after the offset:
expected u32, found string at byte 31 (at /position/line). That is a
ready-made message for a JSON-RPC -32602 Invalid params reply.
The keys in the path come from the input, so the message shows them
made safe to log: a control character, a line or paragraph separator,
or a bidirectional formatting character is written as its Rust escape
(\u{a} for a newline), a backslash as \\, and a key longer than 64
characters is cut there and ends in .... The message is therefore
always one line, and its length is bounded by the depth of the path.
path and pointer keep every key exact.
The error is one pointer wide, so Result<T, DecodeError> stays small.
§Examples
use jsonx_lib::{DecodeErrorKind, Kind, PathSegment};
let err = jsonx_lib::from_str::<Vec<Vec<u8>>>("[[1], [2, true]]").unwrap_err();
assert_eq!(err.kind(), &DecodeErrorKind::WrongType { expected: "u8", found: Kind::Bool });
assert_eq!(err.offset(), 10);
assert_eq!(err.path(), [PathSegment::Index(1), PathSegment::Index(1)]);
assert_eq!(err.pointer(), "/1/1");
assert_eq!(err.to_string(), "expected u8, found boolean at byte 10 (at /1/1)");Implementations§
Source§impl DecodeError
impl DecodeError
Sourcepub fn new(kind: DecodeErrorKind, offset: usize) -> Self
pub fn new(kind: DecodeErrorKind, offset: usize) -> Self
An error of kind at byte offset, with an empty path.
§Examples
use jsonx_lib::{DecodeError, DecodeErrorKind};
let err = DecodeError::new(DecodeErrorKind::InvalidValue("a port"), 12);
assert_eq!(err.to_string(), "invalid value: expected a port at byte 12");Sourcepub fn wrong_type(expected: &'static str, found: Kind, offset: usize) -> Self
pub fn wrong_type(expected: &'static str, found: Kind, offset: usize) -> Self
A DecodeErrorKind::WrongType error.
§Examples
use jsonx_lib::{DecodeError, Kind};
let err = DecodeError::wrong_type("a position", Kind::Array, 4);
assert_eq!(err.to_string(), "expected a position, found array at byte 4");Sourcepub fn invalid_value(expected: &'static str, offset: usize) -> Self
pub fn invalid_value(expected: &'static str, offset: usize) -> Self
A DecodeErrorKind::InvalidValue error.
§Examples
let err = jsonx_lib::DecodeError::invalid_value("a URI", 0);
assert_eq!(err.to_string(), "invalid value: expected a URI at byte 0");Sourcepub fn custom(message: impl Into<Cow<'static, str>>, offset: usize) -> Self
pub fn custom(message: impl Into<Cow<'static, str>>, offset: usize) -> Self
A DecodeErrorKind::Custom error with message.
§Examples
let err = jsonx_lib::DecodeError::custom("end before start", 9);
assert_eq!(err.to_string(), "end before start at byte 9");Sourcepub fn missing_field(name: &'static str, offset: usize) -> Self
pub fn missing_field(name: &'static str, offset: usize) -> Self
A DecodeErrorKind::MissingField error.
§Examples
let err = jsonx_lib::DecodeError::missing_field("uri", 0);
assert_eq!(err.to_string(), "missing field `uri` at byte 0");Sourcepub fn at_key(self, key: impl Into<String>) -> Self
pub fn at_key(self, key: impl Into<String>) -> Self
The error as seen from the object that holds it under key: the key
goes in front of the path. The object readers do this for you.
§Examples
let err = jsonx_lib::DecodeError::invalid_value("a URI", 7).at_key("uri").at_key("textDocument");
assert_eq!(err.pointer(), "/textDocument/uri");Sourcepub fn at_index(self, index: usize) -> Self
pub fn at_index(self, index: usize) -> Self
The error as seen from the array that holds it at index.
§Examples
let err = jsonx_lib::DecodeError::invalid_value("a URI", 7).at_index(2);
assert_eq!(err.pointer(), "/2");Sourcepub fn kind(&self) -> &DecodeErrorKind
pub fn kind(&self) -> &DecodeErrorKind
What went wrong.
Sourcepub fn offset(&self) -> usize
pub fn offset(&self) -> usize
The byte offset in the input where it went wrong: the value that did not fit, or for a syntax error the byte a parse reports.
Sourcepub fn path(&self) -> &[PathSegment]
pub fn path(&self) -> &[PathSegment]
Where in the document, outermost first; empty for the top-level value.
Sourcepub fn pointer(&self) -> String
pub fn pointer(&self) -> String
The path as an RFC 6901
JSON Pointer (/params/items/3, "" for the top level), usable with
Value::pointer.
§Examples
let err = jsonx_lib::DecodeError::invalid_value("x", 0).at_key("a/b").at_key("~");
assert_eq!(err.pointer(), "/~0/a~1b");Sourcepub fn line_col(&self, input: impl AsRef<[u8]>) -> (usize, usize)
pub fn line_col(&self, input: impl AsRef<[u8]>) -> (usize, usize)
The error’s line and column in input, from 1, the column in
characters; see Error::line_col.
§Examples
let text = "{\n \"n\": \"seven\"\n}";
let err = jsonx_lib::from_str::<std::collections::BTreeMap<String, u8>>(text).unwrap_err();
assert_eq!(err.line_col(text), (2, 8));Sourcepub fn utf16_position(&self, input: impl AsRef<[u8]>) -> (usize, usize)
pub fn utf16_position(&self, input: impl AsRef<[u8]>) -> (usize, usize)
The error’s position in input as LSP counts it (from 0, the
character in UTF-16 units); see Error::utf16_position.
§Examples
let text = "[1,\n \"x\"]";
let err = jsonx_lib::from_str::<Vec<u8>>(text).unwrap_err();
assert_eq!(err.utf16_position(text), (1, 1));Sourcepub fn syntax_error(&self) -> Option<Error>
pub fn syntax_error(&self) -> Option<Error>
The parse error, for a DecodeErrorKind::Syntax error.
§Examples
use jsonx_lib::ErrorKind;
let err = jsonx_lib::from_str::<Vec<u8>>("[1,]").unwrap_err();
assert_eq!(err.syntax_error().map(|e| e.kind()), Some(ErrorKind::TrailingComma));Trait Implementations§
Source§impl Clone for DecodeError
impl Clone for DecodeError
Source§impl Debug for DecodeError
impl Debug for DecodeError
Source§impl Display for DecodeError
impl Display for DecodeError
impl Eq for DecodeError
Source§impl Error for DecodeError
impl Error for DecodeError
1.30.0 · Source§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()