mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-04-29 01:07:47 +00:00
Use CSharpier
This commit is contained in:
@@ -7,7 +7,8 @@ namespace DiscordChatExporter.Core.Utils.Extensions;
|
||||
public static class AsyncCollectionExtensions
|
||||
{
|
||||
private static async ValueTask<IReadOnlyList<T>> CollectAsync<T>(
|
||||
this IAsyncEnumerable<T> asyncEnumerable)
|
||||
this IAsyncEnumerable<T> asyncEnumerable
|
||||
)
|
||||
{
|
||||
var list = new List<T>();
|
||||
|
||||
@@ -18,6 +19,6 @@ public static class AsyncCollectionExtensions
|
||||
}
|
||||
|
||||
public static ValueTaskAwaiter<IReadOnlyList<T>> GetAwaiter<T>(
|
||||
this IAsyncEnumerable<T> asyncEnumerable) =>
|
||||
asyncEnumerable.CollectAsync().GetAwaiter();
|
||||
}
|
||||
this IAsyncEnumerable<T> asyncEnumerable
|
||||
) => asyncEnumerable.CollectAsync().GetAwaiter();
|
||||
}
|
||||
|
||||
@@ -11,11 +11,9 @@ public static class BinaryExtensions
|
||||
|
||||
foreach (var b in data)
|
||||
{
|
||||
buffer.Append(
|
||||
b.ToString(isUpperCase ? "X2" : "x2", CultureInfo.InvariantCulture)
|
||||
);
|
||||
buffer.Append(b.ToString(isUpperCase ? "X2" : "x2", CultureInfo.InvariantCulture));
|
||||
}
|
||||
|
||||
return buffer.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,8 @@ public static class CollectionExtensions
|
||||
yield return (o, i++);
|
||||
}
|
||||
|
||||
public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> source) where T : class
|
||||
public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> source)
|
||||
where T : class
|
||||
{
|
||||
foreach (var o in source)
|
||||
{
|
||||
@@ -24,4 +25,4 @@ public static class CollectionExtensions
|
||||
yield return o;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,4 +11,4 @@ public static class ColorExtensions
|
||||
public static int ToRgb(this Color color) => color.ToArgb() & 0xffffff;
|
||||
|
||||
public static string ToHex(this Color color) => $"#{color.R:X2}{color.G:X2}{color.B:X2}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,4 +7,4 @@ public static class DateExtensions
|
||||
{
|
||||
public static string ToLocalString(this DateTimeOffset instant, string format) =>
|
||||
instant.ToLocalTime().ToString(format, CultureInfo.InvariantCulture);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,8 +24,8 @@ public static class ExceptionExtensions
|
||||
|
||||
public static IReadOnlyList<Exception> GetSelfAndChildren(this Exception exception)
|
||||
{
|
||||
var children = new List<Exception> {exception};
|
||||
var children = new List<Exception> { exception };
|
||||
PopulateChildren(exception, children);
|
||||
return children;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,13 +5,12 @@ namespace DiscordChatExporter.Core.Utils.Extensions;
|
||||
|
||||
public static class GenericExtensions
|
||||
{
|
||||
public static TOut Pipe<TIn, TOut>(this TIn input, Func<TIn, TOut> transform) => transform(input);
|
||||
public static TOut Pipe<TIn, TOut>(this TIn input, Func<TIn, TOut> transform) =>
|
||||
transform(input);
|
||||
|
||||
public static T? NullIf<T>(this T value, Func<T, bool> predicate) where T : struct =>
|
||||
!predicate(value)
|
||||
? value
|
||||
: null;
|
||||
public static T? NullIf<T>(this T value, Func<T, bool> predicate)
|
||||
where T : struct => !predicate(value) ? value : null;
|
||||
|
||||
public static T? NullIfDefault<T>(this T value) where T : struct =>
|
||||
value.NullIf(v => EqualityComparer<T>.Default.Equals(v, default));
|
||||
}
|
||||
public static T? NullIfDefault<T>(this T value)
|
||||
where T : struct => value.NullIf(v => EqualityComparer<T>.Default.Equals(v, default));
|
||||
}
|
||||
|
||||
@@ -5,7 +5,5 @@ namespace DiscordChatExporter.Core.Utils.Extensions;
|
||||
public static class HttpExtensions
|
||||
{
|
||||
public static string? TryGetValue(this HttpHeaders headers, string name) =>
|
||||
headers.TryGetValues(name, out var values)
|
||||
? string.Concat(values)
|
||||
: null;
|
||||
}
|
||||
headers.TryGetValues(name, out var values) ? string.Concat(values) : null;
|
||||
}
|
||||
|
||||
@@ -7,14 +7,10 @@ namespace DiscordChatExporter.Core.Utils.Extensions;
|
||||
public static class StringExtensions
|
||||
{
|
||||
public static string? NullIfWhiteSpace(this string str) =>
|
||||
!string.IsNullOrWhiteSpace(str)
|
||||
? str
|
||||
: null;
|
||||
!string.IsNullOrWhiteSpace(str) ? str : null;
|
||||
|
||||
public static string Truncate(this string str, int charCount) =>
|
||||
str.Length > charCount
|
||||
? str[..charCount]
|
||||
: str;
|
||||
str.Length > charCount ? str[..charCount] : str;
|
||||
|
||||
public static string ToSpaceSeparatedWords(this string str)
|
||||
{
|
||||
@@ -41,13 +37,9 @@ public static class StringExtensions
|
||||
}
|
||||
}
|
||||
|
||||
public static T? ParseEnumOrNull<T>(this string str, bool ignoreCase = true) where T : struct, Enum =>
|
||||
Enum.TryParse<T>(str, ignoreCase, out var result)
|
||||
? result
|
||||
: null;
|
||||
public static T? ParseEnumOrNull<T>(this string str, bool ignoreCase = true)
|
||||
where T : struct, Enum => Enum.TryParse<T>(str, ignoreCase, out var result) ? result : null;
|
||||
|
||||
public static StringBuilder AppendIfNotEmpty(this StringBuilder builder, char value) =>
|
||||
builder.Length > 0
|
||||
? builder.Append(value)
|
||||
: builder;
|
||||
}
|
||||
builder.Length > 0 ? builder.Append(value) : builder;
|
||||
}
|
||||
|
||||
@@ -16,11 +16,12 @@ public static class SuperpowerExtensions
|
||||
// Only used for debugging while writing Superpower parsers.
|
||||
// From https://twitter.com/nblumhardt/status/1389349059786264578
|
||||
[ExcludeFromCodeCoverage]
|
||||
public static TextParser<T> Log<T>(this TextParser<T> parser, string description) => i =>
|
||||
{
|
||||
Console.WriteLine($"Trying {description} ->");
|
||||
var r = parser(i);
|
||||
Console.WriteLine($"Result was {r}");
|
||||
return r;
|
||||
};
|
||||
}
|
||||
public static TextParser<T> Log<T>(this TextParser<T> parser, string description) =>
|
||||
i =>
|
||||
{
|
||||
Console.WriteLine($"Trying {description} ->");
|
||||
var r = parser(i);
|
||||
Console.WriteLine($"Result was {r}");
|
||||
return r;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -14,4 +14,4 @@ public static class TimeSpanExtensions
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,16 +15,21 @@ public static class Http
|
||||
public static HttpClient Client { get; } = new();
|
||||
|
||||
private static bool IsRetryableStatusCode(HttpStatusCode statusCode) =>
|
||||
statusCode is HttpStatusCode.TooManyRequests or HttpStatusCode.RequestTimeout ||
|
||||
statusCode is HttpStatusCode.TooManyRequests or HttpStatusCode.RequestTimeout
|
||||
||
|
||||
// Treat all server-side errors as retryable
|
||||
// https://github.com/Tyrrrz/DiscordChatExporter/issues/908
|
||||
(int)statusCode >= 500;
|
||||
|
||||
private static bool IsRetryableException(Exception exception) =>
|
||||
exception.GetSelfAndChildren().Any(ex =>
|
||||
ex is TimeoutException or SocketException or AuthenticationException ||
|
||||
ex is HttpRequestException hrex && IsRetryableStatusCode(hrex.StatusCode ?? HttpStatusCode.OK)
|
||||
);
|
||||
exception
|
||||
.GetSelfAndChildren()
|
||||
.Any(
|
||||
ex =>
|
||||
ex is TimeoutException or SocketException or AuthenticationException
|
||||
|| ex is HttpRequestException hrex
|
||||
&& IsRetryableStatusCode(hrex.StatusCode ?? HttpStatusCode.OK)
|
||||
);
|
||||
|
||||
public static IAsyncPolicy ResiliencePolicy { get; } =
|
||||
Policy
|
||||
@@ -51,4 +56,4 @@ public static class Http
|
||||
},
|
||||
(_, _, _, _) => Task.CompletedTask
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,8 @@ namespace DiscordChatExporter.Core.Utils;
|
||||
|
||||
public static class PathEx
|
||||
{
|
||||
private static readonly HashSet<char> InvalidFileNameChars = new(Path.GetInvalidFileNameChars());
|
||||
private static readonly HashSet<char> InvalidFileNameChars =
|
||||
new(Path.GetInvalidFileNameChars());
|
||||
|
||||
public static string EscapeFileName(string path)
|
||||
{
|
||||
@@ -28,6 +29,5 @@ public static class PathEx
|
||||
}
|
||||
|
||||
public static bool IsDirectoryPath(string path) =>
|
||||
path.EndsWith(Path.DirectorySeparatorChar) ||
|
||||
path.EndsWith(Path.AltDirectorySeparatorChar);
|
||||
}
|
||||
path.EndsWith(Path.DirectorySeparatorChar) || path.EndsWith(Path.AltDirectorySeparatorChar);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,8 @@ public class UrlBuilder
|
||||
{
|
||||
private string _path = "";
|
||||
|
||||
private readonly Dictionary<string, string?> _queryParameters = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly Dictionary<string, string?> _queryParameters =
|
||||
new(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
public UrlBuilder SetPath(string path)
|
||||
{
|
||||
@@ -37,8 +38,10 @@ public class UrlBuilder
|
||||
buffer.Append(_path);
|
||||
|
||||
if (_queryParameters.Any())
|
||||
buffer.Append('?').AppendJoin('&', _queryParameters.Select(kvp => $"{kvp.Key}={kvp.Value}"));
|
||||
buffer
|
||||
.Append('?')
|
||||
.AppendJoin('&', _queryParameters.Select(kvp => $"{kvp.Key}={kvp.Value}"));
|
||||
|
||||
return buffer.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user