1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
    #[error("UTF-8 error: {0}")]
    Utf8(#[from] std::str::Utf8Error),
    #[error("UTF-8 error: {0}")]
    FromUtf8(#[from] std::string::FromUtf8Error),
    #[error("Regex error: {0}")]
    Regex(#[from] regex::Error),
    #[error("{0} capture group not found in your pattern")]
    BarcodeCaptureGroupNotFound(String),
    #[error("Provided unexpected barcode capture group {0}")]
    UnexpectedCaptureGroupName(String),
    #[error("Failed to read a file: {0}")]
    FileRead(#[from] std::io::Error),
    #[error("No match")]
    PatternNotMatched,
}

impl Clone for Error {
    fn clone(&self) -> Self {
        match self {
            Error::Utf8(err) => Error::Utf8(*err),
            Error::FromUtf8(err) => Error::FromUtf8(err.clone()),
            Error::Regex(err) => Error::Regex(err.clone()),
            Error::BarcodeCaptureGroupNotFound(barcode_type) => {
                Error::BarcodeCaptureGroupNotFound(barcode_type.clone())
            }
            Error::UnexpectedCaptureGroupName(capture_group) => {
                Error::UnexpectedCaptureGroupName(capture_group.clone())
            }
            Error::FileRead(err) => Error::FileRead(err.kind().into()),
            Error::PatternNotMatched => Error::PatternNotMatched,
        }
    }
}