Compare commits
5 Commits
v1.0.0-rc2
...
v1.0.0-rc3
| Author | SHA1 | Date | |
|---|---|---|---|
| bb8c2135b5 | |||
| 26a1c604d5 | |||
| 036b34d3c0 | |||
| f39b899514 | |||
| b79192ec6c |
@@ -2,6 +2,5 @@ namespace Just.Railway.SourceGen;
|
|||||||
|
|
||||||
internal static class Constants
|
internal static class Constants
|
||||||
{
|
{
|
||||||
public const int MaxResultTupleSize = 4;
|
public const int MaxResultTupleSize = 5;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
209
Railway.SourceGenerator/EnsureExtensionExecutor.cs
Normal file
209
Railway.SourceGenerator/EnsureExtensionExecutor.cs
Normal file
@@ -0,0 +1,209 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.Immutable;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Just.Railway.SourceGen;
|
||||||
|
|
||||||
|
public sealed class EnsureExtensionExecutor : IGeneratorExecutor
|
||||||
|
{
|
||||||
|
public void Execute(SourceProductionContext context, Compilation source)
|
||||||
|
{
|
||||||
|
var methods = GenerateMethods();
|
||||||
|
var code = $$"""
|
||||||
|
#nullable enable
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics.Contracts;
|
||||||
|
using System.CodeDom.Compiler;
|
||||||
|
|
||||||
|
namespace Just.Railway;
|
||||||
|
|
||||||
|
public static partial class Ensure
|
||||||
|
{
|
||||||
|
{{methods}}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
|
||||||
|
context.AddSource("Ensure.Extensions.g.cs", code);
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GenerateMethods()
|
||||||
|
{
|
||||||
|
List<(string ErrorParameterDecl, string ErrorValueExpr)> errorGenerationDefinitions =
|
||||||
|
[
|
||||||
|
("Error error = default!", "error"),
|
||||||
|
("ErrorFactory errorFactory", "errorFactory(ensure.ValueExpression)")
|
||||||
|
];
|
||||||
|
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
|
||||||
|
sb.AppendLine($"#region Satisfies");
|
||||||
|
errorGenerationDefinitions.ForEach(def => GenerateSatisfiesExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
||||||
|
sb.AppendLine("#endregion");
|
||||||
|
|
||||||
|
sb.AppendLine($"#region NotNull");
|
||||||
|
errorGenerationDefinitions.ForEach(def => GenerateNotNullExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
||||||
|
sb.AppendLine("#endregion");
|
||||||
|
|
||||||
|
sb.AppendLine($"#region NotEmpty");
|
||||||
|
errorGenerationDefinitions.ForEach(def => GenerateNotEmptyExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
||||||
|
sb.AppendLine("#endregion");
|
||||||
|
|
||||||
|
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateNotEmptyExtensions(StringBuilder sb, string errorParameterDecl, string errorValueExpr)
|
||||||
|
{
|
||||||
|
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} is empty.\")";
|
||||||
|
List<(string TemplateDef, string CollectionType, string NotEmptyTest)> typeOverloads =
|
||||||
|
[
|
||||||
|
("<T>", "IEnumerable<T>", "ensure.Value?.Any() == true"),
|
||||||
|
("<T>", "ICollection<T>", "ensure.Value?.Count > 0"),
|
||||||
|
("<T>", "IReadOnlyCollection<T>", "ensure.Value?.Count > 0"),
|
||||||
|
("<T>", "IList<T>", "ensure.Value?.Count > 0"),
|
||||||
|
("<T>", "IReadOnlyList<T>", "ensure.Value?.Count > 0"),
|
||||||
|
("<T>", "ISet<T>", "ensure.Value?.Count > 0"),
|
||||||
|
("<T>", "IReadOnlySet<T>", "ensure.Value?.Count > 0"),
|
||||||
|
("<TKey,TValue>", "IDictionary<TKey,TValue>", "ensure.Value?.Count > 0"),
|
||||||
|
("<TKey,TValue>", "IReadOnlyDictionary<TKey,TValue>", "ensure.Value?.Count > 0"),
|
||||||
|
("<T>", "T[]", "ensure.Value?.Length > 0"),
|
||||||
|
("<T>", "List<T>", "ensure.Value?.Count > 0"),
|
||||||
|
("<T>", "Queue<T>", "ensure.Value?.Count > 0"),
|
||||||
|
("<T>", "HashSet<T>", "ensure.Value?.Count > 0"),
|
||||||
|
("", "string", "!string.IsNullOrEmpty(ensure.Value)"),
|
||||||
|
];
|
||||||
|
|
||||||
|
typeOverloads.ForEach(def => sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Ensure<{{def.CollectionType}}> NotEmpty{{def.TemplateDef}}(this in Ensure<{{def.CollectionType}}> ensure, {{errorParameterDecl}})
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => {{def.NotEmptyTest}}
|
||||||
|
? new(ensure.Value, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
"""));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateNotNullExtensions(StringBuilder sb, string errorParameterDecl, string errorValueExpr)
|
||||||
|
{
|
||||||
|
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} is null.\")";
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Ensure<T> NotNull<T>(this in Ensure<T?> ensure, {{errorParameterDecl}})
|
||||||
|
where T : struct
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => ensure.Value.HasValue
|
||||||
|
? new(ensure.Value.Value, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Ensure<T> NotNull<T>(this in Ensure<T?> ensure, {{errorParameterDecl}})
|
||||||
|
where T : notnull
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => ensure.Value is not null
|
||||||
|
? new(ensure.Value!, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateSatisfiesExtensions(StringBuilder sb, string errorParameterDecl, string errorValueExpr)
|
||||||
|
{
|
||||||
|
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} does not satisfy the requirement.\")";
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Ensure<T> Satisfies<T>(this in Ensure<T> ensure, Func<T, bool> requirement, {{errorParameterDecl}})
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => requirement(ensure.Value)
|
||||||
|
? new(ensure.Value, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
GenerateSatisfiesAsyncExtensions(sb, "Task", errorParameterDecl, errorValueExpr, defaultErrorExpr);
|
||||||
|
GenerateSatisfiesAsyncExtensions(sb, "ValueTask", errorParameterDecl, errorValueExpr, defaultErrorExpr);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateSatisfiesAsyncExtensions(StringBuilder sb, string taskType, string errorParameterDecl, string errorValueExpr, string defaultErrorExpr)
|
||||||
|
{
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<Ensure<T>> Satisfies<T>(this {{taskType}}<Ensure<T>> ensureTask, Func<T, bool> requirement, {{errorParameterDecl}})
|
||||||
|
{
|
||||||
|
var ensure = await ensureTask.ConfigureAwait(false);
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => requirement(ensure.Value)
|
||||||
|
? new(ensure.Value, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensureTask))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<Ensure<T>> Satisfies<T>(this Ensure<T> ensure, Func<T, {{taskType}}<bool>> requirement, {{errorParameterDecl}})
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => await requirement(ensure.Value).ConfigureAwait(false)
|
||||||
|
? new(ensure.Value, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<Ensure<T>> Satisfies<T>(this {{taskType}}<Ensure<T>> ensureTask, Func<T, {{taskType}}<bool>> requirement, {{errorParameterDecl}})
|
||||||
|
{
|
||||||
|
var ensure = await ensureTask.ConfigureAwait(false);
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => await requirement(ensure.Value).ConfigureAwait(false)
|
||||||
|
? new(ensure.Value, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensureTask))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,7 +8,7 @@ using Microsoft.CodeAnalysis;
|
|||||||
namespace Just.Railway.SourceGen;
|
namespace Just.Railway.SourceGen;
|
||||||
|
|
||||||
[Generator]
|
[Generator]
|
||||||
public class ResultMethodGenerator : IIncrementalGenerator
|
public class ExtensionsMethodGenerator : IIncrementalGenerator
|
||||||
{
|
{
|
||||||
private readonly IEnumerable<IGeneratorExecutor> _executors = new IGeneratorExecutor[]
|
private readonly IEnumerable<IGeneratorExecutor> _executors = new IGeneratorExecutor[]
|
||||||
{
|
{
|
||||||
@@ -17,6 +17,9 @@ public class ResultMethodGenerator : IIncrementalGenerator
|
|||||||
new ResultMapExecutor(),
|
new ResultMapExecutor(),
|
||||||
new ResultBindExecutor(),
|
new ResultBindExecutor(),
|
||||||
new ResultTapExecutor(),
|
new ResultTapExecutor(),
|
||||||
|
new ResultAppendExecutor(),
|
||||||
|
new TryExtensionsExecutor(),
|
||||||
|
new EnsureExtensionExecutor(),
|
||||||
};
|
};
|
||||||
|
|
||||||
public void Initialize(IncrementalGeneratorInitializationContext context)
|
public void Initialize(IncrementalGeneratorInitializationContext context)
|
||||||
431
Railway.SourceGenerator/ResultAppendExecutor.cs
Normal file
431
Railway.SourceGenerator/ResultAppendExecutor.cs
Normal file
@@ -0,0 +1,431 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Immutable;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||||
|
|
||||||
|
namespace Just.Railway.SourceGen;
|
||||||
|
|
||||||
|
internal sealed class ResultAppendExecutor : ResultExtensionsExecutor
|
||||||
|
{
|
||||||
|
protected override string ExtensionType => "Append";
|
||||||
|
|
||||||
|
protected override void GenerateHelperMethods(StringBuilder sb)
|
||||||
|
{
|
||||||
|
sb.AppendLine("""
|
||||||
|
private static IEnumerable<string> GetBottom(ResultState r1, ResultState r2, string firstArg = "result", string secondArg = "next")
|
||||||
|
{
|
||||||
|
if (r1 == ResultState.Bottom)
|
||||||
|
yield return firstArg;
|
||||||
|
if (r2 == ResultState.Bottom)
|
||||||
|
yield return secondArg;
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void GenerateMethodsForArgCount(StringBuilder sb, int argCount)
|
||||||
|
{
|
||||||
|
var templateArgNames = Enumerable.Range(1, argCount)
|
||||||
|
.Select(i => $"T{i}")
|
||||||
|
.ToImmutableArray();
|
||||||
|
|
||||||
|
string resultTypeDef = GenerateResultTypeDef(templateArgNames);
|
||||||
|
string resultValueExpansion = GenerateResultValueExpansion(templateArgNames);
|
||||||
|
string methodTemplateDecl = GenerateTemplateDecl(templateArgNames);
|
||||||
|
|
||||||
|
sb.AppendLine($"#region {resultTypeDef}");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultAppendExecutor)}}", "1.0.0.0")]
|
||||||
|
public static {{resultTypeDef}} Append{{methodTemplateDecl}}(this in {{resultTypeDef}} result, Result next)
|
||||||
|
{
|
||||||
|
Error? error = null;
|
||||||
|
if ((result.State & next.State) == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(string.Join(';', GetBottom(result.State, next.State)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.IsFailure)
|
||||||
|
{
|
||||||
|
error += result.Error;
|
||||||
|
}
|
||||||
|
if (next.IsFailure)
|
||||||
|
{
|
||||||
|
error += next.Error;
|
||||||
|
}
|
||||||
|
|
||||||
|
return error is null
|
||||||
|
? Result.Success({{resultValueExpansion}})
|
||||||
|
: error;
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultAppendExecutor)}}", "1.0.0.0")]
|
||||||
|
public static {{resultTypeDef}} Append{{methodTemplateDecl}}(this in {{resultTypeDef}} result, Func<Result> nextFunc)
|
||||||
|
{
|
||||||
|
if (result.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(result));
|
||||||
|
}
|
||||||
|
else if (result.IsFailure)
|
||||||
|
{
|
||||||
|
return result.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
var next = nextFunc();
|
||||||
|
if (next.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(nextFunc));
|
||||||
|
}
|
||||||
|
else if (next.IsFailure)
|
||||||
|
{
|
||||||
|
return next.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.Success({{resultValueExpansion}});
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
GenerateAsyncMethods("Task", sb, templateArgNames, resultTypeDef, resultValueExpansion);
|
||||||
|
GenerateAsyncMethods("ValueTask", sb, templateArgNames, resultTypeDef, resultValueExpansion);
|
||||||
|
|
||||||
|
if (argCount < Constants.MaxResultTupleSize)
|
||||||
|
{
|
||||||
|
GenerateExpandedMethods(sb, templateArgNames, resultTypeDef, resultValueExpansion);
|
||||||
|
GenerateExpandedAsyncMethods("Task", sb, templateArgNames, resultTypeDef, resultValueExpansion);
|
||||||
|
GenerateExpandedAsyncMethods("ValueTask", sb, templateArgNames, resultTypeDef, resultValueExpansion);
|
||||||
|
}
|
||||||
|
|
||||||
|
sb.AppendLine("#endregion");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateAsyncMethods(string taskType, StringBuilder sb, ImmutableArray<string> templateArgNames, string resultTypeDef, string resultValueExpansion)
|
||||||
|
{
|
||||||
|
string methodTemplateDecl = GenerateTemplateDecl(templateArgNames);
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultAppendExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<{{resultTypeDef}}> Append{{methodTemplateDecl}}(this {{taskType}}<{{resultTypeDef}}> resultTask, Func<Result> nextFunc)
|
||||||
|
{
|
||||||
|
var result = await resultTask.ConfigureAwait(false);
|
||||||
|
if (result.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(result));
|
||||||
|
}
|
||||||
|
else if (result.IsFailure)
|
||||||
|
{
|
||||||
|
return result.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
var next = nextFunc();
|
||||||
|
if (next.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(nextFunc));
|
||||||
|
}
|
||||||
|
else if (next.IsFailure)
|
||||||
|
{
|
||||||
|
return next.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.Success({{resultValueExpansion}});
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultAppendExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<{{resultTypeDef}}> Append{{methodTemplateDecl}}(this {{resultTypeDef}} result, Func<{{taskType}}<Result>> nextFunc)
|
||||||
|
{
|
||||||
|
if (result.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(result));
|
||||||
|
}
|
||||||
|
else if (result.IsFailure)
|
||||||
|
{
|
||||||
|
return result.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
var next = await nextFunc().ConfigureAwait(false);
|
||||||
|
if (next.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(nextFunc));
|
||||||
|
}
|
||||||
|
else if (next.IsFailure)
|
||||||
|
{
|
||||||
|
return next.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.Success({{resultValueExpansion}});
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultAppendExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<{{resultTypeDef}}> Append{{methodTemplateDecl}}(this {{taskType}}<{{resultTypeDef}}> resultTask, Func<{{taskType}}<Result>> nextFunc)
|
||||||
|
{
|
||||||
|
var result = await resultTask.ConfigureAwait(false);
|
||||||
|
if (result.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(resultTask));
|
||||||
|
}
|
||||||
|
else if (result.IsFailure)
|
||||||
|
{
|
||||||
|
return result.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
var next = await nextFunc().ConfigureAwait(false);
|
||||||
|
if (next.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(nextFunc));
|
||||||
|
}
|
||||||
|
else if (next.IsFailure)
|
||||||
|
{
|
||||||
|
return next.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.Success({{resultValueExpansion}});
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateExpandedAsyncMethods(string taskType, StringBuilder sb, ImmutableArray<string> templateArgNames, string resultTypeDef, string resultValueExpansion)
|
||||||
|
{
|
||||||
|
var expandedTemplateArgNames = templateArgNames.Add("TNext");
|
||||||
|
string resultExpandedTypeDef = GenerateResultTypeDef(expandedTemplateArgNames);
|
||||||
|
string methodExpandedTemplateDecl = GenerateTemplateDecl(expandedTemplateArgNames);
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultAppendExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<{{resultExpandedTypeDef}}> Append{{methodExpandedTemplateDecl}}(this {{taskType}}<{{resultTypeDef}}> resultTask, Func<TNext> nextFunc)
|
||||||
|
{
|
||||||
|
var result = await resultTask.ConfigureAwait(false);
|
||||||
|
return result.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => Result.Success({{JoinArguments(resultValueExpansion, "nextFunc()")}}),
|
||||||
|
ResultState.Error => result.Error!,
|
||||||
|
_ => throw new ResultNotInitializedException(nameof(result))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultAppendExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<{{resultExpandedTypeDef}}> Append{{methodExpandedTemplateDecl}}(this {{resultTypeDef}} result, Func<{{taskType}}<TNext>> nextFunc)
|
||||||
|
{
|
||||||
|
return result.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => Result.Success({{JoinArguments(resultValueExpansion, "await nextFunc().ConfigureAwait(false)")}}),
|
||||||
|
ResultState.Error => result.Error!,
|
||||||
|
_ => throw new ResultNotInitializedException(nameof(result))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultAppendExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<{{resultExpandedTypeDef}}> Append{{methodExpandedTemplateDecl}}(this {{taskType}}<{{resultTypeDef}}> resultTask, Func<{{taskType}}<TNext>> nextFunc)
|
||||||
|
{
|
||||||
|
var result = await resultTask.ConfigureAwait(false);
|
||||||
|
return result.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => Result.Success({{JoinArguments(resultValueExpansion, "await nextFunc().ConfigureAwait(false)")}}),
|
||||||
|
ResultState.Error => result.Error!,
|
||||||
|
_ => throw new ResultNotInitializedException(nameof(result))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultAppendExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<{{resultExpandedTypeDef}}> Append{{methodExpandedTemplateDecl}}(this {{taskType}}<{{resultTypeDef}}> resultTask, Func<Result<TNext>> nextFunc)
|
||||||
|
{
|
||||||
|
var result = await resultTask.ConfigureAwait(false);
|
||||||
|
if (result.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(resultTask));
|
||||||
|
}
|
||||||
|
else if (result.IsFailure)
|
||||||
|
{
|
||||||
|
return result.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
var next = nextFunc();
|
||||||
|
if (next.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(nextFunc));
|
||||||
|
}
|
||||||
|
else if (next.IsFailure)
|
||||||
|
{
|
||||||
|
return next.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.Success({{JoinArguments(resultValueExpansion, "next.Value")}});
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultAppendExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<{{resultExpandedTypeDef}}> Append{{methodExpandedTemplateDecl}}(this {{resultTypeDef}} result, Func<{{taskType}}<Result<TNext>>> nextFunc)
|
||||||
|
{
|
||||||
|
if (result.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(result));
|
||||||
|
}
|
||||||
|
else if (result.IsFailure)
|
||||||
|
{
|
||||||
|
return result.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
var next = await nextFunc().ConfigureAwait(false);
|
||||||
|
if (next.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(nextFunc));
|
||||||
|
}
|
||||||
|
else if (next.IsFailure)
|
||||||
|
{
|
||||||
|
return next.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.Success({{JoinArguments(resultValueExpansion, "next.Value")}});
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultAppendExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<{{resultExpandedTypeDef}}> Append{{methodExpandedTemplateDecl}}(this {{taskType}}<{{resultTypeDef}}> resultTask, Func<{{taskType}}<Result<TNext>>> nextFunc)
|
||||||
|
{
|
||||||
|
var result = await resultTask.ConfigureAwait(false);
|
||||||
|
if (result.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(resultTask));
|
||||||
|
}
|
||||||
|
else if (result.IsFailure)
|
||||||
|
{
|
||||||
|
return result.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
var next = await nextFunc().ConfigureAwait(false);
|
||||||
|
if (next.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(nextFunc));
|
||||||
|
}
|
||||||
|
else if (next.IsFailure)
|
||||||
|
{
|
||||||
|
return next.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.Success({{JoinArguments(resultValueExpansion, "next.Value")}});
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void GenerateExpandedMethods(StringBuilder sb, ImmutableArray<string> templateArgNames, string resultTypeDef, string resultValueExpansion)
|
||||||
|
{
|
||||||
|
var expandedTemplateArgNames = templateArgNames.Add("TNext");
|
||||||
|
string resultExpandedTypeDef = GenerateResultTypeDef(expandedTemplateArgNames);
|
||||||
|
string methodExpandedTemplateDecl = GenerateTemplateDecl(expandedTemplateArgNames);
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultAppendExecutor)}}", "1.0.0.0")]
|
||||||
|
public static {{resultExpandedTypeDef}} Append{{methodExpandedTemplateDecl}}(this in {{resultTypeDef}} result, TNext next)
|
||||||
|
{
|
||||||
|
return result.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => Result.Success({{JoinArguments(resultValueExpansion, "next")}}),
|
||||||
|
ResultState.Error => result.Error!,
|
||||||
|
_ => throw new ResultNotInitializedException(nameof(result))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultAppendExecutor)}}", "1.0.0.0")]
|
||||||
|
public static {{resultExpandedTypeDef}} Append{{methodExpandedTemplateDecl}}(this in {{resultTypeDef}} result, Func<TNext> nextFunc)
|
||||||
|
{
|
||||||
|
return result.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => Result.Success({{JoinArguments(resultValueExpansion, "nextFunc()")}}),
|
||||||
|
ResultState.Error => result.Error!,
|
||||||
|
_ => throw new ResultNotInitializedException(nameof(result))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultAppendExecutor)}}", "1.0.0.0")]
|
||||||
|
public static {{resultExpandedTypeDef}} Append{{methodExpandedTemplateDecl}}(this in {{resultTypeDef}} result, Result<TNext> next)
|
||||||
|
{
|
||||||
|
Error? error = null;
|
||||||
|
if ((result.State & next.State) == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(string.Join(';', GetBottom(result.State, next.State)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.IsFailure)
|
||||||
|
{
|
||||||
|
error += result.Error;
|
||||||
|
}
|
||||||
|
if (next.IsFailure)
|
||||||
|
{
|
||||||
|
error += next.Error;
|
||||||
|
}
|
||||||
|
|
||||||
|
return error is null
|
||||||
|
? Result.Success({{JoinArguments(resultValueExpansion, "next.Value")}})
|
||||||
|
: error;
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultAppendExecutor)}}", "1.0.0.0")]
|
||||||
|
public static {{resultExpandedTypeDef}} Append{{methodExpandedTemplateDecl}}(this in {{resultTypeDef}} result, Func<Result<TNext>> nextFunc)
|
||||||
|
{
|
||||||
|
if (result.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(result));
|
||||||
|
}
|
||||||
|
else if (result.IsFailure)
|
||||||
|
{
|
||||||
|
return result.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
var next = nextFunc();
|
||||||
|
if (next.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(nextFunc));
|
||||||
|
}
|
||||||
|
else if (next.IsFailure)
|
||||||
|
{
|
||||||
|
return next.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.Success({{JoinArguments(resultValueExpansion, "next.Value")}});
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static string JoinArguments(string arg1, string arg2) => (arg1, arg2) switch
|
||||||
|
{
|
||||||
|
("", "") => "",
|
||||||
|
(string arg, "") => arg,
|
||||||
|
("", string arg) => arg,
|
||||||
|
_ => $"{arg1}, {arg2}"
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -14,17 +14,18 @@ internal sealed class ResultBindExecutor : ResultExtensionsExecutor
|
|||||||
var templateArgNames = Enumerable.Range(1, argCount)
|
var templateArgNames = Enumerable.Range(1, argCount)
|
||||||
.Select(i => $"T{i}")
|
.Select(i => $"T{i}")
|
||||||
.ToImmutableArray();
|
.ToImmutableArray();
|
||||||
string separatedTemplateArgs = string.Join(", ", templateArgNames);
|
|
||||||
|
|
||||||
sb.AppendLine($"#region <{separatedTemplateArgs}>");
|
string resultTypeDef = GenerateResultTypeDef(templateArgNames);
|
||||||
|
|
||||||
string resultValueType = templateArgNames.Length == 1 ? separatedTemplateArgs : $"({separatedTemplateArgs})";
|
|
||||||
string resultValueExpansion = GenerateResultValueExpansion(templateArgNames);
|
string resultValueExpansion = GenerateResultValueExpansion(templateArgNames);
|
||||||
|
string methodTemplateDecl = GenerateTemplateDecl(templateArgNames.Add("R"));
|
||||||
|
string bindTemplateDecl = GenerateTemplateDecl(templateArgNames.Add("Result<R>"));
|
||||||
|
|
||||||
|
sb.AppendLine($"#region {resultTypeDef}");
|
||||||
|
|
||||||
sb.AppendLine($$"""
|
sb.AppendLine($$"""
|
||||||
[PureAttribute]
|
[PureAttribute]
|
||||||
[GeneratedCodeAttribute("{{nameof(ResultBindExecutor)}}", "1.0.0.0")]
|
[GeneratedCodeAttribute("{{nameof(ResultBindExecutor)}}", "1.0.0.0")]
|
||||||
public static Result<R> Bind<{{separatedTemplateArgs}}, R>(this in Result<{{resultValueType}}> result, Func<{{separatedTemplateArgs}}, Result<R>> binding)
|
public static Result<R> Bind{{methodTemplateDecl}}(this in {{resultTypeDef}} result, Func{{bindTemplateDecl}} binding)
|
||||||
{
|
{
|
||||||
return result.State switch
|
return result.State switch
|
||||||
{
|
{
|
||||||
@@ -35,24 +36,22 @@ internal sealed class ResultBindExecutor : ResultExtensionsExecutor
|
|||||||
}
|
}
|
||||||
""");
|
""");
|
||||||
|
|
||||||
sb.AppendLine($$"""
|
GenerateAsyncMethods("Task", sb, templateArgNames, resultTypeDef, resultValueExpansion);
|
||||||
[PureAttribute]
|
GenerateAsyncMethods("ValueTask", sb, templateArgNames, resultTypeDef, resultValueExpansion);
|
||||||
[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)
|
sb.AppendLine("#endregion");
|
||||||
{
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => binding({{resultValueExpansion}}),
|
|
||||||
ResultState.Error => Task.FromResult<Result<R>>(result.Error!),
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
""");
|
|
||||||
|
private static void GenerateAsyncMethods(string taskType, StringBuilder sb, ImmutableArray<string> templateArgNames, string resultTypeDef, string resultValueExpansion)
|
||||||
|
{
|
||||||
|
string methodTemplateDecl = GenerateTemplateDecl(templateArgNames.Add("R"));
|
||||||
|
string bindTemplateDecl = GenerateTemplateDecl(templateArgNames.Add("Result<R>"));
|
||||||
|
string asyncActionTemplateDecl = GenerateTemplateDecl(templateArgNames.Add($"{taskType}<Result<R>>"));
|
||||||
|
|
||||||
sb.AppendLine($$"""
|
sb.AppendLine($$"""
|
||||||
[PureAttribute]
|
[PureAttribute]
|
||||||
[GeneratedCodeAttribute("{{nameof(ResultBindExecutor)}}", "1.0.0.0")]
|
[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)
|
public static async {{taskType}}<Result<R>> Bind{{methodTemplateDecl}}(this {{taskType}}<{{resultTypeDef}}> resultTask, Func{{bindTemplateDecl}} binding)
|
||||||
{
|
{
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
var result = await resultTask.ConfigureAwait(false);
|
||||||
return result.State switch
|
return result.State switch
|
||||||
@@ -67,7 +66,21 @@ internal sealed class ResultBindExecutor : ResultExtensionsExecutor
|
|||||||
sb.AppendLine($$"""
|
sb.AppendLine($$"""
|
||||||
[PureAttribute]
|
[PureAttribute]
|
||||||
[GeneratedCodeAttribute("{{nameof(ResultBindExecutor)}}", "1.0.0.0")]
|
[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)
|
public static {{taskType}}<Result<R>> Bind{{methodTemplateDecl}}(this in {{resultTypeDef}} result, Func{{asyncActionTemplateDecl}} binding)
|
||||||
|
{
|
||||||
|
return result.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => binding({{resultValueExpansion}}),
|
||||||
|
ResultState.Error => {{taskType}}.FromResult<Result<R>>(result.Error!),
|
||||||
|
_ => throw new ResultNotInitializedException(nameof(result))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultBindExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<Result<R>> Bind{{methodTemplateDecl}}(this {{taskType}}<{{resultTypeDef}}> resultTask, Func{{asyncActionTemplateDecl}} binding)
|
||||||
{
|
{
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
var result = await resultTask.ConfigureAwait(false);
|
||||||
return result.State switch
|
return result.State switch
|
||||||
@@ -78,7 +91,5 @@ internal sealed class ResultBindExecutor : ResultExtensionsExecutor
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
""");
|
""");
|
||||||
|
|
||||||
sb.AppendLine("#endregion");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,10 +49,19 @@ internal sealed class ResultCombineExecutor : IGeneratorExecutor
|
|||||||
|
|
||||||
GenerateGetBottomMethod(sb, argCount);
|
GenerateGetBottomMethod(sb, argCount);
|
||||||
|
|
||||||
var argsResultTupleSizes = new List<ImmutableArray<int>>();
|
var permutations = 1 << argCount;
|
||||||
Span<int> templateCounts = stackalloc int[argCount];
|
var argsResultTupleSizes = new ImmutableArray<int>[permutations];
|
||||||
|
|
||||||
Permute(templateCounts, argsResultTupleSizes);
|
Span<int> templateCounts = stackalloc int[argCount];
|
||||||
|
for (int i = 0; i < permutations; i++)
|
||||||
|
{
|
||||||
|
templateCounts.Fill(0);
|
||||||
|
for (int j = 0; j < argCount; j++)
|
||||||
|
{
|
||||||
|
templateCounts[j] = (i & (1 << j)) > 0 ? 1 : 0;
|
||||||
|
}
|
||||||
|
argsResultTupleSizes[i] = templateCounts.ToImmutableArray();
|
||||||
|
}
|
||||||
|
|
||||||
foreach (var argResultTupleSizes in argsResultTupleSizes)
|
foreach (var argResultTupleSizes in argsResultTupleSizes)
|
||||||
{
|
{
|
||||||
@@ -61,23 +70,6 @@ internal sealed class ResultCombineExecutor : IGeneratorExecutor
|
|||||||
|
|
||||||
sb.AppendLine("#endregion");
|
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)
|
private static void GenerateGetBottomMethod(StringBuilder sb, int argCount)
|
||||||
|
|||||||
@@ -31,7 +31,8 @@ internal abstract class ResultExtensionsExecutor : IGeneratorExecutor
|
|||||||
{
|
{
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
|
|
||||||
for (int i = 1; i <= Constants.MaxResultTupleSize; i++)
|
GenerateHelperMethods(sb);
|
||||||
|
for (int i = 0; i <= Constants.MaxResultTupleSize; i++)
|
||||||
{
|
{
|
||||||
GenerateMethodsForArgCount(sb, argCount: i);
|
GenerateMethodsForArgCount(sb, argCount: i);
|
||||||
}
|
}
|
||||||
@@ -39,11 +40,32 @@ internal abstract class ResultExtensionsExecutor : IGeneratorExecutor
|
|||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected string GenerateResultValueExpansion(ImmutableArray<string> templateArgNames)
|
protected static string GenerateTemplateDecl(ImmutableArray<string> templateArgNames) => templateArgNames.Length > 0
|
||||||
|
? $"<{string.Join(", ", templateArgNames)}>"
|
||||||
|
: string.Empty;
|
||||||
|
|
||||||
|
protected static string GenerateResultTypeDef(ImmutableArray<string> templateArgNames) => templateArgNames.Length switch
|
||||||
|
{
|
||||||
|
0 => "Result",
|
||||||
|
1 => $"Result<{string.Join(", ", templateArgNames)}>",
|
||||||
|
_ => $"Result<({string.Join(", ", templateArgNames)})>",
|
||||||
|
};
|
||||||
|
|
||||||
|
protected static string GenerateResultValueExpansion(ImmutableArray<string> templateArgNames)
|
||||||
{
|
{
|
||||||
string resultExpansion;
|
string resultExpansion;
|
||||||
if (templateArgNames.Length > 1)
|
|
||||||
|
switch (templateArgNames.Length)
|
||||||
{
|
{
|
||||||
|
case 0:
|
||||||
|
resultExpansion = string.Empty;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
resultExpansion = "result.Value";
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
var resultExpansionBuilder = new StringBuilder();
|
var resultExpansionBuilder = new StringBuilder();
|
||||||
for (int i = 1; i <= templateArgNames.Length; i++)
|
for (int i = 1; i <= templateArgNames.Length; i++)
|
||||||
{
|
{
|
||||||
@@ -51,10 +73,7 @@ internal abstract class ResultExtensionsExecutor : IGeneratorExecutor
|
|||||||
}
|
}
|
||||||
resultExpansionBuilder.Remove(resultExpansionBuilder.Length - 2, 2);
|
resultExpansionBuilder.Remove(resultExpansionBuilder.Length - 2, 2);
|
||||||
resultExpansion = resultExpansionBuilder.ToString();
|
resultExpansion = resultExpansionBuilder.ToString();
|
||||||
}
|
break;
|
||||||
else
|
|
||||||
{
|
|
||||||
resultExpansion = "result.Value";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return resultExpansion;
|
return resultExpansion;
|
||||||
@@ -62,4 +81,5 @@ internal abstract class ResultExtensionsExecutor : IGeneratorExecutor
|
|||||||
|
|
||||||
protected abstract string ExtensionType { get; }
|
protected abstract string ExtensionType { get; }
|
||||||
protected abstract void GenerateMethodsForArgCount(StringBuilder sb, int argCount);
|
protected abstract void GenerateMethodsForArgCount(StringBuilder sb, int argCount);
|
||||||
|
protected virtual void GenerateHelperMethods(StringBuilder sb) {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,17 +13,17 @@ internal sealed class ResultMapExecutor : ResultExtensionsExecutor
|
|||||||
var templateArgNames = Enumerable.Range(1, argCount)
|
var templateArgNames = Enumerable.Range(1, argCount)
|
||||||
.Select(i => $"T{i}")
|
.Select(i => $"T{i}")
|
||||||
.ToImmutableArray();
|
.ToImmutableArray();
|
||||||
string separatedTemplateArgs = string.Join(", ", templateArgNames);
|
|
||||||
|
|
||||||
sb.AppendLine($"#region <{separatedTemplateArgs}>");
|
string resultTypeDef = GenerateResultTypeDef(templateArgNames);
|
||||||
|
|
||||||
string resultValueType = templateArgNames.Length == 1 ? separatedTemplateArgs : $"({separatedTemplateArgs})";
|
|
||||||
string resultValueExpansion = GenerateResultValueExpansion(templateArgNames);
|
string resultValueExpansion = GenerateResultValueExpansion(templateArgNames);
|
||||||
|
string methodTemplateDecl = GenerateTemplateDecl(templateArgNames.Add("R"));
|
||||||
|
|
||||||
|
sb.AppendLine($"#region {resultTypeDef}");
|
||||||
|
|
||||||
sb.AppendLine($$"""
|
sb.AppendLine($$"""
|
||||||
[PureAttribute]
|
[PureAttribute]
|
||||||
[GeneratedCodeAttribute("{{nameof(ResultMapExecutor)}}", "1.0.0.0")]
|
[GeneratedCodeAttribute("{{nameof(ResultMapExecutor)}}", "1.0.0.0")]
|
||||||
public static Result<R> Map<{{separatedTemplateArgs}}, R>(this in Result<{{resultValueType}}> result, Func<{{separatedTemplateArgs}}, R> mapping)
|
public static Result<R> Map{{methodTemplateDecl}}(this in {{resultTypeDef}} result, Func{{methodTemplateDecl}} mapping)
|
||||||
{
|
{
|
||||||
return result.State switch
|
return result.State switch
|
||||||
{
|
{
|
||||||
@@ -34,50 +34,60 @@ internal sealed class ResultMapExecutor : ResultExtensionsExecutor
|
|||||||
}
|
}
|
||||||
""");
|
""");
|
||||||
|
|
||||||
sb.AppendLine($$"""
|
GenerateAsyncMethods("Task", sb, templateArgNames, resultTypeDef, resultValueExpansion);
|
||||||
[PureAttribute]
|
GenerateAsyncMethods("ValueTask", sb, templateArgNames, resultTypeDef, resultValueExpansion);
|
||||||
[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");
|
sb.AppendLine("#endregion");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void GenerateAsyncMethods(string taskType, StringBuilder sb, ImmutableArray<string> templateArgNames, string resultTypeDef, string resultValueExpansion)
|
||||||
|
{
|
||||||
|
var methodTemplateArgNames = templateArgNames.Add("R");
|
||||||
|
string methodTemplateDecl = GenerateTemplateDecl(methodTemplateArgNames);
|
||||||
|
string asyncActionTemplateDecl = GenerateTemplateDecl(templateArgNames.Add($"{taskType}<R>"));
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultMapExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<Result<R>> Map{{methodTemplateDecl}}(this {{taskType}}<{{resultTypeDef}}> resultTask, Func{{methodTemplateDecl}} 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 {{taskType}}<Result<R>> Map{{methodTemplateDecl}}(this {{resultTypeDef}} result, Func{{asyncActionTemplateDecl}} 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 {{taskType}}<Result<R>> Map{{methodTemplateDecl}}(this {{taskType}}<{{resultTypeDef}}> resultTask, Func{{asyncActionTemplateDecl}} 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))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,17 +16,17 @@ internal sealed class ResultMatchExecutor : ResultExtensionsExecutor
|
|||||||
var templateArgNames = Enumerable.Range(1, argCount)
|
var templateArgNames = Enumerable.Range(1, argCount)
|
||||||
.Select(i => $"T{i}")
|
.Select(i => $"T{i}")
|
||||||
.ToImmutableArray();
|
.ToImmutableArray();
|
||||||
string separatedTemplateArgs = string.Join(", ", templateArgNames);
|
|
||||||
|
|
||||||
sb.AppendLine($"#region <{separatedTemplateArgs}>");
|
string resultTypeDef = GenerateResultTypeDef(templateArgNames);
|
||||||
|
|
||||||
string resultValueType = templateArgNames.Length == 1 ? separatedTemplateArgs : $"({separatedTemplateArgs})";
|
|
||||||
string resultValueExpansion = GenerateResultValueExpansion(templateArgNames);
|
string resultValueExpansion = GenerateResultValueExpansion(templateArgNames);
|
||||||
|
string methodTemplateDecl = GenerateTemplateDecl(templateArgNames.Add("R"));
|
||||||
|
|
||||||
|
sb.AppendLine($"#region {resultTypeDef}");
|
||||||
|
|
||||||
sb.AppendLine($$"""
|
sb.AppendLine($$"""
|
||||||
[PureAttribute]
|
[PureAttribute]
|
||||||
[GeneratedCodeAttribute("{{nameof(ResultMatchExecutor)}}", "1.0.0.0")]
|
[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)
|
public static R Match{{methodTemplateDecl}}(this in {{resultTypeDef}} result, Func{{methodTemplateDecl}} onSuccess, Func<Error, R> onFailure)
|
||||||
{
|
{
|
||||||
return result.State switch
|
return result.State switch
|
||||||
{
|
{
|
||||||
@@ -37,24 +37,22 @@ internal sealed class ResultMatchExecutor : ResultExtensionsExecutor
|
|||||||
}
|
}
|
||||||
""");
|
""");
|
||||||
|
|
||||||
sb.AppendLine($$"""
|
GenerateAsyncMethods("Task", sb, templateArgNames, resultTypeDef, resultValueExpansion);
|
||||||
[PureAttribute]
|
GenerateAsyncMethods("ValueTask", sb, templateArgNames, resultTypeDef, resultValueExpansion);
|
||||||
[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)
|
sb.AppendLine("#endregion");
|
||||||
{
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => onSuccess({{resultValueExpansion}}),
|
|
||||||
ResultState.Error => onFailure(result.Error!),
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
""");
|
|
||||||
|
private static void GenerateAsyncMethods(string taskType, StringBuilder sb, ImmutableArray<string> templateArgNames, string resultTypeDef, string resultValueExpansion)
|
||||||
|
{
|
||||||
|
var methodTemplateArgNames = templateArgNames.Add("R");
|
||||||
|
string methodTemplateDecl = GenerateTemplateDecl(methodTemplateArgNames);
|
||||||
|
string asyncActionTemplateDecl = GenerateTemplateDecl(templateArgNames.Add($"{taskType}<R>"));
|
||||||
|
|
||||||
sb.AppendLine($$"""
|
sb.AppendLine($$"""
|
||||||
[PureAttribute]
|
[PureAttribute]
|
||||||
[GeneratedCodeAttribute("{{nameof(ResultMatchExecutor)}}", "1.0.0.0")]
|
[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)
|
public static async {{taskType}}<R> Match{{methodTemplateDecl}}(this {{taskType}}<{{resultTypeDef}}> resultTask, Func{{methodTemplateDecl}} onSuccess, Func<Error, R> onFailure)
|
||||||
{
|
{
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
var result = await resultTask.ConfigureAwait(false);
|
||||||
return result.State switch
|
return result.State switch
|
||||||
@@ -69,7 +67,21 @@ internal sealed class ResultMatchExecutor : ResultExtensionsExecutor
|
|||||||
sb.AppendLine($$"""
|
sb.AppendLine($$"""
|
||||||
[PureAttribute]
|
[PureAttribute]
|
||||||
[GeneratedCodeAttribute("{{nameof(ResultMatchExecutor)}}", "1.0.0.0")]
|
[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)
|
public static {{taskType}}<R> Match{{methodTemplateDecl}}(this in {{resultTypeDef}} result, Func{{asyncActionTemplateDecl}} onSuccess, Func<Error, {{taskType}}<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 {{taskType}}<R> Match{{methodTemplateDecl}}(this {{taskType}}<{{resultTypeDef}}> resultTask, Func{{asyncActionTemplateDecl}} onSuccess, Func<Error, {{taskType}}<R>> onFailure)
|
||||||
{
|
{
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
var result = await resultTask.ConfigureAwait(false);
|
||||||
var matchTask = result.State switch
|
var matchTask = result.State switch
|
||||||
@@ -81,7 +93,5 @@ internal sealed class ResultMatchExecutor : ResultExtensionsExecutor
|
|||||||
return await matchTask.ConfigureAwait(false);
|
return await matchTask.ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
""");
|
""");
|
||||||
|
|
||||||
sb.AppendLine("#endregion");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,17 +14,17 @@ internal sealed class ResultTapExecutor : ResultExtensionsExecutor
|
|||||||
var templateArgNames = Enumerable.Range(1, argCount)
|
var templateArgNames = Enumerable.Range(1, argCount)
|
||||||
.Select(i => $"T{i}")
|
.Select(i => $"T{i}")
|
||||||
.ToImmutableArray();
|
.ToImmutableArray();
|
||||||
string separatedTemplateArgs = string.Join(", ", templateArgNames);
|
|
||||||
|
|
||||||
sb.AppendLine($"#region <{separatedTemplateArgs}>");
|
string methodTemplateDecl = GenerateTemplateDecl(templateArgNames);
|
||||||
|
string resultTypeDef = GenerateResultTypeDef(templateArgNames);
|
||||||
string resultValueType = templateArgNames.Length == 1 ? separatedTemplateArgs : $"({separatedTemplateArgs})";
|
|
||||||
string resultValueExpansion = GenerateResultValueExpansion(templateArgNames);
|
string resultValueExpansion = GenerateResultValueExpansion(templateArgNames);
|
||||||
|
|
||||||
|
sb.AppendLine($"#region {resultTypeDef}");
|
||||||
|
|
||||||
sb.AppendLine($$"""
|
sb.AppendLine($$"""
|
||||||
[PureAttribute]
|
[PureAttribute]
|
||||||
[GeneratedCodeAttribute("{{nameof(ResultTapExecutor)}}", "1.0.0.0")]
|
[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)
|
public static ref readonly {{resultTypeDef}} Tap{{methodTemplateDecl}}(this in {{resultTypeDef}} result, Action{{methodTemplateDecl}}? onSuccess = null, Action<Error>? onFailure = null)
|
||||||
{
|
{
|
||||||
switch (result.State)
|
switch (result.State)
|
||||||
{
|
{
|
||||||
@@ -41,10 +41,21 @@ internal sealed class ResultTapExecutor : ResultExtensionsExecutor
|
|||||||
}
|
}
|
||||||
""");
|
""");
|
||||||
|
|
||||||
|
GenerateAsyncMethods("Task", sb, templateArgNames, resultTypeDef, resultValueExpansion);
|
||||||
|
GenerateAsyncMethods("ValueTask", sb, templateArgNames, resultTypeDef, resultValueExpansion);
|
||||||
|
|
||||||
|
sb.AppendLine("#endregion");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void GenerateAsyncMethods(string taskType, StringBuilder sb, ImmutableArray<string> templateArgNames, string resultTypeDef, string resultValueExpansion)
|
||||||
|
{
|
||||||
|
string methodTemplateDecl = GenerateTemplateDecl(templateArgNames);
|
||||||
|
string asyncActionTemplateDecl = GenerateTemplateDecl(templateArgNames.Add(taskType));
|
||||||
|
|
||||||
sb.AppendLine($$"""
|
sb.AppendLine($$"""
|
||||||
[PureAttribute]
|
[PureAttribute]
|
||||||
[GeneratedCodeAttribute("{{nameof(ResultTapExecutor)}}", "1.0.0.0")]
|
[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)
|
public static async {{taskType}}<{{resultTypeDef}}> Tap{{methodTemplateDecl}}(this {{taskType}}<{{resultTypeDef}}> resultTask, Action{{methodTemplateDecl}}? onSuccess = null, Action<Error>? onFailure = null)
|
||||||
{
|
{
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
var result = await resultTask.ConfigureAwait(false);
|
||||||
switch (result.State)
|
switch (result.State)
|
||||||
@@ -65,7 +76,7 @@ internal sealed class ResultTapExecutor : ResultExtensionsExecutor
|
|||||||
sb.AppendLine($$"""
|
sb.AppendLine($$"""
|
||||||
[PureAttribute]
|
[PureAttribute]
|
||||||
[GeneratedCodeAttribute("{{nameof(ResultTapExecutor)}}", "1.0.0.0")]
|
[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)
|
public static async {{taskType}}<{{resultTypeDef}}> Tap{{methodTemplateDecl}}(this {{resultTypeDef}} result, Func{{asyncActionTemplateDecl}}? onSuccess = null, Func<Error, {{taskType}}>? onFailure = null)
|
||||||
{
|
{
|
||||||
switch (result.State)
|
switch (result.State)
|
||||||
{
|
{
|
||||||
@@ -87,7 +98,7 @@ internal sealed class ResultTapExecutor : ResultExtensionsExecutor
|
|||||||
sb.AppendLine($$"""
|
sb.AppendLine($$"""
|
||||||
[PureAttribute]
|
[PureAttribute]
|
||||||
[GeneratedCodeAttribute("{{nameof(ResultTapExecutor)}}", "1.0.0.0")]
|
[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)
|
public static async {{taskType}}<{{resultTypeDef}}> Tap{{methodTemplateDecl}}(this {{taskType}}<{{resultTypeDef}}> resultTask, Func{{asyncActionTemplateDecl}}? onSuccess = null, Func<Error, {{taskType}}>? onFailure = null)
|
||||||
{
|
{
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
var result = await resultTask.ConfigureAwait(false);
|
||||||
switch (result.State)
|
switch (result.State)
|
||||||
@@ -106,7 +117,5 @@ internal sealed class ResultTapExecutor : ResultExtensionsExecutor
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
""");
|
""");
|
||||||
|
|
||||||
sb.AppendLine("#endregion");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
211
Railway.SourceGenerator/TryExtensionsExecutor.cs
Normal file
211
Railway.SourceGenerator/TryExtensionsExecutor.cs
Normal file
@@ -0,0 +1,211 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Immutable;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Just.Railway.SourceGen;
|
||||||
|
|
||||||
|
public sealed class TryExtensionsExecutor : 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 Try
|
||||||
|
{
|
||||||
|
{{methods}}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
|
||||||
|
context.AddSource("Try.Run.g.cs", code);
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GenerateMethods()
|
||||||
|
{
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
|
||||||
|
for (int i = 0; i <= Constants.MaxResultTupleSize; i++)
|
||||||
|
{
|
||||||
|
GenerateMethodsForArgCount(sb, argCount: i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateMethodsForArgCount(StringBuilder sb, int argCount)
|
||||||
|
{
|
||||||
|
var templateArgNames = Enumerable.Range(1, argCount)
|
||||||
|
.Select(i => $"T{i}")
|
||||||
|
.ToImmutableArray();
|
||||||
|
var argNames = Enumerable.Range(1, argCount)
|
||||||
|
.Select(i => $"arg{i}")
|
||||||
|
.ToImmutableArray();
|
||||||
|
|
||||||
|
string actionTemplateDecl = GenerateTemplateDecl(templateArgNames);
|
||||||
|
string resultActionTemplateDecl = GenerateTemplateDecl(templateArgNames.Add("Result"));
|
||||||
|
string funcTemplateDecl = GenerateTemplateDecl(templateArgNames.Add("TResult"));
|
||||||
|
string resultFuncTemplateDecl = GenerateTemplateDecl(templateArgNames.Add("Result<TResult>"));
|
||||||
|
string argumentsDeclExpansion = string.Join(", ", templateArgNames.Zip(argNames, (t, n) => $"{t} {n}"));
|
||||||
|
string argumentsExpansion = string.Join(", ", argNames);
|
||||||
|
|
||||||
|
sb.AppendLine($"#region <{string.Join(", ", templateArgNames)}>");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(TryExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Result Run{{actionTemplateDecl}}(Action{{actionTemplateDecl}} action{{TrailingArguments(argumentsDeclExpansion)}})
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
action({{argumentsExpansion}});
|
||||||
|
return Result.Success();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Error.New(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(TryExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Result Run{{actionTemplateDecl}}(Func{{resultActionTemplateDecl}} action{{TrailingArguments(argumentsDeclExpansion)}})
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return action({{argumentsExpansion}});
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Error.New(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(TryExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Result<TResult> Run{{funcTemplateDecl}}(Func{{funcTemplateDecl}} func{{TrailingArguments(argumentsDeclExpansion)}})
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return Result.Success(func({{argumentsExpansion}}));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Error.New(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(TryExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Result<TResult> Run{{funcTemplateDecl}}(Func{{resultFuncTemplateDecl}} func{{TrailingArguments(argumentsDeclExpansion)}})
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return func({{argumentsExpansion}});
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Error.New(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
GenerateAsyncMethods(sb, templateArgNames, actionTemplateDecl, funcTemplateDecl, argumentsDeclExpansion, argumentsExpansion, "Task");
|
||||||
|
GenerateAsyncMethods(sb, templateArgNames, actionTemplateDecl, funcTemplateDecl, argumentsDeclExpansion, argumentsExpansion, "ValueTask");
|
||||||
|
|
||||||
|
sb.AppendLine("#endregion");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void GenerateAsyncMethods(StringBuilder sb, ImmutableArray<string> templateArgNames, string actionTemplateDecl, string funcTemplateDecl, string argumentsDeclExpansion, string argumentsExpansion, string taskType)
|
||||||
|
{
|
||||||
|
string actionTaskTemplateDecl = GenerateTemplateDecl(templateArgNames.Add(taskType));
|
||||||
|
string resultActionTaskTemplateDecl = GenerateTemplateDecl(templateArgNames.Add($"{taskType}<Result>"));
|
||||||
|
string funcTaskTemplateDecl = GenerateTemplateDecl(templateArgNames.Add($"{taskType}<TResult>"));
|
||||||
|
string resultFuncTaskTemplateDecl = GenerateTemplateDecl(templateArgNames.Add($"{taskType}<Result<TResult>>"));
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(TryExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<Result> Run{{actionTemplateDecl}}(Func{{actionTaskTemplateDecl}} action{{TrailingArguments(argumentsDeclExpansion)}})
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await action({{argumentsExpansion}}).ConfigureAwait(false);
|
||||||
|
return Result.Success();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Error.New(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(TryExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<Result> Run{{actionTemplateDecl}}(Func{{resultActionTaskTemplateDecl}} action{{TrailingArguments(argumentsDeclExpansion)}})
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return await action({{argumentsExpansion}}).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Error.New(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(TryExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<Result<TResult>> Run{{funcTemplateDecl}}(Func{{funcTaskTemplateDecl}} func{{TrailingArguments(argumentsDeclExpansion)}})
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return Result.Success(await func({{argumentsExpansion}}).ConfigureAwait(false));
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Error.New(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(TryExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<Result<TResult>> Run{{funcTemplateDecl}}(Func{{resultFuncTaskTemplateDecl}} func{{TrailingArguments(argumentsDeclExpansion)}})
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return await func({{argumentsExpansion}}).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Error.New(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string TrailingArguments(string argumentsExpansion) => string.IsNullOrEmpty(argumentsExpansion)
|
||||||
|
? string.Empty
|
||||||
|
: $", {argumentsExpansion}";
|
||||||
|
private static string GenerateTemplateDecl(ImmutableArray<string> templateArgNames) => templateArgNames.Length > 0
|
||||||
|
? $"<{string.Join(", ", templateArgNames)}>"
|
||||||
|
: string.Empty;
|
||||||
|
}
|
||||||
@@ -1,13 +1,12 @@
|
|||||||
namespace Just.Railway;
|
namespace Just.Railway;
|
||||||
|
|
||||||
public static class Ensure
|
public static partial class Ensure
|
||||||
{
|
{
|
||||||
public delegate Error ErrorFactory(string valueExpression);
|
public delegate Error ErrorFactory(string valueExpression);
|
||||||
|
|
||||||
public const string DefaultErrorType = "EnsureFailed";
|
public const string DefaultErrorType = "EnsureFailed";
|
||||||
|
|
||||||
[Pure] public static Ensure<T> That<T>(T value, [CallerArgumentExpression(nameof(value))]string valueExpression = "") => new(value, valueExpression);
|
[Pure] public static Ensure<T> That<T>(T value, [CallerArgumentExpression(nameof(value))]string valueExpression = "") => new(value, valueExpression);
|
||||||
[Pure] public static async Task<Ensure<T>> That<T>(Task<T> value, [CallerArgumentExpression(nameof(value))]string valueExpression = "") => new(await value.ConfigureAwait(false), valueExpression);
|
|
||||||
|
|
||||||
[Pure] public static Result<T> Result<T>(this in Ensure<T> ensure) => ensure.State switch
|
[Pure] public static Result<T> Result<T>(this in Ensure<T> ensure) => ensure.State switch
|
||||||
{
|
{
|
||||||
@@ -15,8 +14,7 @@ public static class Ensure
|
|||||||
ResultState.Error => new(ensure.Error!),
|
ResultState.Error => new(ensure.Error!),
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
};
|
};
|
||||||
[Pure]
|
[Pure] public static async Task<Result<T>> Result<T>(this Task<Ensure<T>> ensureTask)
|
||||||
public static async Task<Result<T>> Result<T>(this Task<Ensure<T>> ensureTask)
|
|
||||||
{
|
{
|
||||||
var ensure = await ensureTask.ConfigureAwait(false);
|
var ensure = await ensureTask.ConfigureAwait(false);
|
||||||
return ensure.State switch
|
return ensure.State switch
|
||||||
@@ -26,261 +24,18 @@ public static class Ensure
|
|||||||
_ => throw new EnsureNotInitializedException(nameof(ensureTask))
|
_ => throw new EnsureNotInitializedException(nameof(ensureTask))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
[Pure]
|
[Pure] public static async ValueTask<Result<T>> Result<T>(this ValueTask<Ensure<T>> ensureTask)
|
||||||
public static Ensure<T> Satisfies<T>(this in Ensure<T> ensure, Func<T, bool> requirement, Error error = default!)
|
|
||||||
{
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => requirement(ensure.Value)
|
|
||||||
? new(ensure.Value, ensure.ValueExpression)
|
|
||||||
: new(error ?? Error.New(DefaultErrorType, $"Value {{{ensure.ValueExpression}}} does not satisfy the requirement."), ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static Ensure<T> Satisfies<T>(this in Ensure<T> ensure, Func<T, bool> requirement, ErrorFactory errorFactory)
|
|
||||||
{
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => requirement(ensure.Value)
|
|
||||||
? new(ensure.Value, ensure.ValueExpression)
|
|
||||||
: new(errorFactory(ensure.ValueExpression), ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Ensure<T>> Satisfies<T>(this Task<Ensure<T>> ensureTask, Func<T, bool> requirement, Error error = default!)
|
|
||||||
{
|
{
|
||||||
var ensure = await ensureTask.ConfigureAwait(false);
|
var ensure = await ensureTask.ConfigureAwait(false);
|
||||||
return ensure.State switch
|
return ensure.State switch
|
||||||
{
|
{
|
||||||
ResultState.Success => requirement(ensure.Value)
|
ResultState.Success => new(ensure.Value),
|
||||||
? new(ensure.Value, ensure.ValueExpression)
|
ResultState.Error => new(ensure.Error!),
|
||||||
: new(error ?? Error.New(DefaultErrorType, $"Value {{{ensure.ValueExpression}}} does not satisfy the requirement."), ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensureTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Ensure<T>> Satisfies<T>(this Task<Ensure<T>> ensureTask, Func<T, bool> requirement, ErrorFactory errorFactory)
|
|
||||||
{
|
|
||||||
var ensure = await ensureTask.ConfigureAwait(false);
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => requirement(ensure.Value)
|
|
||||||
? new(ensure.Value, ensure.ValueExpression)
|
|
||||||
: new(errorFactory(ensure.ValueExpression), ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensureTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Ensure<T>> Satisfies<T>(this Ensure<T> ensure, Func<T, Task<bool>> requirement, Error error = default!)
|
|
||||||
{
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => await requirement(ensure.Value).ConfigureAwait(false)
|
|
||||||
? new(ensure.Value, ensure.ValueExpression)
|
|
||||||
: new(error ?? Error.New(DefaultErrorType, $"Value {{{ensure.ValueExpression}}} does not satisfy the requirement."), ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Ensure<T>> Satisfies<T>(this Ensure<T> ensure, Func<T, Task<bool>> requirement, ErrorFactory errorFactory)
|
|
||||||
{
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => await requirement(ensure.Value).ConfigureAwait(false)
|
|
||||||
? new(ensure.Value, ensure.ValueExpression)
|
|
||||||
: new(errorFactory(ensure.ValueExpression), ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Ensure<T>> Satisfies<T>(this Task<Ensure<T>> ensureTask, Func<T, Task<bool>> requirement, Error error = default!)
|
|
||||||
{
|
|
||||||
var ensure = await ensureTask.ConfigureAwait(false);
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => await requirement(ensure.Value).ConfigureAwait(false)
|
|
||||||
? new(ensure.Value, ensure.ValueExpression)
|
|
||||||
: new(error ?? Error.New(DefaultErrorType, $"Value {{{ensure.ValueExpression}}} does not satisfy the requirement."), ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensureTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Ensure<T>> Satisfies<T>(this Task<Ensure<T>> ensureTask, Func<T, Task<bool>> requirement, ErrorFactory errorFactory)
|
|
||||||
{
|
|
||||||
var ensure = await ensureTask.ConfigureAwait(false);
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => await requirement(ensure.Value).ConfigureAwait(false)
|
|
||||||
? new(ensure.Value, ensure.ValueExpression)
|
|
||||||
: new(errorFactory(ensure.ValueExpression), ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensureTask))
|
_ => throw new EnsureNotInitializedException(nameof(ensureTask))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
[Pure]
|
[Pure] public static Ensure<string> NotWhitespace(this in Ensure<string> ensure, Error error = default!)
|
||||||
public static Ensure<T> NotNull<T>(this in Ensure<T?> ensure, Error error = default!)
|
|
||||||
where T : struct
|
|
||||||
{
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => ensure.Value.HasValue
|
|
||||||
? new(ensure.Value.Value, ensure.ValueExpression)
|
|
||||||
: new(error ?? Error.New(DefaultErrorType, $"Value {{{ensure.ValueExpression}}} is null."), ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Ensure<T>> NotNull<T>(this Task<Ensure<T?>> ensureTask, Error error = default!)
|
|
||||||
where T : struct
|
|
||||||
{
|
|
||||||
var ensure = await ensureTask.ConfigureAwait(false);
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => ensure.Value.HasValue
|
|
||||||
? new(ensure.Value.Value, ensure.ValueExpression)
|
|
||||||
: new(error ?? Error.New(DefaultErrorType, $"Value {{{ensure.ValueExpression}}} is null."), ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensureTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static Ensure<T> NotNull<T>(this in Ensure<T?> ensure, Error error = default!)
|
|
||||||
where T : notnull
|
|
||||||
{
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => ensure.Value is not null
|
|
||||||
? new(ensure.Value, ensure.ValueExpression)
|
|
||||||
: new(error ?? Error.New(DefaultErrorType, $"Value {{{ensure.ValueExpression}}} is null."), ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Ensure<T>> NotNull<T>(this Task<Ensure<T?>> ensureTask, Error error = default!)
|
|
||||||
where T : notnull
|
|
||||||
{
|
|
||||||
var ensure = await ensureTask.ConfigureAwait(false);
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => ensure.Value is not null
|
|
||||||
? new(ensure.Value, ensure.ValueExpression)
|
|
||||||
: new(error ?? Error.New(DefaultErrorType, $"Value {{{ensure.ValueExpression}}} is null."), ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensureTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
[Pure]
|
|
||||||
public static Ensure<T[]> NotEmpty<T>(this in Ensure<T[]> ensure, Error error = default!)
|
|
||||||
{
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => ensure.Value is not null && ensure.Value.Length > 0
|
|
||||||
? new(ensure.Value, ensure.ValueExpression)
|
|
||||||
: new(error ?? Error.New(DefaultErrorType, $"Value {{{ensure.ValueExpression}}} is empty."), ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static Ensure<List<T>> NotEmpty<T>(this in Ensure<List<T>> ensure, Error error = default!)
|
|
||||||
{
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => ensure.Value is not null && ensure.Value.Count > 0
|
|
||||||
? new(ensure.Value, ensure.ValueExpression)
|
|
||||||
: new(error ?? Error.New(DefaultErrorType, $"Value {{{ensure.ValueExpression}}} is empty."), ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static Ensure<IReadOnlyCollection<T>> NotEmpty<T>(this in Ensure<IReadOnlyCollection<T>> ensure, Error error = default!)
|
|
||||||
{
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => ensure.Value is not null && ensure.Value.Count > 0
|
|
||||||
? new(ensure.Value, ensure.ValueExpression)
|
|
||||||
: new(error ?? Error.New(DefaultErrorType, $"Value {{{ensure.ValueExpression}}} is empty."), ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static Ensure<ICollection<T>> NotEmpty<T>(this in Ensure<ICollection<T>> ensure, Error error = default!)
|
|
||||||
{
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => ensure.Value is not null && ensure.Value.Count > 0
|
|
||||||
? new(ensure.Value, ensure.ValueExpression)
|
|
||||||
: new(error ?? Error.New(DefaultErrorType, $"Value {{{ensure.ValueExpression}}} is empty."), ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static Ensure<IReadOnlyList<T>> NotEmpty<T>(this in Ensure<IReadOnlyList<T>> ensure, Error error = default!)
|
|
||||||
{
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => ensure.Value is not null && ensure.Value.Count > 0
|
|
||||||
? new(ensure.Value, ensure.ValueExpression)
|
|
||||||
: new(error ?? Error.New(DefaultErrorType, $"Value {{{ensure.ValueExpression}}} is empty."), ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static Ensure<IList<T>> NotEmpty<T>(this in Ensure<IList<T>> ensure, Error error = default!)
|
|
||||||
{
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => ensure.Value is not null && ensure.Value.Count > 0
|
|
||||||
? new(ensure.Value, ensure.ValueExpression)
|
|
||||||
: new(error ?? Error.New(DefaultErrorType, $"Value {{{ensure.ValueExpression}}} is empty."), ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static Ensure<IEnumerable<T>> NotEmpty<T>(this in Ensure<IEnumerable<T>> ensure, Error error = default!)
|
|
||||||
{
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => ensure.Value is not null && ensure.Value.Any()
|
|
||||||
? new(ensure.Value, ensure.ValueExpression)
|
|
||||||
: new(error ?? Error.New(DefaultErrorType, $"Value {{{ensure.ValueExpression}}} is empty."), ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static Ensure<string> NotEmpty(this in Ensure<string> ensure, Error error = default!)
|
|
||||||
{
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => string.IsNullOrEmpty(ensure.Value)
|
|
||||||
? new(error ?? Error.New(DefaultErrorType, $"Value {{{ensure.ValueExpression}}} is empty."), ensure.ValueExpression)
|
|
||||||
: new(ensure.Value, ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
[Pure]
|
|
||||||
public static Ensure<string> NotWhitespace(this in Ensure<string> ensure, Error error = default!)
|
|
||||||
{
|
{
|
||||||
return ensure.State switch
|
return ensure.State switch
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,11 +4,15 @@
|
|||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<EmitCompilerGeneratedFiles Condition="'$(Configuration)'=='Debug'">true</EmitCompilerGeneratedFiles>
|
|
||||||
|
|
||||||
<AssemblyName>Just.Railway</AssemblyName>
|
<AssemblyName>Just.Railway</AssemblyName>
|
||||||
<RootNamespace>Just.Railway</RootNamespace>
|
<RootNamespace>Just.Railway</RootNamespace>
|
||||||
|
|
||||||
|
<Authors>JustFixMe</Authors>
|
||||||
|
<Copyright>Copyright (c) 2023 JustFixMe</Copyright>
|
||||||
|
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||||
|
<RepositoryUrl>https://gitea.jstdev.ru/just/Just.Railway/</RepositoryUrl>
|
||||||
|
|
||||||
|
<EmitCompilerGeneratedFiles Condition="'$(Configuration)'=='Debug'">true</EmitCompilerGeneratedFiles>
|
||||||
<ReleaseVersion Condition=" '$(ReleaseVersion)' == '' ">1.0.0</ReleaseVersion>
|
<ReleaseVersion Condition=" '$(ReleaseVersion)' == '' ">1.0.0</ReleaseVersion>
|
||||||
<VersionSuffix Condition=" '$(VersionSuffix)' != '' ">$(VersionSuffix)</VersionSuffix>
|
<VersionSuffix Condition=" '$(VersionSuffix)' != '' ">$(VersionSuffix)</VersionSuffix>
|
||||||
<VersionPrefix Condition=" '$(VersionSuffix)' != '' ">$(ReleaseVersion)</VersionPrefix>
|
<VersionPrefix Condition=" '$(VersionSuffix)' != '' ">$(ReleaseVersion)</VersionPrefix>
|
||||||
|
|||||||
@@ -17,13 +17,41 @@ public readonly partial struct Result : IEquatable<Result>
|
|||||||
State = error is null ? ResultState.Success : ResultState.Error;
|
State = error is null ? ResultState.Success : ResultState.Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Pure] public static Result Success() => new(null);
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
[Pure] public static Result<T> Success<T>(T value) => new(value);
|
public static Result Success() => new(null);
|
||||||
[Pure] public static Result Failure(Error error) => new(error ?? throw new ArgumentNullException(nameof(error)));
|
|
||||||
[Pure] public static Result<T> Failure<T>(Error error) => new(error ?? throw new ArgumentNullException(nameof(error)));
|
|
||||||
|
|
||||||
[Pure] public static implicit operator Result(Error error) => new(error ?? throw new ArgumentNullException(nameof(error)));
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
[Pure] public static implicit operator Result<SuccessUnit>(Result result) => result.State switch
|
public static Result<T> Success<T>(T value) => new(value);
|
||||||
|
|
||||||
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static Result<(T1, T2)> Success<T1, T2>(T1 value1, T2 value2) => new((value1, value2));
|
||||||
|
|
||||||
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static Result<(T1, T2, T3)> Success<T1, T2, T3>(T1 value1, T2 value2, T3 value3) => new((value1, value2, value3));
|
||||||
|
|
||||||
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static Result<(T1, T2, T3, T4)> Success<T1, T2, T3, T4>(T1 value1, T2 value2, T3 value3, T4 value4) => new((value1, value2, value3, value4));
|
||||||
|
|
||||||
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static Result<(T1, T2, T3, T4, T5)> Success<T1, T2, T3, T4, T5>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5) => new((value1, value2, value3, value4, value5));
|
||||||
|
|
||||||
|
|
||||||
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static Result Failure(Error error) => new(error ?? throw new ArgumentNullException(nameof(error)));
|
||||||
|
|
||||||
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static Result Failure(Exception exception) => new(Error.New(exception) ?? throw new ArgumentNullException(nameof(exception)));
|
||||||
|
|
||||||
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static Result<T> Failure<T>(Error error) => new(error ?? throw new ArgumentNullException(nameof(error)));
|
||||||
|
|
||||||
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static Result<T> Failure<T>(Exception exception) => new(Error.New(exception) ?? throw new ArgumentNullException(nameof(exception)));
|
||||||
|
|
||||||
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static implicit operator Result(Error error) => new(error ?? throw new ArgumentNullException(nameof(error)));
|
||||||
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static implicit operator Result<SuccessUnit>(Result result) => result.State switch
|
||||||
{
|
{
|
||||||
ResultState.Success => new(new SuccessUnit()),
|
ResultState.Success => new(new SuccessUnit()),
|
||||||
ResultState.Error => new(result.Error!),
|
ResultState.Error => new(result.Error!),
|
||||||
@@ -33,6 +61,23 @@ public readonly partial struct Result : IEquatable<Result>
|
|||||||
[Pure] public bool IsSuccess => Error is null;
|
[Pure] public bool IsSuccess => Error is null;
|
||||||
[Pure] public bool IsFailure => Error is not null;
|
[Pure] public bool IsFailure => Error is not null;
|
||||||
|
|
||||||
|
[Pure] public bool Success([MaybeNullWhen(false)]out SuccessUnit? u, [MaybeNullWhen(true), NotNullWhen(false)]out Error? error)
|
||||||
|
{
|
||||||
|
switch (State)
|
||||||
|
{
|
||||||
|
case ResultState.Success:
|
||||||
|
u = new SuccessUnit();
|
||||||
|
error = default;
|
||||||
|
return true;
|
||||||
|
|
||||||
|
case ResultState.Error:
|
||||||
|
u = default;
|
||||||
|
error = Error!;
|
||||||
|
return false;
|
||||||
|
|
||||||
|
default: throw new ResultNotInitializedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
[Pure] public bool TryGetError([MaybeNullWhen(false)]out Error error)
|
[Pure] public bool TryGetError([MaybeNullWhen(false)]out Error error)
|
||||||
{
|
{
|
||||||
if (IsSuccess)
|
if (IsSuccess)
|
||||||
@@ -104,7 +149,7 @@ public readonly struct Result<T> : IEquatable<Result<T>>
|
|||||||
[Pure] public bool IsSuccess => State == ResultState.Success;
|
[Pure] public bool IsSuccess => State == ResultState.Success;
|
||||||
[Pure] public bool IsFailure => State == ResultState.Error;
|
[Pure] public bool IsFailure => State == ResultState.Error;
|
||||||
|
|
||||||
[Pure] public bool Unwrap([MaybeNullWhen(false)]out T value, [MaybeNullWhen(true)]out Error error)
|
[Pure] public bool Success([MaybeNullWhen(false)]out T value, [MaybeNullWhen(true), NotNullWhen(false)]out Error? error)
|
||||||
{
|
{
|
||||||
switch (State)
|
switch (State)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,770 +2,41 @@ namespace Just.Railway;
|
|||||||
|
|
||||||
public static partial class ResultExtensions
|
public static partial class ResultExtensions
|
||||||
{
|
{
|
||||||
#region Match<>
|
|
||||||
|
|
||||||
[Pure]
|
|
||||||
public static R Match<R>(this in Result result, Func<R> onSuccess, Func<Error, R> onFailure)
|
|
||||||
{
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => onSuccess(),
|
|
||||||
ResultState.Error => onFailure(result.Error!),
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
[Pure]
|
|
||||||
public static Task<R> Match<R>(this in Result result, Func<Task<R>> onSuccess, Func<Error, Task<R>> onFailure)
|
|
||||||
{
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => onSuccess(),
|
|
||||||
ResultState.Error => onFailure(result.Error!),
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
[Pure] public static async Task<R> Match<R>(this Task<Result> resultTask, Func<R> onSuccess, Func<Error, R> onFailure)
|
|
||||||
{
|
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => onSuccess(),
|
|
||||||
ResultState.Error => onFailure(result.Error!),
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(resultTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<R> Match<R>(this Task<Result> resultTask, Func<Task<R>> onSuccess, Func<Error, Task<R>> onFailure)
|
|
||||||
{
|
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
|
||||||
var matchTask = result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => onSuccess(),
|
|
||||||
ResultState.Error => onFailure(result.Error!),
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(resultTask))
|
|
||||||
};
|
|
||||||
return await matchTask.ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Map<>
|
|
||||||
|
|
||||||
[Pure]
|
|
||||||
public static Result<R> Map<R>(this in Result result, Func<R> mapping)
|
|
||||||
{
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => mapping(),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Result<R>> Map<R>(this Result result, Func<Task<R>> mapping)
|
|
||||||
{
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => await mapping().ConfigureAwait(false),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Result<R>> Map<R>(this Task<Result> resultTask, Func<R> mapping)
|
|
||||||
{
|
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => mapping(),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(resultTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Result<R>> Map<R>(this Task<Result> resultTask, Func<Task<R>> mapping)
|
|
||||||
{
|
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => await mapping().ConfigureAwait(false),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(resultTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Bind<>
|
|
||||||
|
|
||||||
[Pure]
|
|
||||||
public static Result Bind(this in Result result, Func<Result> binding)
|
|
||||||
{
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => binding(),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static Task<Result> Bind(this in Result result, Func<Task<Result>> binding)
|
|
||||||
{
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => binding(),
|
|
||||||
ResultState.Error => Task.FromResult<Result>(result.Error!),
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Result> Bind(this Task<Result> resultTask, Func<Result> binding)
|
|
||||||
{
|
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => binding(),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(resultTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Result> Bind(this Task<Result> resultTask, Func<Task<Result>> binding)
|
|
||||||
{
|
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => await binding().ConfigureAwait(false),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(resultTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
[Pure]
|
|
||||||
public static Result<R> Bind<R>(this in Result result, Func<Result<R>> binding)
|
|
||||||
{
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => binding(),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static Task<Result<R>> Bind<R>(this in Result result, Func<Task<Result<R>>> binding)
|
|
||||||
{
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => binding(),
|
|
||||||
ResultState.Error => Task.FromResult<Result<R>>(result.Error!),
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Result<R>> Bind<R>(this Task<Result> resultTask, Func<Result<R>> binding)
|
|
||||||
{
|
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => binding(),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(resultTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Result<R>> Bind<R>(this Task<Result> resultTask, Func<Task<Result<R>>> binding)
|
|
||||||
{
|
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => await binding().ConfigureAwait(false),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(resultTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Append
|
|
||||||
|
|
||||||
#region <>
|
|
||||||
|
|
||||||
[Pure] public static Result Append(this in Result result, Result next)
|
|
||||||
{
|
|
||||||
Error? error = null;
|
|
||||||
|
|
||||||
if ((result.State & next.State) == ResultState.Bottom)
|
|
||||||
{
|
|
||||||
throw new ResultNotInitializedException(string.Join(';', GetBottom(result.State, next.State)));
|
|
||||||
|
|
||||||
static IEnumerable<string> GetBottom(ResultState r1, ResultState r2)
|
|
||||||
{
|
|
||||||
if (r1 == ResultState.Bottom)
|
|
||||||
yield return nameof(result);
|
|
||||||
if (r2 == ResultState.Bottom)
|
|
||||||
yield return nameof(next);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result.IsFailure)
|
|
||||||
{
|
|
||||||
error += result.Error;
|
|
||||||
}
|
|
||||||
if (next.IsFailure)
|
|
||||||
{
|
|
||||||
error += next.Error;
|
|
||||||
}
|
|
||||||
return error is null
|
|
||||||
? new(null)
|
|
||||||
: new(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region <T>
|
|
||||||
|
|
||||||
[Pure] public static Result<T> Append<T>(this in Result result, T value)
|
|
||||||
{
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => value,
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure] public static Result<T> Append<T>(this in Result result, Result<T> next)
|
|
||||||
{
|
|
||||||
Error? error = null;
|
|
||||||
|
|
||||||
if ((result.State & next.State) == ResultState.Bottom)
|
|
||||||
{
|
|
||||||
throw new ResultNotInitializedException(string.Join(';', GetBottom(result.State, next.State)));
|
|
||||||
|
|
||||||
static IEnumerable<string> GetBottom(ResultState r1, ResultState r2)
|
|
||||||
{
|
|
||||||
if (r1 == ResultState.Bottom)
|
|
||||||
yield return nameof(result);
|
|
||||||
if (r2 == ResultState.Bottom)
|
|
||||||
yield return nameof(next);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result.IsFailure)
|
|
||||||
{
|
|
||||||
error += result.Error;
|
|
||||||
}
|
|
||||||
if (next.IsFailure)
|
|
||||||
{
|
|
||||||
error += next.Error;
|
|
||||||
}
|
|
||||||
return error is null
|
|
||||||
? new(next.Value)
|
|
||||||
: new(error);
|
|
||||||
}
|
|
||||||
[Pure] public static Result<T> Append<T>(this in Result<T> result, Result next)
|
|
||||||
{
|
|
||||||
Error? error = null;
|
|
||||||
|
|
||||||
if ((result.State & next.State) == ResultState.Bottom)
|
|
||||||
{
|
|
||||||
throw new ResultNotInitializedException(string.Join(';', GetBottom(result.State, next.State)));
|
|
||||||
|
|
||||||
static IEnumerable<string> GetBottom(ResultState r1, ResultState r2)
|
|
||||||
{
|
|
||||||
if (r1 == ResultState.Bottom)
|
|
||||||
yield return nameof(result);
|
|
||||||
if (r2 == ResultState.Bottom)
|
|
||||||
yield return nameof(next);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result.IsFailure)
|
|
||||||
{
|
|
||||||
error += result.Error;
|
|
||||||
}
|
|
||||||
if (next.IsFailure)
|
|
||||||
{
|
|
||||||
error += next.Error;
|
|
||||||
}
|
|
||||||
return error is null
|
|
||||||
? new(result.Value)
|
|
||||||
: new(error);
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static Result<T> Append<T>(this in Result result, Func<Result<T>> next)
|
|
||||||
{
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => next(),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
[Pure]
|
|
||||||
public static Task<Result<T>> Append<T>(this in Result result, Func<Task<Result<T>>> next)
|
|
||||||
{
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => next(),
|
|
||||||
ResultState.Error => Task.FromResult<Result<T>>(result.Error!),
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Result<T>> Append<T>(this Task<Result> resultTask, Func<Task<Result<T>>> next)
|
|
||||||
{
|
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => await next().ConfigureAwait(false),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(resultTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Result<T>> Append<T>(this Task<Result> resultTask, Func<Result<T>> next)
|
|
||||||
{
|
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => next(),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(resultTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region <T1, T2>
|
|
||||||
|
|
||||||
[Pure] public static Result<(T1, T2)> Append<T1, T2>(this in Result<T1> result, T2 value)
|
|
||||||
{
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => (result.Value, value),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure] public static Result<(T1, T2)> Append<T1, T2>(this in Result<T1> result, Result<T2> next)
|
|
||||||
{
|
|
||||||
Error? error = null;
|
|
||||||
|
|
||||||
if ((result.State & next.State) == ResultState.Bottom)
|
|
||||||
{
|
|
||||||
throw new ResultNotInitializedException(string.Join(';', GetBottom(result.State, next.State)));
|
|
||||||
|
|
||||||
static IEnumerable<string> GetBottom(ResultState r1, ResultState r2)
|
|
||||||
{
|
|
||||||
if (r1 == ResultState.Bottom)
|
|
||||||
yield return nameof(result);
|
|
||||||
if (r2 == ResultState.Bottom)
|
|
||||||
yield return nameof(next);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result.IsFailure)
|
|
||||||
{
|
|
||||||
error += result.Error;
|
|
||||||
}
|
|
||||||
if (next.IsFailure)
|
|
||||||
{
|
|
||||||
error += next.Error;
|
|
||||||
}
|
|
||||||
return error is null
|
|
||||||
? new((result.Value, next.Value))
|
|
||||||
: new(error);
|
|
||||||
}
|
|
||||||
[Pure] public static Result<(T1, T2)> Append<T1, T2>(this in Result<(T1, T2)> result, Result next)
|
|
||||||
{
|
|
||||||
Error? error = null;
|
|
||||||
|
|
||||||
if ((result.State & next.State) == ResultState.Bottom)
|
|
||||||
{
|
|
||||||
throw new ResultNotInitializedException(string.Join(';', GetBottom(result.State, next.State)));
|
|
||||||
|
|
||||||
static IEnumerable<string> GetBottom(ResultState r1, ResultState r2)
|
|
||||||
{
|
|
||||||
if (r1 == ResultState.Bottom)
|
|
||||||
yield return nameof(result);
|
|
||||||
if (r2 == ResultState.Bottom)
|
|
||||||
yield return nameof(next);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result.IsFailure)
|
|
||||||
{
|
|
||||||
error += result.Error;
|
|
||||||
}
|
|
||||||
if (next.IsFailure)
|
|
||||||
{
|
|
||||||
error += next.Error;
|
|
||||||
}
|
|
||||||
return error is null
|
|
||||||
? new(result.Value)
|
|
||||||
: new(error);
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static Result<(T1, T2)> Append<T1, T2>(this in Result<T1> result, Func<Result<T2>> next)
|
|
||||||
{
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => result.Append(next()),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Result<(T1, T2)>> Append<T1, T2>(this Result<T1> result, Func<Task<Result<T2>>> next)
|
|
||||||
{
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => result.Append(await next().ConfigureAwait(false)),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Result<(T1, T2)>> Append<T1, T2>(this Task<Result<T1>> resultTask, Func<Task<Result<T2>>> next)
|
|
||||||
{
|
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => result.Append(await next().ConfigureAwait(false)),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(resultTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Result<(T1, T2)>> Append<T1, T2>(this Task<Result<T1>> resultTask, Func<Result<T2>> next)
|
|
||||||
{
|
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => result.Append(next()),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(resultTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region <T1, T2, T3>
|
|
||||||
|
|
||||||
[Pure] public static Result<(T1, T2, T3)> Append<T1, T2, T3>(this in Result<(T1, T2)> result, T3 value)
|
|
||||||
{
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => (result.Value.Item1, result.Value.Item2, value),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure] public static Result<(T1, T2, T3)> Append<T1, T2, T3>(this in Result<(T1, T2)> result, Result<T3> next)
|
|
||||||
{
|
|
||||||
Error? error = null;
|
|
||||||
|
|
||||||
if ((result.State & next.State) == ResultState.Bottom)
|
|
||||||
{
|
|
||||||
throw new ResultNotInitializedException(string.Join(';', GetBottom(result.State, next.State)));
|
|
||||||
|
|
||||||
static IEnumerable<string> GetBottom(ResultState r1, ResultState r2)
|
|
||||||
{
|
|
||||||
if (r1 == ResultState.Bottom)
|
|
||||||
yield return nameof(result);
|
|
||||||
if (r2 == ResultState.Bottom)
|
|
||||||
yield return nameof(next);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result.IsFailure)
|
|
||||||
{
|
|
||||||
error += result.Error;
|
|
||||||
}
|
|
||||||
if (next.IsFailure)
|
|
||||||
{
|
|
||||||
error += next.Error;
|
|
||||||
}
|
|
||||||
return error is null
|
|
||||||
? new((result.Value.Item1, result.Value.Item2, next.Value))
|
|
||||||
: new(error);
|
|
||||||
}
|
|
||||||
[Pure] public static Result<(T1, T2, T3)> Append<T1, T2, T3>(this in Result<(T1, T2, T3)> result, Result next)
|
|
||||||
{
|
|
||||||
Error? error = null;
|
|
||||||
|
|
||||||
if ((result.State & next.State) == ResultState.Bottom)
|
|
||||||
{
|
|
||||||
throw new ResultNotInitializedException(string.Join(';', GetBottom(result.State, next.State)));
|
|
||||||
|
|
||||||
static IEnumerable<string> GetBottom(ResultState r1, ResultState r2)
|
|
||||||
{
|
|
||||||
if (r1 == ResultState.Bottom)
|
|
||||||
yield return nameof(result);
|
|
||||||
if (r2 == ResultState.Bottom)
|
|
||||||
yield return nameof(next);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result.IsFailure)
|
|
||||||
{
|
|
||||||
error += result.Error;
|
|
||||||
}
|
|
||||||
if (next.IsFailure)
|
|
||||||
{
|
|
||||||
error += next.Error;
|
|
||||||
}
|
|
||||||
return error is null
|
|
||||||
? new(result.Value)
|
|
||||||
: new(error);
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static Result<(T1, T2, T3)> Append<T1, T2, T3>(this in Result<(T1, T2)> result, Func<Result<T3>> next)
|
|
||||||
{
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => result.Append(next()),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Result<(T1, T2, T3)>> Append<T1, T2, T3>(this Result<(T1, T2)> result, Func<Task<Result<T3>>> next)
|
|
||||||
{
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => result.Append(await next().ConfigureAwait(false)),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Result<(T1, T2, T3)>> Append<T1, T2, T3>(this Task<Result<(T1, T2)>> resultTask, Func<Task<Result<T3>>> next)
|
|
||||||
{
|
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => result.Append(await next().ConfigureAwait(false)),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(resultTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Result<(T1, T2, T3)>> Append<T1, T2, T3>(this Task<Result<(T1, T2)>> resultTask, Func<Result<T3>> next)
|
|
||||||
{
|
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => result.Append(next()),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(resultTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region <T1, T2, T3, T4>
|
|
||||||
|
|
||||||
[Pure] public static Result<(T1, T2, T3, T4)> Append<T1, T2, T3, T4>(this in Result<(T1, T2, T3)> result, T4 value)
|
|
||||||
{
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => (result.Value.Item1, result.Value.Item2, result.Value.Item3, value),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure] public static Result<(T1, T2, T3, T4)> Append<T1, T2, T3, T4>(this in Result<(T1, T2, T3)> result, Result<T4> next)
|
|
||||||
{
|
|
||||||
Error? error = null;
|
|
||||||
|
|
||||||
if ((result.State & next.State) == ResultState.Bottom)
|
|
||||||
{
|
|
||||||
throw new ResultNotInitializedException(string.Join(';', GetBottom(result.State, next.State)));
|
|
||||||
|
|
||||||
static IEnumerable<string> GetBottom(ResultState r1, ResultState r2)
|
|
||||||
{
|
|
||||||
if (r1 == ResultState.Bottom)
|
|
||||||
yield return nameof(result);
|
|
||||||
if (r2 == ResultState.Bottom)
|
|
||||||
yield return nameof(next);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result.IsFailure)
|
|
||||||
{
|
|
||||||
error += result.Error;
|
|
||||||
}
|
|
||||||
if (next.IsFailure)
|
|
||||||
{
|
|
||||||
error += next.Error;
|
|
||||||
}
|
|
||||||
return error is null
|
|
||||||
? new((result.Value.Item1, result.Value.Item2, result.Value.Item3, next.Value))
|
|
||||||
: new(error);
|
|
||||||
}
|
|
||||||
[Pure] public static Result<(T1, T2, T3, T4)> Append<T1, T2, T3, T4>(this in Result<(T1, T2, T3, T4)> result, Result next)
|
|
||||||
{
|
|
||||||
Error? error = null;
|
|
||||||
|
|
||||||
if ((result.State & next.State) == ResultState.Bottom)
|
|
||||||
{
|
|
||||||
throw new ResultNotInitializedException(string.Join(';', GetBottom(result.State, next.State)));
|
|
||||||
|
|
||||||
static IEnumerable<string> GetBottom(ResultState r1, ResultState r2)
|
|
||||||
{
|
|
||||||
if (r1 == ResultState.Bottom)
|
|
||||||
yield return nameof(result);
|
|
||||||
if (r2 == ResultState.Bottom)
|
|
||||||
yield return nameof(next);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (result.IsFailure)
|
|
||||||
{
|
|
||||||
error += result.Error;
|
|
||||||
}
|
|
||||||
if (next.IsFailure)
|
|
||||||
{
|
|
||||||
error += next.Error;
|
|
||||||
}
|
|
||||||
return error is null
|
|
||||||
? new(result.Value)
|
|
||||||
: new(error);
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static Result<(T1, T2, T3, T4)> Append<T1, T2, T3, T4>(this in Result<(T1, T2, T3)> result, Func<Result<T4>> next)
|
|
||||||
{
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => result.Append(next()),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Result<(T1, T2, T3, T4)>> Append<T1, T2, T3, T4>(this Result<(T1, T2, T3)> result, Func<Task<Result<T4>>> next)
|
|
||||||
{
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => result.Append(await next().ConfigureAwait(false)),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Result<(T1, T2, T3, T4)>> Append<T1, T2, T3, T4>(this Task<Result<(T1, T2, T3)>> resultTask, Func<Task<Result<T4>>> next)
|
|
||||||
{
|
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => result.Append(await next().ConfigureAwait(false)),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(resultTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Result<(T1, T2, T3, T4)>> Append<T1, T2, T3, T4>(this Task<Result<(T1, T2, T3)>> resultTask, Func<Result<T4>> next)
|
|
||||||
{
|
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
|
||||||
return result.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => result.Append(next()),
|
|
||||||
ResultState.Error => result.Error!,
|
|
||||||
_ => throw new ResultNotInitializedException(nameof(resultTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Tap<>
|
|
||||||
[Pure]
|
|
||||||
public static ref readonly Result Tap(this in Result result, Action? onSuccess = null, Action<Error>? onFailure = null)
|
|
||||||
{
|
|
||||||
switch (result.State)
|
|
||||||
{
|
|
||||||
case ResultState.Success:
|
|
||||||
onSuccess?.Invoke();
|
|
||||||
break;
|
|
||||||
case ResultState.Error:
|
|
||||||
onFailure?.Invoke(result.Error!);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default: throw new ResultNotInitializedException(nameof(result));
|
|
||||||
}
|
|
||||||
|
|
||||||
return ref result;
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Result> Tap(this Task<Result> resultTask, Action? onSuccess = null, Action<Error>? onFailure = null)
|
|
||||||
{
|
|
||||||
var result = await resultTask.ConfigureAwait(false);
|
|
||||||
switch (result.State)
|
|
||||||
{
|
|
||||||
case ResultState.Success:
|
|
||||||
onSuccess?.Invoke();
|
|
||||||
break;
|
|
||||||
case ResultState.Error:
|
|
||||||
onFailure?.Invoke(result.Error!);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default: throw new ResultNotInitializedException(nameof(resultTask));
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
[Pure]
|
|
||||||
public static async Task<Result> Tap(this Result result, Func<Task>? onSuccess = null, Func<Error, Task>? onFailure = null)
|
|
||||||
{
|
|
||||||
switch (result.State)
|
|
||||||
{
|
|
||||||
case ResultState.Success:
|
|
||||||
if (onSuccess is not null)
|
|
||||||
await onSuccess.Invoke().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> Tap(this Task<Result> resultTask, Func<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().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
|
#region Merge
|
||||||
|
|
||||||
|
public static Result Merge(this IEnumerable<Result> results)
|
||||||
|
{
|
||||||
|
List<Error>? errors = null;
|
||||||
|
bool hasErrors = false;
|
||||||
|
|
||||||
|
foreach (var result in results.OrderBy(x => x.State))
|
||||||
|
{
|
||||||
|
switch (result.State)
|
||||||
|
{
|
||||||
|
case ResultState.Error:
|
||||||
|
hasErrors = true;
|
||||||
|
errors ??= [];
|
||||||
|
errors.Add(result.Error!);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case ResultState.Success:
|
||||||
|
if (hasErrors) goto afterLoop;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default: throw new ResultNotInitializedException(nameof(results));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
afterLoop:
|
||||||
|
return hasErrors
|
||||||
|
? new(new ManyErrors(errors!))
|
||||||
|
: new(null);
|
||||||
|
}
|
||||||
|
public static async Task<Result> Merge(this IEnumerable<Task<Result>> tasks)
|
||||||
|
{
|
||||||
|
var results = await Task.WhenAll(tasks).ConfigureAwait(false);
|
||||||
|
return results.Merge();
|
||||||
|
}
|
||||||
|
|
||||||
public static Result<IEnumerable<T>> Merge<T>(this IEnumerable<Result<T>> results)
|
public static Result<IEnumerable<T>> Merge<T>(this IEnumerable<Result<T>> results)
|
||||||
{
|
{
|
||||||
List<T>? values = null;
|
List<T>? values = null;
|
||||||
|
|||||||
5
Railway/Try.cs
Normal file
5
Railway/Try.cs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
namespace Just.Railway;
|
||||||
|
|
||||||
|
public static partial class Try
|
||||||
|
{
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user