Skip to content

Commit

Permalink
removed ResultType from public api, no more switch statements!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
0xba1 committed Jun 30, 2022
1 parent b3e1e0c commit 992d4b4
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 109 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,11 @@ Result<FallibleOpSuccess, FallibleOpFailure> fallibleOp() {
final result = fallibleOp();
switch(result.type) {
case ResultType.ok:
print('Success with value: ${result.unwrap()}');
break;
case ResultType.err:
print('Failure with error: ${result.unwrapErr()};');
break;
result.inspect((value) {
print('Success with value: $value');
}).inspectErr((error) {
print('Failure with error: $error');
});
}
```

Expand Down
13 changes: 5 additions & 8 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ Future<Result<Map<String, dynamic>, int>> getBooks() async {
void main() async {
final result = await getBooks();

switch (result.type) {
case ResultType.ok:
print('Books: ${result.unwrap()}');
break;
case ResultType.err:
print('Http request failed with status code ${result.unwrapErr()}');
break;
}
result.inspect((value) {
print('Books: $value');
}).inspectErr((error) {
print('Http request failed with status code $error');
});
}
1 change: 0 additions & 1 deletion lib/okay.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ library okay;

export 'src/_ok_err.dart';
export 'src/_result.dart';
export 'src/_result_type.dart';
27 changes: 17 additions & 10 deletions lib/src/_result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import 'package:meta/meta.dart';
import 'package:okay/src/_exceptions.dart';
import 'package:okay/src/_result_type.dart';

part 'result/adapter.dart';
part 'result/boolean.dart';
Expand All @@ -20,17 +19,17 @@ class Result<T, E> {
const Result.ok(T okValue)
: _okValue = okValue,
_errValue = null,
_type = ResultType.ok;
_type = _ResultType.ok;

/// Failure `Result`
const Result.err(E errValue)
: _okValue = null,
_errValue = errValue,
_type = ResultType.err;
_type = _ResultType.err;

final T? _okValue;
final E? _errValue;
final ResultType _type;
final _ResultType _type;

T get _ok => _okValue as T;
E get _err => _errValue as E;
Expand All @@ -51,32 +50,40 @@ class Result<T, E> {
/// break;
/// }
/// ```
ResultType get type => _type;
@override
bool operator ==(Object? other) =>
other is Result &&
other.type == _type &&
other._type == _type &&
other._okValue == _okValue &&
other._errValue == _errValue;

@override
int get hashCode {
switch (_type) {
case ResultType.ok:
case _ResultType.ok:
return Object.hash(_type, _okValue);
case ResultType.err:
case _ResultType.err:
return Object.hash(_type, _errValue);
}
}

@override
String toString() {
switch (_type) {
case ResultType.ok:
case _ResultType.ok:
return 'ok( $_okValue )';
case ResultType.err:
case _ResultType.err:
return 'err( $_errValue )';
}
}
}

/// Indicating `Result` of type `ok` or `err`
enum _ResultType {
/// Success `Result`
ok,

/// Failure `Result`
err,
}
8 changes: 0 additions & 8 deletions lib/src/_result_type.dart

This file was deleted.

