mirror of https://github.com/ErsatzTV/ErsatzTV.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.5 KiB
43 lines
1.5 KiB
using System.Diagnostics.CodeAnalysis; |
|
using Microsoft.Extensions.Logging; |
|
|
|
namespace ErsatzTV.Core; |
|
|
|
[SuppressMessage("ReSharper", "VSTHRD003")] |
|
public static class LanguageExtensions |
|
{ |
|
public static Either<BaseError, TR> ToEither<TR>(this Validation<BaseError, TR> validation) => |
|
validation.ToEither().MapLeft(errors => errors.Join()); |
|
|
|
public static Task<Either<BaseError, TR>> ToEitherAsync<TR>(this Validation<BaseError, Task<TR>> validation) => |
|
validation.ToEither() |
|
.MapLeft(errors => errors.Join()) |
|
.MapAsync<BaseError, Task<TR>, TR>(e => e); |
|
|
|
public static Task<Either<BaseError, TR>> Apply<T, TR>( |
|
this Validation<BaseError, T> validation, |
|
Func<T, Task<TR>> task) => |
|
validation.Match<Task<Either<BaseError, TR>>>( |
|
async t => await task(t), |
|
error => Task.FromResult(Left<BaseError, TR>(error.Join()))); |
|
|
|
[SuppressMessage("Usage", "CA2254:Template should be a static expression")] |
|
public static Task<T> LogFailure<T>( |
|
this TryAsync<T> tryAsync, |
|
T defaultValue, |
|
ILogger logger, |
|
string message, |
|
params object[] args) => |
|
tryAsync.IfFail( |
|
ex => |
|
{ |
|
logger.LogError(ex, message, args); |
|
return defaultValue; |
|
}); |
|
|
|
public static Task<Unit> LogFailure( |
|
this TryAsync<Unit> tryAsync, |
|
ILogger logger, |
|
string message, |
|
params object[] args) => LogFailure(tryAsync, Unit.Default, logger, message, args); |
|
}
|
|
|