Compare commits
4 Commits
v1.0.0-rc1
...
v1.0.0-rc2
| Author | SHA1 | Date | |
|---|---|---|---|
| 0452b4f867 | |||
| 6726ba07b3 | |||
| ac18863426 | |||
| db2fa49ff5 |
@@ -24,9 +24,6 @@ jobs:
|
||||
- name: Setup nuget source
|
||||
run: dotnet nuget add source --name gitea_registry https://gitea.jstdev.ru/api/packages/just/nuget/index.json
|
||||
|
||||
- name: Parsing release tag
|
||||
run: echo ${{ gitea.ref_name }} | sed -E 's|^(v([0-9]+(\.[0-9]+){2}))(-([a-z0-9]+)){1}|/p:ReleaseVersion=\2 /p:VersionSuffix=\5|; s|^(v([0-9]+(\.[0-9]+){2}))$|/p:ReleaseVersion=\2|'
|
||||
|
||||
- name: Create the package
|
||||
env:
|
||||
RELEASE_VERSION: ${{ gitea.ref_name }}
|
||||
|
||||
@@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Railway", "Railway\Railway.
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Raliway.Tests", "Raliway.Tests\Raliway.Tests.csproj", "{607F91E4-83A2-48C4-BAC2-2205BEE81D93}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Railway.SourceGenerator", "Railway.SourceGenerator\Railway.SourceGenerator.csproj", "{1A3B8F0A-7A30-4AA8-BC15-47FA2D75B6BF}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -24,5 +26,9 @@ Global
|
||||
{607F91E4-83A2-48C4-BAC2-2205BEE81D93}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{607F91E4-83A2-48C4-BAC2-2205BEE81D93}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{607F91E4-83A2-48C4-BAC2-2205BEE81D93}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{1A3B8F0A-7A30-4AA8-BC15-47FA2D75B6BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{1A3B8F0A-7A30-4AA8-BC15-47FA2D75B6BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{1A3B8F0A-7A30-4AA8-BC15-47FA2D75B6BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{1A3B8F0A-7A30-4AA8-BC15-47FA2D75B6BF}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
||||
7
Railway.SourceGenerator/Constants.cs
Normal file
7
Railway.SourceGenerator/Constants.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Just.Railway.SourceGen;
|
||||
|
||||
internal static class Constants
|
||||
{
|
||||
public const int MaxResultTupleSize = 4;
|
||||
|
||||
}
|
||||
9
Railway.SourceGenerator/IGeneratorExecutor.cs
Normal file
9
Railway.SourceGenerator/IGeneratorExecutor.cs
Normal 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);
|
||||
}
|
||||
20
Railway.SourceGenerator/Railway.SourceGenerator.csproj
Normal file
20
Railway.SourceGenerator/Railway.SourceGenerator.csproj
Normal file
@@ -0,0 +1,20 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<LangVersion>latest</LangVersion>
|
||||
<IsPackable>false</IsPackable>
|
||||
<RootNamespace>Just.Railway.SourceGen</RootNamespace>
|
||||
|
||||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
84
Railway.SourceGenerator/ResultBindExecutor.cs
Normal file
84
Railway.SourceGenerator/ResultBindExecutor.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
204
Railway.SourceGenerator/ResultCombineExecutor.cs
Normal file
204
Railway.SourceGenerator/ResultCombineExecutor.cs
Normal file
@@ -0,0 +1,204 @@
|
||||
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 ResultCombineExecutor : IGeneratorExecutor
|
||||
{
|
||||
public void Execute(SourceProductionContext context, Compilation source)
|
||||
{
|
||||
var methods = GenerateCombineMethods();
|
||||
var code = $$"""
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.CodeDom.Compiler;
|
||||
|
||||
namespace Just.Railway;
|
||||
|
||||
public readonly partial struct Result
|
||||
{
|
||||
{{methods}}
|
||||
}
|
||||
""";
|
||||
|
||||
context.AddSource("Result.Combine.g.cs", code);
|
||||
}
|
||||
|
||||
|
||||
private string GenerateCombineMethods()
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
|
||||
for (int i = 2; i <= Constants.MaxResultTupleSize; i++)
|
||||
{
|
||||
GenerateCombineMethodsForArgCount(sb, argCount: i);
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private void GenerateCombineMethodsForArgCount(StringBuilder sb, int argCount)
|
||||
{
|
||||
sb.AppendLine($"#region Combine {argCount} Results");
|
||||
|
||||
GenerateGetBottomMethod(sb, argCount);
|
||||
|
||||
var argsResultTupleSizes = new List<ImmutableArray<int>>();
|
||||
Span<int> templateCounts = stackalloc int[argCount];
|
||||
|
||||
Permute(templateCounts, argsResultTupleSizes);
|
||||
|
||||
foreach (var argResultTupleSizes in argsResultTupleSizes)
|
||||
{
|
||||
sb.AppendLine(GenerateCombineMethodBody(argResultTupleSizes));
|
||||
}
|
||||
|
||||
sb.AppendLine("#endregion");
|
||||
|
||||
static void Permute(Span<int> templateCounts, ICollection<ImmutableArray<int>> argsResultTupleSizes, int lvl = 0)
|
||||
{
|
||||
int sum = 0;
|
||||
for (int i = 0; i < lvl; i++)
|
||||
{
|
||||
sum += templateCounts[i];
|
||||
}
|
||||
for (templateCounts[lvl] = 0; templateCounts[lvl] <= Constants.MaxResultTupleSize - sum; templateCounts[lvl]++)
|
||||
{
|
||||
if (lvl == templateCounts.Length - 1)
|
||||
{
|
||||
argsResultTupleSizes.Add(templateCounts.ToImmutableArray());
|
||||
continue;
|
||||
}
|
||||
Permute(templateCounts, argsResultTupleSizes, lvl + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void GenerateGetBottomMethod(StringBuilder sb, int argCount)
|
||||
{
|
||||
var args = Enumerable.Range(1, argCount)
|
||||
.Select(i => $"result{i}")
|
||||
.ToImmutableArray();
|
||||
var argsDecl = string.Join(", ", args.Select(x => $"ResultState {x}"));
|
||||
sb.AppendLine($"[GeneratedCodeAttribute(\"{nameof(ResultCombineExecutor)}\", \"1.0.0.0\")]");
|
||||
sb.AppendLine($"private static IEnumerable<string> GetBottom({argsDecl})");
|
||||
sb.AppendLine("{");
|
||||
foreach (var arg in args)
|
||||
{
|
||||
sb.AppendLine($" if ({arg} == ResultState.Bottom) yield return \"{arg}\";");
|
||||
}
|
||||
sb.AppendLine("}");
|
||||
}
|
||||
|
||||
private string GenerateCombineMethodBody(ImmutableArray<int> argResultTupleSizes)
|
||||
{
|
||||
var resultTupleSize = argResultTupleSizes.Sum();
|
||||
|
||||
var paramNames = Enumerable.Range(1, argResultTupleSizes.Length)
|
||||
.Select(i => $"result{i}")
|
||||
.ToImmutableArray();
|
||||
var templateArgNames = Enumerable.Range(1, resultTupleSize)
|
||||
.Select(i => $"T{i}")
|
||||
.ToImmutableArray();
|
||||
|
||||
string templateDecl = templateArgNames.IsEmpty
|
||||
? string.Empty
|
||||
: $"<{string.Join(", ", templateArgNames)}>";
|
||||
string resultTypeDecl = GetResultTypeDecl(templateArgNames);
|
||||
string paramDecl;
|
||||
{
|
||||
var paramDeclBuilder = new StringBuilder();
|
||||
int currentTemplateArg = 0;
|
||||
for (int i = 0; i < argResultTupleSizes.Length; i++)
|
||||
{
|
||||
var argResultTupleSize = argResultTupleSizes[i];
|
||||
string currentParamType = GetResultTypeDecl(templateArgNames.Slice(currentTemplateArg, argResultTupleSize));
|
||||
currentTemplateArg += argResultTupleSize;
|
||||
paramDeclBuilder.Append($"in {currentParamType} {paramNames[i]}, ");
|
||||
}
|
||||
paramDeclBuilder.Remove(paramDeclBuilder.Length-2, 2);
|
||||
paramDecl = paramDeclBuilder.ToString();
|
||||
}
|
||||
|
||||
var paramNameStates = paramNames.Select(x => $"{x}.State")
|
||||
.ToImmutableArray();
|
||||
string bottomStateCheck = string.Join(" & ", paramNameStates);
|
||||
string statesSeparatedList = string.Join(", ", paramNameStates);
|
||||
|
||||
string failureChecks;
|
||||
{
|
||||
var failureChecksBuilder = new StringBuilder();
|
||||
foreach (var paramName in paramNames)
|
||||
{
|
||||
failureChecksBuilder.AppendLine($" if ({paramName}.IsFailure) error += {paramName}.Error;");
|
||||
}
|
||||
failureChecks = failureChecksBuilder.ToString();
|
||||
}
|
||||
string resultExpansion;
|
||||
switch (resultTupleSize)
|
||||
{
|
||||
case 0:
|
||||
resultExpansion = "null";
|
||||
break;
|
||||
|
||||
case 1:
|
||||
resultExpansion = $"{paramNames[argResultTupleSizes.IndexOf(1)]}.Value";
|
||||
break;
|
||||
|
||||
default:
|
||||
var resultExpansionBuilder = new StringBuilder();
|
||||
resultExpansionBuilder.Append("(");
|
||||
for (int i = 0; i < argResultTupleSizes.Length; i++)
|
||||
{
|
||||
if (argResultTupleSizes[i] == 0) continue;
|
||||
if (argResultTupleSizes[i] == 1)
|
||||
{
|
||||
resultExpansionBuilder.Append($"{paramNames[i]}.Value, ");
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int valueIndex = 1; valueIndex <= argResultTupleSizes[i]; valueIndex++)
|
||||
{
|
||||
resultExpansionBuilder.Append($"{paramNames[i]}.Value.Item{valueIndex}, ");
|
||||
}
|
||||
}
|
||||
resultExpansionBuilder.Remove(resultExpansionBuilder.Length - 2, 2);
|
||||
resultExpansionBuilder.Append(")");
|
||||
resultExpansion = resultExpansionBuilder.ToString();
|
||||
break;
|
||||
}
|
||||
|
||||
string returnExpr = $"return error is null ? new({resultExpansion}) : new(error);";
|
||||
var method = $$"""
|
||||
[GeneratedCodeAttribute("{{nameof(ResultCombineExecutor)}}", "1.0.0.0")]
|
||||
[PureAttribute]
|
||||
public static {{resultTypeDecl}} Combine{{templateDecl}}({{paramDecl}})
|
||||
{
|
||||
if (({{bottomStateCheck}}) == ResultState.Bottom)
|
||||
{
|
||||
throw new ResultNotInitializedException(string.Join(';', GetBottom({{statesSeparatedList}})));
|
||||
}
|
||||
Error? error = null;
|
||||
{{failureChecks}}
|
||||
{{returnExpr}}
|
||||
}
|
||||
""";
|
||||
return method;
|
||||
|
||||
static string GetResultTypeDecl(IReadOnlyList<string> templateArgNames)
|
||||
{
|
||||
return templateArgNames.Count switch
|
||||
{
|
||||
0 => "Result",
|
||||
1 => $"Result<{templateArgNames[0]}>",
|
||||
_ => $"Result<({string.Join(", ", templateArgNames)})>"
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
65
Railway.SourceGenerator/ResultExtensionsExecutor.cs
Normal file
65
Railway.SourceGenerator/ResultExtensionsExecutor.cs
Normal 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);
|
||||
}
|
||||
83
Railway.SourceGenerator/ResultMapExecutor.cs
Normal file
83
Railway.SourceGenerator/ResultMapExecutor.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
87
Railway.SourceGenerator/ResultMatchExecutor.cs
Normal file
87
Railway.SourceGenerator/ResultMatchExecutor.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
29
Railway.SourceGenerator/ResultMethodGenerator.cs
Normal file
29
Railway.SourceGenerator/ResultMethodGenerator.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
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(),
|
||||
new ResultTapExecutor(),
|
||||
};
|
||||
|
||||
public void Initialize(IncrementalGeneratorInitializationContext context)
|
||||
{
|
||||
foreach (var executor in _executors)
|
||||
{
|
||||
context.RegisterSourceOutput(context.CompilationProvider, executor.Execute);
|
||||
}
|
||||
}
|
||||
}
|
||||
112
Railway.SourceGenerator/ResultTapExecutor.cs
Normal file
112
Railway.SourceGenerator/ResultTapExecutor.cs
Normal file
@@ -0,0 +1,112 @@
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace Just.Railway.SourceGen;
|
||||
|
||||
internal sealed class ResultTapExecutor : ResultExtensionsExecutor
|
||||
{
|
||||
protected override string ExtensionType => "Tap";
|
||||
|
||||
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(ResultTapExecutor)}}", "1.0.0.0")]
|
||||
public static ref readonly Result<{{resultValueType}}> Tap<{{separatedTemplateArgs}}>(this in Result<{{resultValueType}}> result, Action<{{separatedTemplateArgs}}>? onSuccess = null, Action<Error>? onFailure = null)
|
||||
{
|
||||
switch (result.State)
|
||||
{
|
||||
case ResultState.Success:
|
||||
onSuccess?.Invoke({{resultValueExpansion}});
|
||||
break;
|
||||
case ResultState.Error:
|
||||
onFailure?.Invoke(result.Error!);
|
||||
break;
|
||||
|
||||
default: throw new ResultNotInitializedException(nameof(result));
|
||||
}
|
||||
return ref result;
|
||||
}
|
||||
""");
|
||||
|
||||
sb.AppendLine($$"""
|
||||
[PureAttribute]
|
||||
[GeneratedCodeAttribute("{{nameof(ResultTapExecutor)}}", "1.0.0.0")]
|
||||
public static async Task<Result<{{resultValueType}}>> Tap<{{separatedTemplateArgs}}>(this Task<Result<{{resultValueType}}>> resultTask, Action<{{separatedTemplateArgs}}>? onSuccess = null, Action<Error>? onFailure = null)
|
||||
{
|
||||
var result = await resultTask.ConfigureAwait(false);
|
||||
switch (result.State)
|
||||
{
|
||||
case ResultState.Success:
|
||||
onSuccess?.Invoke({{resultValueExpansion}});
|
||||
break;
|
||||
case ResultState.Error:
|
||||
onFailure?.Invoke(result.Error!);
|
||||
break;
|
||||
|
||||
default: throw new ResultNotInitializedException(nameof(resultTask));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
""");
|
||||
|
||||
sb.AppendLine($$"""
|
||||
[PureAttribute]
|
||||
[GeneratedCodeAttribute("{{nameof(ResultTapExecutor)}}", "1.0.0.0")]
|
||||
public static async Task<Result<{{resultValueType}}>> Tap<{{separatedTemplateArgs}}>(this Result<{{resultValueType}}> result, Func<{{separatedTemplateArgs}}, Task>? onSuccess = null, Func<Error, Task>? onFailure = null)
|
||||
{
|
||||
switch (result.State)
|
||||
{
|
||||
case ResultState.Success:
|
||||
if (onSuccess is not null)
|
||||
await onSuccess.Invoke({{resultValueExpansion}}).ConfigureAwait(false);
|
||||
break;
|
||||
case ResultState.Error:
|
||||
if (onFailure is not null)
|
||||
await onFailure.Invoke(result.Error!).ConfigureAwait(false);
|
||||
break;
|
||||
|
||||
default: throw new ResultNotInitializedException(nameof(result));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
""");
|
||||
|
||||
sb.AppendLine($$"""
|
||||
[PureAttribute]
|
||||
[GeneratedCodeAttribute("{{nameof(ResultTapExecutor)}}", "1.0.0.0")]
|
||||
public static async Task<Result<{{resultValueType}}>> Tap<{{separatedTemplateArgs}}>(this Task<Result<{{resultValueType}}>> resultTask, Func<{{separatedTemplateArgs}}, Task>? onSuccess = null, Func<Error, Task>? onFailure = null)
|
||||
{
|
||||
var result = await resultTask.ConfigureAwait(false);
|
||||
switch (result.State)
|
||||
{
|
||||
case ResultState.Success:
|
||||
if (onSuccess is not null)
|
||||
await onSuccess.Invoke({{resultValueExpansion}}).ConfigureAwait(false);
|
||||
break;
|
||||
case ResultState.Error:
|
||||
if (onFailure is not null)
|
||||
await onFailure.Invoke(result.Error!).ConfigureAwait(false);
|
||||
break;
|
||||
|
||||
default: throw new ResultNotInitializedException(nameof(resultTask));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
""");
|
||||
|
||||
sb.AppendLine("#endregion");
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<EmitCompilerGeneratedFiles Condition="'$(Configuration)'=='Debug'">true</EmitCompilerGeneratedFiles>
|
||||
|
||||
<AssemblyName>Just.Railway</AssemblyName>
|
||||
<RootNamespace>Just.Railway</RootNamespace>
|
||||
@@ -19,4 +20,10 @@
|
||||
<InternalsVisibleTo Include="$(AssemblyName).Tests" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Railway.SourceGenerator\Railway.SourceGenerator.csproj"
|
||||
OutputItemType="Analyzer"
|
||||
ReferenceOutputAssembly="false" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
1057
Railway/Result.cs
1057
Railway/Result.cs
File diff suppressed because it is too large
Load Diff
@@ -1,10 +1,9 @@
|
||||
namespace Just.Railway;
|
||||
|
||||
public static class ResultExtensions
|
||||
public static partial class ResultExtensions
|
||||
{
|
||||
#region Match
|
||||
#region Match<>
|
||||
|
||||
#region <>
|
||||
[Pure]
|
||||
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
|
||||
|
||||
#region <T>
|
||||
[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 <>
|
||||
#region Map<>
|
||||
|
||||
[Pure]
|
||||
public static Result<R> Map<R>(this in Result result, Func<R> mapping)
|
||||
@@ -306,211 +101,7 @@ public static class ResultExtensions
|
||||
|
||||
#endregion
|
||||
|
||||
#region <T>
|
||||
|
||||
[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 <>
|
||||
#region Bind<>
|
||||
|
||||
[Pure]
|
||||
public static Result Bind(this in Result result, Func<Result> binding)
|
||||
@@ -600,195 +191,6 @@ public static class ResultExtensions
|
||||
|
||||
#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 <>
|
||||
@@ -1284,7 +686,7 @@ public static class ResultExtensions
|
||||
|
||||
#endregion
|
||||
|
||||
#region Tap
|
||||
#region Tap<>
|
||||
[Pure]
|
||||
public static ref readonly Result Tap(this in Result result, Action? onSuccess = null, Action<Error>? onFailure = null)
|
||||
{
|
||||
@@ -1360,289 +762,6 @@ public static class ResultExtensions
|
||||
return result;
|
||||
}
|
||||
|
||||
[Pure]
|
||||
public static ref readonly Result<T> Tap<T>(this in Result<T> result, Action<T>? onSuccess = null, Action<Error>? onFailure = null)
|
||||
{
|
||||
switch (result.State)
|
||||
{
|
||||
case ResultState.Success:
|
||||
onSuccess?.Invoke(result.Value!);
|
||||
break;
|
||||
case ResultState.Error:
|
||||
onFailure?.Invoke(result.Error!);
|
||||
break;
|
||||
|
||||
default: throw new ResultNotInitializedException(nameof(result));
|
||||
}
|
||||
return ref result;
|
||||
}
|
||||
[Pure]
|
||||
public static async Task<Result<T>> Tap<T>(this Task<Result<T>> resultTask, Action<T>? onSuccess = null, Action<Error>? onFailure = null)
|
||||
{
|
||||
var result = await resultTask.ConfigureAwait(false);
|
||||
switch (result.State)
|
||||
{
|
||||
case ResultState.Success:
|
||||
onSuccess?.Invoke(result.Value!);
|
||||
break;
|
||||
case ResultState.Error:
|
||||
onFailure?.Invoke(result.Error!);
|
||||
break;
|
||||
|
||||
default: throw new ResultNotInitializedException(nameof(resultTask));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
[Pure]
|
||||
public static async Task<Result<T>> Tap<T>(this Result<T> result, Func<T, Task>? onSuccess = null, Func<Error, Task>? onFailure = null)
|
||||
{
|
||||
switch (result.State)
|
||||
{
|
||||
case ResultState.Success:
|
||||
if (onSuccess is not null)
|
||||
await onSuccess.Invoke(result.Value!).ConfigureAwait(false);
|
||||
break;
|
||||
case ResultState.Error:
|
||||
if (onFailure is not null)
|
||||
await onFailure.Invoke(result.Error!).ConfigureAwait(false);
|
||||
break;
|
||||
|
||||
default: throw new ResultNotInitializedException(nameof(result));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
[Pure]
|
||||
public static async Task<Result<T>> Tap<T>(this Task<Result<T>> resultTask, Func<T, Task>? onSuccess = null, Func<Error, Task>? onFailure = null)
|
||||
{
|
||||
var result = await resultTask.ConfigureAwait(false);
|
||||
switch (result.State)
|
||||
{
|
||||
case ResultState.Success:
|
||||
if (onSuccess is not null)
|
||||
await onSuccess.Invoke(result.Value!).ConfigureAwait(false);
|
||||
break;
|
||||
case ResultState.Error:
|
||||
if (onFailure is not null)
|
||||
await onFailure.Invoke(result.Error!).ConfigureAwait(false);
|
||||
break;
|
||||
|
||||
default: throw new ResultNotInitializedException(nameof(resultTask));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
[Pure]
|
||||
public static ref readonly Result<(T1, T2)> Tap<T1, T2>(this in Result<(T1, T2)> result, Action<T1, T2>? onSuccess = null, Action<Error>? onFailure = null)
|
||||
{
|
||||
switch (result.State)
|
||||
{
|
||||
case ResultState.Success:
|
||||
onSuccess?.Invoke(result.Value.Item1, result.Value.Item2);
|
||||
break;
|
||||
case ResultState.Error:
|
||||
onFailure?.Invoke(result.Error!);
|
||||
break;
|
||||
|
||||
default: throw new ResultNotInitializedException(nameof(result));
|
||||
}
|
||||
return ref result;
|
||||
}
|
||||
[Pure]
|
||||
public static async Task<Result<(T1, T2)>> Tap<T1, T2>(this Task<Result<(T1, T2)>> resultTask, Action<T1, T2>? onSuccess = null, Action<Error>? onFailure = null)
|
||||
{
|
||||
var result = await resultTask.ConfigureAwait(false);
|
||||
switch (result.State)
|
||||
{
|
||||
case ResultState.Success:
|
||||
onSuccess?.Invoke(result.Value.Item1, result.Value.Item2);
|
||||
break;
|
||||
case ResultState.Error:
|
||||
onFailure?.Invoke(result.Error!);
|
||||
break;
|
||||
|
||||
default: throw new ResultNotInitializedException(nameof(resultTask));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
[Pure]
|
||||
public static async Task<Result<(T1, T2)>> Tap<T1, T2>(this Result<(T1, T2)> result, Func<T1, T2, Task>? onSuccess = null, Func<Error, Task>? onFailure = null)
|
||||
{
|
||||
switch (result.State)
|
||||
{
|
||||
case ResultState.Success:
|
||||
if (onSuccess is not null)
|
||||
await onSuccess.Invoke(result.Value.Item1, result.Value.Item2).ConfigureAwait(false);
|
||||
break;
|
||||
case ResultState.Error:
|
||||
if (onFailure is not null)
|
||||
await onFailure.Invoke(result.Error!).ConfigureAwait(false);
|
||||
break;
|
||||
|
||||
default: throw new ResultNotInitializedException(nameof(result));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
[Pure]
|
||||
public static async Task<Result<(T1, T2)>> Tap<T1, T2>(this Task<Result<(T1, T2)>> resultTask, Func<T1, T2, Task>? onSuccess = null, Func<Error, Task>? onFailure = null)
|
||||
{
|
||||
var result = await resultTask.ConfigureAwait(false);
|
||||
switch (result.State)
|
||||
{
|
||||
case ResultState.Success:
|
||||
if (onSuccess is not null)
|
||||
await onSuccess.Invoke(result.Value.Item1, result.Value.Item2).ConfigureAwait(false);
|
||||
break;
|
||||
case ResultState.Error:
|
||||
if (onFailure is not null)
|
||||
await onFailure.Invoke(result.Error!).ConfigureAwait(false);
|
||||
break;
|
||||
|
||||
default: throw new ResultNotInitializedException(nameof(resultTask));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
[Pure]
|
||||
public static ref readonly Result<(T1, T2, T3)> Tap<T1, T2, T3>(this in Result<(T1, T2, T3)> result, Action<T1, T2, T3>? onSuccess = null, Action<Error>? onFailure = null)
|
||||
{
|
||||
switch (result.State)
|
||||
{
|
||||
case ResultState.Success:
|
||||
onSuccess?.Invoke(result.Value.Item1, result.Value.Item2, result.Value.Item3);
|
||||
break;
|
||||
case ResultState.Error:
|
||||
onFailure?.Invoke(result.Error!);
|
||||
break;
|
||||
|
||||
default: throw new ResultNotInitializedException(nameof(result));
|
||||
}
|
||||
return ref result;
|
||||
}
|
||||
[Pure]
|
||||
public static async Task<Result<(T1, T2, T3)>> Tap<T1, T2, T3>(this Task<Result<(T1, T2, T3)>> resultTask, Action<T1, T2, T3>? onSuccess = null, Action<Error>? onFailure = null)
|
||||
{
|
||||
var result = await resultTask.ConfigureAwait(false);
|
||||
switch (result.State)
|
||||
{
|
||||
case ResultState.Success:
|
||||
onSuccess?.Invoke(result.Value.Item1, result.Value.Item2, result.Value.Item3);
|
||||
break;
|
||||
case ResultState.Error:
|
||||
onFailure?.Invoke(result.Error!);
|
||||
break;
|
||||
|
||||
default: throw new ResultNotInitializedException(nameof(resultTask));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
[Pure]
|
||||
public static async Task<Result<(T1, T2, T3)>> Tap<T1, T2, T3>(this Result<(T1, T2, T3)> result, Func<T1, T2, T3, Task>? onSuccess = null, Func<Error, Task>? onFailure = null)
|
||||
{
|
||||
switch (result.State)
|
||||
{
|
||||
case ResultState.Success:
|
||||
if (onSuccess is not null)
|
||||
await onSuccess.Invoke(result.Value.Item1, result.Value.Item2, result.Value.Item3).ConfigureAwait(false);
|
||||
break;
|
||||
case ResultState.Error:
|
||||
if (onFailure is not null)
|
||||
await onFailure.Invoke(result.Error!).ConfigureAwait(false);
|
||||
break;
|
||||
|
||||
default: throw new ResultNotInitializedException(nameof(result));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
[Pure]
|
||||
public static async Task<Result<(T1, T2, T3)>> Tap<T1, T2, T3>(this Task<Result<(T1, T2, T3)>> resultTask, Func<T1, T2, T3, Task>? onSuccess = null, Func<Error, Task>? onFailure = null)
|
||||
{
|
||||
var result = await resultTask.ConfigureAwait(false);
|
||||
switch (result.State)
|
||||
{
|
||||
case ResultState.Success:
|
||||
if (onSuccess is not null)
|
||||
await onSuccess.Invoke(result.Value.Item1, result.Value.Item2, result.Value.Item3).ConfigureAwait(false);
|
||||
break;
|
||||
case ResultState.Error:
|
||||
if (onFailure is not null)
|
||||
await onFailure.Invoke(result.Error!).ConfigureAwait(false);
|
||||
break;
|
||||
|
||||
default: throw new ResultNotInitializedException(nameof(resultTask));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
[Pure]
|
||||
public static ref readonly Result<(T1, T2, T3, T4)> Tap<T1, T2, T3, T4>(this in Result<(T1, T2, T3, T4)> result, Action<T1, T2, T3, T4>? onSuccess = null, Action<Error>? onFailure = null)
|
||||
{
|
||||
switch (result.State)
|
||||
{
|
||||
case ResultState.Success:
|
||||
onSuccess?.Invoke(result.Value.Item1, result.Value.Item2, result.Value.Item3, result.Value.Item4);
|
||||
break;
|
||||
case ResultState.Error:
|
||||
onFailure?.Invoke(result.Error!);
|
||||
break;
|
||||
|
||||
default: throw new ResultNotInitializedException(nameof(result));
|
||||
}
|
||||
return ref result;
|
||||
}
|
||||
[Pure]
|
||||
public static async Task<Result<(T1, T2, T3, T4)>> Tap<T1, T2, T3, T4>(this Task<Result<(T1, T2, T3, T4)>> resultTask, Action<T1, T2, T3, T4>? onSuccess = null, Action<Error>? onFailure = null)
|
||||
{
|
||||
var result = await resultTask.ConfigureAwait(false);
|
||||
switch (result.State)
|
||||
{
|
||||
case ResultState.Success:
|
||||
onSuccess?.Invoke(result.Value.Item1, result.Value.Item2, result.Value.Item3, result.Value.Item4);
|
||||
break;
|
||||
case ResultState.Error:
|
||||
onFailure?.Invoke(result.Error!);
|
||||
break;
|
||||
|
||||
default: throw new ResultNotInitializedException(nameof(resultTask));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
[Pure]
|
||||
public static async Task<Result<(T1, T2, T3, T4)>> Tap<T1, T2, T3, T4>(this Result<(T1, T2, T3, T4)> result, Func<T1, T2, T3, T4, Task>? onSuccess = null, Func<Error, Task>? onFailure = null)
|
||||
{
|
||||
switch (result.State)
|
||||
{
|
||||
case ResultState.Success:
|
||||
if (onSuccess is not null)
|
||||
await onSuccess.Invoke(result.Value.Item1, result.Value.Item2, result.Value.Item3, result.Value.Item4).ConfigureAwait(false);
|
||||
break;
|
||||
case ResultState.Error:
|
||||
if (onFailure is not null)
|
||||
await onFailure.Invoke(result.Error!).ConfigureAwait(false);
|
||||
break;
|
||||
|
||||
default: throw new ResultNotInitializedException(nameof(result));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
[Pure]
|
||||
public static async Task<Result<(T1, T2, T3, T4)>> Tap<T1, T2, T3, T4>(this Task<Result<(T1, T2, T3, T4)>> resultTask, Func<T1, T2, T3, T4, Task>? onSuccess = null, Func<Error, Task>? onFailure = null)
|
||||
{
|
||||
var result = await resultTask.ConfigureAwait(false);
|
||||
switch (result.State)
|
||||
{
|
||||
case ResultState.Success:
|
||||
if (onSuccess is not null)
|
||||
await onSuccess.Invoke(result.Value.Item1, result.Value.Item2, result.Value.Item3, result.Value.Item4).ConfigureAwait(false);
|
||||
break;
|
||||
case ResultState.Error:
|
||||
if (onFailure is not null)
|
||||
await onFailure.Invoke(result.Error!).ConfigureAwait(false);
|
||||
break;
|
||||
|
||||
default: throw new ResultNotInitializedException(nameof(resultTask));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Merge
|
||||
|
||||
Reference in New Issue
Block a user