added Bind, Map and Match extensions generators
All checks were successful
.NET Test / test (push) Successful in 4m41s

This commit is contained in:
2023-11-28 21:41:01 +04:00
parent ac18863426
commit 6726ba07b3
8 changed files with 365 additions and 617 deletions

View File

@@ -0,0 +1,9 @@
using System;
using Microsoft.CodeAnalysis;
namespace Just.Railway.SourceGen;
internal interface IGeneratorExecutor
{
public abstract void Execute(SourceProductionContext context, Compilation source);
}

View File

@@ -0,0 +1,84 @@
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
namespace Just.Railway.SourceGen;
internal sealed class ResultBindExecutor : ResultExtensionsExecutor
{
protected override string ExtensionType => "Bind";
protected override void GenerateMethodsForArgCount(StringBuilder sb, int argCount)
{
var templateArgNames = Enumerable.Range(1, argCount)
.Select(i => $"T{i}")
.ToImmutableArray();
string separatedTemplateArgs = string.Join(", ", templateArgNames);
sb.AppendLine($"#region <{separatedTemplateArgs}>");
string resultValueType = templateArgNames.Length == 1 ? separatedTemplateArgs : $"({separatedTemplateArgs})";
string resultValueExpansion = GenerateResultValueExpansion(templateArgNames);
sb.AppendLine($$"""
[PureAttribute]
[GeneratedCodeAttribute("{{nameof(ResultBindExecutor)}}", "1.0.0.0")]
public static Result<R> Bind<{{separatedTemplateArgs}}, R>(this in Result<{{resultValueType}}> result, Func<{{separatedTemplateArgs}}, Result<R>> binding)
{
return result.State switch
{
ResultState.Success => binding({{resultValueExpansion}}),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(result))
};
}
""");
sb.AppendLine($$"""
[PureAttribute]
[GeneratedCodeAttribute("{{nameof(ResultBindExecutor)}}", "1.0.0.0")]
public static Task<Result<R>> Bind<{{separatedTemplateArgs}}, R>(this in Result<{{resultValueType}}> result, Func<{{separatedTemplateArgs}}, Task<Result<R>>> binding)
{
return result.State switch
{
ResultState.Success => binding({{resultValueExpansion}}),
ResultState.Error => Task.FromResult<Result<R>>(result.Error!),
_ => throw new ResultNotInitializedException(nameof(result))
};
}
""");
sb.AppendLine($$"""
[PureAttribute]
[GeneratedCodeAttribute("{{nameof(ResultBindExecutor)}}", "1.0.0.0")]
public static async Task<Result<R>> Bind<{{separatedTemplateArgs}}, R>(this Task<Result<{{resultValueType}}>> resultTask, Func<{{separatedTemplateArgs}}, Result<R>> binding)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => binding({{resultValueExpansion}}),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
""");
sb.AppendLine($$"""
[PureAttribute]
[GeneratedCodeAttribute("{{nameof(ResultBindExecutor)}}", "1.0.0.0")]
public static async Task<Result<R>> Bind<{{separatedTemplateArgs}}, R>(this Task<Result<{{resultValueType}}>> resultTask, Func<{{separatedTemplateArgs}}, Task<Result<R>>> binding)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => await binding({{resultValueExpansion}}).ConfigureAwait(false),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
""");
sb.AppendLine("#endregion");
}
}

View File

@@ -7,19 +7,9 @@ using Microsoft.CodeAnalysis;
namespace Just.Railway.SourceGen; namespace Just.Railway.SourceGen;
[Generator] internal sealed class ResultCombineExecutor : IGeneratorExecutor
public class ResultCombineGenerator : IIncrementalGenerator
{ {
public ResultCombineGenerator() public void Execute(SourceProductionContext context, Compilation source)
{
}
public void Initialize(IncrementalGeneratorInitializationContext context)
{
context.RegisterSourceOutput(context.CompilationProvider, Execute);
}
private void Execute(SourceProductionContext context, Compilation source)
{ {
var methods = GenerateCombineMethods(); var methods = GenerateCombineMethods();
var code = $$""" var code = $$"""
@@ -40,6 +30,7 @@ public class ResultCombineGenerator : IIncrementalGenerator
context.AddSource("Result.Combine.g.cs", code); context.AddSource("Result.Combine.g.cs", code);
} }
private string GenerateCombineMethods() private string GenerateCombineMethods()
{ {
var sb = new StringBuilder(); var sb = new StringBuilder();
@@ -95,7 +86,7 @@ public class ResultCombineGenerator : IIncrementalGenerator
.Select(i => $"result{i}") .Select(i => $"result{i}")
.ToImmutableArray(); .ToImmutableArray();
var argsDecl = string.Join(", ", args.Select(x => $"ResultState {x}")); var argsDecl = string.Join(", ", args.Select(x => $"ResultState {x}"));
sb.AppendLine($"[GeneratedCodeAttribute(\"{nameof(ResultCombineGenerator)}\", \"1.0.0.0\")]"); sb.AppendLine($"[GeneratedCodeAttribute(\"{nameof(ResultCombineExecutor)}\", \"1.0.0.0\")]");
sb.AppendLine($"private static IEnumerable<string> GetBottom({argsDecl})"); sb.AppendLine($"private static IEnumerable<string> GetBottom({argsDecl})");
sb.AppendLine("{"); sb.AppendLine("{");
foreach (var arg in args) foreach (var arg in args)
@@ -185,7 +176,7 @@ public class ResultCombineGenerator : IIncrementalGenerator
string returnExpr = $"return error is null ? new({resultExpansion}) : new(error);"; string returnExpr = $"return error is null ? new({resultExpansion}) : new(error);";
var method = $$""" var method = $$"""
[GeneratedCodeAttribute("{{nameof(ResultCombineGenerator)}}", "1.0.0.0")] [GeneratedCodeAttribute("{{nameof(ResultCombineExecutor)}}", "1.0.0.0")]
[PureAttribute] [PureAttribute]
public static {{resultTypeDecl}} Combine{{templateDecl}}({{paramDecl}}) public static {{resultTypeDecl}} Combine{{templateDecl}}({{paramDecl}})
{ {
@@ -211,4 +202,3 @@ public class ResultCombineGenerator : IIncrementalGenerator
} }
} }
} }

View File

@@ -0,0 +1,65 @@
using System.Collections.Immutable;
using System.Text;
using Microsoft.CodeAnalysis;
namespace Just.Railway.SourceGen;
internal abstract class ResultExtensionsExecutor : IGeneratorExecutor
{
public void Execute(SourceProductionContext context, Compilation source)
{
var methods = GenerateMethods();
var code = $$"""
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.CodeDom.Compiler;
namespace Just.Railway;
public static partial class ResultExtensions
{
{{methods}}
}
""";
context.AddSource($"ResultExtensions.{ExtensionType}.g.cs", code);
}
private string GenerateMethods()
{
var sb = new StringBuilder();
for (int i = 1; i <= Constants.MaxResultTupleSize; i++)
{
GenerateMethodsForArgCount(sb, argCount: i);
}
return sb.ToString();
}
protected string GenerateResultValueExpansion(ImmutableArray<string> templateArgNames)
{
string resultExpansion;
if (templateArgNames.Length > 1)
{
var resultExpansionBuilder = new StringBuilder();
for (int i = 1; i <= templateArgNames.Length; i++)
{
resultExpansionBuilder.Append($"result.Value.Item{i}, ");
}
resultExpansionBuilder.Remove(resultExpansionBuilder.Length - 2, 2);
resultExpansion = resultExpansionBuilder.ToString();
}
else
{
resultExpansion = "result.Value";
}
return resultExpansion;
}
protected abstract string ExtensionType { get; }
protected abstract void GenerateMethodsForArgCount(StringBuilder sb, int argCount);
}

