| | | 1 | | /******************************************************************************** |
| | | 2 | | * RouteNode.cs * |
| | | 3 | | * * |
| | | 4 | | * Author: Denes Solti * |
| | | 5 | | ********************************************************************************/ |
| | | 6 | | using System; |
| | | 7 | | using System.Collections.Frozen; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Collections.Immutable; |
| | | 10 | | using System.Linq; |
| | | 11 | | |
| | | 12 | | namespace NanoRoute.Internals |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Represents a node in the route tree. |
| | | 16 | | /// </summary> |
| | | 17 | | internal sealed class RouteNode |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Gets the verb-tagged handlers registered for the current route node. |
| | | 21 | | /// </summary> |
| | | 22 | | public IList<KeyValuePair<HttpVerb, HandlerRegistration>> Handlers { get; } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Gets literal branches keyed by case-insensitive segment value. |
| | | 26 | | /// </summary> |
| | | 27 | | public IDictionary<ReadOnlyMemory<char>, RouteNode> LiteralBranches { get; } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Gets parser-based branches. |
| | | 31 | | /// </summary> |
| | | 32 | | public IList<KeyValuePair<ParameterParser, RouteNode>> ParsedBranches { get; } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Returns true if this node is read-only. |
| | | 36 | | /// </summary> |
| | | 37 | | public bool Frozen { get; } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Gets the only branch reachable from this node when it has no handlers and a single child branch kind. |
| | | 41 | | /// </summary> |
| | | 42 | | public object? SingleBranch { get; } |
| | | 43 | | |
| | 2 | 44 | | public RouteNode() |
| | 2 | 45 | | { |
| | 2 | 46 | | Handlers = new List<KeyValuePair<HttpVerb, HandlerRegistration>>(); |
| | 2 | 47 | | LiteralBranches = new Dictionary<ReadOnlyMemory<char>, RouteNode>(ReadOnlyMemoryCharComparer.Instance); |
| | 2 | 48 | | ParsedBranches = new List<KeyValuePair<ParameterParser, RouteNode>>(); |
| | 2 | 49 | | } |
| | | 50 | | |
| | 2 | 51 | | private RouteNode(RouteNode src) |
| | 2 | 52 | | { |
| | 2 | 53 | | LiteralBranches = src.LiteralBranches.ToFrozenDictionary |
| | 2 | 54 | | ( |
| | 2 | 55 | | static kvp => kvp.Key, |
| | 2 | 56 | | static kvp => kvp.Value.Freeze(), |
| | 2 | 57 | | ReadOnlyMemoryCharComparer.Instance |
| | 2 | 58 | | ); |
| | | 59 | | |
| | 2 | 60 | | ParsedBranches = src |
| | 2 | 61 | | .ParsedBranches |
| | 2 | 62 | | .Select |
| | 2 | 63 | | ( |
| | 2 | 64 | | static kvp => new KeyValuePair<ParameterParser, RouteNode> |
| | 2 | 65 | | ( |
| | 2 | 66 | | kvp.Key, |
| | 2 | 67 | | kvp.Value.Freeze() |
| | 2 | 68 | | ) |
| | 2 | 69 | | ) |
| | 2 | 70 | | .ToImmutableArray(); |
| | | 71 | | |
| | 2 | 72 | | Handlers = src.Handlers.ToImmutableArray(); |
| | | 73 | | |
| | 2 | 74 | | if (Handlers.Count is 0) |
| | 2 | 75 | | SingleBranch = (LiteralBranches.Count, ParsedBranches.Count) switch |
| | 2 | 76 | | { |
| | 2 | 77 | | (1, 0) => LiteralBranches.Single(), |
| | 2 | 78 | | (0, 1) => ParsedBranches.Single(), |
| | 2 | 79 | | _ => default |
| | 2 | 80 | | }; |
| | | 81 | | |
| | 2 | 82 | | Frozen = true; |
| | 2 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Creates a frozen snapshot from this node. |
| | | 87 | | /// </summary> |
| | 2 | 88 | | public RouteNode Freeze() => new(this); |
| | | 89 | | } |
| | | 90 | | } |