use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
Io(std::io::Error),
Parse(String),
Usb(String),
Libvirt(String),
Signal(String),
Monitor(String),
}
impl Error {
pub fn parse(msg: impl Into<String>) -> Self {
Error::Parse(msg.into())
}
pub fn usb(msg: impl Into<String>) -> Self {
Error::Usb(msg.into())
}
pub fn libvirt(msg: impl Into<String>) -> Self {
Error::Libvirt(msg.into())
}
pub fn signal(msg: impl Into<String>) -> Self {
Error::Signal(msg.into())
}
pub fn monitor(msg: impl Into<String>) -> Self {
Error::Monitor(msg.into())
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io(e) => write!(f, "I/O error: {e}"),
Error::Parse(m) => write!(f, "parse error: {m}"),
Error::Usb(m) => write!(f, "USB error: {m}"),
Error::Libvirt(m) => write!(f, "libvirt error: {m}"),
Error::Signal(m) => write!(f, "signal error: {m}"),
Error::Monitor(m) => write!(f, "monitor error: {m}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::Io(e)
}
}
impl From<std::ffi::NulError> for Error {
fn from(e: std::ffi::NulError) -> Self {
Error::Libvirt(format!("string contains a NUL byte: {e}"))
}
}