mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-28 21:44:21 +01:00
syntactic changes: factor duplicated code into helper function that can be reused
This commit is contained in:
@@ -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 })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user