-
Hello! I've created a builder that creates a Is there a way for me to require that the developer explicitly /// SqliteAdapter
///
/// An adapter for the application that implements SQLite.
pub struct SqliteAdapter(Connection);
#[bon]
impl SqliteAdapter {
/// Creates a new SqliteAdapter
#[builder]
fn path(path: PathBuf) -> Result<Self, AdapterError> {
let cnx = Connection::open(path)?;
Ok(SqliteAdapter(cnx))
}
/// Creates an in-memory SqliteAdapter for the purpose of testing
#[builder]
fn in_memory() -> Self {
match Connection::open_in_memory() {
Ok(cnx) => SqliteAdapter(cnx),
_ => panic!("Could not open Sqlite connection in memory."),
}
}
/// Run migrations – should be called before SqliteAdapter is used
fn run_migrations(&mut self) -> Result<(), rustqlite::Error>;
}
// Ideal syntax would look like:
// SqliteAdapter::in_memory().run_migrations().call();
// And ideally:
// SqliteAdapter::in_memory().call() // Should panic or warn developer. I looked into Thanks again for this great crate– I use it all the time :) |
Beta Was this translation helpful? Give feedback.
Answered by
lkdm
Sep 28, 2024
Replies: 1 comment 2 replies
-
Forgive me because I'm a Rust noob– but having thought about it, the body of the method is the finishing function: #[builder(finish_fn = init)]
fn in_memory() -> Self {
match Connection::open_in_memory() {
Ok(cnx) => {
let mut adapter = SqliteAdapter(cnx);
adapter.run_migrations().expect("Migrations should run as expected");
adapter
}
_ => panic!("Could not open Sqlite connection in memory."),
}
} |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
lkdm
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Forgive me because I'm a Rust noob– but having thought about it, the body of the method is the finishing function: