2024-03-05 13:51:03 +08:00
|
|
|
abstract class FlowyResult<S, F extends Object> {
|
2024-02-24 20:54:10 +07:00
|
|
|
const FlowyResult();
|
|
|
|
|
|
|
|
factory FlowyResult.success(S s) => FlowySuccess(s);
|
|
|
|
|
2024-03-05 13:51:03 +08:00
|
|
|
factory FlowyResult.failure(F f) => FlowyFailure(f);
|
2024-02-24 20:54:10 +07:00
|
|
|
|
2024-03-05 13:51:03 +08:00
|
|
|
T fold<T>(T Function(S s) onSuccess, T Function(F f) onFailure);
|
2024-02-24 20:54:10 +07:00
|
|
|
|
|
|
|
FlowyResult<T, F> map<T>(T Function(S success) fn);
|
2024-03-05 13:51:03 +08:00
|
|
|
FlowyResult<S, T> mapError<T extends Object>(T Function(F failure) fn);
|
2024-02-24 20:54:10 +07:00
|
|
|
|
2024-03-22 16:15:18 +07:00
|
|
|
bool get isSuccess;
|
|
|
|
bool get isFailure;
|
2024-02-24 20:54:10 +07:00
|
|
|
|
|
|
|
S? toNullable();
|
2024-03-03 08:36:12 +07:00
|
|
|
|
2024-03-05 13:51:03 +08:00
|
|
|
void onSuccess(void Function(S s) onSuccess);
|
|
|
|
void onFailure(void Function(F f) onFailure);
|
2024-03-03 08:36:12 +07:00
|
|
|
|
2024-03-05 13:51:03 +08:00
|
|
|
S getOrElse(S Function(F failure) onFailure);
|
|
|
|
S getOrThrow();
|
2024-03-22 16:15:18 +07:00
|
|
|
|
|
|
|
F getFailure();
|
2024-02-24 20:54:10 +07:00
|
|
|
}
|
|
|
|
|
2024-03-05 13:51:03 +08:00
|
|
|
class FlowySuccess<S, F extends Object> implements FlowyResult<S, F> {
|
2024-02-24 20:54:10 +07:00
|
|
|
final S _value;
|
|
|
|
|
|
|
|
FlowySuccess(this._value);
|
|
|
|
|
|
|
|
S get value => _value;
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool operator ==(Object other) =>
|
|
|
|
identical(this, other) ||
|
|
|
|
other is FlowySuccess &&
|
|
|
|
runtimeType == other.runtimeType &&
|
|
|
|
_value == other._value;
|
|
|
|
|
|
|
|
@override
|
|
|
|
int get hashCode => _value.hashCode;
|
|
|
|
|
|
|
|
@override
|
|
|
|
String toString() => 'Success(value: $_value)';
|
|
|
|
|
|
|
|
@override
|
|
|
|
T fold<T>(T Function(S s) onSuccess, T Function(F e) onFailure) =>
|
|
|
|
onSuccess(_value);
|
|
|
|
|
|
|
|
@override
|
|
|
|
map<T>(T Function(S success) fn) {
|
|
|
|
return FlowySuccess(fn(_value));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2024-03-05 13:51:03 +08:00
|
|
|
FlowyResult<S, T> mapError<T extends Object>(T Function(F error) fn) {
|
2024-02-24 20:54:10 +07:00
|
|
|
return FlowySuccess(_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2024-03-22 16:15:18 +07:00
|
|
|
bool get isSuccess => true;
|
2024-02-24 20:54:10 +07:00
|
|
|
|
|
|
|
@override
|
2024-03-22 16:15:18 +07:00
|
|
|
bool get isFailure => false;
|
2024-02-24 20:54:10 +07:00
|
|
|
|
|
|
|
@override
|
|
|
|
S? toNullable() {
|
|
|
|
return _value;
|
|
|
|
}
|
2024-03-03 08:36:12 +07:00
|
|
|
|
|
|
|
@override
|
|
|
|
void onSuccess(void Function(S success) onSuccess) {
|
|
|
|
onSuccess(_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onFailure(void Function(F failure) onFailure) {}
|
2024-03-05 13:51:03 +08:00
|
|
|
|
|
|
|
@override
|
|
|
|
S getOrElse(S Function(F failure) onFailure) {
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
S getOrThrow() {
|
|
|
|
return _value;
|
|
|
|
}
|
2024-03-22 16:15:18 +07:00
|
|
|
|
|
|
|
@override
|
|
|
|
F getFailure() {
|
|
|
|
throw UnimplementedError();
|
|
|
|
}
|
2024-02-24 20:54:10 +07:00
|
|
|
}
|
|
|
|
|
2024-03-05 13:51:03 +08:00
|
|
|
class FlowyFailure<S, F extends Object> implements FlowyResult<S, F> {
|
|
|
|
final F _value;
|
2024-02-24 20:54:10 +07:00
|
|
|
|
2024-03-05 13:51:03 +08:00
|
|
|
FlowyFailure(this._value);
|
2024-02-24 20:54:10 +07:00
|
|
|
|
2024-03-05 13:51:03 +08:00
|
|
|
F get error => _value;
|
2024-02-24 20:54:10 +07:00
|
|
|
|
|
|
|
@override
|
|
|
|
bool operator ==(Object other) =>
|
|
|
|
identical(this, other) ||
|
|
|
|
other is FlowyFailure &&
|
|
|
|
runtimeType == other.runtimeType &&
|
2024-03-05 13:51:03 +08:00
|
|
|
_value == other._value;
|
2024-02-24 20:54:10 +07:00
|
|
|
|
|
|
|
@override
|
2024-03-05 13:51:03 +08:00
|
|
|
int get hashCode => _value.hashCode;
|
2024-02-24 20:54:10 +07:00
|
|
|
|
|
|
|
@override
|
2024-03-05 13:51:03 +08:00
|
|
|
String toString() => 'Failure(error: $_value)';
|
2024-02-24 20:54:10 +07:00
|
|
|
|
|
|
|
@override
|
|
|
|
T fold<T>(T Function(S s) onSuccess, T Function(F e) onFailure) =>
|
2024-03-05 13:51:03 +08:00
|
|
|
onFailure(_value);
|
2024-02-24 20:54:10 +07:00
|
|
|
|
|
|
|
@override
|
|
|
|
map<T>(T Function(S success) fn) {
|
2024-03-05 13:51:03 +08:00
|
|
|
return FlowyFailure(_value);
|
2024-02-24 20:54:10 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2024-03-05 13:51:03 +08:00
|
|
|
FlowyResult<S, T> mapError<T extends Object>(T Function(F error) fn) {
|
|
|
|
return FlowyFailure(fn(_value));
|
2024-02-24 20:54:10 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2024-03-22 16:15:18 +07:00
|
|
|
bool get isSuccess => false;
|
2024-02-24 20:54:10 +07:00
|
|
|
|
|
|
|
@override
|
2024-03-22 16:15:18 +07:00
|
|
|
bool get isFailure => true;
|
2024-02-24 20:54:10 +07:00
|
|
|
|
|
|
|
@override
|
|
|
|
S? toNullable() {
|
|
|
|
return null;
|
|
|
|
}
|
2024-03-03 08:36:12 +07:00
|
|
|
|
|
|
|
@override
|
|
|
|
void onSuccess(void Function(S success) onSuccess) {}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void onFailure(void Function(F failure) onFailure) {
|
2024-03-05 13:51:03 +08:00
|
|
|
onFailure(_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
S getOrElse(S Function(F failure) onFailure) {
|
|
|
|
return onFailure(_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
S getOrThrow() {
|
|
|
|
throw _value;
|
2024-03-03 08:36:12 +07:00
|
|
|
}
|
2024-03-22 16:15:18 +07:00
|
|
|
|
|
|
|
@override
|
|
|
|
F getFailure() {
|
|
|
|
return _value;
|
|
|
|
}
|
2024-02-24 20:54:10 +07:00
|
|
|
}
|