Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: revert unwrap #28

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading