legit-hasher 0.1.6

Every legit-hashes definition, images and text, for callers in any language: a command-line runner over JSON lines with resumable batches, and with the `serve` feature a worker that takes hashing tasks from Redis Streams
Documentation
//! The crate's error type: what stops a whole run. A bad request never does; it gets an error
//! result instead.

use std::fmt;
use std::path::PathBuf;

/// The result of a fallible operation in this crate.
pub type Result<T, E = Error> = std::result::Result<T, E>;

/// What stopped a run.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    /// Reading the input, writing the output, or reading the output being resumed failed.
    Io {
        /// What was being done, such as "couldn't write the output".
        context: String,
        /// The underlying error.
        source: std::io::Error,
    },
    /// The output file being resumed holds a complete line that isn't a result line.
    NotAResult {
        /// The output file.
        path: PathBuf,
        /// The line, counting from 1.
        line: u64,
    },
    /// The runner itself failed, such as a thread that panicked outside any request.
    Internal(String),
}

impl Error {
    pub(crate) fn io(context: impl Into<String>, source: std::io::Error) -> Self {
        Self::Io {
            context: context.into(),
            source,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Io { context, source } => write!(f, "{context}: {source}"),
            Self::NotAResult { path, line } => write!(
                f,
                "line {line} of `{}` isn't a result line, so it can't be resumed; check the file is this runner's output",
                path.display()
            ),
            Self::Internal(message) => write!(f, "the runner failed: {message}"),
        }
    }
}

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