use std::io;
use aranya_daemon_api as api;
use tarpc::client::RpcError;
#[cfg(feature = "afc")]
use crate::afc::Error as AfcError;
pub type Result<T, E = Error> = core::result::Result<T, E>;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("IPC error")]
Ipc(#[from] IpcError),
#[error("daemon error")]
Aranya(#[from] AranyaError),
#[error("does not exist")]
DoesNotExist,
#[error("configuration error")]
Config(#[from] ConfigError),
#[cfg(feature = "afc")]
#[cfg_attr(docsrs, doc(cfg(feature = "afc")))]
#[error("AFC error")]
Afc(#[from] AfcError),
#[error(transparent)]
Bug(#[from] buggy::Bug),
#[error(transparent)]
Other(#[from] OtherError),
}
impl From<core::convert::Infallible> for Error {
fn from(value: core::convert::Infallible) -> Self {
match value {}
}
}
#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct OtherError {
#[from]
err: anyhow::Error,
}
pub(crate) fn other<E>(err: E) -> OtherError
where
E: Into<anyhow::Error>,
{
OtherError { err: err.into() }
}
#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct AranyaError {
#[from]
err: api::Error,
}
pub(crate) fn aranya_error(err: api::Error) -> Error {
match err {
api::Error::DoesNotExist(_) => Error::DoesNotExist,
err => Error::Aranya(err.into()),
}
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ConfigError {
#[error(transparent)]
InvalidArg(#[from] InvalidArg),
}
#[derive(Debug, thiserror::Error)]
#[error("invalid argument `{arg}`: {reason}")]
pub struct InvalidArg {
arg: &'static str,
reason: &'static str,
}
impl InvalidArg {
pub(crate) const fn new(arg: &'static str, reason: &'static str) -> Self {
Self { arg, reason }
}
}
#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct IpcError(#[from] pub(crate) IpcRepr);
impl IpcError {
pub(crate) fn new<E>(err: E) -> Self
where
E: Into<IpcRepr>,
{
Self(err.into())
}
}
#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub(crate) enum IpcRepr {
InvalidArg(#[from] InvalidArg),
Io(#[from] io::Error),
Tarpc(#[from] RpcError),
Other(#[from] anyhow::Error),
}