use std::{borrow::Cow, convert::Infallible, error, fmt, io, num::NonZeroUsize, sync::Arc};
use zbus_names::Error as NamesError;
use zvariant::Error as VariantError;
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum Error {
Variant(VariantError),
Name(NamesError),
Xml(XmlError),
Io(Arc<io::Error>),
#[deprecated(
since = "5.2.0",
note = "This variant is no longer returned from any of our API. \
Match on `Error::Xml` instead."
)]
#[allow(deprecated)]
QuickXml(DeError),
#[deprecated(
since = "5.2.0",
note = "This variant is no longer returned from any of our API. \
Match on `Error::Xml` or `Error::Io` instead."
)]
#[allow(deprecated)]
QuickXmlSer(SeError),
}
impl PartialEq for Error {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Variant(s), Self::Variant(o)) => s == o,
(Self::Name(s), Self::Name(o)) => s == o,
(Self::Xml(s), Self::Xml(o)) => s == o,
(_, _) => false,
}
}
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Error::Variant(e) => Some(e),
Error::Name(e) => Some(e),
Error::Xml(e) => Some(e),
Error::Io(e) => Some(e),
#[allow(deprecated)]
Error::QuickXml(e) => Some(e),
#[allow(deprecated)]
Error::QuickXmlSer(e) => Some(e),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Variant(e) => write!(f, "{e}"),
Error::Name(e) => write!(f, "{e}"),
Error::Xml(e) => write!(f, "XML error: {e}"),
Error::Io(e) => write!(f, "I/O error: {e}"),
#[allow(deprecated)]
Error::QuickXml(e) => write!(f, "XML error: {e}"),
#[allow(deprecated)]
Error::QuickXmlSer(e) => write!(f, "XML serialization error: {e}"),
}
}
}
impl From<VariantError> for Error {
fn from(val: VariantError) -> Self {
Error::Variant(val)
}
}
impl From<NamesError> for Error {
fn from(val: NamesError) -> Self {
Error::Name(val)
}
}
impl From<XmlError> for Error {
fn from(val: XmlError) -> Self {
Error::Xml(val)
}
}
impl From<io::Error> for Error {
fn from(val: io::Error) -> Self {
Error::Io(Arc::new(val))
}
}
impl From<Infallible> for Error {
fn from(i: Infallible) -> Self {
match i {}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct XmlError {
message: String,
position: usize,
}
impl XmlError {
pub(crate) fn new(message: impl Into<String>, position: usize) -> Self {
Self {
message: message.into(),
position,
}
}
pub fn message(&self) -> &str {
&self.message
}
pub fn position(&self) -> usize {
self.position
}
}
impl fmt::Display for XmlError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} (at byte offset {})", self.message, self.position)
}
}
impl error::Error for XmlError {}
#[doc(hidden)]
#[deprecated(
since = "5.2.0",
note = "This error is no longer returned from any of our API. \
Match on `Error::Xml` instead."
)]
#[derive(Clone, Debug)]
pub enum DeError {
Custom(String),
InvalidXml(String),
KeyNotRead,
UnexpectedStart(Vec<u8>),
UnexpectedEof,
TooManyEvents(NonZeroUsize),
}
#[allow(deprecated)]
impl fmt::Display for DeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Custom(s) => f.write_str(s),
Self::InvalidXml(e) => f.write_str(e),
Self::KeyNotRead => f.write_str(
"invalid `Deserialize` implementation: `MapAccess::next_value[_seed]` was called \
before `MapAccess::next_key[_seed]`",
),
Self::UnexpectedStart(e) => {
write!(
f,
"unexpected `Event::Start({})`",
String::from_utf8_lossy(e)
)
}
Self::UnexpectedEof => f.write_str("unexpected `Event::Eof`"),
Self::TooManyEvents(s) => write!(f, "deserializer buffered {s} events, limit exceeded"),
}
}
}
#[allow(deprecated)]
impl error::Error for DeError {}
#[doc(hidden)]
#[deprecated(
since = "5.2.0",
note = "This error is no longer returned from any of our API. \
Match on `Error::Xml` or `Error::Io` instead."
)]
#[derive(Clone, Debug)]
pub enum SeError {
Custom(String),
Io(Arc<io::Error>),
Fmt(std::fmt::Error),
Unsupported(Cow<'static, str>),
NonEncodable(std::str::Utf8Error),
}
#[allow(deprecated)]
impl fmt::Display for SeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Custom(s) => f.write_str(s),
Self::Io(e) => write!(f, "I/O error: {e}"),
Self::Fmt(e) => write!(f, "formatting error: {e}"),
Self::Unsupported(s) => write!(f, "unsupported value: {s}"),
Self::NonEncodable(e) => write!(f, "malformed UTF-8: {e}"),
}
}
}
#[allow(deprecated)]
impl error::Error for SeError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Self::Io(e) => Some(e),
_ => None,
}
}
}
pub type Result<T> = std::result::Result<T, Error>;