Skip to content

Commit

Permalink
Use function body
Browse files Browse the repository at this point in the history
  • Loading branch information
dnperfors committed Nov 16, 2024
1 parent b3a5f8e commit 5ff86a6
Showing 1 changed file with 50 additions and 35 deletions.
85 changes: 50 additions & 35 deletions src/TestableHttpClient/Utils/UriPatternParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,49 +75,64 @@ private static UriPattern ParsePattern(ReadOnlySpan<char> patternSpan)
Query = ParseQuery(queryPattern)
};

static Value<string> ParseScheme(ReadOnlySpan<char> scheme) => scheme switch
static Value<string> ParseScheme(ReadOnlySpan<char> scheme)
{
[] => Value.Any(),
['*'] => Value.Any(),
_ when scheme.IndexOf('*') != -1 => Value.Pattern(scheme.ToString()),
_ => Value.Exact(scheme.ToString())
};
return scheme switch
{
[] => Value.Any(),
['*'] => Value.Any(),
_ when scheme.IndexOf('*') != -1 => Value.Pattern(scheme.ToString()),
_ => Value.Exact(scheme.ToString())
};
}

static Value<string> ParseHost(ReadOnlySpan<char> host) => host switch
static Value<string> ParseHost(ReadOnlySpan<char> host)
{
[] => Value.Any(),
['*'] => Value.Any(),
_ when host.IndexOf('*') != -1 => Value.Pattern(host.ToString()),
_ => Value.Exact(host.ToString())
};
return host switch
{
[] => Value.Any(),
['*'] => Value.Any(),
_ when host.IndexOf('*') != -1 => Value.Pattern(host.ToString()),
_ => Value.Exact(host.ToString())
};
}

static Value<string> ParsePort(ReadOnlySpan<char> port) => port switch
static Value<string> ParsePort(ReadOnlySpan<char> port)
{
[] => Value.Any(),
[':'] => throw new UriPatternParserException("Invalid port"),
[':', '*'] => Value.Any(),
[':', .. var rest] when rest.IndexOf('*') != -1 => Value.Pattern(rest.ToString()),
[':', .. var rest] => Value.Exact(rest.ToString()),
_ => throw new UnreachableException()
};
return port switch
{
[] => Value.Any(),
[':'] => throw new UriPatternParserException("Invalid port"),
[':', '*'] => Value.Any(),
[':', .. var rest] when rest.IndexOf('*') != -1 => Value.Pattern(rest.ToString()),
[':', .. var rest] => Value.Exact(rest.ToString()),
_ => throw new UnreachableException()
};
}

static Value<string> ParsePath(ReadOnlySpan<char> path) => path switch
static Value<string> ParsePath(ReadOnlySpan<char> path)
{
[] => Value.Any(),
['/', '*'] => Value.Any(),
['/', .. var rest] when rest.IndexOf('*') != -1 => Value.Pattern(path.ToString()),
['/', ..] => Value.Exact(path.ToString()),
_ => throw new UnreachableException()
};
return path switch
{
[] => Value.Any(),
['/', '*'] => Value.Any(),
['/', .. var rest] when rest.IndexOf('*') != -1 => Value.Pattern(path.ToString()),
['/', ..] => Value.Exact(path.ToString()),
_ => throw new UnreachableException()
};
}

static Value<string> ParseQuery(ReadOnlySpan<char> query) => query switch
static Value<string> ParseQuery(ReadOnlySpan<char> query)
{
[] => Value.Any(),
['?'] => Value.Any(),
['?', '*'] => Value.Any(),
['?', .. var rest] when rest.IndexOf('*') != -1 => Value.Pattern(rest.ToString()),
['?', .. var rest] => Value.Exact(rest.ToString()),
_ => throw new UnreachableException()
};
return query switch
{
[] => Value.Any(),
['?'] => Value.Any(),
['?', '*'] => Value.Any(),
['?', .. var rest] when rest.IndexOf('*') != -1 => Value.Pattern(rest.ToString()),
['?', .. var rest] => Value.Exact(rest.ToString()),
_ => throw new UnreachableException()
};
}
}
}

0 comments on commit 5ff86a6

Please sign in to comment.