use std::convert::From;
use std::io;
use std::path::PathBuf;
use std::string::FromUtf8Error;
use config::ConfigError;
use crate::dbus::State;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("Device {0} is already mounted at mountpoint {1}")]
AlreadyMounted(String, String),
#[error("Device {0} is not yet mounted")]
DeviceNotMounted(String),
#[error("Device {0} is a base device without a partition")]
IsBaseDevice(String),
#[error("Device {0} does not have a filesystem")]
IncompatibleBlockDevice(String),
#[error("Device {0} does not have a compatible filesystem")]
IncompatibleFilesystem(String),
#[error("Device path {0} is not valid")]
InvalidDevicePath(String),
#[error("A problem occurred while communicating over dbus: {0}")]
Dbus(zbus::Error),
#[error("A problem occurred while communicating over dbus internally: {0}")]
DbusInternal(zbus::fdo::Error),
#[error("An internal error occurred communicating between threads over channels: {0}")]
StateChannel(tokio::sync::mpsc::error::SendError<State>),
#[error("An error occurred reading or writing a file: {0}")]
File(io::Error),
#[error("Unable to get information on a RAUC update bundle {0}")]
BundleInfo(String, String),
#[error("RAUC update bundle path {0} is invalid")]
BundlePath(PathBuf),
#[error("Version ({0}) of RAUC update bundle {1} is invalid: {2}")]
BundleVersion(String, String, String),
#[error("Version ({0}) of slot {1} is invalid: {2}")]
SlotVersion(String, String, String),
#[error("An error occurred reading configuration: {0}")]
Config(ConfigError),
#[error("No compatible RAUC update bundle found")]
NoUpdateBundle,
#[error("An error occurred trying to convert a string: {0}")]
String(FromUtf8Error),
#[error("There is more than one override update bundle")]
TooManyOverrides(Vec<PathBuf>),
#[error("Unmounting mountpoint {0} failed")]
UnmountFailed(String),
#[error("Update failed: {0}")]
UpdateFailed(String),
#[error("Caterpillar is in wrong state: {0}")]
WrongState(String),
#[error("Failed initializing: {0}")]
Init(String),
#[error("An error occurred: {0}")]
Default(String),
}
impl From<tokio::sync::mpsc::error::SendError<State>> for Error {
fn from(value: tokio::sync::mpsc::error::SendError<State>) -> Self {
Error::StateChannel(value)
}
}
impl From<zbus::fdo::Error> for Error {
fn from(value: zbus::fdo::Error) -> Self {
Error::DbusInternal(value)
}
}
impl From<FromUtf8Error> for Error {
fn from(value: FromUtf8Error) -> Self {
Error::String(value)
}
}
impl From<io::Error> for Error {
fn from(value: io::Error) -> Self {
Error::File(value)
}
}
impl From<zbus::Error> for Error {
fn from(err: zbus::Error) -> Error {
Error::Dbus(err)
}
}
impl From<ConfigError> for Error {
fn from(err: ConfigError) -> Error {
Error::Config(err)
}
}