< Summary

Information
Class: TeleFlow.Telegram.Internal.Handlers.TelegramRouteDescriptor
Assembly: TeleFlow.Framework
File(s): /_/src/TeleFlow.Framework/Internal/Handlers/TelegramRouteDescriptor.cs
Line coverage
37%
Covered lines: 30
Uncovered lines: 51
Coverable lines: 81
Total lines: 194
Line coverage: 37%
Branch coverage
75%
Covered branches: 25
Total branches: 33
Branch coverage: 75.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
.ctor(...)100%210%
.ctor(...)100%210%
.ctor(...)100%210%
.ctor(...)100%22100%
MapLegacyRouteKind(...)0%4260%
GetHandlerKind(...)100%99100%
get_Command()50%22100%
NormalizeCommandPattern(...)100%88100%
GetSpecificity(...)83.33%66100%

File(s)

/_/src/TeleFlow.Framework/Internal/Handlers/TelegramRouteDescriptor.cs

#LineLine coverage
 1namespace TeleFlow.Telegram.Internal.Handlers;
 2
 3internal sealed class TelegramRouteDescriptor
 4{
 5    public TelegramRouteDescriptor(
 6        TelegramHandlerKind kind,
 7        string? command,
 8        IReadOnlyList<TelegramTextFilter> textFilters,
 9        Type? callbackPayloadType)
 010        : this(
 011            MapLegacyRouteKind(kind, command),
 012            command,
 013            TelegramCommandPolicy.Default,
 014            textFilters,
 015            callbackPayloadType,
 016            routeValues: [],
 017            filters: [],
 018            chatMemberTransitions: [],
 019            roleRequirements: [])
 20    {
 021    }
 22
 23    public TelegramRouteDescriptor(
 24        TelegramRouteKind routeKind,
 25        string? pattern,
 26        TelegramCommandPolicy? commandPolicy,
 27        IReadOnlyList<TelegramTextFilter> textFilters,
 28        Type? callbackPayloadType,
 29        IReadOnlyList<TelegramRouteValueDescriptor> routeValues)
 030        : this(
 031            routeKind,
 032            pattern,
 033            commandPolicy,
 034            textFilters,
 035            callbackPayloadType,
 036            routeValues,
 037            filters: [],
 038            chatMemberTransitions: [],
 039            roleRequirements: [])
 40    {
 041    }
 42
 43    public TelegramRouteDescriptor(
 44        TelegramRouteKind routeKind,
 45        string? pattern,
 46        TelegramCommandPolicy? commandPolicy,
 47        IReadOnlyList<TelegramTextFilter> textFilters,
 48        Type? callbackPayloadType,
 49        IReadOnlyList<TelegramRouteValueDescriptor> routeValues,
 50        IReadOnlyList<TelegramFilterDescriptor> filters)
 051        : this(
 052            routeKind,
 053            pattern,
 054            commandPolicy,
 055            textFilters,
 056            callbackPayloadType,
 057            routeValues,
 058            filters,
 059            chatMemberTransitions: [],
 060            roleRequirements: [])
 61    {
 062    }
 63
 64    public TelegramRouteDescriptor(
 65        TelegramRouteKind routeKind,
 66        string? pattern,
 67        TelegramCommandPolicy? commandPolicy,
 68        IReadOnlyList<TelegramTextFilter> textFilters,
 69        Type? callbackPayloadType,
 70        IReadOnlyList<TelegramRouteValueDescriptor> routeValues,
 71        IReadOnlyList<TelegramFilterDescriptor> filters,
 72        IReadOnlyList<TelegramChatMemberTransitionDescriptor> chatMemberTransitions)
 073        : this(
 074            routeKind,
 075            pattern,
 076            commandPolicy,
 077            textFilters,
 078            callbackPayloadType,
 079            routeValues,
 080            filters,
 081            chatMemberTransitions,
 082            roleRequirements: [])
 83    {
 084    }
 85
 86    public TelegramRouteDescriptor(
 87        TelegramRouteKind routeKind,
 88        string? pattern,
 89        TelegramCommandPolicy? commandPolicy,
 90        IReadOnlyList<TelegramTextFilter> textFilters,
 91        Type? callbackPayloadType,
 92        IReadOnlyList<TelegramRouteValueDescriptor> routeValues,
 93        IReadOnlyList<TelegramFilterDescriptor> filters,
 94        IReadOnlyList<TelegramChatMemberTransitionDescriptor> chatMemberTransitions,
 95        IReadOnlyList<TelegramRoleRequirementDescriptor> roleRequirements)
 96    {
 115297        ArgumentNullException.ThrowIfNull(textFilters);
 115298        ArgumentNullException.ThrowIfNull(routeValues);
 115299        ArgumentNullException.ThrowIfNull(filters);
 1152100        ArgumentNullException.ThrowIfNull(chatMemberTransitions);
 1152101        ArgumentNullException.ThrowIfNull(roleRequirements);
 102
 1152103        var normalizedPattern = NormalizeCommandPattern(routeKind, pattern);
 104
 105        RouteKind = routeKind;
 1152106        Kind = GetHandlerKind(routeKind);
 107        Pattern = normalizedPattern;
 1152108        CommandPolicy = commandPolicy ?? TelegramCommandPolicy.Default;
 109        TextFilters = textFilters;
 110        CallbackPayloadType = callbackPayloadType;
 111        RouteValues = routeValues;
 112        Filters = filters;
 113        ChatMemberTransitions = chatMemberTransitions;
 114        RoleRequirements = roleRequirements;
 1152115        Matcher = TelegramRouteMatcher.Create(routeKind, normalizedPattern, CommandPolicy.IgnoreCase);
 1152116        Specificity = GetSpecificity(routeKind, normalizedPattern);
 1152117    }
 118
 119    private static TelegramRouteKind MapLegacyRouteKind(
 120        TelegramHandlerKind kind,
 121        string? command)
 122    {
 0123        return kind switch
 0124        {
 0125            TelegramHandlerKind.Command => TelegramRouteKind.CommandExact,
 0126            TelegramHandlerKind.Callback => TelegramRouteKind.Callback,
 0127            TelegramHandlerKind.Message when command is null => TelegramRouteKind.MessageAny,
 0128            _ => throw new InvalidOperationException($"Unsupported Telegram handler kind '{kind}'.")
 0129        };
 130    }
 131
 132    private static TelegramHandlerKind GetHandlerKind(TelegramRouteKind routeKind)
 133    {
 1152134        return routeKind switch
 1152135        {
 1152136            TelegramRouteKind.CommandExact or
 1152137                TelegramRouteKind.CommandTemplate or
 383138                TelegramRouteKind.CommandRegex => TelegramHandlerKind.Command,
 268139            TelegramRouteKind.Callback => TelegramHandlerKind.Callback,
 1152140            TelegramRouteKind.ChatMemberUpdated or
 42141                TelegramRouteKind.MyChatMemberUpdated => TelegramHandlerKind.ChatMember,
 459142            _ => TelegramHandlerKind.Message
 1152143        };
 144    }
 145
 146    public TelegramHandlerKind Kind { get; }
 147
 148    public TelegramRouteKind RouteKind { get; }
 149
 150    public string? Pattern { get; }
 151
 1116152    public string? Command => Kind == TelegramHandlerKind.Command ? Pattern : null;
 153
 154    public TelegramCommandPolicy CommandPolicy { get; }
 155
 156    public IReadOnlyList<TelegramTextFilter> TextFilters { get; }
 157
 158    public Type? CallbackPayloadType { get; }
 159
 160    public IReadOnlyList<TelegramRouteValueDescriptor> RouteValues { get; }
 161
 162    public IReadOnlyList<TelegramFilterDescriptor> Filters { get; }
 163
 164    public IReadOnlyList<TelegramChatMemberTransitionDescriptor> ChatMemberTransitions { get; }
 165
 166    public IReadOnlyList<TelegramRoleRequirementDescriptor> RoleRequirements { get; }
 167
 168    public TelegramRouteMatcher Matcher { get; }
 169
 170    public int Specificity { get; }
 171
 172    private static string? NormalizeCommandPattern(
 173        TelegramRouteKind routeKind,
 174        string? pattern)
 175    {
 1152176        if (pattern is null ||
 1152177            routeKind is not (TelegramRouteKind.CommandExact or TelegramRouteKind.CommandTemplate))
 178        {
 772179            return pattern;
 180        }
 181
 380182        return TelegramCommandTextNormalizer.Normalize(pattern);
 183    }
 184
 185    private static int GetSpecificity(
 186        TelegramRouteKind routeKind,
 187        string? pattern)
 188    {
 1152189        return routeKind is TelegramRouteKind.TextTemplate or TelegramRouteKind.CommandTemplate &&
 1152190               !string.IsNullOrWhiteSpace(pattern)
 1152191            ? TelegramTemplateRouteParser.GetSpecificity(pattern)
 1152192            : 0;
 193    }
 194}

