@@ -45,6 +45,14 @@ fn ensure_runtime() -> &'static Runtime {
4545 } )
4646}
4747
48+ /// Helper function to get a reference to the activity database connections
49+ fn get_activity_db ( ) -> Result < std:: sync:: MutexGuard < ' static , DatabaseConnections > , ActivityError > {
50+ let cell = DB . get ( ) . ok_or ( ActivityError :: ConnectionError {
51+ error_details : "Database not initialized. Call init_db first." . to_string ( )
52+ } ) ?;
53+ Ok ( cell. lock ( ) . unwrap ( ) )
54+ }
55+
4856#[ uniffi:: export]
4957pub async fn decode ( invoice : String ) -> Result < Scanner , DecodingError > {
5058 let rt = ensure_runtime ( ) ;
@@ -290,10 +298,7 @@ pub fn get_activities(
290298 limit : Option < u32 > ,
291299 sort_direction : Option < SortDirection >
292300) -> Result < Vec < Activity > , ActivityError > {
293- let cell = DB . get ( ) . ok_or ( ActivityError :: ConnectionError {
294- error_details : "Database not initialized. Call init_db first." . to_string ( )
295- } ) ?;
296- let guard = cell. lock ( ) . unwrap ( ) ;
301+ let guard = get_activity_db ( ) ?;
297302 let db = guard. activity_db . as_ref ( ) . ok_or ( ActivityError :: ConnectionError {
298303 error_details : "Database not initialized. Call init_db first." . to_string ( )
299304 } ) ?;
@@ -302,10 +307,7 @@ pub fn get_activities(
302307
303308#[ uniffi:: export]
304309pub fn upsert_activity ( activity : Activity ) -> Result < ( ) , ActivityError > {
305- let cell = DB . get ( ) . ok_or ( ActivityError :: ConnectionError {
306- error_details : "Database not initialized. Call init_db first." . to_string ( )
307- } ) ?;
308- let mut guard = cell. lock ( ) . unwrap ( ) ;
310+ let mut guard = get_activity_db ( ) ?;
309311 let db = guard. activity_db . as_mut ( ) . ok_or ( ActivityError :: ConnectionError {
310312 error_details : "Database not initialized. Call init_db first." . to_string ( )
311313 } ) ?;
@@ -314,10 +316,7 @@ pub fn upsert_activity(activity: Activity) -> Result<(), ActivityError> {
314316
315317#[ uniffi:: export]
316318pub fn insert_activity ( activity : Activity ) -> Result < ( ) , ActivityError > {
317- let cell = DB . get ( ) . ok_or ( ActivityError :: ConnectionError {
318- error_details : "Database not initialized. Call init_db first." . to_string ( )
319- } ) ?;
320- let mut guard = cell. lock ( ) . unwrap ( ) ;
319+ let mut guard = get_activity_db ( ) ?;
321320 let db = guard. activity_db . as_mut ( ) . ok_or ( ActivityError :: ConnectionError {
322321 error_details : "Database not initialized. Call init_db first." . to_string ( )
323322 } ) ?;
@@ -329,10 +328,7 @@ pub fn insert_activity(activity: Activity) -> Result<(), ActivityError> {
329328
330329#[ uniffi:: export]
331330pub fn update_activity ( activity_id : String , activity : Activity ) -> Result < ( ) , ActivityError > {
332- let cell = DB . get ( ) . ok_or ( ActivityError :: ConnectionError {
333- error_details : "Database not initialized. Call init_db first." . to_string ( )
334- } ) ?;
335- let mut guard = cell. lock ( ) . unwrap ( ) ;
331+ let mut guard = get_activity_db ( ) ?;
336332 let db = guard. activity_db . as_mut ( ) . ok_or ( ActivityError :: ConnectionError {
337333 error_details : "Database not initialized. Call init_db first." . to_string ( )
338334 } ) ?;
@@ -344,10 +340,7 @@ pub fn update_activity(activity_id: String, activity: Activity) -> Result<(), Ac
344340
345341#[ uniffi:: export]
346342pub fn get_activity_by_id ( activity_id : String ) -> Result < Option < Activity > , ActivityError > {
347- let cell = DB . get ( ) . ok_or ( ActivityError :: ConnectionError {
348- error_details : "Database not initialized. Call init_db first." . to_string ( )
349- } ) ?;
350- let guard = cell. lock ( ) . unwrap ( ) ;
343+ let guard = get_activity_db ( ) ?;
351344 let db = guard. activity_db . as_ref ( ) . ok_or ( ActivityError :: ConnectionError {
352345 error_details : "Database not initialized. Call init_db first." . to_string ( )
353346 } ) ?;
@@ -356,10 +349,7 @@ pub fn get_activity_by_id(activity_id: String) -> Result<Option<Activity>, Activ
356349
357350#[ uniffi:: export]
358351pub fn delete_activity_by_id ( activity_id : String ) -> Result < bool , ActivityError > {
359- let cell = DB . get ( ) . ok_or ( ActivityError :: ConnectionError {
360- error_details : "Database not initialized. Call init_db first." . to_string ( )
361- } ) ?;
362- let mut guard = cell. lock ( ) . unwrap ( ) ;
352+ let mut guard = get_activity_db ( ) ?;
363353 let db = guard. activity_db . as_mut ( ) . ok_or ( ActivityError :: ConnectionError {
364354 error_details : "Database not initialized. Call init_db first." . to_string ( )
365355 } ) ?;
@@ -368,10 +358,7 @@ pub fn delete_activity_by_id(activity_id: String) -> Result<bool, ActivityError>
368358
369359#[ uniffi:: export]
370360pub fn add_tags ( activity_id : String , tags : Vec < String > ) -> Result < ( ) , ActivityError > {
371- let cell = DB . get ( ) . ok_or ( ActivityError :: ConnectionError {
372- error_details : "Database not initialized. Call init_db first." . to_string ( )
373- } ) ?;
374- let mut guard = cell. lock ( ) . unwrap ( ) ;
361+ let mut guard = get_activity_db ( ) ?;
375362 let db = guard. activity_db . as_mut ( ) . ok_or ( ActivityError :: ConnectionError {
376363 error_details : "Database not initialized. Call init_db first." . to_string ( )
377364 } ) ?;
@@ -380,10 +367,7 @@ pub fn add_tags(activity_id: String, tags: Vec<String>) -> Result<(), ActivityEr
380367
381368#[ uniffi:: export]
382369pub fn remove_tags ( activity_id : String , tags : Vec < String > ) -> Result < ( ) , ActivityError > {
383- let cell = DB . get ( ) . ok_or ( ActivityError :: ConnectionError {
384- error_details : "Database not initialized. Call init_db first." . to_string ( )
385- } ) ?;
386- let mut guard = cell. lock ( ) . unwrap ( ) ;
370+ let mut guard = get_activity_db ( ) ?;
387371 let db = guard. activity_db . as_mut ( ) . ok_or ( ActivityError :: ConnectionError {
388372 error_details : "Database not initialized. Call init_db first." . to_string ( )
389373 } ) ?;
@@ -392,10 +376,7 @@ pub fn remove_tags(activity_id: String, tags: Vec<String>) -> Result<(), Activit
392376
393377#[ uniffi:: export]
394378pub fn get_tags ( activity_id : String ) -> Result < Vec < String > , ActivityError > {
395- let cell = DB . get ( ) . ok_or ( ActivityError :: ConnectionError {
396- error_details : "Database not initialized. Call init_db first." . to_string ( )
397- } ) ?;
398- let guard = cell. lock ( ) . unwrap ( ) ;
379+ let guard = get_activity_db ( ) ?;
399380 let db = guard. activity_db . as_ref ( ) . ok_or ( ActivityError :: ConnectionError {
400381 error_details : "Database not initialized. Call init_db first." . to_string ( )
401382 } ) ?;
@@ -404,10 +385,7 @@ pub fn get_tags(activity_id: String) -> Result<Vec<String>, ActivityError> {
404385
405386#[ uniffi:: export]
406387pub fn get_activities_by_tag ( tag : String , limit : Option < u32 > , sort_direction : Option < SortDirection > ) -> Result < Vec < Activity > , ActivityError > {
407- let cell = DB . get ( ) . ok_or ( ActivityError :: ConnectionError {
408- error_details : "Database not initialized. Call init_db first." . to_string ( )
409- } ) ?;
410- let guard = cell. lock ( ) . unwrap ( ) ;
388+ let guard = get_activity_db ( ) ?;
411389 let db = guard. activity_db . as_ref ( ) . ok_or ( ActivityError :: ConnectionError {
412390 error_details : "Database not initialized. Call init_db first." . to_string ( )
413391 } ) ?;
@@ -416,10 +394,7 @@ pub fn get_activities_by_tag(tag: String, limit: Option<u32>, sort_direction: Op
416394
417395#[ uniffi:: export]
418396pub fn get_all_unique_tags ( ) -> Result < Vec < String > , ActivityError > {
419- let cell = DB . get ( ) . ok_or ( ActivityError :: ConnectionError {
420- error_details : "Database not initialized. Call init_db first." . to_string ( )
421- } ) ?;
422- let guard = cell. lock ( ) . unwrap ( ) ;
397+ let guard = get_activity_db ( ) ?;
423398 let db = guard. activity_db . as_ref ( ) . ok_or ( ActivityError :: ConnectionError {
424399 error_details : "Database not initialized. Call init_db first." . to_string ( )
425400 } ) ?;
@@ -1248,10 +1223,7 @@ pub fn trezor_compose_transaction(
12481223
12491224#[ uniffi:: export]
12501225pub fn activity_wipe_all ( ) -> Result < ( ) , ActivityError > {
1251- let cell = DB . get ( ) . ok_or ( ActivityError :: ConnectionError {
1252- error_details : "Database not initialized. Call init_db first." . to_string ( )
1253- } ) ?;
1254- let mut guard = cell. lock ( ) . unwrap ( ) ;
1226+ let mut guard = get_activity_db ( ) ?;
12551227 let db = guard. activity_db . as_mut ( ) . ok_or ( ActivityError :: ConnectionError {
12561228 error_details : "Database not initialized. Call init_db first." . to_string ( )
12571229 } ) ?;
@@ -1307,30 +1279,32 @@ pub async fn blocktank_wipe_all() -> Result<(), BlocktankError> {
13071279pub async fn wipe_all_databases ( ) -> Result < String , DbError > {
13081280 let rt = ensure_runtime ( ) ;
13091281
1310- // Wipe activity database
1282+ // Wipe activity database - require it to be initialized
13111283 {
13121284 let cell = DB . get ( ) . ok_or ( DbError :: InitializationError {
13131285 error_details : "Database not initialized. Call init_db first." . to_string ( )
13141286 } ) ?;
13151287 let mut guard = cell. lock ( ) . unwrap ( ) ;
1316- if let Some ( db) = guard. activity_db . as_mut ( ) {
1317- db. wipe_all ( ) . map_err ( |e| DbError :: InitializationError {
1318- error_details : format ! ( "Failed to wipe activity database: {}" , e)
1319- } ) ?;
1320- }
1288+ let db = guard. activity_db . as_mut ( ) . ok_or ( DbError :: InitializationError {
1289+ error_details : "Activity database not initialized. Call init_db first." . to_string ( )
1290+ } ) ?;
1291+ db. wipe_all ( ) . map_err ( |e| DbError :: InitializationError {
1292+ error_details : format ! ( "Failed to wipe activity database: {}" , e)
1293+ } ) ?;
13211294 }
13221295
1323- // Wipe blocktank database
1296+ // Wipe blocktank database - require it to be initialized
13241297 rt. spawn ( async move {
13251298 let cell = ASYNC_DB . get ( ) . ok_or ( DbError :: InitializationError {
13261299 error_details : "Database not initialized. Call init_db first." . to_string ( )
13271300 } ) ?;
13281301 let guard = cell. lock ( ) . await ;
1329- if let Some ( db) = guard. blocktank_db . as_ref ( ) {
1330- db. wipe_all ( ) . await . map_err ( |e| DbError :: InitializationError {
1331- error_details : format ! ( "Failed to wipe blocktank database: {}" , e)
1332- } ) ?;
1333- }
1302+ let db = guard. blocktank_db . as_ref ( ) . ok_or ( DbError :: InitializationError {
1303+ error_details : "Blocktank database not initialized. Call init_db first." . to_string ( )
1304+ } ) ?;
1305+ db. wipe_all ( ) . await . map_err ( |e| DbError :: InitializationError {
1306+ error_details : format ! ( "Failed to wipe blocktank database: {}" , e)
1307+ } ) ?;
13341308 Ok :: < ( ) , DbError > ( ( ) )
13351309 } ) . await . unwrap ( ) ?;
13361310
0 commit comments