From 40917f5c4d7689a15ac9b9f2c76c3e4ab9f9ecd1 Mon Sep 17 00:00:00 2001 From: mcmah309 Date: Mon, 18 Nov 2024 22:39:47 +0000 Subject: [PATCH] feat: Add back guard methods --- README.md | 15 +++++++++------ lib/anyhow.dart | 2 +- lib/rust.dart | 5 +++++ lib/src/guard.dart | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 lib/rust.dart create mode 100644 lib/src/guard.dart diff --git a/README.md b/README.md index ea147ee..80e1c8b 100644 --- a/README.md +++ b/README.md @@ -162,25 +162,28 @@ import 'package:rust/rust.dart' as rust; typedef Result = rust.Result ``` -Thus they can be used together - +Thus inheriting all of its capabilities and the two types can be used together - ```dart import 'package:anyhow/anyhow.dart' as anyhow; import 'package:rust/rust.dart'; void main(){ Result x = Ok(1); - anyhow.Result y = Ok(1); // or - Ok(1).context(1); + x.context(2); + anyhow.Result y = Ok(1); + y.context(2); + assert(x == y); + Cell(1); // e.g. `rust` package type } ``` or ```dart -import 'package:anyhow/anyhow.dart'; -import 'package:rust/rust.dart' hide Result; +import 'package:anyhow/rust.dart'; // overlays anyhow on top of the `rust` package void main(){ Result x = Ok(1); - Ok(1).context(1); + x.context(1); + Cell(1); // e.g. `rust` package type } ``` diff --git a/lib/anyhow.dart b/lib/anyhow.dart index 29d270d..2f60203 100644 --- a/lib/anyhow.dart +++ b/lib/anyhow.dart @@ -2,7 +2,7 @@ library anyhow; export 'src/error.dart'; +export 'src/guard.dart'; -export 'package:rust/src/result/guard.dart'; export 'package:rust/src/result/record_to_result_extensions.dart'; export 'package:rust/src/result/result_extensions.dart'; diff --git a/lib/rust.dart b/lib/rust.dart new file mode 100644 index 0000000..4b7f8b1 --- /dev/null +++ b/lib/rust.dart @@ -0,0 +1,5 @@ +library rust; + +export 'anyhow.dart'; +export 'package:rust/rust.dart' + hide Result, Ok, Err, guard, guardAsync, guardAsyncResult, guardResult, FutureResult; diff --git a/lib/src/guard.dart b/lib/src/guard.dart new file mode 100644 index 0000000..f1ea86f --- /dev/null +++ b/lib/src/guard.dart @@ -0,0 +1,41 @@ +import 'package:anyhow/anyhow.dart'; + +/// Executes the function in a protected context. [func] is called inside a try catch block. If the result is not +/// catch, then return value [func] returned inside an [Ok]. If [func] throws, then the thrown value is returned +/// inside an [Err]. +Result guard(S Function() func) { + assert(S is! Result, "Use guardResult instead"); + try { + return Ok(func()); + } catch (e) { + return Err(Error(e)); + } +} + +/// Result unwrapping version of [guard]. Where [func] returns an [Result], but can still throw. +Result guardResult(Result Function() func) { + try { + return func(); + } catch (e) { + return Err(Error(e)); + } +} + +/// Async version of [guard] +FutureResult guardAsync(Future Function() func) async { + assert(S is! Result, "Use guardAsyncResult instead"); + try { + return Ok(await func()); + } catch (e) { + return Err(Error(e)); + } +} + +/// Async version of [guardResult] +FutureResult guardAsyncResult(Future> Function() func) async { + try { + return await func(); + } catch (e) { + return Err(Error(e)); + } +} \ No newline at end of file