From 58effa0ebdb938aa64977e2109fa7714f53978c9 Mon Sep 17 00:00:00 2001 From: Kirill Bubochkin Date: Mon, 3 Jun 2024 18:28:24 +0200 Subject: [PATCH] feat: revert unwrap (#28) --- lib/src/either/try_either.dart | 29 +++++++++++++++++++---------- test/either/try_either_test.dart | 8 ++++---- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/src/either/try_either.dart b/lib/src/either/try_either.dart index 2dfcd3d..e5718f2 100644 --- a/lib/src/either/try_either.dart +++ b/lib/src/either/try_either.dart @@ -4,18 +4,17 @@ import 'package:dfunc/dfunc.dart'; typedef Result = Either; typedef AsyncResult = Future>; - -extension ResultUnwrap on Result { - R unwrap() => fold((e) => throw e, (r) => r); -} +typedef BindEither = T Function(Result); /// Wraps [block] function into try..catch and returns [Right] with the /// result. In case of any [Exception] returns [Left] with the exception. /// /// It doesn't catch [Error]s as it usually means a bug in the code. -Result tryEither(T Function() block) { +Result tryEither(T Function(BindEither bind) block) { + A bind(Result either) => either.fold((e) => throw e, identity); + try { - return Either.right(block()); + return Either.right(block(bind)); } on Exception catch (error) { return Either.left(error); } @@ -26,19 +25,29 @@ Result tryEither(T Function() block) { /// with [Left] containing the exception. /// /// It doesn't catch [Error]s as it usually means a bug in the code. -AsyncResult tryEitherAsync(FutureOr Function() block) async { +AsyncResult tryEitherAsync( + FutureOr Function(BindEither bind) block, +) async { try { - return Either.right(await block()); + A bind(Result either) => either.fold((e) => throw e, identity); + + return Either.right(await block(bind)); } on Exception catch (error) { return Either.left(error); } } extension FutureToEitherExt on Future { - AsyncResult toEither() => tryEitherAsync(() => this); + AsyncResult toEither() async { + try { + return Either.right(await this); + } on Exception catch (error) { + return Either.left(error); + } + } } extension IterableResultExt on Iterable> { Result> sequence() => - tryEither(() => this.map((e) => e.unwrap()).toList()); + tryEither((bind) => this.map((e) => bind(e)).toList()); } diff --git a/test/either/try_either_test.dart b/test/either/try_either_test.dart index 46cbecc..641dd86 100644 --- a/test/either/try_either_test.dart +++ b/test/either/try_either_test.dart @@ -4,13 +4,13 @@ import 'package:test/test.dart'; void main() { group('tryEither', () { test('returns right', () { - final result = tryEither(() => int.parse('123')); + final result = tryEither((_) => int.parse('123')); expect(result, const Result.right(123)); }); test('returns left on exception', () { - final result = tryEither(() => int.parse('wrong')); + final result = tryEither((_) => int.parse('wrong')); expect(result.isLeft(), true); }); @@ -18,13 +18,13 @@ void main() { group('tryEitherAsync', () { test('returns right', () async { - final result = await tryEitherAsync(() => int.parse('123')); + final result = await tryEitherAsync((_) => int.parse('123')); expect(result, const Result.right(123)); }); test('returns left on exception', () async { - final result = await tryEitherAsync(() => int.parse('wrong')); + final result = await tryEitherAsync((_) => int.parse('wrong')); expect(result.isLeft(), true); });