joist 1.0.0

A backend framework for Rust: routing, middleware, JSON, config and plugins, with no third-party dependencies.
Documentation
use std::fmt;
use std::io;

/// Everything that can go wrong inside Joist.
///
/// `#[non_exhaustive]`, so a `match` on it needs a `_` arm. The roadmap adds
/// subsystems, each of which brings a failure of its own, and an enum that
/// cannot grow one without a major release is an enum that gets a stringly
/// typed variant bolted onto it instead.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    /// Socket or stream failure.
    Io(io::Error),
    /// The request could not be parsed. The string is safe to log but should
    /// not be echoed to the client verbatim.
    BadRequest(String),
    /// The request body exceeded the configured limit.
    BodyTooLarge { limit: usize },
    /// Something Joist does not implement, such as a `chunked` request body.
    Unsupported(&'static str),
    /// Configuration could not be read or parsed.
    Config(String),
    /// JSON could not be parsed.
    Json(String),
}

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

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Io(e) => write!(f, "io error: {e}"),
            Error::BadRequest(m) => write!(f, "bad request: {m}"),
            Error::BodyTooLarge { limit } => {
                write!(f, "request body exceeds the {limit} byte limit")
            }
            Error::Unsupported(what) => write!(f, "unsupported: {what}"),
            Error::Config(m) => write!(f, "config error: {m}"),
            Error::Json(m) => write!(f, "json 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<io::Error> for Error {
    fn from(e: io::Error) -> Self {
        Error::Io(e)
    }
}