View File

@@ -0,0 +1,83 @@
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
namespace Just.Railway.SourceGen;
internal sealed class ResultMapExecutor : ResultExtensionsExecutor
{
protected override string ExtensionType => "Map";
protected override void GenerateMethodsForArgCount(StringBuilder sb, int argCount)
{
var templateArgNames = Enumerable.Range(1, argCount)
.Select(i => $"T{i}")
.ToImmutableArray();
string separatedTemplateArgs = string.Join(", ", templateArgNames);
sb.AppendLine($"#region <{separatedTemplateArgs}>");
string resultValueType = templateArgNames.Length == 1 ? separatedTemplateArgs : $"({separatedTemplateArgs})";
string resultValueExpansion = GenerateResultValueExpansion(templateArgNames);
sb.AppendLine($$"""
[PureAttribute]
[GeneratedCodeAttribute("{{nameof(ResultMapExecutor)}}", "1.0.0.0")]
public static Result<R> Map<{{separatedTemplateArgs}}, R>(this in Result<{{resultValueType}}> result, Func<{{separatedTemplateArgs}}, R> mapping)
{
return result.State switch
{
ResultState.Success => mapping({{resultValueExpansion}}),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(result))
};
}
""");
sb.AppendLine($$"""
[PureAttribute]
[GeneratedCodeAttribute("{{nameof(ResultMapExecutor)}}", "1.0.0.0")]
public static async Task<Result<R>> Map<{{separatedTemplateArgs}}, R>(this Result<{{resultValueType}}> result, Func<{{separatedTemplateArgs}}, Task<R>> mapping)
{
return result.State switch
{
ResultState.Success => await mapping({{resultValueExpansion}}).ConfigureAwait(false),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(result))
};
}
""");
sb.AppendLine($$"""
[PureAttribute]
[GeneratedCodeAttribute("{{nameof(ResultMapExecutor)}}", "1.0.0.0")]
public static async Task<Result<R>> Map<{{separatedTemplateArgs}}, R>(this Task<Result<{{resultValueType}}>> resultTask, Func<{{separatedTemplateArgs}}, R> mapping)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => mapping({{resultValueExpansion}}),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
""");
sb.AppendLine($$"""
[PureAttribute]
[GeneratedCodeAttribute("{{nameof(ResultMapExecutor)}}", "1.0.0.0")]
public static async Task<Result<R>> Map<{{separatedTemplateArgs}}, R>(this Task<Result<{{resultValueType}}>> resultTask, Func<{{separatedTemplateArgs}}, Task<R>> mapping)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => await mapping({{resultValueExpansion}}).ConfigureAwait(false),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
""");
sb.AppendLine("#endregion");
}
}

View File

