-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #239 from yakforward-ou/yak_result_v3
yak result v3
- Loading branch information
Showing
72 changed files
with
1,470 additions
and
1,605 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
### 3.0.0 | ||
- rework due to yak_result v3 | ||
|
||
### 2.1.1 | ||
- update dependency | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
### 3.0.0 | ||
- rework using "extension type" | ||
- BREAKING CHANGES | ||
|
||
### 2.1.1 | ||
- update dependency | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import 'package:meta/meta.dart'; | ||
|
||
@internal | ||
sealed class ResultExceptions { | ||
static final notFailureException = Exception('Result is a Success'); | ||
static final notSuccessException = Exception('Result is a Failure'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,29 @@ | ||
import 'package:meta/meta.dart'; | ||
import 'package:yak_utils/yak_utils.dart'; | ||
|
||
import '../all.dart'; | ||
|
||
/// represent a failure of a function | ||
class Failure<T> extends Result<T> implements ValueResult<T>, VoidResult<T> { | ||
class Failure<T> { | ||
final Object reason; | ||
final StackTrace stackTrace; | ||
|
||
const Failure([ | ||
this.reason = const Object(), | ||
this.stackTrace = StackTrace.empty, | ||
]); | ||
|
||
factory Failure.fromError(Error error) => Failure( | ||
error, | ||
error.stackTrace ?? StackTrace.empty, | ||
); | ||
const Failure(this.reason, this.stackTrace); | ||
const factory Failure.empty() = _EmptyFailure; | ||
|
||
@override | ||
@nonVirtual | ||
bool operator ==(Object other) => hashCode == other.hashCode; | ||
bool operator ==(Object other) => | ||
other is Failure<T> && hashCode == other.hashCode; | ||
|
||
@override | ||
@nonVirtual | ||
int get hashCode => Object.hashAll([runtimeType, reason, stackTrace]); | ||
int get hashCode => Object.hash(reason, stackTrace); | ||
} | ||
|
||
final class _EmptyFailure<T> implements Failure<T> { | ||
const _EmptyFailure(); | ||
|
||
@override | ||
@nonVirtual | ||
Json toJson() => { | ||
'Result': 'Failure', | ||
'reason': '${reason.runtimeType}', | ||
'stackTrace': '$stackTrace', | ||
}; | ||
final reason = const Object(); | ||
|
||
@override | ||
final stackTrace = StackTrace.empty; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,58 @@ | ||
import 'package:meta/meta.dart'; | ||
import 'package:yak_utils/yak_utils.dart'; | ||
import 'exceptions.dart'; | ||
import 'failure.dart'; | ||
|
||
/// represent the outcome of a function | ||
abstract class Result<T> { | ||
/// allows a const constructor | ||
const Result(); | ||
extension type Result<T extends Object>._(Object result) { | ||
bool get isSuccess => result is T; | ||
bool get isFailure => result is Failure<T>; | ||
const Result.success(T this.result); | ||
Result._failure(Failure<T> this.result); | ||
|
||
@override | ||
@nonVirtual | ||
String toString() => '${toJson()}'; | ||
factory Result.failure([ | ||
Object reason = const Object(), | ||
StackTrace stackTrace = StackTrace.empty, | ||
]) => | ||
Result._failure(Failure(reason, stackTrace)); | ||
|
||
/// allow compatibility with [dart:convert] | ||
Json toJson(); | ||
} | ||
Failure<T> get asFailure => isFailure | ||
? result as Failure<T> | ||
: throw ResultExceptions.notFailureException; | ||
|
||
/// a result returning value | ||
abstract class ValueResult<T> extends Result<T> { | ||
const ValueResult(); | ||
T get asSuccess => | ||
isSuccess ? result as T : throw ResultExceptions.notSuccessException; | ||
} | ||
|
||
/// a result without returning value | ||
abstract class VoidResult<T> extends Result<T> { | ||
const VoidResult(); | ||
} | ||
/// inspired by [https://github.com/eernstg/extension_type_unions] | ||
// extension UnionInjectExtension<X> on X { | ||
// Union2<X, Never> get u21 => Union2.in1(this); | ||
// Union2<Never, X> get u22 => Union2.in2(this); | ||
// } | ||
|
||
// /// Emulate the union of the types [X1] and [X2]. | ||
// extension type Union2<X1, X2>._(Object? value) { | ||
// /// Create a [Union2] value from the first type argument. | ||
// Union2.in1(X1 this.value); | ||
|
||
// /// Create a [Union2] value from the second type argument. | ||
// Union2.in2(X2 this.value); | ||
|
||
// /// Return true iff this [Union2] has type [X1] or [X2]. | ||
// bool get isValid => value is X1 || value is X2; | ||
|
||
// /// Return the [value] if it has type [X1], otherwise null. | ||
// X1? get as1OrNull => value is X1 ? value as X1 : null; | ||
|
||
// /// Return the [value] if it has type [X2], otherwise null. | ||
// X2? get as2OrNull => value is X2 ? value as X2 : null; | ||
|
||
// /// Return the [value] if it has type [X1], otherwise throw. | ||
// X1 get as1 => value as X1; | ||
|
||
// /// Return the [value] if it has type [X2], otherwise throw. | ||
// X2 get as2 => value as X2; | ||
|
||
// /// Return type iff the [value] has type [X1]. | ||
// bool get is1 => value is X1; | ||
|
||
// /// Return type iff the [value] has type [X2]. | ||
// bool get is2 => value is X2; | ||
// } |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import 'package:meta/meta.dart'; | ||
|
||
import 'exceptions.dart'; | ||
import 'failure.dart'; | ||
|
||
@immutable | ||
abstract class VoidResult { | ||
const VoidResult(); | ||
|
||
bool get isSuccess; | ||
|
||
@nonVirtual | ||
bool get isFailure => !isSuccess; | ||
|
||
Failure<void> get asFailure; | ||
|
||
const factory VoidResult.success() = VoidSuccess; | ||
const factory VoidResult.failure([Object reason, StackTrace stackTrace]) = | ||
VoidFailure; | ||
|
||
@override | ||
@nonVirtual | ||
bool operator ==(Object other) => | ||
other is VoidResult && hashCode == other.hashCode; | ||
|
||
@override | ||
@nonVirtual | ||
int get hashCode => Object.hash(isSuccess, asFailure); | ||
} | ||
|
||
@immutable | ||
final class VoidSuccess extends VoidResult { | ||
const VoidSuccess(); | ||
|
||
@override | ||
final isSuccess = true; | ||
|
||
@override | ||
Failure<void> get asFailure => throw ResultExceptions.notFailureException; | ||
} | ||
|
||
@immutable | ||
final class VoidFailure extends VoidResult implements Failure<void> { | ||
const VoidFailure([ | ||
this.reason = const Object(), | ||
this.stackTrace = StackTrace.empty, | ||
]); | ||
@override | ||
final Object reason; | ||
@override | ||
final StackTrace stackTrace; | ||
|
||
@override | ||
final isSuccess = false; | ||
|
||
@override | ||
Failure<void> get asFailure => this; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,2 @@ | ||
export 'is_x.dart'; | ||
export 'as_failure.dart'; | ||
export 'failure_recast.dart'; | ||
export 'as_void.dart'; | ||
export 'as_success.dart'; | ||
export 'recast.dart'; | ||
export 'iterable_combine.dart'; | ||
export 'iterable_combine_unary.dart'; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.