pqr 0.1.0

Post-quantum resistant encryption, decryption, and key generation from the command line. Easy to use, with sane defaults.
//! Items used by two or more domains: the failure type and its exit codes.

use std::fmt;

/// Process exit codes (decrypt table from the spec).
pub const EXIT_USAGE: u8 = 1;
pub const EXIT_NOT_FOR_US: u8 = 2;
pub const EXIT_AUTH: u8 = 3;

/// A user-facing failure: a message for stderr and an exit code.
#[derive(Debug)]
pub struct Failure {
    pub code: u8,
    pub message: String,
}

impl Failure {
    pub fn new(code: u8, message: impl Into<String>) -> Self {
        Self {
            code,
            message: message.into(),
        }
    }

    pub fn usage(message: impl Into<String>) -> Self {
        Self::new(EXIT_USAGE, message)
    }
}

impl fmt::Display for Failure {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.message)
    }
}

impl From<std::io::Error> for Failure {
    fn from(err: std::io::Error) -> Self {
        Self::usage(format!("io error: {err}"))
    }
}

pub type Result<T> = std::result::Result<T, Failure>;