Skip to content

Instantly share code, notes, and snippets.

@Meai1
Created August 11, 2018 16:40
Show Gist options
  • Save Meai1/9c667fd94f35d2fbaf9c48b045d663de to your computer and use it in GitHub Desktop.
Save Meai1/9c667fd94f35d2fbaf9c48b045d663de to your computer and use it in GitHub Desktop.

Revisions

  1. Meai1 created this gist Aug 11, 2018.
    34 changes: 34 additions & 0 deletions generics.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    library petitparser.core.actions.action;

    import 'package:dart_server_test/src/core/combinators/delegate.dart';
    import 'package:dart_server_test/src/core/contexts/context.dart';
    import 'package:dart_server_test/src/core/contexts/result.dart';
    import 'package:dart_server_test/src/core/parser.dart';

    /// Typed action callback.
    typedef R ActionCallback<T, R>(T value);

    /// A parser that performs a transformation with a given function on the
    /// successful parse result of the delegate.
    class ActionParser<T, R> extends DelegateParser<R> {
    final ActionCallback<T, R> callback;

    ActionParser(Parser<T> delegate, this.callback) : super(delegate);

    @override
    Result<R> parseOn(Context context) {
    Result<T> result = delegate.parseOn(context);
    if (result.isSuccess) {
    return result.success(callback(result.value));
    } else {
    return result.failure(result.message);
    }
    }

    @override
    ActionParser<T, R> copy() => ActionParser<T, R>(delegate, callback);

    @override
    bool hasEqualProperties(ActionParser<T, R> other) =>
    super.hasEqualProperties(other) && callback == other.callback;
    }