escpos_rust/
error.rs

1/// Errors that this crate throws.
2#[derive(Debug)]
3pub enum Error {
4    /// Error related to rusb
5    RusbError(rusb::Error),
6    /// For text printing, the replaced sequence could not be found
7    CP437Error(String),
8    /// Error regarding image treatment
9    ImageError(image::ImageError),
10    /// This means no bulk endpoint could be found
11    NoBulkEndpoint,
12    /// No replacement string for an instruction was found
13    NoReplacementFound(String),
14    /// PrintData should've been supplied.
15    NoPrintData,
16    /// The specified font does not seem to be supported by the printer profile
17    UnsupportedFont,
18    /// At least one font needs to be available in the profile
19    NoFontFound,
20    /// Indicates that a builder method was called on the wrong printer connection
21    UnsupportedForPrinterConnection,
22    PrinterError(String),
23    WrongMarkdown,
24    NoTables,
25    NoTableFound(String),
26    NoWidth,
27    NoQrContent(String),
28    NoQrContents,
29    Encoding,
30    BarcodeError,
31}
32
33impl std::fmt::Display for Error {
34    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
35        let content = match self {
36            Error::RusbError(e) => format!("rusb error: {}", e),
37            Error::CP437Error(detail) => format!("CP437 error: {}", detail),
38            Error::ImageError(e) => format!("Image error: {}", e),
39            Error::NoBulkEndpoint => "No bulk endpoint could be found".to_string(),
40            Error::NoReplacementFound(replacement) => format!("Could not find replacement for tag {{{}}}", replacement),
41            Error::NoPrintData => "Print data must be supplied for this instruction".to_string(),
42            Error::UnsupportedFont => "The specified font does not seem to be supported by the printer profile".to_string(),
43            Error::NoFontFound => "No Font was found in the profile".to_string(),
44            Error::UnsupportedForPrinterConnection => "The called method does not work with the current printer connection".to_string(),
45            Error::PrinterError(detail) => format!("An error occured while printing, {}", detail),
46            Error::WrongMarkdown => "Incorrect markdown structure".to_string(),
47            Error::NoTables => "Not a single table was found in the PrintData structure".to_string(),
48            Error::NoTableFound(table) => format!("No table was found for id {{{}}}", table),
49            Error::NoWidth => "No width was found for the selected font".to_string(),
50            Error::NoQrContent(name) => format!("Could not find qr code content for \"{}\"", name),
51            Error::NoQrContents => "Could not find qr contents".to_string(),
52            Error::Encoding => "An unsupported utf-8 character was found when passing to cp437".to_string(),
53            Error::BarcodeError => "Barcode Error".to_string()
54        };
55        write!(formatter, "{}", content)
56    }
57}
58
59impl std::error::Error for Error{}