sd-switch 0.5.4

A systemd unit reload/restart utility for Home Manager
Documentation
use std::{
    error, fmt, io,
    path::{Path, PathBuf},
};

#[derive(Debug)]
pub enum Error {
    ZBus(zbus::Error),
    IniParse(PathBuf, ini::ParseError),
    Io(io::Error),
    SdSwitch(String),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Error::ZBus(ref err) => err.fmt(f),
            Error::Io(ref err) => err.fmt(f),
            Error::IniParse(path, ref err) => write!(f, "{}: {}", path.display(), err),
            Error::SdSwitch(ref err) => err.fmt(f),
        }
    }
}

impl error::Error for Error {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            Error::ZBus(err) => err.source(),
            Error::Io(err) => err.source(),
            Error::IniParse(_, err) => err.source(),
            Error::SdSwitch(_) => None,
        }
    }
}

impl From<zbus::Error> for Error {
    fn from(err: zbus::Error) -> Self {
        Error::ZBus(err)
    }
}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Self {
        Error::Io(err)
    }
}

impl Error {
    pub fn from_ini(path: &Path, err: ini::Error) -> Error {
        match err {
            ini::Error::Io(e) => Error::Io(e),
            ini::Error::Parse(e) => Error::IniParse(path.to_path_buf(), e),
        }
    }
}