@@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
namespace Just.Railway.SourceGen;
internal sealed class ResultMatchExecutor : ResultExtensionsExecutor
{
protected override string ExtensionType => "Match";
protected override void GenerateMethodsForArgCount(StringBuilder sb, int argCount)
{
var templateArgNames = Enumerable.Range(1, argCount)
.Select(i => $"T{i}")
.ToImmutableArray();
string separatedTemplateArgs = string.Join(", ", templateArgNames);
sb.AppendLine($"#region <{separatedTemplateArgs}>");
string resultValueType = templateArgNames.Length == 1 ? separatedTemplateArgs : $"({separatedTemplateArgs})";
string resultValueExpansion = GenerateResultValueExpansion(templateArgNames);
sb.AppendLine($$"""
[PureAttribute]
[GeneratedCodeAttribute("{{nameof(ResultMatchExecutor)}}", "1.0.0.0")]
public static R Match<{{separatedTemplateArgs}}, R>(this in Result<{{resultValueType}}> result, Func<{{separatedTemplateArgs}}, R> onSuccess, Func<Error, R> onFailure)
{
return result.State switch
{
ResultState.Success => onSuccess({{resultValueExpansion}}),
ResultState.Error => onFailure(result.Error!),
_ => throw new ResultNotInitializedException(nameof(result))
};
}
""");
sb.AppendLine($$"""
[PureAttribute]
[GeneratedCodeAttribute("{{nameof(ResultMatchExecutor)}}", "1.0.0.0")]
public static Task<R> Match<{{separatedTemplateArgs}}, R>(this in Result<{{resultValueType}}> result, Func<{{separatedTemplateArgs}}, Task<R>> onSuccess, Func<Error, Task<R>> onFailure)
{
return result.State switch
{
ResultState.Success => onSuccess({{resultValueExpansion}}),
ResultState.Error => onFailure(result.Error!),
_ => throw new ResultNotInitializedException(nameof(result))
};
}
""");
sb.AppendLine($$"""
[PureAttribute]
[GeneratedCodeAttribute("{{nameof(ResultMatchExecutor)}}", "1.0.0.0")]
public static async Task<R> Match<{{separatedTemplateArgs}}, R>(this Task<Result<{{resultValueType}}>> resultTask, Func<{{separatedTemplateArgs}}, R> onSuccess, Func<Error, R> onFailure)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => onSuccess({{resultValueExpansion}}),
ResultState.Error => onFailure(result.Error!),
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
""");
sb.AppendLine($$"""
[PureAttribute]
[GeneratedCodeAttribute("{{nameof(ResultMatchExecutor)}}", "1.0.0.0")]
public static async Task<R> Match<{{separatedTemplateArgs}}, R>(this Task<Result<{{resultValueType}}>> resultTask, Func<{{separatedTemplateArgs}}, Task<R>> onSuccess, Func<Error, Task<R>> onFailure)
{
var result = await resultTask.ConfigureAwait(false);
var matchTask = result.State switch
{
ResultState.Success => onSuccess({{resultValueExpansion}}),
ResultState.Error => onFailure(result.Error!),
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
return await matchTask.ConfigureAwait(false);
}
""");
sb.AppendLine("#endregion");
}
}

View File

@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
namespace Just.Railway.SourceGen;
[Generator]
public class ResultMethodGenerator : IIncrementalGenerator
{
private readonly IEnumerable<IGeneratorExecutor> _executors = new IGeneratorExecutor[]
{
new ResultCombineExecutor(),
new ResultMatchExecutor(),
new ResultMapExecutor(),
new ResultBindExecutor(),
};
public void Initialize(IncrementalGeneratorInitializationContext context)
{
foreach (var executor in _executors)
{
context.RegisterSourceOutput(context.CompilationProvider, executor.Execute);
}
}
}

View File

