syntactic changes: factor duplicated code into helper function that can be reused

This commit is contained in:
Jorge López
2025-01-18 18:50:03 +01:00
parent bbe3cded38
commit 683125fefb

View File

@@ -267,20 +267,26 @@ impl Connection {
#[allow(clippy::arc_with_non_send_sync)]
#[pyfunction]
pub fn connect(path: &str) -> Result<Connection> {
#[inline(always)]
fn open_or(
io: Arc<dyn limbo_core::IO>,
path: &str,
) -> std::result::Result<Arc<limbo_core::Database>, PyErr> {
limbo_core::Database::open_file(io, path).map_err(|e| {
PyErr::new::<DatabaseError, _>(format!("Failed to open database: {:?}", e))
})
}
match path {
":memory:" => {
let io: Arc<dyn limbo_core::IO> = Arc::new(limbo_core::MemoryIO::new()?);
let db = limbo_core::Database::open_file(io.clone(), path).map_err(|e| {
PyErr::new::<DatabaseError, _>(format!("Failed to open database: {:?}", e))
})?;
let db = open_or(io.clone(), path)?;
let conn: Rc<limbo_core::Connection> = db.connect();
Ok(Connection { conn, io })
}
path => {
let io: Arc<dyn limbo_core::IO> = Arc::new(limbo_core::PlatformIO::new()?);
let db = limbo_core::Database::open_file(io.clone(), path).map_err(|e| {
PyErr::new::<DatabaseError, _>(format!("Failed to open database: {:?}", e))
})?;
let db = open_or(io.clone(), path)?;
let conn: Rc<limbo_core::Connection> = db.connect();
Ok(Connection { conn, io })
}