Methods/Properties

.ctor(TeleFlow.Telegram.Internal.Handlers.TelegramHandlerKind,System.String,System.Collections.Generic.IReadOnlyList`1<TeleFlow.Telegram.Internal.Handlers.TelegramTextFilter>,System.Type)
.ctor(TeleFlow.Telegram.Internal.Handlers.TelegramRouteKind,System.String,TeleFlow.Telegram.Internal.Handlers.TelegramCommandPolicy,System.Collections.Generic.IReadOnlyList`1<TeleFlow.Telegram.Internal.Handlers.TelegramTextFilter>,System.Type,System.Collections.Generic.IReadOnlyList`1<TeleFlow.Telegram.Internal.Handlers.TelegramRouteValueDescriptor>)
.ctor(TeleFlow.Telegram.Internal.Handlers.TelegramRouteKind,System.String,TeleFlow.Telegram.Internal.Handlers.TelegramCommandPolicy,System.Collections.Generic.IReadOnlyList`1<TeleFlow.Telegram.Internal.Handlers.TelegramTextFilter>,System.Type,System.Collections.Generic.IReadOnlyList`1<TeleFlow.Telegram.Internal.Handlers.TelegramRouteValueDescriptor>,System.Collections.Generic.IReadOnlyList`1<TeleFlow.Telegram.Internal.Handlers.TelegramFilterDescriptor>)
.ctor(TeleFlow.Telegram.Internal.Handlers.TelegramRouteKind,System.String,TeleFlow.Telegram.Internal.Handlers.TelegramCommandPolicy,System.Collections.Generic.IReadOnlyList`1<TeleFlow.Telegram.Internal.Handlers.TelegramTextFilter>,System.Type,System.Collections.Generic.IReadOnlyList`1<TeleFlow.Telegram.Internal.Handlers.TelegramRouteValueDescriptor>,System.Collections.Generic.IReadOnlyList`1<TeleFlow.Telegram.Internal.Handlers.TelegramFilterDescriptor>,System.Collections.Generic.IReadOnlyList`1<TeleFlow.Telegram.Internal.Handlers.TelegramChatMemberTransitionDescriptor>)
.ctor(TeleFlow.Telegram.Internal.Handlers.TelegramRouteKind,System.String,TeleFlow.Telegram.Internal.Handlers.TelegramCommandPolicy,System.Collections.Generic.IReadOnlyList`1<TeleFlow.Telegram.Internal.Handlers.TelegramTextFilter>,System.Type,System.Collections.Generic.IReadOnlyList`1<TeleFlow.Telegram.Internal.Handlers.TelegramRouteValueDescriptor>,System.Collections.Generic.IReadOnlyList`1<TeleFlow.Telegram.Internal.Handlers.TelegramFilterDescriptor>,System.Collections.Generic.IReadOnlyList`1<TeleFlow.Telegram.Internal.Handlers.TelegramChatMemberTransitionDescriptor>,System.Collections.Generic.IReadOnlyList`1<TeleFlow.Telegram.Internal.Handlers.TelegramRoleRequirementDescriptor>)
MapLegacyRouteKind(TeleFlow.Telegram.Internal.Handlers.TelegramHandlerKind,System.String)
GetHandlerKind(TeleFlow.Telegram.Internal.Handlers.TelegramRouteKind)
get_Command()
NormalizeCommandPattern(TeleFlow.Telegram.Internal.Handlers.TelegramRouteKind,System.String)
GetSpecificity(TeleFlow.Telegram.Internal.Handlers.TelegramRouteKind,System.String)