@@ -1,10 +1,9 @@
namespace Just.Railway; namespace Just.Railway;
public static class ResultExtensions public static partial class ResultExtensions
{ {
#region Match #region Match<>
#region <>
[Pure] [Pure]
public static R Match<R>(this in Result result, Func<R> onSuccess, Func<Error, R> onFailure) public static R Match<R>(this in Result result, Func<R> onSuccess, Func<Error, R> onFailure)
{ {
@@ -52,211 +51,7 @@ public static class ResultExtensions
#endregion #endregion
#region <T> #region Map<>
[Pure]
public static R Match<T, R>(this in Result<T> result, Func<T, R> onSuccess, Func<Error, R> onFailure)
{
return result.State switch
{
ResultState.Success => onSuccess(result.Value),
ResultState.Error => onFailure(result.Error!),
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static Task<R> Match<T, R>(this in Result<T> result, Func<T, Task<R>> onSuccess, Func<Error, Task<R>> onFailure)
{
return result.State switch
{
ResultState.Success => onSuccess(result.Value),
ResultState.Error => onFailure(result.Error!),
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static async Task<R> Match<T, R>(this Task<Result<T>> resultTask, Func<T, R> onSuccess, Func<Error, R> onFailure)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => onSuccess(result.Value),
ResultState.Error => onFailure(result.Error!),
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
[Pure]
public static async Task<R> Match<T, R>(this Task<Result<T>> resultTask, Func<T, Task<R>> onSuccess, Func<Error, Task<R>> onFailure)
{
var result = await resultTask.ConfigureAwait(false);
var matchTask = result.State switch
{
ResultState.Success => onSuccess(result.Value),
ResultState.Error => onFailure(result.Error!),
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
return await matchTask.ConfigureAwait(false);
}
#endregion
#region <T1, T2>
[Pure]
public static R Match<T1, T2, R>(this in Result<(T1, T2)> result, Func<T1, T2, R> onSuccess, Func<Error, R> onFailure)
{
return result.State switch
{
ResultState.Success => onSuccess(result.Value.Item1, result.Value.Item2),
ResultState.Error => onFailure(result.Error!),
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static Task<R> Match<T1, T2, R>(this in Result<(T1, T2)> result, Func<T1, T2, Task<R>> onSuccess, Func<Error, Task<R>> onFailure)
{
return result.State switch
{
ResultState.Success => onSuccess(result.Value.Item1, result.Value.Item2),
ResultState.Error => onFailure(result.Error!),
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static async Task<R> Match<T1, T2, R>(this Task<Result<(T1, T2)>> resultTask, Func<T1, T2, R> onSuccess, Func<Error, R> onFailure)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => onSuccess(result.Value.Item1, result.Value.Item2),
ResultState.Error => onFailure(result.Error!),
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
[Pure]
public static async Task<R> Match<T1, T2, R>(this Task<Result<(T1, T2)>> resultTask, Func<T1, T2, Task<R>> onSuccess, Func<Error, Task<R>> onFailure)
{
var result = await resultTask.ConfigureAwait(false);
var matchTask = result.State switch
{
ResultState.Success => onSuccess(result.Value.Item1, result.Value.Item2),
ResultState.Error => onFailure(result.Error!),
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
return await matchTask.ConfigureAwait(false);
}
#endregion
#region <T1, T2, T3>
[Pure]
public static R Match<T1, T2, T3, R>(this in Result<(T1, T2, T3)> result, Func<T1, T2, T3, R> onSuccess, Func<Error, R> onFailure)
{
return result.State switch
{
ResultState.Success => onSuccess(result.Value.Item1, result.Value.Item2, result.Value.Item3),
ResultState.Error => onFailure(result.Error!),
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static Task<R> Match<T1, T2, T3, R>(this in Result<(T1, T2, T3)> result, Func<T1, T2, T3, Task<R>> onSuccess, Func<Error, Task<R>> onFailure)
{
return result.State switch
{
ResultState.Success => onSuccess(result.Value.Item1, result.Value.Item2, result.Value.Item3),
ResultState.Error => onFailure(result.Error!),
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static async Task<R> Match<T1, T2, T3, R>(this Task<Result<(T1, T2, T3)>> resultTask, Func<T1, T2, T3, R> onSuccess, Func<Error, R> onFailure)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => onSuccess(result.Value.Item1, result.Value.Item2, result.Value.Item3),
ResultState.Error => onFailure(result.Error!),
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
[Pure]
public static async Task<R> Match<T1, T2, T3, R>(this Task<Result<(T1, T2, T3)>> resultTask, Func<T1, T2, T3, Task<R>> onSuccess, Func<Error, Task<R>> onFailure)
{
var result = await resultTask.ConfigureAwait(false);
var matchTask = result.State switch
{
ResultState.Success => onSuccess(result.Value.Item1, result.Value.Item2, result.Value.Item3),
ResultState.Error => onFailure(result.Error!),
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
return await matchTask.ConfigureAwait(false);
}
#endregion
#region <T1, T2, T3, T4>
[Pure]
public static R Match<T1, T2, T3, T4, R>(this in Result<(T1, T2, T3, T4)> result, Func<T1, T2, T3, T4, R> onSuccess, Func<Error, R> onFailure)
{
return result.State switch
{
ResultState.Success => onSuccess(result.Value.Item1, result.Value.Item2, result.Value.Item3, result.Value.Item4),
ResultState.Error => onFailure(result.Error!),
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static Task<R> Match<T1, T2, T3, T4, R>(this in Result<(T1, T2, T3, T4)> result, Func<T1, T2, T3, T4, Task<R>> onSuccess, Func<Error, Task<R>> onFailure)
{
return result.State switch
{
ResultState.Success => onSuccess(result.Value.Item1, result.Value.Item2, result.Value.Item3, result.Value.Item4),
ResultState.Error => onFailure(result.Error!),
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static async Task<R> Match<T1, T2, T3, T4, R>(this Task<Result<(T1, T2, T3, T4)>> resultTask, Func<T1, T2, T3, T4, R> onSuccess, Func<Error, R> onFailure)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => onSuccess(result.Value.Item1, result.Value.Item2, result.Value.Item3, result.Value.Item4),
ResultState.Error => onFailure(result.Error!),
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
[Pure]
public static async Task<R> Match<T1, T2, T3, T4, R>(this Task<Result<(T1, T2, T3, T4)>> resultTask, Func<T1, T2, T3, T4, Task<R>> onSuccess, Func<Error, Task<R>> onFailure)
{
var result = await resultTask.ConfigureAwait(false);
var matchTask = result.State switch
{
ResultState.Success => onSuccess(result.Value.Item1, result.Value.Item2, result.Value.Item3, result.Value.Item4),
ResultState.Error => onFailure(result.Error!),
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
return await matchTask.ConfigureAwait(false);
}
#endregion
#endregion
#region Map
#region <>
[Pure] [Pure]
public static Result<R> Map<R>(this in Result result, Func<R> mapping) public static Result<R> Map<R>(this in Result result, Func<R> mapping)
@@ -306,211 +101,7 @@ public static class ResultExtensions
#endregion #endregion
#region <T> #region Bind<>
[Pure]
public static Result<R> Map<T, R>(this in Result<T> result, Func<T, R> mapping)
{
return result.State switch
{
ResultState.Success => mapping(result.Value),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static async Task<Result<R>> Map<T, R>(this Result<T> result, Func<T, Task<R>> mapping)
{
return result.State switch
{
ResultState.Success => await mapping(result.Value).ConfigureAwait(false),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static async Task<Result<R>> Map<T, R>(this Task<Result<T>> resultTask, Func<T, R> mapping)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => mapping(result.Value),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
[Pure]
public static async Task<Result<R>> Map<T, R>(this Task<Result<T>> resultTask, Func<T, Task<R>> mapping)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => await mapping(result.Value).ConfigureAwait(false),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
#endregion
#region <T1, T2>
[Pure]
public static Result<R> Map<T1, T2, R>(this in Result<(T1, T2)> result, Func<T1, T2, R> mapping)
{
return result.State switch
{
ResultState.Success => mapping(result.Value.Item1, result.Value.Item2),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static async Task<Result<R>> Map<T1, T2, R>(this Result<(T1, T2)> result, Func<T1, T2, Task<R>> mapping)
{
return result.State switch
{
ResultState.Success => await mapping(result.Value.Item1, result.Value.Item2).ConfigureAwait(false),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static async Task<Result<R>> Map<T1, T2, R>(this Task<Result<(T1, T2)>> resultTask, Func<T1, T2, R> mapping)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => mapping(result.Value.Item1, result.Value.Item2),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
[Pure]
public static async Task<Result<R>> Map<T1, T2, R>(this Task<Result<(T1, T2)>> resultTask, Func<T1, T2, Task<R>> mapping)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => await mapping(result.Value.Item1, result.Value.Item2).ConfigureAwait(false),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
#endregion
#region <T1, T2, T3>
[Pure]
public static Result<R> Map<T1, T2, T3, R>(this in Result<(T1, T2, T3)> result, Func<T1, T2, T3, R> mapping)
{
return result.State switch
{
ResultState.Success => mapping(result.Value.Item1, result.Value.Item2, result.Value.Item3),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static async Task<Result<R>> Map<T1, T2, T3, R>(this Result<(T1, T2, T3)> result, Func<T1, T2, T3, Task<R>> mapping)
{
return result.State switch
{
ResultState.Success => await mapping(result.Value.Item1, result.Value.Item2, result.Value.Item3).ConfigureAwait(false),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static async Task<Result<R>> Map<T1, T2, T3, R>(this Task<Result<(T1, T2, T3)>> resultTask, Func<T1, T2, T3, R> mapping)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => mapping(result.Value.Item1, result.Value.Item2, result.Value.Item3),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
[Pure]
public static async Task<Result<R>> Map<T1, T2, T3, R>(this Task<Result<(T1, T2, T3)>> resultTask, Func<T1, T2, T3, Task<R>> mapping)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => await mapping(result.Value.Item1, result.Value.Item2, result.Value.Item3).ConfigureAwait(false),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
#endregion
#region <T1, T2, T3, T4>
[Pure]
public static Result<R> Map<T1, T2, T3, T4, R>(this in Result<(T1, T2, T3, T4)> result, Func<T1, T2, T3, T4, R> mapping)
{
return result.State switch
{
ResultState.Success => mapping(result.Value.Item1, result.Value.Item2, result.Value.Item3, result.Value.Item4),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static async Task<Result<R>> Map<T1, T2, T3, T4, R>(this Result<(T1, T2, T3, T4)> result, Func<T1, T2, T3, T4, Task<R>> mapping)
{
return result.State switch
{
ResultState.Success => await mapping(result.Value.Item1, result.Value.Item2, result.Value.Item3, result.Value.Item4).ConfigureAwait(false),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static async Task<Result<R>> Map<T1, T2, T3, T4, R>(this Task<Result<(T1, T2, T3, T4)>> resultTask, Func<T1, T2, T3, T4, R> mapping)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => mapping(result.Value.Item1, result.Value.Item2, result.Value.Item3, result.Value.Item4),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
[Pure]
public static async Task<Result<R>> Map<T1, T2, T3, T4, R>(this Task<Result<(T1, T2, T3, T4)>> resultTask, Func<T1, T2, T3, T4, Task<R>> mapping)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => await mapping(result.Value.Item1, result.Value.Item2, result.Value.Item3, result.Value.Item4).ConfigureAwait(false),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
#endregion
#endregion
#region Bind
#region <>
[Pure] [Pure]
public static Result Bind(this in Result result, Func<Result> binding) public static Result Bind(this in Result result, Func<Result> binding)
@@ -600,195 +191,6 @@ public static class ResultExtensions
#endregion #endregion
#region <T>
[Pure]
public static Result<R> Bind<T, R>(this in Result<T> result, Func<T, Result<R>> binding)
{
return result.State switch
{
ResultState.Success => binding(result.Value),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static Task<Result<R>> Bind<T, R>(this in Result<T> result, Func<T, Task<Result<R>>> binding)
{
return result.State switch
{
ResultState.Success => binding(result.Value),
ResultState.Error => Task.FromResult<Result<R>>(result.Error!),
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static async Task<Result<R>> Bind<T, R>(this Task<Result<T>> resultTask, Func<T, Result<R>> binding)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => binding(result.Value),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
[Pure]
public static async Task<Result<R>> Bind<T, R>(this Task<Result<T>> resultTask, Func<T, Task<Result<R>>> binding)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => await binding(result.Value).ConfigureAwait(false),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
#endregion
#region <T1, T2>
[Pure]
public static Result<R> Bind<T1, T2, R>(this in Result<(T1, T2)> result, Func<T1, T2, Result<R>> binding)
{
return result.State switch
{
ResultState.Success => binding(result.Value.Item1, result.Value.Item2),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static Task<Result<R>> Bind<T1, T2, R>(this in Result<(T1, T2)> result, Func<T1, T2, Task<Result<R>>> binding)
{
return result.State switch
{
ResultState.Success => binding(result.Value.Item1, result.Value.Item2),
ResultState.Error => Task.FromResult<Result<R>>(result.Error!),
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static async Task<Result<R>> Bind<T1, T2, R>(this Task<Result<(T1, T2)>> resultTask, Func<T1, T2, Result<R>> binding)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => binding(result.Value.Item1, result.Value.Item2),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
[Pure]
public static async Task<Result<R>> Bind<T1, T2, R>(this Task<Result<(T1, T2)>> resultTask, Func<T1, T2, Task<Result<R>>> binding)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => await binding(result.Value.Item1, result.Value.Item2).ConfigureAwait(false),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
#endregion
#region <T1, T2, T3>
[Pure]
public static Result<R> Bind<T1, T2, T3, R>(this in Result<(T1, T2, T3)> result, Func<T1, T2, T3, Result<R>> binding)
{
return result.State switch
{
ResultState.Success => binding(result.Value.Item1, result.Value.Item2, result.Value.Item3),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static Task<Result<R>> Bind<T1, T2, T3, R>(this in Result<(T1, T2, T3)> result, Func<T1, T2, T3, Task<Result<R>>> binding)
{
return result.State switch
{
ResultState.Success => binding(result.Value.Item1, result.Value.Item2, result.Value.Item3),
ResultState.Error => Task.FromResult<Result<R>>(result.Error!),
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static async Task<Result<R>> Bind<T1, T2, T3, R>(this Task<Result<(T1, T2, T3)>> resultTask, Func<T1, T2, T3, Result<R>> binding)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => binding(result.Value.Item1, result.Value.Item2, result.Value.Item3),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
[Pure]
public static async Task<Result<R>> Bind<T1, T2, T3, R>(this Task<Result<(T1, T2, T3)>> resultTask, Func<T1, T2, T3, Task<Result<R>>> binding)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => await binding(result.Value.Item1, result.Value.Item2, result.Value.Item3).ConfigureAwait(false),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
#endregion
#region <T1, T2, T3, T4>
[Pure]
public static Result<R> Bind<T1, T2, T3, T4, R>(this in Result<(T1, T2, T3, T4)> result, Func<T1, T2, T3, T4, Result<R>> binding)
{
return result.State switch
{
ResultState.Success => binding(result.Value.Item1, result.Value.Item2, result.Value.Item3, result.Value.Item4),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static Task<Result<R>> Bind<T1, T2, T3, T4, R>(this in Result<(T1, T2, T3, T4)> result, Func<T1, T2, T3, T4, Task<Result<R>>> binding)
{
return result.State switch
{
ResultState.Success => binding(result.Value.Item1, result.Value.Item2, result.Value.Item3, result.Value.Item4),
ResultState.Error => Task.FromResult<Result<R>>(result.Error!),
_ => throw new ResultNotInitializedException(nameof(result))
};
}
[Pure]
public static async Task<Result<R>> Bind<T1, T2, T3, T4, R>(this Task<Result<(T1, T2, T3, T4)>> resultTask, Func<T1, T2, T3, T4, Result<R>> binding)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => binding(result.Value.Item1, result.Value.Item2, result.Value.Item3, result.Value.Item4),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
[Pure]
public static async Task<Result<R>> Bind<T1, T2, T3, T4, R>(this Task<Result<(T1, T2, T3, T4)>> resultTask, Func<T1, T2, T3, T4, Task<Result<R>>> binding)
{
var result = await resultTask.ConfigureAwait(false);
return result.State switch
{
ResultState.Success => await binding(result.Value.Item1, result.Value.Item2, result.Value.Item3, result.Value.Item4).ConfigureAwait(false),
ResultState.Error => result.Error!,
_ => throw new ResultNotInitializedException(nameof(resultTask))
};
}
#endregion
#endregion
#region Append #region Append
#region <> #region <>