Created
August 11, 2018 16:40
-
-
Save Meai1/9c667fd94f35d2fbaf9c48b045d663de to your computer and use it in GitHub Desktop.
Revisions
-
Meai1 created this gist
Aug 11, 2018 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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; }