tauri_plugin_bluetooth_manager/
error.rs

1use serde::{ser::Serializer, Serialize};
2use std::fmt;
3
4#[derive(Debug)]
5pub enum Error {
6    Zbus(zbus::Error),
7    Zvariant(zbus::zvariant::Error),
8    CommandError(String),
9    NotFound(String),
10}
11
12impl std::error::Error for Error {
13    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
14        match self {
15            Error::Zbus(e) => Some(e),
16            Error::Zvariant(e) => Some(e),
17            _ => None,
18        }
19    }
20}
21
22impl fmt::Display for Error {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        match self {
25            Error::Zbus(e) => write!(f, "D-Bus error: {}", e),
26            Error::Zvariant(e) => write!(f, "D-Bus variant error: {}", e),
27            Error::CommandError(s) => write!(f, "Command error: {}", s),
28            Error::NotFound(s) => write!(f, "Not found: {}", s),
29        }
30    }
31}
32
33impl Serialize for Error {
34    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
35    where
36        S: Serializer,
37    {
38        serializer.serialize_str(&self.to_string())
39    }
40}
41
42impl From<zbus::Error> for Error {
43    fn from(err: zbus::Error) -> Self {
44        Error::Zbus(err)
45    }
46}
47
48impl From<zbus::zvariant::Error> for Error {
49    fn from(err: zbus::zvariant::Error) -> Self {
50        Error::Zvariant(err)
51    }
52}
53
54impl From<std::convert::Infallible> for Error {
55    fn from(_err: std::convert::Infallible) -> Self {
56        Error::CommandError("Infallible error encountered".to_string())
57    }
58}
59
60pub type Result<T> = std::result::Result<T, Error>;