Files
escarmouche/src/util.rs
T
2023-02-11 10:27:24 -05:00

54 lines
1.3 KiB
Rust

use std::io;
use rusqlite::{Connection, OpenFlags};
#[derive(Debug)]
pub enum Error {
SqlError(rusqlite::Error),
InvalidDrawer(String),
InvalidAmount(String),
InvalidPayee(String),
DoubleSpendingError(String),
IOError(io::Error),
Testing(String),
}
pub type CustomResult<T, E = Error> = Result<T, E>;
trait SQLiteConnectionFactory {
fn connection(&self) -> Connection;
fn readonly(&self) -> Connection;
}
pub struct PathSQLiteConnectionFactory {
path: String,
}
impl PathSQLiteConnectionFactory {
pub fn new(path: String) -> PathSQLiteConnectionFactory {
PathSQLiteConnectionFactory { path }
}
}
impl SQLiteConnectionFactory for PathSQLiteConnectionFactory {
fn connection(&self) -> Connection {
Connection::open(&self.path).unwrap()
}
fn readonly(&self) -> Connection {
Connection::open_with_flags(&self.path, OpenFlags::SQLITE_OPEN_READ_ONLY).unwrap()
}
}
pub struct InMemorySQLiteConnectionFactory {}
impl SQLiteConnectionFactory for InMemorySQLiteConnectionFactory {
fn connection(&self) -> Connection {
Connection::open_in_memory().unwrap()
}
fn readonly(&self) -> Connection {
Connection::open_in_memory_with_flags(OpenFlags::SQLITE_OPEN_READ_ONLY).unwrap()
}
}