From a7aa55adef91477f0118269fd6c53f8c4072da5c Mon Sep 17 00:00:00 2001 From: Orar Date: Sun, 29 Oct 2023 12:32:12 +0000 Subject: [PATCH] Implement std::error::Error for ImageError --- src/convert/image.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/convert/image.rs b/src/convert/image.rs index 66ec259..b4b953d 100644 --- a/src/convert/image.rs +++ b/src/convert/image.rs @@ -22,6 +22,8 @@ //! # } //! ``` +use std::error::Error; +use std::fmt::{Formatter, Write}; use std::io; use crate::QRCode; @@ -52,6 +54,18 @@ pub enum ImageError { EncodingError(String), } +impl std::error::Error for ImageError {} + +impl std::fmt::Display for ImageError { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + match self { + ImageError::IoError(io_err) => f.write_str(io_err.to_string().as_str()), + ImageError::ImageError(error) => f.write_str(error.as_str()), + ImageError::EncodingError(error) => f.write_str(error.as_str()) + } + } +} + /// Creates an ImageBuilder instance, which contains an [`SvgBuilder`] impl Default for ImageBuilder { fn default() -> Self {