-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
56 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |