From a59a41f2a7aa611215a75e39e9cc5e50b42363a9 Mon Sep 17 00:00:00 2001 From: yggverse Date: Sat, 23 Nov 2024 19:28:25 +0200 Subject: [PATCH] implement Display trait for Error --- src/profile/bookmark/memory/error.rs | 10 ++++++---- src/profile/identity/error.rs | 10 ++++++---- src/profile/identity/gemini/auth/error.rs | 10 ++++++---- src/profile/identity/gemini/auth/memory/error.rs | 12 +++++++----- src/profile/identity/gemini/error.rs | 14 ++++++++------ 5 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/profile/bookmark/memory/error.rs b/src/profile/bookmark/memory/error.rs index b048c121..8a91ad46 100644 --- a/src/profile/bookmark/memory/error.rs +++ b/src/profile/bookmark/memory/error.rs @@ -1,16 +1,18 @@ +use std::fmt::{Display, Formatter, Result}; + #[derive(Debug)] pub enum Error { Overwrite(String), Unexpected, } -impl Error { - pub fn to_string(&self) -> String { +impl Display for Error { + fn fmt(&self, f: &mut Formatter) -> Result { match self { Self::Overwrite(key) => { - format!("Overwrite attempt for existing record `{key}`") + write!(f, "Overwrite attempt for existing record `{key}`") } - Self::Unexpected => "Unexpected error".to_string(), + Self::Unexpected => write!(f, "Unexpected error"), } } } diff --git a/src/profile/identity/error.rs b/src/profile/identity/error.rs index feb7a678..6e49458e 100644 --- a/src/profile/identity/error.rs +++ b/src/profile/identity/error.rs @@ -1,17 +1,19 @@ +use std::fmt::{Display, Formatter, Result}; + #[derive(Debug)] pub enum Error { Database(sqlite::Error), Gemini(super::gemini::Error), } -impl Error { - pub fn to_string(&self) -> String { +impl Display for Error { + fn fmt(&self, f: &mut Formatter) -> Result { match self { Self::Database(reason) => { - format!("Database error: {}", reason) + write!(f, "Database error: {reason}") } Self::Gemini(reason) => { - format!("Could not init Gemini identity: {}", reason.to_string()) + write!(f, "Could not init Gemini identity: {reason}") } } } diff --git a/src/profile/identity/gemini/auth/error.rs b/src/profile/identity/gemini/auth/error.rs index 5cbee27e..eb878428 100644 --- a/src/profile/identity/gemini/auth/error.rs +++ b/src/profile/identity/gemini/auth/error.rs @@ -1,14 +1,16 @@ +use std::fmt::{Display, Formatter, Result}; + #[derive(Debug)] pub enum Error { Database(sqlite::Error), Memory(super::memory::Error), } -impl Error { - pub fn to_string(&self) -> String { +impl Display for Error { + fn fmt(&self, f: &mut Formatter) -> Result { match self { - Self::Database(reason) => format!("Database error: {}", reason), - Self::Memory(reason) => format!("Memory error: {}", reason.to_string()), + Self::Database(reason) => write!(f, "Database error: {reason}"), + Self::Memory(reason) => write!(f, "Memory error: {reason}"), } } } diff --git a/src/profile/identity/gemini/auth/memory/error.rs b/src/profile/identity/gemini/auth/memory/error.rs index e37ce1a7..b4124dd5 100644 --- a/src/profile/identity/gemini/auth/memory/error.rs +++ b/src/profile/identity/gemini/auth/memory/error.rs @@ -1,3 +1,5 @@ +use std::fmt::{Display, Formatter, Result}; + #[derive(Debug)] pub enum Error { Clear, @@ -5,14 +7,14 @@ pub enum Error { Unexpected, } -impl Error { - pub fn to_string(&self) -> String { +impl Display for Error { + fn fmt(&self, f: &mut Formatter) -> Result { match self { - Self::Clear => "Could not cleanup memory index".to_string(), + Self::Clear => write!(f, "Could not cleanup memory index"), Self::Overwrite(key) => { - format!("Overwrite attempt for existing record `{key}`") + write!(f, "Overwrite attempt for existing record `{key}`") } - Self::Unexpected => "Unexpected error".to_string(), + Self::Unexpected => write!(f, "Unexpected error"), } } } diff --git a/src/profile/identity/gemini/error.rs b/src/profile/identity/gemini/error.rs index 6519439c..ef07b6d8 100644 --- a/src/profile/identity/gemini/error.rs +++ b/src/profile/identity/gemini/error.rs @@ -1,3 +1,5 @@ +use std::fmt::{Display, Formatter, Result}; + #[derive(Debug)] pub enum Error { Auth(super::auth::Error), @@ -6,17 +8,17 @@ pub enum Error { Memory(super::memory::Error), } -impl Error { - pub fn to_string(&self) -> String { +impl Display for Error { + fn fmt(&self, f: &mut Formatter) -> Result { match self { - Self::Auth(reason) => format!("Could not create auth: {}", reason.to_string()), + Self::Auth(reason) => write!(f, "Could not create auth: {reason}"), Self::Certificate(reason) => { - format!("Could not create certificate: {}", reason) + write!(f, "Could not create certificate: {reason}") } Self::Database(reason) => { - format!("Database error: {}", reason) + write!(f, "Database error: {reason}") } - Self::Memory(reason) => format!("Memory error: {}", reason.to_string()), + Self::Memory(reason) => write!(f, "Memory error: {reason}"), } } }