@@ -22,13 +22,13 @@ import 'package:flutter/foundation.dart' show kIsWeb;
2222final class AppDbState extends Opaque {}
2323
2424/// Typedef for the rust functions
25- typedef PointerStringFFICallBack = Pointer < Utf8 > Function (
26- Pointer <AppDbState >, Pointer <Utf8 >);
25+ typedef PointerStringFFICallBack =
26+ Pointer <Utf8 > Function ( Pointer < AppDbState >, Pointer <Utf8 >);
2727typedef PointerAppDbStateCallBAck = Pointer <AppDbState > Function (Pointer <Utf8 >);
28- typedef PointerBoolFFICallBack = Pointer < Bool > Function (
29- Pointer <AppDbState >, Pointer <Utf8 >);
30- typedef PointerBoolFFICallBackDirect = Pointer < Bool > Function (
31- Pointer <AppDbState >);
28+ typedef PointerBoolFFICallBack =
29+ Pointer <Bool > Function ( Pointer < AppDbState >, Pointer <Utf8 >);
30+ typedef PointerBoolFFICallBackDirect =
31+ Pointer <Bool > Function ( Pointer < AppDbState >);
3232typedef PointerListFFICallBack = Pointer <Utf8 > Function (Pointer <AppDbState >);
3333
3434class LocalDbBridge extends LocalSbRequestImpl {
@@ -39,7 +39,7 @@ class LocalDbBridge extends LocalSbRequestImpl {
3939 LocalDbResult <DynamicLibrary , String >? _lib;
4040 Pointer <AppDbState >? _dbInstance; // Cambiado de late a nullable
4141 String ?
42- _lastDatabaseName; // Almacena el último nombre de base de datos utilizado
42+ _lastDatabaseName; // Almacena el último nombre de base de datos utilizado
4343
4444 Future <void > initForTesting (String databaseName, String libPath) async {
4545 if (! databaseName.contains ('.db' )) {
@@ -118,19 +118,25 @@ class LocalDbBridge extends LocalSbRequestImpl {
118118 Future <bool > ensureConnectionValid () async {
119119 // Check if the instance pointer is null
120120 if (_dbInstance == null || _dbInstance == nullptr) {
121- log ('Database connection invalid (null pointer), attempting to reinitialize...' );
121+ log (
122+ 'Database connection invalid (null pointer), attempting to reinitialize...' ,
123+ );
122124 return await _attemptReinitialization ();
123125 }
124126
125127 // Check if the Rust instance is still valid
126128 try {
127129 if (! _isDatabaseValid (_dbInstance! )) {
128- log ('Database connection invalid (Rust validation failed), attempting to reinitialize...' );
130+ log (
131+ 'Database connection invalid (Rust validation failed), attempting to reinitialize...' ,
132+ );
129133 await _closeCurrentConnection ();
130134 return await _attemptReinitialization ();
131135 }
132136 } catch (e) {
133- log ('Error validating database connection: $e , attempting to reinitialize...' );
137+ log (
138+ 'Error validating database connection: $e , attempting to reinitialize...' ,
139+ );
134140 await _closeCurrentConnection ();
135141 return await _attemptReinitialization ();
136142 }
@@ -178,32 +184,51 @@ class LocalDbBridge extends LocalSbRequestImpl {
178184 void _bindFunctions () {
179185 switch (_lib) {
180186 case Ok (data: DynamicLibrary lib):
181- _createDatabase = lib.lookupFunction< PointerAppDbStateCallBAck ,
182- PointerAppDbStateCallBAck > (FFiFunctions .createDb.cName);
183- _post = lib.lookupFunction< PointerStringFFICallBack ,
184- PointerStringFFICallBack > (FFiFunctions .pushData.cName);
185- _get =
186- lib.lookupFunction <PointerListFFICallBack , PointerListFFICallBack >(
187- FFiFunctions .getAll.cName);
188- _getById = lib.lookupFunction< PointerStringFFICallBack ,
189- PointerStringFFICallBack > (FFiFunctions .getById.cName);
190- _put = lib.lookupFunction< PointerStringFFICallBack ,
191- PointerStringFFICallBack > (FFiFunctions .updateData.cName);
192- _delete =
193- lib.lookupFunction <PointerBoolFFICallBack , PointerBoolFFICallBack >(
194- FFiFunctions .delete.cName);
195- _clearAllRecords = lib.lookupFunction< PointerBoolFFICallBackDirect ,
196- PointerBoolFFICallBackDirect > (FFiFunctions .clearAllRecords.cName);
197- _closeDatabase = lib.lookupFunction<
198- Pointer <Utf8 > Function (Pointer <AppDbState >),
199- Pointer <Utf8 > Function (
200- Pointer <AppDbState >)> (FFiFunctions .closeDatabase.cName);
201- _freeCString = lib.lookupFunction< Void Function (Pointer <Utf8 >),
202- void Function (Pointer <Utf8 >)> (FFiFunctions .freeCString.cName);
203- _isDatabaseValid = lib.lookupFunction<
204- Bool Function (Pointer <AppDbState >),
205- bool Function (
206- Pointer <AppDbState >)> (FFiFunctions .isDatabaseValid.cName);
187+ _createDatabase = lib
188+ .lookupFunction<
189+ PointerAppDbStateCallBAck ,
190+ PointerAppDbStateCallBAck
191+ > (FFiFunctions .createDb.cName);
192+ _post = lib
193+ .lookupFunction <PointerStringFFICallBack , PointerStringFFICallBack >(
194+ FFiFunctions .pushData.cName,
195+ );
196+ _get = lib
197+ .lookupFunction <PointerListFFICallBack , PointerListFFICallBack >(
198+ FFiFunctions .getAll.cName,
199+ );
200+ _getById = lib
201+ .lookupFunction <PointerStringFFICallBack , PointerStringFFICallBack >(
202+ FFiFunctions .getById.cName,
203+ );
204+ _put = lib
205+ .lookupFunction <PointerStringFFICallBack , PointerStringFFICallBack >(
206+ FFiFunctions .updateData.cName,
207+ );
208+ _delete = lib
209+ .lookupFunction <PointerBoolFFICallBack , PointerBoolFFICallBack >(
210+ FFiFunctions .delete.cName,
211+ );
212+ _clearAllRecords = lib
213+ .lookupFunction<
214+ PointerBoolFFICallBackDirect ,
215+ PointerBoolFFICallBackDirect
216+ > (FFiFunctions .clearAllRecords.cName);
217+ _closeDatabase = lib
218+ .lookupFunction<
219+ Pointer <Utf8 > Function (Pointer <AppDbState >),
220+ Pointer <Utf8 > Function (Pointer <AppDbState >)
221+ > (FFiFunctions .closeDatabase.cName);
222+ _freeCString = lib
223+ .lookupFunction<
224+ Void Function (Pointer <Utf8 >),
225+ void Function (Pointer <Utf8 >)
226+ > (FFiFunctions .freeCString.cName);
227+ _isDatabaseValid = lib
228+ .lookupFunction<
229+ Bool Function (Pointer <AppDbState >),
230+ bool Function (Pointer <AppDbState >)
231+ > (FFiFunctions .isDatabaseValid.cName);
207232 break ;
208233 case Err (error: String error):
209234 log (error);
@@ -220,7 +245,8 @@ class LocalDbBridge extends LocalSbRequestImpl {
220245
221246 if (_dbInstance == nullptr) {
222247 throw Exception (
223- 'Failed to create database instance. Returned null pointer.' );
248+ 'Failed to create database instance. Returned null pointer.' ,
249+ );
224250 }
225251
226252 calloc.free (dbNamePointer);
@@ -232,7 +258,8 @@ class LocalDbBridge extends LocalSbRequestImpl {
232258
233259 @override
234260 Future <LocalDbResult <LocalDbModel , ErrorLocalDb >> post (
235- LocalDbModel model) async {
261+ LocalDbModel model,
262+ ) async {
236263 if (! (await ensureConnectionValid ())) {
237264 return Err (ErrorLocalDb .databaseError ('Database connection is invalid' ));
238265 }
@@ -256,15 +283,21 @@ class LocalDbBridge extends LocalSbRequestImpl {
256283 }
257284
258285 final modelData = LocalDbModel .fromJson (
259- Map <String , dynamic >.from (jsonDecode (response['Ok' ])));
286+ Map <String , dynamic >.from (jsonDecode (response['Ok' ])),
287+ );
260288
261289 return Ok (modelData);
262290 } catch (error, stack) {
263291 log (error.toString ());
264292 log (stack.toString ());
265293 print (stack.toString ());
266- return Err (ErrorLocalDb .fromRustError (error.toString (),
267- originalError: error, stackTrace: stack));
294+ return Err (
295+ ErrorLocalDb .fromRustError (
296+ error.toString (),
297+ originalError: error,
298+ stackTrace: stack,
299+ ),
300+ );
268301 }
269302 }
270303
@@ -300,14 +333,20 @@ class LocalDbBridge extends LocalSbRequestImpl {
300333 } catch (error, stackTrace) {
301334 log (error.toString ());
302335 log (stackTrace.toString ());
303- return Err (ErrorLocalDb .fromRustError (error.toString (),
304- originalError: error, stackTrace: stackTrace));
336+ return Err (
337+ ErrorLocalDb .fromRustError (
338+ error.toString (),
339+ originalError: error,
340+ stackTrace: stackTrace,
341+ ),
342+ );
305343 }
306344 }
307345
308346 @override
309347 Future <LocalDbResult <LocalDbModel , ErrorLocalDb >> put (
310- LocalDbModel model) async {
348+ LocalDbModel model,
349+ ) async {
311350 if (! (await ensureConnectionValid ())) {
312351 return Err (ErrorLocalDb .databaseError ('Database connection is invalid' ));
313352 }
@@ -336,8 +375,13 @@ class LocalDbBridge extends LocalSbRequestImpl {
336375 } catch (error, stackTrace) {
337376 log (error.toString ());
338377 log (stackTrace.toString ());
339- return Err (ErrorLocalDb .fromRustError (error.toString (),
340- originalError: error, stackTrace: stackTrace));
378+ return Err (
379+ ErrorLocalDb .fromRustError (
380+ error.toString (),
381+ originalError: error,
382+ stackTrace: stackTrace,
383+ ),
384+ );
341385 }
342386 }
343387
@@ -355,8 +399,13 @@ class LocalDbBridge extends LocalSbRequestImpl {
355399 } catch (error, stackTrace) {
356400 log (error.toString ());
357401 log (stackTrace.toString ());
358- return Err (ErrorLocalDb .fromRustError (error.toString (),
359- originalError: error, stackTrace: stackTrace));
402+ return Err (
403+ ErrorLocalDb .fromRustError (
404+ error.toString (),
405+ originalError: error,
406+ stackTrace: stackTrace,
407+ ),
408+ );
360409 }
361410 }
362411
@@ -382,8 +431,13 @@ class LocalDbBridge extends LocalSbRequestImpl {
382431 } catch (error, stackTrace) {
383432 log (error.toString ());
384433 log (stackTrace.toString ());
385- return Err (ErrorLocalDb .fromRustError (error.toString (),
386- originalError: error, stackTrace: stackTrace));
434+ return Err (
435+ ErrorLocalDb .fromRustError (
436+ error.toString (),
437+ originalError: error,
438+ stackTrace: stackTrace,
439+ ),
440+ );
387441 }
388442 }
389443
@@ -399,8 +453,11 @@ class LocalDbBridge extends LocalSbRequestImpl {
399453
400454 if (resultFfi == nullptr) {
401455 log ('Error: NULL pointer returned from GetAll FFI call' );
402- return Err (ErrorLocalDb .notFound (
403- 'Failed to retrieve data: null pointer returned' ));
456+ return Err (
457+ ErrorLocalDb .notFound (
458+ 'Failed to retrieve data: null pointer returned' ,
459+ ),
460+ );
404461 }
405462
406463 final resultTransformed = resultFfi.cast <Utf8 >().toDartString ();
@@ -424,15 +481,20 @@ class LocalDbBridge extends LocalSbRequestImpl {
424481 } catch (error, stackTrace) {
425482 log (error.toString ());
426483 log (stackTrace.toString ());
427- return Err (ErrorLocalDb .fromRustError (error.toString (),
428- originalError: error, stackTrace: stackTrace));
484+ return Err (
485+ ErrorLocalDb .fromRustError (
486+ error.toString (),
487+ originalError: error,
488+ stackTrace: stackTrace,
489+ ),
490+ );
429491 }
430492 }
431493}
432494
433495sealed class CurrentPlatform {
434496 static Future <LocalDbResult <DynamicLibrary , String >>
435- loadRustNativeLib () async {
497+ loadRustNativeLib () async {
436498 // Web platform doesn't use FFI, this should not be called on web
437499 if (kIsWeb) {
438500 return Err ("FFI is not supported on web platform" );
0 commit comments