Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
ookami-kb committed Feb 16, 2024
1 parent ed7121b commit e63446a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 47 deletions.
27 changes: 0 additions & 27 deletions lib/src/either/either.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,6 @@ sealed class Either<L, R> {

const factory Either.right(R value) = Right<L, R>.new;

/// 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.
static Result<R> wrap<R>(R Function() block) {
try {
return Either.right(block());
} on Exception catch (e) {
return Either.left(e);
}
}

/// Wraps [block] function into try..catch asynchronously and returns [Future]
/// with [Right] with the result. In case of any [Exception] returns [Future]
/// with [Left] containing the exception.
///
/// It doesn't catch [Error]s as it usually means a bug in the code.
static AsyncResult<R> wrapAsync<R>(
FutureOr<R> Function() block,
) async {
try {
return Either.right(await block());
} on Exception catch (e) {
return Either.left(e);
}
}

bool isLeft() => this is Left;

bool isRight() => this is Right;
Expand Down
39 changes: 23 additions & 16 deletions lib/src/either/try_either.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,40 @@ import 'dart:async';

import 'package:dfunc/dfunc.dart';

typedef BindEither = T Function<T>(Result<T>);

extension EitherUnwrap<R> on Result<R> {
R unwrap() => fold((e) => throw e, (r) => r);
}

@Deprecated('Use Either.wrapAsync instead')
AsyncResult<T> tryEitherAsync<T>(
FutureOr<T> Function(BindEither bind) block,
) async {
A bind<A>(Result<A> either) => either.unwrap();

return Either.wrapAsync(() => block(bind));
/// 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) {
try {
return Either.right(block());
} on Exception catch (e) {
return Either.left(e);
}
}

@Deprecated('Use Either.wrap instead')
Result<T> tryEither<T>(T Function(BindEither bind) block) {
A bind<A>(Result<A> either) => either.unwrap();

return Either.wrap(() => block(bind));
/// Wraps [block] function into try..catch asynchronously and returns [Future]
/// with [Right] with the result. In case of any [Exception] returns [Future]
/// 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 {
try {
return Either.right(await block());
} on Exception catch (e) {
return Either.left(e);
}
}

extension FutureToEitherExt<T> on Future<T> {
AsyncResult<T> toEither() async => Either.wrapAsync(() => this);
AsyncResult<T> toEither() async => tryEitherAsync(() => this);
}

extension IterableResultExt<T> on Iterable<Result<T>> {
Result<List<T>> sequence() =>
Either.wrap(() => this.map((e) => e.unwrap()).toList());
tryEither(() => this.map((e) => e.unwrap()).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 = Either.wrap(() => int.parse('123'));
final result = tryEither(() => int.parse('123'));

expect(result, const Result<int>.right(123));
});

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

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

group('tryEitherAsync', () {
test('returns right', () async {
final result = await Either.wrapAsync(() => 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 Either.wrapAsync(() => int.parse('wrong'));
final result = await tryEitherAsync(() => int.parse('wrong'));

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

0 comments on commit e63446a

Please sign in to comment.