Skip to content

Commit

Permalink
Merge pull request #239 from yakforward-ou/yak_result_v3
Browse files Browse the repository at this point in the history
yak result v3
  • Loading branch information
iapicca authored Apr 18, 2024
2 parents 0ad0743 + d70ecc0 commit b4365d5
Show file tree
Hide file tree
Showing 72 changed files with 1,470 additions and 1,605 deletions.
10 changes: 5 additions & 5 deletions examples/yak_result/bin/yak_result_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ int meaningOfLife() {

/// you can use `yak_runner` for this purpose
/// [https://pub.dev/packages/yak_runner]
ValueResult<T> safelyTry<T>(T Function() function) {
Result<T> safelyTry<T extends Object>(T Function() function) {
try {
return ValueSuccess(function());
return Result.success(function());
} on Exception catch (e, s) {
return Failure(e, s);
return Result.failure(e, s);
} on Error catch (e) {
return Failure.fromError(e);
return Result.failure(e, e.stackTrace ?? StackTrace.empty);
}
}

void main() {
final result = safelyTry(meaningOfLife);
if (result.isSuccess) {
print('${result.success.value}');
print('${result.asSuccess}');
return;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/yak_result/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ environment:
sdk: '>=3.3.3 <4.0.0'

dependencies:
yak_result: ^2.1.1
yak_result: ^3.0.0

dev_dependencies:
lints: ^3.0.0
2 changes: 1 addition & 1 deletion examples/yak_runner/bin/yak_runner_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ void main() {

final result = oneDividedBy.run(1);
if (result.isSuccess) {
print(result.success.value); // print `1`
print(result.asSuccess); // print `1`
}
}
2 changes: 1 addition & 1 deletion examples/yak_runner/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ environment:
sdk: '>=3.3.3 <4.0.0'

dependencies:
yak_runner: ^2.1.1
yak_runner: ^3.0.0

dev_dependencies:
lints: ^3.0.0
3 changes: 3 additions & 0 deletions packages/yak_flutter/CHANGELOG.md
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

Expand Down
4 changes: 2 additions & 2 deletions packages/yak_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: yak_flutter
description: A collection of tools and extensions for Flutter.
version: 2.1.1
version: 3.0.0
homepage: https://github.com/yakforward-ou/yak_packages

environment:
Expand All @@ -10,7 +10,7 @@ dependencies:
flutter:
sdk: flutter
yak_tween: ^2.0.2
yak_runner: ^2.1.1
yak_runner: ^3.0.0
yak_utils: ^2.1.1

dev_dependencies:
Expand Down
4 changes: 4 additions & 0 deletions packages/yak_result/CHANGELOG.md
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

Expand Down
3 changes: 0 additions & 3 deletions packages/yak_result/lib/src/all.dart

This file was deleted.

3 changes: 0 additions & 3 deletions packages/yak_result/lib/src/classes/all.dart

This file was deleted.

7 changes: 7 additions & 0 deletions packages/yak_result/lib/src/classes/exceptions.dart
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');
}
35 changes: 14 additions & 21 deletions packages/yak_result/lib/src/classes/failure.dart
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;
}
71 changes: 52 additions & 19 deletions packages/yak_result/lib/src/classes/result.dart
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;
// }
52 changes: 0 additions & 52 deletions packages/yak_result/lib/src/classes/success.dart

This file was deleted.

58 changes: 58 additions & 0 deletions packages/yak_result/lib/src/classes/void_result.dart
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;
}
7 changes: 1 addition & 6 deletions packages/yak_result/lib/src/extensions/all.dart
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';
11 changes: 0 additions & 11 deletions packages/yak_result/lib/src/extensions/as_failure.dart

This file was deleted.

20 changes: 0 additions & 20 deletions packages/yak_result/lib/src/extensions/as_success.dart

This file was deleted.

7 changes: 0 additions & 7 deletions packages/yak_result/lib/src/extensions/as_void.dart

This file was deleted.

6 changes: 0 additions & 6 deletions packages/yak_result/lib/src/extensions/failure_recast.dart

This file was deleted.

Loading

0 comments on commit b4365d5

Please sign in to comment.