error.rs

3.0 kB · rust · 108 lines

1use super::Json;2use std::fmt;34/// The one error type of the crate.5#[non_exhaustive]6#[derive(Debug)]7pub enum Error {8    /// A value that broke a rule, carrying the message.9    Value(String),10    /// A length, extent or dtype that does not match, carrying the message.11    Shape(String),12    /// A count that runs past the width of its integer, carrying the message.13    Overflow(String),14    /// A json text that would not parse, carrying the reader's own error.15    Json(serde_json::Error),16    /// A png or gif codec that refused, carrying its message.17    Codec(String),18}1920impl fmt::Display for Error {21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {22        match self {23            Error::Value(message) => write!(f, "{message}"),24            Error::Shape(message) => write!(f, "{message}"),25            Error::Overflow(message) => write!(f, "{message}"),26            Error::Json(error) => write!(f, "json: {error}"),27            Error::Codec(message) => write!(f, "{message}"),28        }29    }30}3132impl std::error::Error for Error {33    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {34        match self {35            Error::Json(error) => Some(error),36            _ => None,37        }38    }39}4041/// The crate's result, erring with Error.42pub type Result<T> = std::result::Result<T, Error>;4344/// Wraps a message in an Err of the value variant.45///46/// # Errors47///48/// Always errs; the Ok side is only there to fit the caller's return type.49pub fn value_error<T>(message: impl Into<String>) -> Result<T> {50    Err(Error::Value(message.into()))51}5253/// Wraps a message in an Err of the shape variant.54///55/// # Errors56///57/// Always errs; the Ok side is only there to fit the caller's return type.58pub fn shape_error<T>(message: impl Into<String>) -> Result<T> {59    Err(Error::Shape(message.into()))60}6162/// Wraps a message in an Err of the overflow variant.63///64/// # Errors65///66/// Always errs; the Ok side is only there to fit the caller's return type.67pub fn overflow_error<T>(message: impl Into<String>) -> Result<T> {68    Err(Error::Overflow(message.into()))69}7071impl From<serde_json::Error> for Error {72    fn from(error: serde_json::Error) -> Error {73        Error::Json(error)74    }75}7677impl From<png::EncodingError> for Error {78    fn from(error: png::EncodingError) -> Error {79        Error::Codec(error.to_string())80    }81}8283impl From<png::DecodingError> for Error {84    fn from(error: png::DecodingError) -> Error {85        Error::Codec(error.to_string())86    }87}8889impl From<gif::EncodingError> for Error {90    fn from(error: gif::EncodingError) -> Error {91        Error::Codec(error.to_string())92    }93}9495/// Parses JSON text into a value.96///97/// # Errors98///99/// Errs when the text is not valid json, naming where it broke.100///101/// ```102/// let v = mrlyrs::core::error::parse(r#"{"tags": [3, 5]}"#).unwrap();103/// assert_eq!(v["tags"][1], 5);104/// assert!(mrlyrs::core::error::parse("[1,").is_err());105/// ```106pub fn parse(text: &str) -> Result<Json> {107    Ok(serde_json::from_str(text)?)108}