decode method

  1. @override
KaiselConfig<R>? decode(
  1. Uri uri
)
override

Decode a URL into a configuration, or return null if unrecognised (the parser will then use the fallback stack).

Implementation

@override
KaiselConfig<R>? decode(Uri uri) {
  final segments = uri.pathSegments
      .where((s) => s.isNotEmpty)
      .toList(growable: false);

  for (final mount in modules) {
    final prefix = mount._prefixSegments();
    if (_segmentsStartWith(segments, prefix)) {
      final relative = segments.sublist(prefix.length);
      final moduleStack = mount.codec.decodeAny(relative);
      if (moduleStack != null && moduleStack.isNotEmpty) {
        return KaiselConfig<R>(
          mainStack: [mount.mountRoute],
          nestedState: KaiselModuleConfig(stack: moduleStack),
        );
      }
      // Prefix matched but the module codec rejected the relative
      // segments. The URL clearly belongs to this module's
      // namespace; don't silently fall through to baseCodec.
      return null;
    }
  }

  return baseCodec.decode(uri);
}