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

View File

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

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

View File

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