Skip to main content

oo7/
error.rs

1use std::fmt;
2
3/// Alias for [`std::result::Result`] with the error type [`Error`].
4pub type Result<T> = std::result::Result<T, Error>;
5
6/// The error type for oo7.
7#[derive(Debug)]
8pub enum Error {
9    /// File backend error.
10    File(crate::file::Error),
11    /// Secret Service error.
12    DBus(crate::dbus::Error),
13}
14
15impl From<crate::file::Error> for Error {
16    fn from(e: crate::file::Error) -> Self {
17        Self::File(e)
18    }
19}
20
21impl From<crate::dbus::Error> for Error {
22    fn from(e: crate::dbus::Error) -> Self {
23        Self::DBus(e)
24    }
25}
26
27impl std::error::Error for Error {}
28
29impl fmt::Display for Error {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        match self {
32            Self::File(e) => write!(f, "File backend error {e}"),
33            Self::DBus(e) => write!(f, "DBus error {e}"),
34        }
35    }
36}
37
38/// Errors that can occur when working with schemas.
39#[derive(Debug)]
40#[cfg(feature = "schema")]
41pub enum SchemaError {
42    /// A required field is missing from the attributes.
43    MissingField(&'static str),
44
45    /// The schema name doesn't match the expected schema.
46    SchemaMismatch {
47        /// The expected schema name.
48        expected: String,
49        /// The actual schema name found.
50        found: String,
51    },
52
53    /// A field value could not be parsed into the expected type.
54    InvalidValue {
55        /// The field name that has an invalid value.
56        field: &'static str,
57        /// The invalid value.
58        value: String,
59    },
60}
61
62#[cfg(feature = "schema")]
63impl std::error::Error for SchemaError {}
64
65#[cfg(feature = "schema")]
66impl std::fmt::Display for SchemaError {
67    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68        match self {
69            Self::MissingField(field) => write!(f, "Missing required field: {field}"),
70            Self::SchemaMismatch { expected, found } => {
71                write!(f, "Schema mismatch: expected {expected}, found {found}")
72            }
73            Self::InvalidValue { field, value } => {
74                write!(f, "Invalid field value for {field}: {value}")
75            }
76        }
77    }
78}