diff options
author | Joris | 2025-01-31 22:28:53 +0100 |
---|---|---|
committer | Joris | 2025-01-31 22:28:53 +0100 |
commit | 0adf5a093494bdb7f5d5c0f12913133e333ddfad (patch) | |
tree | ada6df0f3480647bec99429819f1bfffd36194ce /src/model/report.rs | |
parent | 24eeb54a6b7159964e8887ade7fa5173b50feb3a (diff) |
Migrate to tokio_rusqlite
Diffstat (limited to 'src/model/report.rs')
-rw-r--r-- | src/model/report.rs | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/src/model/report.rs b/src/model/report.rs index 4858402..e944745 100644 --- a/src/model/report.rs +++ b/src/model/report.rs @@ -1,6 +1,7 @@ -use serde::Serialize; +use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ValueRef}; +use std::fmt; -#[derive(Debug, sqlx::FromRow, Serialize)] +#[derive(Debug, serde::Serialize)] pub struct Report { pub date: String, pub name: String, @@ -8,9 +9,32 @@ pub struct Report { pub action: Action, } -#[derive(Debug, PartialEq, Serialize, sqlx::Type)] +#[derive(Debug, PartialEq, serde::Serialize)] pub enum Action { Created, Updated, Deleted, } + +impl fmt::Display for Action { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:?}", self) + } +} + +impl FromSql for Action { + fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> { + match value { + ValueRef::Text(text) => match std::str::from_utf8(text) { + Ok("Created") => Ok(Action::Created), + Ok("Updated") => Ok(Action::Updated), + Ok("Deleted") => Ok(Action::Deleted), + Ok(str) => Err(FromSqlError::Other( + format!("Unknown action: {str}").into(), + )), + Err(err) => Err(FromSqlError::Other(err.into())), + }, + _ => Err(FromSqlError::InvalidType), + } + } +} |