fuzzel-pass 0.1.2

A password-store frontend for auto-typing passwords
use std::{fmt::Display, io::Error as IoError};

/// A list of errors that may occur.
#[derive(Debug)]
pub enum Error {
    /// When an I/O error occurs
    Io(IoError),

    /// When password file is empty
    EmptyPassword,

    /// When error parsing document bellow password happens
    Yaml(String),

    /// When a child process returns a non-zero status
    ChildExitStatusFailed(String, i32),
}

impl PartialEq for Error {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::EmptyPassword, Self::EmptyPassword) => true,
            (Self::Io(err), Self::Io(other_err)) => err.kind() == other_err.kind(),
            (Self::Yaml(err), Self::Yaml(other_err)) => err == other_err,
            (
                Self::ChildExitStatusFailed(exe, code),
                Self::ChildExitStatusFailed(other_exe, other_code),
            ) => exe == other_exe && code == other_code,
            (_, _) => false,
        }
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let message = match self {
            Self::EmptyPassword => "password file is empty".into(),
            Self::Io(why) => format!("I/O error: {why}"),
            Self::Yaml(why) => format!("YAML parsing error: {why}"),
            Self::ChildExitStatusFailed(exe, code) => {
                format!("External program `{exe}` ended with status code {code}",)
            }
        };

        write!(f, "{message}")
    }
}