16 changes: 8 additions & 8 deletions lib/src/result/boolean.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ extension Boolean<T, E> on Result<T, E> {
/// ```
Result<U, E> and<U>(Result<U, E> res) {
switch (_type) {
case ResultType.ok:
case _ResultType.ok:
return res;
case ResultType.err:
case _ResultType.err:
return Result.err(_err);
}
}
Expand Down Expand Up @@ -59,9 +59,9 @@ extension Boolean<T, E> on Result<T, E> {
/// ```
Result<U, E> andThen<U>(Result<U, E> Function(T) okMap) {
switch (_type) {
case ResultType.ok:
case _ResultType.ok:
return okMap(_ok);
case ResultType.err:
case _ResultType.err:
return Result.err(_err);
}
}
Expand Down Expand Up @@ -92,9 +92,9 @@ extension Boolean<T, E> on Result<T, E> {
/// ```
Result<T, F> or<F>(Result<T, F> res) {
switch (_type) {
case ResultType.ok:
case _ResultType.ok:
return Result.ok(_ok);
case ResultType.err:
case _ResultType.err:
return res;
}
}
Expand Down Expand Up @@ -122,9 +122,9 @@ extension Boolean<T, E> on Result<T, E> {
/// ```
Result<T, F> orElse<F>(Result<T, F> Function(E) errMap) {
switch (_type) {
case ResultType.ok:
case _ResultType.ok:
return Result.ok(_ok);
case ResultType.err:
case _ResultType.err:
return errMap(_err);
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/src/result/contains.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ extension Contains<T, E> on Result<T, E> {
/// ```
bool contains(Object x) {
switch (_type) {
case ResultType.ok:
case _ResultType.ok:
return _ok == x;
case ResultType.err:
case _ResultType.err:
return false;
}
}
Expand All @@ -45,9 +45,9 @@ extension Contains<T, E> on Result<T, E> {
/// ```
bool containsErr(Object x) {
switch (_type) {
case ResultType.err:
case _ResultType.err:
return _err == x;
case ResultType.ok:
case _ResultType.ok:
return false;
}
}
Expand Down
24 changes: 12 additions & 12 deletions lib/src/result/extractors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ extension Extractors<T, E> on Result<T, E> {
/// ```
T expect(String message) {
switch (_type) {
case ResultType.ok:
case _ResultType.ok:
return _ok;
case ResultType.err:
case _ResultType.err:
throw ExpectException(
errorMessage: message,
errString: _errValue.toString(),
Expand Down Expand Up @@ -58,9 +58,9 @@ extension Extractors<T, E> on Result<T, E> {
/// ```
T unwrap() {
switch (_type) {
case ResultType.ok:
case _ResultType.ok:
return _ok;
case ResultType.err:
case _ResultType.err:
throw UnwrapException(
errString: _errValue.toString(),
);
Expand All @@ -83,9 +83,9 @@ extension Extractors<T, E> on Result<T, E> {
/// ```
E expectErr(String message) {
switch (_type) {
case ResultType.err:
case _ResultType.err:
return _err;
case ResultType.ok:
case _ResultType.ok:
throw ExpectErrException(
errorMessage: message,
okString: _okValue.toString(),
Expand All @@ -111,9 +111,9 @@ extension Extractors<T, E> on Result<T, E> {
/// ```
E unwrapErr() {
switch (_type) {
case ResultType.err:
case _ResultType.err:
return _err;
case ResultType.ok:
case _ResultType.ok:
throw UnwrapErrException(
okString: _okValue.toString(),
);
Expand All @@ -137,9 +137,9 @@ extension Extractors<T, E> on Result<T, E> {
/// ```
T unwrapOr(T fallback) {
switch (_type) {
case ResultType.ok:
case _ResultType.ok:
return _ok;
case ResultType.err:
case _ResultType.err:
return fallback;
}
}
Expand All @@ -159,9 +159,9 @@ extension Extractors<T, E> on Result<T, E> {
/// ```
T unwrapOrElse(T Function(E) fallback) {
switch (_type) {
case ResultType.ok:
case _ResultType.ok:
return _ok;
case ResultType.err:
case _ResultType.err:
return fallback(_err);
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/result/inspect.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension Inspect<T, E> on Result<T, E> {
/// .map((val) => val * val);
/// ```
Result<T, E> inspect(void Function(T) f) {
if (_type == ResultType.ok) {
if (_type == _ResultType.ok) {
f(_ok);
}
return this;
Expand All @@ -42,7 +42,7 @@ extension Inspect<T, E> on Result<T, E> {
/// .inspectErr((val) => print("'$val' could not be parsed into an integer")) // prints "'$four' could not be parsed into an integer"
/// ```
Result<T, E> inspectErr(void Function(E) f) {
if (_type == ResultType.err) {
if (_type == _ResultType.err) {
f(_err);
}
return this;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/result/ok_or_err.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ extension OkOrErr<T> on Result<T, T> {
/// ```
T okOrErr() {
switch (_type) {
case ResultType.ok:
case _ResultType.ok:
return _ok;
case ResultType.err:
case _ResultType.err:
return _err;
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/src/result/querying_values.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension QueryingValues<T, E> on Result<T, E> {
/// Result<int, String> y = err('An unexpected error occured');
/// expect(x.isOk, false);
/// ```
bool get isOk => _type == ResultType.ok;
bool get isOk => _type == _ResultType.ok;

/// Returns `true` if the result is `ok` and the value matches the predicate `f`
///
Expand All @@ -33,7 +33,7 @@ extension QueryingValues<T, E> on Result<T, E> {
/// expect(x.isOkAnd((_) => true), false);
/// ```
bool isOkAnd(bool Function(T) f) {
if (_type == ResultType.ok) {
if (_type == _ResultType.ok) {
return f(_ok);
}

Expand All @@ -53,7 +53,7 @@ extension QueryingValues<T, E> on Result<T, E> {
/// Result<int, String> x = ok(0);
/// expect(x.isErr, false);
/// ```
bool get isErr => _type == ResultType.err;
bool get isErr => _type == _ResultType.err;

/// Returns `true` if the result is `err` and the value matches the predicate `f`
///
Expand All @@ -68,7 +68,7 @@ extension QueryingValues<T, E> on Result<T, E> {
/// expect(x.isErrAnd((_) => true), false);
/// ```
bool isErrAnd(bool Function(E) f) {
if (_type == ResultType.err) {
if (_type == _ResultType.err) {
return f(_err);
}

Expand Down
16 changes: 8 additions & 8 deletions lib/src/result/transformers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ extension Transformers<T, E> on Result<T, E> {
/// ```
Result<U, E> map<U>(U Function(T) okMap) {
switch (_type) {
case ResultType.ok:
case _ResultType.ok:
return Result.ok(okMap(_ok));
case ResultType.err:
case _ResultType.err:
return Result.err(_err);
}
}
Expand All @@ -50,9 +50,9 @@ extension Transformers<T, E> on Result<T, E> {
/// ```
U mapOr<U>({required U fallback, required U Function(T) okMap}) {
switch (_type) {
case ResultType.ok:
case _ResultType.ok:
return okMap(_ok);
case ResultType.err:
case _ResultType.err:
return fallback;
}
}
Expand Down Expand Up @@ -92,9 +92,9 @@ extension Transformers<T, E> on Result<T, E> {
required U Function(T) okMap,
}) {
switch (_type) {
case ResultType.ok:
case _ResultType.ok:
return okMap(_ok);
case ResultType.err:
case _ResultType.err:
return errMap(_err);
}
}
Expand All @@ -121,9 +121,9 @@ extension Transformers<T, E> on Result<T, E> {
/// ```
Result<T, F> mapErr<F>(F Function(E) errMap) {
switch (_type) {
case ResultType.err:
case _ResultType.err:
return Result.err(errMap(_err));
case ResultType.ok:
case _ResultType.ok:
return Result.ok(_ok);
}
}
Expand Down
Loading

0 comments on commit 992d4b4

Please sign in to comment.