implement Display trait for Error

This commit is contained in:
yggverse 2024-11-23 19:28:25 +02:00
parent a10825e91a
commit a59a41f2a7
5 changed files with 33 additions and 23 deletions

View file

@ -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}"),
}
}
}

View file

@ -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"),
}
}
}