Skip to main content

barkit_extract/
error.rs

1use thiserror::Error;
2
3#[derive(Error, Debug)]
4pub enum Error {
5    #[error("UTF-8 error: {0}")]
6    Utf8(#[from] std::str::Utf8Error),
7    #[error("UTF-8 error: {0}")]
8    FromUtf8(#[from] std::string::FromUtf8Error),
9    #[error("Regex error: {0}")]
10    Regex(#[from] regex::Error),
11    #[error("{0} capture group not found in your pattern")]
12    BarcodeCaptureGroupNotFound(String),
13    #[error("Provided unexpected barcode capture group {0}")]
14    UnexpectedCaptureGroupName(String),
15    #[error("I/O error: {0}")]
16    IO(#[from] std::io::Error),
17    #[error("No match")]
18    PatternNotMatched,
19    #[error("Fancy regex error: {0}")]
20    FancyRegex(#[from] fancy_regex::Error),
21    #[error("Failed to choose permutation mask")]
22    PermutationMaskSize,
23}
24
25impl Clone for Error {
26    fn clone(&self) -> Self {
27        match self {
28            Error::Utf8(err) => Error::Utf8(*err),
29            Error::FromUtf8(err) => Error::FromUtf8(err.clone()),
30            Error::Regex(err) => Error::Regex(err.clone()),
31            Error::BarcodeCaptureGroupNotFound(barcode_type) => {
32                Error::BarcodeCaptureGroupNotFound(barcode_type.clone())
33            }
34            Error::UnexpectedCaptureGroupName(capture_group) => {
35                Error::UnexpectedCaptureGroupName(capture_group.clone())
36            }
37            Error::IO(err) => Error::IO(err.kind().into()),
38            Error::PatternNotMatched => Error::PatternNotMatched,
39            Error::FancyRegex(err) => Error::FancyRegex(err.clone()),
40            Error::PermutationMaskSize => Error::PermutationMaskSize,
41        }
42    }
43}