use log::error; use rusqlite::{Connection, Params, Row}; pub fn get(db: &Connection, query: &str, params: P, row_mapper: F) -> rusqlite::Result where F: FnMut(&Row<'_>) -> rusqlite::Result, P: Params, { let mut stmt = match db.prepare(query) { Ok(stmt) => stmt, Err(e) => { error!("Couldn't prepare get statement: {e:?}"); return Err(e); } }; stmt.query_row(params, row_mapper).inspect_err(|e| { error!("Couldn't read from database: {e:?}"); }) } pub fn list( db: &Connection, query: &str, params: P, row_mapper: F, ) -> rusqlite::Result> where F: FnMut(&Row<'_>) -> rusqlite::Result, P: Params, { let mut stmt = match db.prepare(query) { Ok(stmt) => stmt, Err(e) => { error!("Couldn't prepare list statement: {e:?}"); return Err(e); } }; let result = stmt.query_map(params, row_mapper); match result { Ok(res) => { let records: rusqlite::Result> = res.collect(); match records { Ok(records) => Ok(records), Err(e) => { error!("Couldn't read from database: {e:?}"); Err(e) } } } Err(rusqlite::Error::QueryReturnedNoRows) => Ok(vec![]), Err(e) => { error!("Couldn't read from database: {e:?}"); Err(e) } } }