tyrx 0.1.2

Typed, ergonomic regular expression library
Documentation
use std::ops::Range;
use std::error::Error as StdError;
use regex::Error as RegexError;
use thiserror::Error;


#[derive(Debug, Error)]
pub enum Error {
    #[error("haystack does not match regex")]
    NoMatch,
    #[error("haystack does not match group `{0}`")]
    NoGroup(&'static str),
    #[error("neither variant of enum `{0}` matched")]
    NoVariant(&'static str),
    #[error(transparent)]
    Regex(#[from] RegexError),
    #[error("parse error in group `{group}` at {start}..{end}: {source}", start = .span.start, end = .span.end)]
    FromStr {
        group: &'static str,
        span: Range<usize>,
        #[source]
        source: Box<dyn StdError + Send + Sync + 'static>,
    },
    #[error(transparent)]
    Other(Box<dyn StdError + Send + Sync + 'static>),
}

impl Error {
    pub fn group_from_str<E>(group: &'static str, span: Range<usize>, source: E) -> Self
    where
        E: StdError + Send + Sync + 'static,
    {
        Error::FromStr {
            group,
            span,
            source: Box::new(source),
        }
    }

    pub fn other(source: impl StdError + Send + Sync + 'static) -> Self {
        Error::Other(Box::new(source))
    }

    pub fn message(message: impl ToString) -> Self {
        Error::Other(message.to_string().into())
    }
}

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