From 27fcc7a6a3dee7777449c0517a11838310aa164d Mon Sep 17 00:00:00 2001 From: pml68 Date: Sat, 26 Oct 2024 22:22:45 +0200 Subject: feat: implement `std::error::Error` for custom `Error` enum --- iced_builder/Cargo.toml | 1 + iced_builder/src/lib.rs | 36 +++++++++++------------------------- 2 files changed, 12 insertions(+), 25 deletions(-) (limited to 'iced_builder') diff --git a/iced_builder/Cargo.toml b/iced_builder/Cargo.toml index d788bc2..d08d485 100644 --- a/iced_builder/Cargo.toml +++ b/iced_builder/Cargo.toml @@ -19,6 +19,7 @@ rfd = "0.15.0" rust-format = "0.3.4" unique_id = "0.1.5" indexmap = { version = "2.6.0", features = ["serde"] } +thiserror = "1.0.65" [[bin]] name = "iced-builder" diff --git a/iced_builder/src/lib.rs b/iced_builder/src/lib.rs index 1c7af06..14a044e 100644 --- a/iced_builder/src/lib.rs +++ b/iced_builder/src/lib.rs @@ -8,41 +8,27 @@ use types::{ element_name::ElementName, project::Project, rendered_element::RenderedElement, DesignerPage, }; -#[derive(Debug, Clone)] +use thiserror::Error; + +#[derive(Debug, Clone, Error)] pub enum Error { - IOError(std::io::ErrorKind), + #[error("an IO error accured: {0}")] + IOError(String), + #[error("a Serde error accured: {0}")] SerdeError(String), + #[error("an RustFmt error accured: {0}")] FormatError(String), + #[error("the element tree contains no matching element")] NonExistentElement, + #[error("the file dialog has been closed without selecting a valid option")] DialogClosed, + #[error("{0}")] String(String), } -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::SerdeError(string) | Self::FormatError(string) | Self::String(string) => { - write!(f, "{}", string) - } - Self::IOError(kind) => { - write!(f, "{}", kind) - } - Self::NonExistentElement => { - write!(f, "The element tree contains no matching element.") - } - Self::DialogClosed => { - write!( - f, - "The file dialog has been closed without selecting a valid option." - ) - } - } - } -} - impl From for Error { fn from(value: std::io::Error) -> Self { - Self::IOError(value.kind()) + Self::IOError(value.to_string()) } } -- cgit v1.2.3