Skip to content

Commit

Permalink
Restore Result.isSuccess and isFailure, there are quite a few ext…
Browse files Browse the repository at this point in the history
…ernal dependencies left.
  • Loading branch information
renggli committed Dec 29, 2024
1 parent 54e243c commit 778e69f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/src/core/result.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import 'exception.dart';
sealed class Result<R> extends Context {
const Result(super.buffer, super.position);

/// Returns `true` if this result indicates a parse success.
@Deprecated('Use `is Success` operator instead')
bool get isSuccess => false;

/// Returns `true` if this result indicates a parse failure.
@Deprecated('Use `is Failure` operator instead')
bool get isFailure => false;

/// Returns the parsed value of this result, or throws a [ParserException]
/// if this is a parse failure.
@inlineVm
Expand All @@ -21,6 +29,10 @@ sealed class Result<R> extends Context {
class Success<R> extends Result<R> {
const Success(super.buffer, super.position, this.value);

@override
@Deprecated('Use `is Success` operator instead')
bool get isSuccess => true;

@override
final R value;

Expand All @@ -36,6 +48,10 @@ class Success<R> extends Result<R> {
class Failure extends Result<Never> {
const Failure(super.buffer, super.position, this.message);

@override
@Deprecated('Use `is Failure` operator instead')
bool get isFailure => true;

@override
Never get value => throw ParserException(this);

Expand Down
8 changes: 8 additions & 0 deletions test/context_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ void main() {
final success = context.success('result');
expect(success.buffer, buffer);
expect(success.position, 0);
expect(success.isSuccess, isTrue);
expect(success.isFailure, isFalse);
expect(success.value, 'result');
expect(() => success.message, throwsA(isUnsupportedError));
expect(success.toString(), isNot(startsWith('Instance of')));
Expand All @@ -29,6 +31,8 @@ void main() {
final success = context.success('result', 2);
expect(success.buffer, buffer);
expect(success.position, 2);
expect(success.isSuccess, isTrue);
expect(success.isFailure, isFalse);
expect(success.value, 'result');
expect(() => success.message, throwsA(isUnsupportedError));
expect(success.toString(), isNot(startsWith('Instance of')));
Expand All @@ -41,6 +45,8 @@ void main() {
final failure = context.failure('error');
expect(failure.buffer, buffer);
expect(failure.position, 0);
expect(failure.isSuccess, isFalse);
expect(failure.isFailure, isTrue);
expect(
() => failure.value,
throwsA(isParserException
Expand All @@ -59,6 +65,8 @@ void main() {
final failure = context.failure('problem', 2);
expect(failure.buffer, buffer);
expect(failure.position, 2);
expect(failure.isSuccess, isFalse);
expect(failure.isFailure, isTrue);
expect(
() => failure.value,
throwsA(isParserException
Expand Down

0 comments on commit 778e69f

Please sign in to comment.