From 47ea7901b73085ead704ec2bd45402ae0c4ce5a2 Mon Sep 17 00:00:00 2001 From: Kevin Hoerr Date: Thu, 26 Feb 2026 21:57:41 +0000 Subject: Use SQLite to store counter --- src/main/rust/store/mod.rs | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) (limited to 'src/main/rust/store/mod.rs') diff --git a/src/main/rust/store/mod.rs b/src/main/rust/store/mod.rs index 42f1809..7c7d9ce 100644 --- a/src/main/rust/store/mod.rs +++ b/src/main/rust/store/mod.rs @@ -1,11 +1,23 @@ -pub mod sqlite; +mod sqlite; -use miette::Result; +use std::num::TryFromIntError; + +use ::sqlite::Error as SQLiteLibraryError; +use miette::{Diagnostic, Result}; +use thiserror::Error; pub enum Context { SQLite(sqlite::SQLite), } +#[derive(Debug, Diagnostic, Error)] +pub enum ContextError { + #[error("SQLite error")] + SQLiteError(#[from] SQLiteLibraryError), + #[error("Int conversion error")] + ConversionError(#[from] TryFromIntError), +} + impl Default for Context { fn default() -> Self { Context::SQLite(sqlite::SQLite::default()) @@ -13,7 +25,23 @@ impl Default for Context { } pub trait Store { - fn init(&mut self) -> Result<()>; + fn init(&mut self) -> Result<(), ContextError>; + fn get_counter(&self) -> Result; + fn set_counter(&mut self, new_count: u8) -> Result; +} + +impl Context { + pub fn unwrap_store(&mut self) -> &mut impl Store { + match self { + Context::SQLite(store) => store, + } + } + + pub fn get_store(&self) -> &impl Store { + match self { + Context::SQLite(store) => store, + } + } } #[cfg(test)] -- cgit v1.3