Skip to content

Commit

Permalink
feat: revert unwrap (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
ookami-kb committed Jun 3, 2024
1 parent f5c32b1 commit 58effa0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
29 changes: 19 additions & 10 deletions lib/src/either/try_either.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ import 'package:dfunc/dfunc.dart';

typedef Result<T> = Either<Exception, T>;
typedef AsyncResult<T> = Future<Result<T>>;

extension ResultUnwrap<R> on Result<R> {
R unwrap() => fold((e) => throw e, (r) => r);
}
typedef BindEither = T Function<T>(Result<T>);

/// 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<T> tryEither<T>(T Function() block) {
Result<T> tryEither<T>(T Function(BindEither bind) block) {
A bind<A>(Result<A> 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);
}
Expand All @@ -26,19 +25,29 @@ Result<T> tryEither<T>(T Function() block) {
/// with [Left] containing the exception.
///
/// It doesn't catch [Error]s as it usually means a bug in the code.
AsyncResult<T> tryEitherAsync<T>(FutureOr<T> Function() block) async {
AsyncResult<T> tryEitherAsync<T>(
FutureOr<T> Function(BindEither bind) block,
) async {
try {
return Either.right(await block());
A bind<A>(Result<A> either) => either.fold((e) => throw e, identity);

return Either.right(await block(bind));
} on Exception catch (error) {
return Either.left(error);
}
}

extension FutureToEitherExt<T> on Future<T> {
AsyncResult<T> toEither() => tryEitherAsync(() => this);
AsyncResult<T> toEither() async {
try {
return Either.right(await this);
} on Exception catch (error) {
return Either.left(error);
}
}
}

extension IterableResultExt<T> on Iterable<Result<T>> {
Result<List<T>> sequence() =>
tryEither(() => this.map((e) => e.unwrap()).toList());
tryEither((bind) => this.map((e) => bind(e)).toList());
}
8 changes: 4 additions & 4 deletions test/either/try_either_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ 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<int>.right(123));
});

test('returns left on exception', () {
final result = tryEither(() => int.parse('wrong'));
final result = tryEither((_) => int.parse('wrong'));

expect(result.isLeft(), true);
});
});

group('tryEitherAsync', () {
test('returns right', () async {
final result = await tryEitherAsync(() => int.parse('123'));
final result = await tryEitherAsync((_) => int.parse('123'));

expect(result, const Result<int>.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);
});
Expand Down

0 comments on commit 58effa0

Please sign in to comment.