parse method

Result<T, T>? parse(
  1. ParseContext<T> context,
  2. Iterable<String> segments,
  3. RouteBuilder<T> createError
)

Returns a Result<T, T>? where null means this doesn't match the current path segment, Result.ok means a match was found and Result.err means an error was encountered trying to parse the path segments.

The right child is looked up by first looking up the current segment in all child segments that have a LiteralParser, like Segment.path. Then, if no literal child was matched, the remaining children are tried literally.

Implementation

Result<T, T>? parse(ParseContext<T> context, Iterable<String> segments,
    RouteBuilder<T> createError) {
  createError = this.createError ?? createError;
  final result = parser.parse(context, segments.first);
  if (!result.matched) {
    return null;
  }

  result.applyTo(context);

  segments = segments.skip(1);
  if (segments.isEmpty) {
    final create = this.create;
    return create != null
        ? Result.ok(create(context))
        : Result.err(createError(context));
  }

  final literalResult = _children.literalChildren[segments.first];
  return literalResult != null
      ? literalResult.parse(context, segments, createError)
      : _children.nonLiteralChildren
          .map((child) => child.parse(context, segments, createError))
          .firstWhere(
            (result) => result != null,
            orElse: () => Result.err(createError(context)),
          );
}