escpos_rs/
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    /// If no image mode was detected during image printing
21    NoImageModeFound,
22    /// Indicates that a builder method was called on the wrong printer connection
23    UnsupportedForPrinterConnection,
24    PrinterError(String),
25    WrongMarkdown,
26    NoTables,
27    NoTableFound(String),
28    NoWidth,
29    NoQrContent(String),
30    NoQrContents,
31    Encoding
32}
33
34impl std::fmt::Display for Error {
35    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
36        let content = match self {
37            Error::RusbError(e) => format!("rusb error: {}", e),
38            Error::CP437Error(detail) => format!("CP437 error: {}", detail),
39            Error::ImageError(e) => format!("Image error: {}", e),
40            Error::NoBulkEndpoint => "No bulk endpoint could be found".to_string(),
41            Error::NoReplacementFound(replacement) => format!("Could not find replacement for tag {{{}}}", replacement),
42            Error::NoPrintData => "Print data must be supplied for this instruction".to_string(),
43            Error::UnsupportedFont => "The specified font does not seem to be supported by the printer profile".to_string(),
44            Error::NoFontFound => "No Font was found in the profile".to_string(),
45            Error::NoImageModeFound => "Image mode was not found in the profile".to_string(),
46            Error::UnsupportedForPrinterConnection => "The called method does not work with the current printer connection".to_string(),
47            Error::PrinterError(detail) => format!("An error occured while printing, {}", detail),
48            Error::WrongMarkdown => "Incorrect markdown structure".to_string(),
49            Error::NoTables => "Not a single table was found in the PrintData structure".to_string(),
50            Error::NoTableFound(table) => format!("No table was found for id {{{}}}", table),
51            Error::NoWidth => "No width was found for the selected font".to_string(),
52            Error::NoQrContent(name) => format!("Could not find qr code content for \"{}\"", name),
53            Error::NoQrContents => "Could not find qr contents".to_string(),
54            Error::Encoding => "An unsupported utf-8 character was found when passing to cp437".to_string()
55        };
56        write!(formatter, "{}", content)
57    }
58}
59
60impl std::error::Error for Error{}