//! When serializing or deserializing CBOR goes wrong. use core::fmt; use core::result; use serde::de; use serde::ser; #[cfg(feature = "std")] use std::error; #[cfg(feature = "std")] use std::io;
/// This type represents all possible errors that can occur when serializing or deserializing CBOR /// data. pubstruct Error(ErrorImpl);
/// Alias for a `Result` with the error type `serde_cbor::Error`. pubtype Result<T> = result::Result<T, Error>;
/// Categorizes the cause of a `serde_cbor::Error`. #[derive(Copy, Clone, Debug, Eq, PartialEq)] pubenum Category { /// The error was caused by a failure to read or write bytes on an IO stream.
Io, /// The error was caused by input that was not syntactically valid CBOR.
Syntax, /// The error was caused by input data that was semantically incorrect.
Data, /// The error was caused by prematurely reaching the end of the input data.
Eof,
}
impl Error { /// The byte offset at which the error occurred. pubfn offset(&self) -> u64 { self.0.offset
}
#[cfg(all(not(feature = "std"), feature = "unsealed_read_write"))] /// Creates an error signalling that the underlying `Read` encountered an I/O error. pubfn io() -> Error {
Error(ErrorImpl {
code: ErrorCode::Io,
offset: 0,
})
}
#[cfg(feature = "unsealed_read_write")] /// Creates an error signalling that the scratch buffer was too small to fit the data. pubfn scratch_too_small(offset: u64) -> Error {
Error(ErrorImpl {
code: ErrorCode::ScratchTooSmall,
offset,
})
}
/// Returns true if this error was caused by a failure to read or write bytes on an IO stream. pubfn is_io(&self) -> bool { matchself.classify() {
Category::Io => true,
_ => false,
}
}
/// Returns true if this error was caused by input that was not syntactically valid CBOR. pubfn is_syntax(&self) -> bool { matchself.classify() {
Category::Syntax => true,
_ => false,
}
}
/// Returns true if this error was caused by data that was semantically incorrect. pubfn is_data(&self) -> bool { matchself.classify() {
Category::Data => true,
_ => false,
}
}
/// Returns true if this error was caused by prematurely reaching the end of the input data. pubfn is_eof(&self) -> bool { matchself.classify() {
Category::Eof => true,
_ => false,
}
}
/// Returns true if this error was caused by the scratch buffer being too small. /// /// Note this being `true` implies that `is_io()` is also `true`. pubfn is_scratch_too_small(&self) -> bool { matchself.0.code {
ErrorCode::ScratchTooSmall => true,
_ => false,
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.