Skip to content

Commit

Permalink
refactor: Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmah309 committed Jan 29, 2024
1 parent bd5adda commit 21440c6
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/src/option/future_option.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ extension FutureOptionExtension<T extends Object> on FutureOption<T> {

//************************************************************************//

// ignore: library_private_types_in_public_api
// ignore: library_private_types_in_public_api
Future<T> operator [](_OptionEarlyReturnKey op) {
return then((value) => value[op]);
}
Expand Down
11 changes: 6 additions & 5 deletions lib/src/option/option.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ sealed class Option<T extends Object> {
/// immediately the context that "$" belongs to is returned with None(). e.g.
/// ```
/// Option<int> intNone() => const None();
///
///
/// Option<int> earlyReturn(int val) => Option(($){
/// int x = intNone()[$]; // returns [None] immediately
/// return Some(val + 3);
Expand Down Expand Up @@ -42,8 +42,9 @@ sealed class Option<T extends Object> {
///```
/// This should be used at the top level of a function as above. Passing "$" to any other functions, nesting, or
/// attempting to bring "$" out of the original scope should be avoided.
// ignore: library_private_types_in_public_api
static Future<Option<T>> async<T extends Object>(_OptionAsyncEarlyReturnFunction<T> fn) async {
static Future<Option<T>> async<T extends Object>(
// ignore: library_private_types_in_public_api
_OptionAsyncEarlyReturnFunction<T> fn) async {
try {
return await fn(const _OptionEarlyReturnKey._());
} on _OptionEarlyReturnNotification catch (_) {
Expand Down Expand Up @@ -479,8 +480,8 @@ final class _OptionEarlyReturnNotification {
typedef _OptionEarlyReturnFunction<T extends Object> = Option<T> Function(
_OptionEarlyReturnKey);

typedef _OptionAsyncEarlyReturnFunction<T extends Object> = Future<Option<T>> Function(
_OptionEarlyReturnKey);
typedef _OptionAsyncEarlyReturnFunction<T extends Object> = Future<Option<T>>
Function(_OptionEarlyReturnKey);

//************************************************************************//

Expand Down
9 changes: 4 additions & 5 deletions lib/src/result/result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@ part 'future_result.dart';
/// .org/std/result/enum.Result.html
/// {@endtemplate}
sealed class Result<S, F extends Object> {

/// Creates a context for early return, similar to "Do notation".
/// Here "$" is used as the "Early Return Key". when "$" is used on a type [Err],
/// immediately the context that "$" belongs to is returned with that [Err].
/// immediately the context that "$" belongs to is returned with that [Err].
/// Works like the Rust "?" operator, which is a "Early Return Operator".
/// e.g.
/// ```dart
/// Result<int,String> innerFn() => Err("message");
///
///
/// Result<int, String> innerFn2() => Ok(1);
///
///
/// Result<int, String> earlyReturn() => Result(($) {
/// int y = 2;
/// int x = innerFn()[$]; // returns [Err] here immediately
Expand All @@ -46,7 +45,7 @@ sealed class Result<S, F extends Object> {

/// Creates a async context for early return, similar to "Do notation".
/// Here "$" is used as the "Early Return Key". when "$" is used on a type [Err],
/// immediately the context that "$" belongs to is returned with that [Err].
/// immediately the context that "$" belongs to is returned with that [Err].
/// Works like the Rust "?" operator, which is a "Early Return Operator".
/// e.g.
///
Expand Down
5 changes: 3 additions & 2 deletions test/option/option_future_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ void main() {
expect(await x.expect("Error"), 1);

x = Future.value(const None());
expect(() async => await x.expect("Error occurred"), throwsA(isA<Error>()));
expect(
() async => await x.expect("Error occurred"), throwsA(isA<Error>()));
});

test("filter", () async {
Expand Down Expand Up @@ -238,7 +239,7 @@ void main() {
double x = await doubleNone()[$];
return Some((val + x).toInt());
});
final _ = await earlyReturn(2);
final _ = await earlyReturn(2);
expect(await earlyReturn(2), const None());
});

Expand Down
12 changes: 4 additions & 8 deletions test/result/future_result_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ void main() {
});

test('With Exit', () async {
FutureResult<int, String> testDoNotation() =>
Result.async(($) async {
FutureResult<int, String> testDoNotation() => Result.async(($) async {
int y = Ok(1)[$];
int z = Ok(1).mapErr((err) => err.toString())[$];
int x = await regularErr()[$];
Expand All @@ -225,8 +224,7 @@ void main() {
});

test('With Exit 2', () async {
FutureResult<int, String> testDoNotation() =>
Result.async(($) async {
FutureResult<int, String> testDoNotation() => Result.async(($) async {
int y = Ok(1)[$];
int z = Ok(1).mapErr((err) => err.toString())[$];
int x = await earlyReturnErr()[$];
Expand All @@ -240,8 +238,7 @@ void main() {
});

test('Normal Ok', () async {
FutureResult<int, String> testDoNotation() =>
Result.async(($) async {
FutureResult<int, String> testDoNotation() => Result.async(($) async {
int y = 3;
int z = 2;
int x = 1;
Expand All @@ -251,8 +248,7 @@ void main() {
});

test('Normal Err', () async {
FutureResult<int, String> testDoNotation() =>
Result.async(($) async {
FutureResult<int, String> testDoNotation() => Result.async(($) async {
int y = 3;
int z = 2;
int x = 1;
Expand Down

0 comments on commit 21440c6

Please sign in to comment.