Skip to content

Commit

Permalink
feat: Add back guard methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmah309 committed Nov 18, 2024
1 parent f7758e9 commit 40917f5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,25 +162,28 @@ import 'package:rust/rust.dart' as rust;
typedef Result<S> = rust.Result<S, Error>
```
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<int,anyhow.Error> x = Ok(1);
anyhow.Result<int> y = Ok(1); // or
Ok(1).context(1);
x.context(2);
anyhow.Result<int> 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<int> x = Ok(1);
Ok(1).context(1);
x.context(1);
Cell(1); // e.g. `rust` package type
}
```

Expand Down
2 changes: 1 addition & 1 deletion lib/anyhow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
5 changes: 5 additions & 0 deletions lib/rust.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
library rust;

export 'anyhow.dart';
export 'package:rust/rust.dart'
hide Result, Ok, Err, guard, guardAsync, guardAsyncResult, guardResult, FutureResult;
41 changes: 41 additions & 0 deletions lib/src/guard.dart
Original file line number Diff line number Diff line change
@@ -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<S> guard<S>(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<S> guardResult<S>(Result<S> Function() func) {
try {
return func();
} catch (e) {
return Err(Error(e));
}
}

/// Async version of [guard]
FutureResult<S> guardAsync<S>(Future<S> 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<S> guardAsyncResult<S>(Future<Result<S>> Function() func) async {
try {
return await func();
} catch (e) {
return Err(Error(e));
}
}

0 comments on commit 40917f5

Please sign in to comment.