qrcode_generator/
qr_code_error.rs

1use std::{
2    error::Error,
3    fmt::{self, Display, Formatter},
4    io,
5};
6
7#[cfg(feature = "image")]
8use image::ImageError;
9
10#[allow(clippy::upper_case_acronyms)]
11/// Errors when encoding QR code.
12#[derive(Debug)]
13pub enum QRCodeError {
14    DataTooLong,
15    IOError(io::Error),
16    #[cfg(feature = "image")]
17    ImageError(ImageError),
18    ImageSizeTooSmall,
19    ImageSizeTooLarge,
20}
21
22impl From<io::Error> for QRCodeError {
23    #[inline]
24    fn from(error: io::Error) -> Self {
25        QRCodeError::IOError(error)
26    }
27}
28
29#[cfg(feature = "image")]
30impl From<ImageError> for QRCodeError {
31    #[inline]
32    fn from(error: ImageError) -> Self {
33        QRCodeError::ImageError(error)
34    }
35}
36
37impl Display for QRCodeError {
38    #[inline]
39    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
40        match self {
41            QRCodeError::DataTooLong => {
42                f.write_str("the supplied data does not fit any QR Code version")
43            },
44            QRCodeError::IOError(error) => Display::fmt(error, f),
45            #[cfg(feature = "image")]
46            QRCodeError::ImageError(error) => Display::fmt(error, f),
47            QRCodeError::ImageSizeTooSmall => {
48                f.write_str("image size is too small to draw the whole QR code")
49            },
50            QRCodeError::ImageSizeTooLarge => f.write_str("image size is too large to generate"),
51        }
52    }
53}
54
55impl Error for QRCodeError {}