Add single-threaded SimpleVault
This commit is contained in:
parent
b432f0ac37
commit
e7c555aa2d
2 changed files with 46 additions and 0 deletions
45
src/simple.rs
Normal file
45
src/simple.rs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
//! A simple, single-threaded vault.
|
||||
//!
|
||||
//! This vault may be useful if you want to re-use existing [`Action`]s and
|
||||
//! [`Migration`]s but don't need the additional guarantees and overhead of the
|
||||
//! other vaults.
|
||||
|
||||
use rusqlite::Connection;
|
||||
|
||||
use crate::{Action, Migration};
|
||||
|
||||
/// A simple, single-threaded vault.
|
||||
pub struct SimpleVault(Connection);
|
||||
|
||||
impl SimpleVault {
|
||||
/// Create a new vault from an existing [`Connection`], applying the
|
||||
/// migrations in the process.
|
||||
pub fn new(conn: Connection, migrations: &[Migration]) -> rusqlite::Result<Self> {
|
||||
Self::new_and_prepare(conn, migrations, |_| Ok(()))
|
||||
}
|
||||
|
||||
/// Create a new vault from an existing [`Connection`], applying the
|
||||
/// migrations in the process.
|
||||
///
|
||||
/// The `prepare` parameter allows access to the database after all
|
||||
/// migrations have occurred. This parameter could be replaced by executing
|
||||
/// an [`Action`] performing the same operations.
|
||||
pub fn new_and_prepare(
|
||||
mut conn: Connection,
|
||||
migrations: &[Migration],
|
||||
prepare: impl FnOnce(&mut Connection) -> rusqlite::Result<()>,
|
||||
) -> rusqlite::Result<Self> {
|
||||
crate::migrate(&mut conn, migrations)?;
|
||||
prepare(&mut conn)?;
|
||||
Ok(Self(conn))
|
||||
}
|
||||
|
||||
/// Execute an [`Action`] and return the result.
|
||||
pub fn execute<A>(&mut self, action: A) -> rusqlite::Result<A::Result>
|
||||
where
|
||||
A: Action + Send + 'static,
|
||||
A::Result: Send,
|
||||
{
|
||||
action.run(&mut self.0)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue