diff --git a/Source/.editorconfig b/Source/.editorconfig new file mode 100644 index 00000000..a26238b4 --- /dev/null +++ b/Source/.editorconfig @@ -0,0 +1,9 @@ +root = true +# editorconfig.org + +# C# files +[*.cs] +tab_width = 4 +indent_style = space +indent_size = 4 +end_of_line = crlf \ No newline at end of file diff --git a/Source/DocSamples/Samples.cs b/Source/DocSamples/Samples.cs index 92325cbb..1746db76 100644 --- a/Source/DocSamples/Samples.cs +++ b/Source/DocSamples/Samples.cs @@ -58,7 +58,7 @@ public static void MatchSimpleAsyncRunner() public async Task MatchSimpleAsync() { #region match-simple-async - var answerOutput = + var answerOutput = AsyncAssertItIsTheAnswerToEverything(42) .Match( ok => $"{ok} no more words needed!", diff --git a/Source/FunicularSwitch.Analyzers.CodeFixProviders/MatchNullCodeFixProvider.cs b/Source/FunicularSwitch.Analyzers.CodeFixProviders/MatchNullCodeFixProvider.cs index 59ddde38..ee23132b 100644 --- a/Source/FunicularSwitch.Analyzers.CodeFixProviders/MatchNullCodeFixProvider.cs +++ b/Source/FunicularSwitch.Analyzers.CodeFixProviders/MatchNullCodeFixProvider.cs @@ -27,12 +27,12 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken); var diagnosticNode = root?.FindNode(diagnosticSpan); - + if (diagnosticNode is not InvocationExpressionSyntax invocationExpressionSyntax) { return; } - + if (invocationExpressionSyntax.Expression is not MemberAccessExpressionSyntax m) { return; diff --git a/Source/FunicularSwitch.Analyzers/MatchNullAnalyzer.cs b/Source/FunicularSwitch.Analyzers/MatchNullAnalyzer.cs index 548f4d9d..a935007a 100644 --- a/Source/FunicularSwitch.Analyzers/MatchNullAnalyzer.cs +++ b/Source/FunicularSwitch.Analyzers/MatchNullAnalyzer.cs @@ -22,9 +22,9 @@ public class MatchNullAnalyzer : DiagnosticAnalyzer public override void Initialize(AnalysisContext context) { context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); - + context.EnableConcurrentExecution(); - + context.RegisterSyntaxNodeAction(AnalyzeInvocation, SyntaxKind.InvocationExpression); } @@ -34,7 +34,7 @@ private void AnalyzeInvocation(SyntaxNodeAnalysisContext context) { return; } - + if (context.SemanticModel.GetSymbolInfo(invocationExpressionSyntax, context.CancellationToken) .Symbol is not IMethodSymbol methodSymbol) { diff --git a/Source/FunicularSwitch.Benchmarks/GenResBenchmark.cs b/Source/FunicularSwitch.Benchmarks/GenResBenchmark.cs index 3170efbe..801d572f 100644 --- a/Source/FunicularSwitch.Benchmarks/GenResBenchmark.cs +++ b/Source/FunicularSwitch.Benchmarks/GenResBenchmark.cs @@ -30,17 +30,14 @@ public GenericResult CreateOkWithType() public GenericResult CreateErrorWithType() { return GenericResult.Error(40); - - - } - + [Benchmark] public GenericResult CreateOkWithImplicitOperator() { return GenericResult.Ok(42); } - + [Benchmark] public GenericResult CreateErrorWithImplicitOperator() { diff --git a/Source/FunicularSwitch.Generators.AwesomeAssertions.Templates/MyErrorType.cs b/Source/FunicularSwitch.Generators.AwesomeAssertions.Templates/MyErrorType.cs index 21652d8d..f95574f9 100644 --- a/Source/FunicularSwitch.Generators.AwesomeAssertions.Templates/MyErrorType.cs +++ b/Source/FunicularSwitch.Generators.AwesomeAssertions.Templates/MyErrorType.cs @@ -3,6 +3,6 @@ namespace FunicularSwitch.Generators.AwesomeAssertions.Templates { public abstract class MyErrorType { - public sealed class MyDerivedErrorType : MyErrorType {} + public sealed class MyDerivedErrorType : MyErrorType { } } } \ No newline at end of file diff --git a/Source/FunicularSwitch.Generators.AwesomeAssertions/AssertionMethods/Generator.cs b/Source/FunicularSwitch.Generators.AwesomeAssertions/AssertionMethods/Generator.cs index 8d7aaa35..06718e48 100644 --- a/Source/FunicularSwitch.Generators.AwesomeAssertions/AssertionMethods/Generator.cs +++ b/Source/FunicularSwitch.Generators.AwesomeAssertions/AssertionMethods/Generator.cs @@ -125,7 +125,7 @@ internal partial class {{unionTypeFullName}}Assertions{{typeParametersText}} : O } } """; - + var extensionsCode = $$""" #nullable enable diff --git a/Source/FunicularSwitch.Generators.AwesomeAssertions/AssertionMethods/Parser.cs b/Source/FunicularSwitch.Generators.AwesomeAssertions/AssertionMethods/Parser.cs index 57b26ddc..83bd6dad 100644 --- a/Source/FunicularSwitch.Generators.AwesomeAssertions/AssertionMethods/Parser.cs +++ b/Source/FunicularSwitch.Generators.AwesomeAssertions/AssertionMethods/Parser.cs @@ -12,7 +12,7 @@ public static IEnumerable GetResultTypes( CancellationToken cancellationToken) { var allTypesInAssembly = GetAllTypes(assembly); - + return GetTypesWithAttribute(allTypesInAssembly, ResultTypeAttribute) .Where(tuple => tuple.Type.DeclaredAccessibility == Accessibility.Public || (tuple.Type.DeclaredAccessibility == Accessibility.Internal && generateForInternalTypes)) .Select(tuple => @@ -29,7 +29,7 @@ public static IEnumerable GetUnionTypes( CancellationToken cancellationToken) { var allTypesInAssembly = GetAllTypes(assembly); - + var unionTypes = GetTypesWithAttribute(allTypesInAssembly, UnionTypeAttribute) .Where(tuple => tuple.Type.DeclaredAccessibility == Accessibility.Public || (tuple.Type.DeclaredAccessibility == Accessibility.Internal && generateForInternalTypes)); diff --git a/Source/FunicularSwitch.Generators.AwesomeAssertions/AssertionMethodsGenerator.cs b/Source/FunicularSwitch.Generators.AwesomeAssertions/AssertionMethodsGenerator.cs index 8c3060a6..ab80c007 100644 --- a/Source/FunicularSwitch.Generators.AwesomeAssertions/AssertionMethodsGenerator.cs +++ b/Source/FunicularSwitch.Generators.AwesomeAssertions/AssertionMethodsGenerator.cs @@ -9,7 +9,7 @@ namespace FunicularSwitch.Generators.AwesomeAssertions; public class AssertionMethodsGenerator : IIncrementalGenerator { const string FunicularSwitchNamespace = "FunicularSwitch"; - + public void Initialize(IncrementalGeneratorInitializationContext context) { context.RegisterPostInitializationOutput(ctx => @@ -46,7 +46,7 @@ private static void Execute( new ResultTypeSchema(resultType!, null), context.ReportDiagnostic, context.CancellationToken); - + var optionAssertionsText = Templates.GenerateAssertionsForTemplates.OptionAssertions.Replace(Generator.TemplateNamespace, FunicularSwitchNamespace); context.AddSource($"{FunicularSwitchNamespace}.OptionAssertions.g.cs", optionAssertionsText); var optionAssertionExtensionsText = Templates.GenerateAssertionsForTemplates.OptionAssertionExtensions.Replace(Generator.TemplateNamespace, FunicularSwitchNamespace); diff --git a/Source/FunicularSwitch.Generators.AwesomeAssertions/IsExternalInit.cs b/Source/FunicularSwitch.Generators.AwesomeAssertions/IsExternalInit.cs index e44d655d..273677de 100644 --- a/Source/FunicularSwitch.Generators.AwesomeAssertions/IsExternalInit.cs +++ b/Source/FunicularSwitch.Generators.AwesomeAssertions/IsExternalInit.cs @@ -4,4 +4,4 @@ namespace System.Runtime.CompilerServices; [EditorBrowsable(EditorBrowsableState.Never)] -internal class IsExternalInit{} \ No newline at end of file +internal class IsExternalInit { } \ No newline at end of file diff --git a/Source/FunicularSwitch.Generators.Common/EquatableArray.cs b/Source/FunicularSwitch.Generators.Common/EquatableArray.cs index a80b6246..9a47a311 100644 --- a/Source/FunicularSwitch.Generators.Common/EquatableArray.cs +++ b/Source/FunicularSwitch.Generators.Common/EquatableArray.cs @@ -41,7 +41,7 @@ public static EquatableArray AsEquatableArray(this ImmutableArray array private readonly T[]? _array; public bool IsDefault => _array == null; - + /// /// Creates a new instance. /// diff --git a/Source/FunicularSwitch.Generators.Common/Result.cs b/Source/FunicularSwitch.Generators.Common/Result.cs index 64bed2fe..1ae0bdbf 100644 --- a/Source/FunicularSwitch.Generators.Common/Result.cs +++ b/Source/FunicularSwitch.Generators.Common/Result.cs @@ -20,32 +20,32 @@ public static GenerationResult SelectMany(this GenerationResult m public static GenerationResult Select(this GenerationResult ma, Func fn) => ma.Map(fn); } - + public readonly record struct GenerationResult(T? Value, EquatableArray Diagnostics, bool HasValue) { - public static readonly GenerationResult Empty = new(default, ImmutableArray.Empty, false); - - public GenerationResult AddDiagnostics(DiagnosticInfo diagnosticInfo) => + public static readonly GenerationResult Empty = new(default, ImmutableArray.Empty, false); + + public GenerationResult AddDiagnostics(DiagnosticInfo diagnosticInfo) => this with { Diagnostics = Diagnostics.AsImmutableArray().Add(diagnosticInfo) }; - - public GenerationResult SetValue(T value) => + + public GenerationResult SetValue(T value) => this with { Value = value, HasValue = true }; public static implicit operator GenerationResult(DiagnosticInfo diagnostic) => Empty.AddDiagnostics(diagnostic); - + public static implicit operator GenerationResult(EquatableArray diagnostics) => new(default, diagnostics, false); - + public static implicit operator GenerationResult(T value) => Empty.SetValue(value); public GenerationResult Bind(Func> bind) { if (!HasValue) return Diagnostics; - + var newValue = bind(Value!); return newValue with { Diagnostics = Diagnostics.AsImmutableArray().AddRange(newValue.Diagnostics) }; } - + public GenerationResult Map(Func map) { var newValue = !HasValue diff --git a/Source/FunicularSwitch.Generators.Common/SourceProductionContextExtension.cs b/Source/FunicularSwitch.Generators.Common/SourceProductionContextExtension.cs index c4737e0c..fe617d59 100644 --- a/Source/FunicularSwitch.Generators.Common/SourceProductionContextExtension.cs +++ b/Source/FunicularSwitch.Generators.Common/SourceProductionContextExtension.cs @@ -4,7 +4,7 @@ namespace FunicularSwitch.Generators.Common { public static class SourceProductionContextExtension { - public static void ReportDiagnostic(this SourceProductionContext context, DiagnosticInfo diagnostic) => + public static void ReportDiagnostic(this SourceProductionContext context, DiagnosticInfo diagnostic) => context.ReportDiagnostic(Diagnostic.Create(diagnostic.Descriptor, diagnostic.Location?.ToLocation())); } } \ No newline at end of file diff --git a/Source/FunicularSwitch.Generators.Templates/EnumTypeAttributes.cs b/Source/FunicularSwitch.Generators.Templates/EnumTypeAttributes.cs index f8adff12..3a2720d9 100644 --- a/Source/FunicularSwitch.Generators.Templates/EnumTypeAttributes.cs +++ b/Source/FunicularSwitch.Generators.Templates/EnumTypeAttributes.cs @@ -7,10 +7,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -23,16 +23,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -41,21 +41,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/FunicularSwitch.Generators.Templates/MyError.cs b/Source/FunicularSwitch.Generators.Templates/MyError.cs index eca3faf2..10961908 100644 --- a/Source/FunicularSwitch.Generators.Templates/MyError.cs +++ b/Source/FunicularSwitch.Generators.Templates/MyError.cs @@ -17,7 +17,7 @@ public MyError Merge__MemberOrExtensionMethod(MyError other) => this is Aggregat ? a.Add(other) : other is Aggregated_ oa ? oa.Add(this) - : Aggregated(new []{this, other}); + : Aggregated([this, other]); public class Generic_ : MyError { @@ -49,7 +49,7 @@ public class Aggregated_ : MyError public Aggregated_(System.Collections.Generic.IEnumerable errors) : base(UnionCases.Aggregated) => Errors = errors.ToList(); - public MyError Add(MyError other) => Aggregated(Errors.Concat(new []{other})); + public MyError Add(MyError other) => Aggregated(Errors.Concat([other])); } internal enum UnionCases diff --git a/Source/FunicularSwitch.Generators.Templates/ResultType.cs b/Source/FunicularSwitch.Generators.Templates/ResultType.cs index c8920575..cc451ed0 100644 --- a/Source/FunicularSwitch.Generators.Templates/ResultType.cs +++ b/Source/FunicularSwitch.Generators.Templates/ResultType.cs @@ -1,610 +1,610 @@ -#nullable enable +#nullable enable using global::System.Linq; //additional using directives -namespace FunicularSwitch.Generators.Templates +namespace FunicularSwitch.Generators.Templates { -#pragma warning disable 1591 - public abstract partial class MyResult - { - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static MyResult Error(MyError details) => new MyResult.Error_(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static MyResultError Error(MyError details) => new(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static MyResult Ok(T value) => new MyResult.Ok_(value); - public bool IsError => GetType().GetGenericTypeDefinition() == typeof(MyResult<>.Error_); - public bool IsOk => !IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public abstract MyError? GetErrorOrDefault(); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult Try([global::JetBrains.Annotations.InstantHandle] global::System.Func action, [global::JetBrains.Annotations.InstantHandle] global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task> Try([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> action, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult Try([global::JetBrains.Annotations.InstantHandle] global::System.Func> action, [global::JetBrains.Annotations.InstantHandle] global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task> Try([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func>> action, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - } - - public abstract partial class MyResult : MyResult, global::System.Collections.Generic.IEnumerable - { - - [global::System.Diagnostics.DebuggerNonUserCode] - [global::JetBrains.Annotations.MustUseReturnValue] - public new static MyResult Error(MyError message) => Error(message); - - [global::System.Diagnostics.DebuggerNonUserCode] - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult Ok(T value) => Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static implicit operator MyResult(T value) => MyResult.Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static implicit operator MyResult(MyResultError myResultError) => myResultError.WithOk(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator true(MyResult result) => result.IsOk; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator false(MyResult result) => result.IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !(MyResult result) => result.IsError; - - //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode - bool Equals(MyResult other) => this switch - { - Ok_ ok => ok.Equals((object)other), - Error_ error => error.Equals((object)other), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(MyResult)}") - }; - - public override int GetHashCode() => this switch - { - Ok_ ok => ok.GetHashCode(), - Error_ error => error.GetHashCode(), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(MyResult)}") - }; - - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != this.GetType()) return false; - return Equals((MyResult)obj); - } - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(MyResult? left, MyResult? right) => Equals(left, right); - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(MyResult? left, MyResult? right) => !Equals(left, right); - - [global::System.Diagnostics.DebuggerStepThrough] - public void Match([global::JetBrains.Annotations.InstantHandle] global::System.Action ok, [global::JetBrains.Annotations.InstantHandle] global::System.Action? error = null) => Match( - v => - { - ok.Invoke(v); - return 42; - }, - err => - { - error?.Invoke(err); - return 42; - }); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public T1 Match([global::JetBrains.Annotations.InstantHandle] global::System.Func ok, [global::JetBrains.Annotations.InstantHandle] global::System.Func error) - { - return this switch - { - Ok_ okMyResult => ok(okMyResult.Value), - Error_ errorMyResult => error(errorMyResult.Details), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public async global::System.Threading.Tasks.Task Match([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> ok, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> error) - { - return this switch - { - Ok_ okMyResult => await ok(okMyResult.Value).ConfigureAwait(false), - Error_ errorMyResult => await error(errorMyResult.Details).ConfigureAwait(false), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public global::System.Threading.Tasks.Task Match([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> ok, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func error) => - Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); - - [global::System.Diagnostics.DebuggerStepThrough] - public async global::System.Threading.Tasks.Task Match([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func ok) - { - if (this is Ok_ okMyResult) await ok(okMyResult.Value).ConfigureAwait(false); - } - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public T Match([global::JetBrains.Annotations.InstantHandle] global::System.Func error) => Match(v => v, error); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public MyResult Bind([global::JetBrains.Annotations.InstantHandle] global::System.Func> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return bind(ok.Value); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public async global::System.Threading.Tasks.Task> Bind([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func>> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return await bind(ok.Value).ConfigureAwait(false); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public MyResult Map([global::JetBrains.Annotations.InstantHandle] global::System.Func map) - { - switch (this) - { - case Ok_ ok: - try - { - return MyResult.Ok(map(ok.Value)); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public async global::System.Threading.Tasks.Task> Map( - [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> map) - { - switch (this) - { - case Ok_ ok: - try - { - return MyResult.Ok(await map(ok.Value).ConfigureAwait(false)); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - //createGenericResultConversions - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T? GetValueOrDefault() - => Match( - v => (T?)v, - _ => default - ); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public T GetValueOrDefault([global::JetBrains.Annotations.InstantHandle] global::System.Func defaultValue) - => Match( - v => v, - _ => defaultValue() - ); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T GetValueOrDefault(T defaultValue) - => Match( - v => v, - _ => defaultValue - ); - - [global::System.Diagnostics.DebuggerStepThrough] - public T GetValueOrThrow() - => Match( - v => v, - details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); - - [global::JetBrains.Annotations.MustUseReturnValue] - public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); - - [global::JetBrains.Annotations.MustUseReturnValue] - public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); - global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); - - public sealed partial class Ok_ : MyResult - { - public T Value { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Ok_(T value) => Value = value; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override MyError? GetErrorOrDefault() => null; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Ok_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Ok_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); - } - - public sealed partial class Error_ : MyResult - { - public MyError Details { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Error_(MyError details) => Details = details; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public MyResult.Error_ Convert() => new MyResult.Error_(Details); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override MyError? GetErrorOrDefault() => Details; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Error_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return Equals(Details, other.Details); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Error_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); - } - } - - public readonly partial struct MyResultError : global::System.IEquatable - { - readonly MyError _details; - - internal MyResultError(MyError details) => _details = details; - - [global::System.Diagnostics.Contracts.Pure] - public MyResult WithOk() => MyResult.Error(_details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(MyResultError other) => _details.Equals(other._details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) => obj is MyResultError other && Equals(other); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => _details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(MyResultError left, MyResultError right) => left.Equals(right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(MyResultError left, MyResultError right) => !left.Equals(right); - } - - public static partial class MyResultExtension - { - #region bind - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> bind) - => (await result.ConfigureAwait(false)).Bind(bind); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func>> bind) - => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); - - #endregion - - #region map - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func map) - => (await result.ConfigureAwait(false)).Map(map); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> bind) - => Bind(result, async v => MyResult.Ok(await bind(v).ConfigureAwait(false))); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult MapError(this MyResult result, [global::JetBrains.Annotations.InstantHandle] global::System.Func mapError) - { - if (result is MyResult.Error_ e) - return MyResult.Error(mapError(e.Details)); - return result; - } - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); - - #endregion - - #region match - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> ok, - [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> ok, - [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func ok, - [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func error) - => (await result.ConfigureAwait(false)).Match(ok, error); - - #endregion - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static MyResult Flatten(this MyResult> result) => result.Bind(r => r); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult As(this MyResult result, [global::JetBrains.Annotations.InstantHandle] global::System.Func errorTIsNotT1) => - result.Bind(r => - { - if (r is T1 converted) - return converted; - return MyResult.Error(errorTIsNotT1()); - }); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult As(this MyResult result, [global::JetBrains.Annotations.InstantHandle] global::System.Func errorIsNotT1) => - result.As(errorIsNotT1); - - #region query-expression pattern - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult Select(this MyResult result, global::System.Func selector) => result.Map(selector); - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult SelectMany(this MyResult result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static global::System.Threading.Tasks.Task> SelectMany(this MyResult result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - - #endregion - - //createGenericResultConversionExtensions - } -} - -namespace FunicularSwitch.Generators.Templates.Extensions +#pragma warning disable 1591 + public abstract partial class MyResult + { + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static MyResult Error(MyError details) => new MyResult.Error_(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static MyResultError Error(MyError details) => new(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static MyResult Ok(T value) => new MyResult.Ok_(value); + public bool IsError => GetType().GetGenericTypeDefinition() == typeof(MyResult<>.Error_); + public bool IsOk => !IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public abstract MyError? GetErrorOrDefault(); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult Try([global::JetBrains.Annotations.InstantHandle] global::System.Func action, [global::JetBrains.Annotations.InstantHandle] global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task> Try([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> action, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult Try([global::JetBrains.Annotations.InstantHandle] global::System.Func> action, [global::JetBrains.Annotations.InstantHandle] global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task> Try([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func>> action, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + } + + public abstract partial class MyResult : MyResult, global::System.Collections.Generic.IEnumerable + { + + [global::System.Diagnostics.DebuggerNonUserCode] + [global::JetBrains.Annotations.MustUseReturnValue] + public new static MyResult Error(MyError message) => Error(message); + + [global::System.Diagnostics.DebuggerNonUserCode] + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult Ok(T value) => Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static implicit operator MyResult(T value) => MyResult.Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static implicit operator MyResult(MyResultError myResultError) => myResultError.WithOk(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator true(MyResult result) => result.IsOk; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator false(MyResult result) => result.IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !(MyResult result) => result.IsError; + + //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode + bool Equals(MyResult other) => this switch + { + Ok_ ok => ok.Equals((object)other), + Error_ error => error.Equals((object)other), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(MyResult)}") + }; + + public override int GetHashCode() => this switch + { + Ok_ ok => ok.GetHashCode(), + Error_ error => error.GetHashCode(), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(MyResult)}") + }; + + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((MyResult)obj); + } + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(MyResult? left, MyResult? right) => Equals(left, right); + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(MyResult? left, MyResult? right) => !Equals(left, right); + + [global::System.Diagnostics.DebuggerStepThrough] + public void Match([global::JetBrains.Annotations.InstantHandle] global::System.Action ok, [global::JetBrains.Annotations.InstantHandle] global::System.Action? error = null) => Match( + v => + { + ok.Invoke(v); + return 42; + }, + err => + { + error?.Invoke(err); + return 42; + }); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public T1 Match([global::JetBrains.Annotations.InstantHandle] global::System.Func ok, [global::JetBrains.Annotations.InstantHandle] global::System.Func error) + { + return this switch + { + Ok_ okMyResult => ok(okMyResult.Value), + Error_ errorMyResult => error(errorMyResult.Details), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public async global::System.Threading.Tasks.Task Match([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> ok, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> error) + { + return this switch + { + Ok_ okMyResult => await ok(okMyResult.Value).ConfigureAwait(false), + Error_ errorMyResult => await error(errorMyResult.Details).ConfigureAwait(false), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public global::System.Threading.Tasks.Task Match([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> ok, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func error) => + Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); + + [global::System.Diagnostics.DebuggerStepThrough] + public async global::System.Threading.Tasks.Task Match([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func ok) + { + if (this is Ok_ okMyResult) await ok(okMyResult.Value).ConfigureAwait(false); + } + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public T Match([global::JetBrains.Annotations.InstantHandle] global::System.Func error) => Match(v => v, error); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public MyResult Bind([global::JetBrains.Annotations.InstantHandle] global::System.Func> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return bind(ok.Value); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public async global::System.Threading.Tasks.Task> Bind([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func>> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return await bind(ok.Value).ConfigureAwait(false); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public MyResult Map([global::JetBrains.Annotations.InstantHandle] global::System.Func map) + { + switch (this) + { + case Ok_ ok: + try + { + return MyResult.Ok(map(ok.Value)); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public async global::System.Threading.Tasks.Task> Map( + [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> map) + { + switch (this) + { + case Ok_ ok: + try + { + return MyResult.Ok(await map(ok.Value).ConfigureAwait(false)); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + //createGenericResultConversions + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T? GetValueOrDefault() + => Match( + v => (T?)v, + _ => default + ); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public T GetValueOrDefault([global::JetBrains.Annotations.InstantHandle] global::System.Func defaultValue) + => Match( + v => v, + _ => defaultValue() + ); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T GetValueOrDefault(T defaultValue) + => Match( + v => v, + _ => defaultValue + ); + + [global::System.Diagnostics.DebuggerStepThrough] + public T GetValueOrThrow() + => Match( + v => v, + details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); + + [global::JetBrains.Annotations.MustUseReturnValue] + public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); + + [global::JetBrains.Annotations.MustUseReturnValue] + public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); + global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); + + public sealed partial class Ok_ : MyResult + { + public T Value { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Ok_(T value) => Value = value; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override MyError? GetErrorOrDefault() => null; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Ok_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Ok_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); + } + + public sealed partial class Error_ : MyResult + { + public MyError Details { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Error_(MyError details) => Details = details; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public MyResult.Error_ Convert() => new MyResult.Error_(Details); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override MyError? GetErrorOrDefault() => Details; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Error_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Equals(Details, other.Details); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Error_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); + } + } + + public readonly partial struct MyResultError : global::System.IEquatable + { + readonly MyError _details; + + internal MyResultError(MyError details) => _details = details; + + [global::System.Diagnostics.Contracts.Pure] + public MyResult WithOk() => MyResult.Error(_details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(MyResultError other) => _details.Equals(other._details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) => obj is MyResultError other && Equals(other); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => _details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(MyResultError left, MyResultError right) => left.Equals(right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(MyResultError left, MyResultError right) => !left.Equals(right); + } + + public static partial class MyResultExtension + { + #region bind + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> bind) + => (await result.ConfigureAwait(false)).Bind(bind); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func>> bind) + => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); + + #endregion + + #region map + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func map) + => (await result.ConfigureAwait(false)).Map(map); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> bind) + => Bind(result, async v => MyResult.Ok(await bind(v).ConfigureAwait(false))); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult MapError(this MyResult result, [global::JetBrains.Annotations.InstantHandle] global::System.Func mapError) + { + if (result is MyResult.Error_ e) + return MyResult.Error(mapError(e.Details)); + return result; + } + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); + + #endregion + + #region match + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> ok, + [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> ok, + [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func ok, + [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func error) + => (await result.ConfigureAwait(false)).Match(ok, error); + + #endregion + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static MyResult Flatten(this MyResult> result) => result.Bind(r => r); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult As(this MyResult result, [global::JetBrains.Annotations.InstantHandle] global::System.Func errorTIsNotT1) => + result.Bind(r => + { + if (r is T1 converted) + return converted; + return MyResult.Error(errorTIsNotT1()); + }); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult As(this MyResult result, [global::JetBrains.Annotations.InstantHandle] global::System.Func errorIsNotT1) => + result.As(errorIsNotT1); + + #region query-expression pattern + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult Select(this MyResult result, global::System.Func selector) => result.Map(selector); + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult SelectMany(this MyResult result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static global::System.Threading.Tasks.Task> SelectMany(this MyResult result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + + #endregion + + //createGenericResultConversionExtensions + } +} + +namespace FunicularSwitch.Generators.Templates.Extensions { - public static partial class MyResultExtension - { - [global::JetBrains.Annotations.MustUseReturnValue] - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable items, - global::System.Func> choose, - global::System.Action onError) - => items - .Select(i => choose(i)) - .Choose(onError); - - [global::JetBrains.Annotations.MustUseReturnValue] - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable> results, - global::System.Action onError) - => results - .Where(r => - r.Match(_ => true, error => - { - onError(error); - return false; - })) - .Select(r => r.GetValueOrThrow()); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult As(this object? item, [global::JetBrains.Annotations.InstantHandle] global::System.Func error) => - !(item is T t) ? MyResult.Error(error()) : t; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult NotNull(this T? item, [global::JetBrains.Annotations.InstantHandle] global::System.Func error) => - item ?? MyResult.Error(error()); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult NotNullOrEmpty(this string? s, [global::JetBrains.Annotations.InstantHandle] global::System.Func error) - => string.IsNullOrEmpty(s) ? MyResult.Error(error()) : s!; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult NotNullOrWhiteSpace(this string? s, [global::JetBrains.Annotations.InstantHandle] global::System.Func error) - => string.IsNullOrWhiteSpace(s) ? MyResult.Error(error()) : s!; - - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult First(this global::System.Collections.Generic.IEnumerable candidates, [global::JetBrains.Annotations.InstantHandle] global::System.Func predicate, [global::JetBrains.Annotations.InstantHandle] global::System.Func noMatch) => - candidates - .FirstOrDefault(i => predicate(i)) - .NotNull(noMatch); - } -#pragma warning restore 1591 -} + public static partial class MyResultExtension + { + [global::JetBrains.Annotations.MustUseReturnValue] + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable items, + global::System.Func> choose, + global::System.Action onError) + => items + .Select(i => choose(i)) + .Choose(onError); + + [global::JetBrains.Annotations.MustUseReturnValue] + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable> results, + global::System.Action onError) + => results + .Where(r => + r.Match(_ => true, error => + { + onError(error); + return false; + })) + .Select(r => r.GetValueOrThrow()); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult As(this object? item, [global::JetBrains.Annotations.InstantHandle] global::System.Func error) => + !(item is T t) ? MyResult.Error(error()) : t; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult NotNull(this T? item, [global::JetBrains.Annotations.InstantHandle] global::System.Func error) => + item ?? MyResult.Error(error()); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult NotNullOrEmpty(this string? s, [global::JetBrains.Annotations.InstantHandle] global::System.Func error) + => string.IsNullOrEmpty(s) ? MyResult.Error(error()) : s!; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult NotNullOrWhiteSpace(this string? s, [global::JetBrains.Annotations.InstantHandle] global::System.Func error) + => string.IsNullOrWhiteSpace(s) ? MyResult.Error(error()) : s!; + + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult First(this global::System.Collections.Generic.IEnumerable candidates, [global::JetBrains.Annotations.InstantHandle] global::System.Func predicate, [global::JetBrains.Annotations.InstantHandle] global::System.Func noMatch) => + candidates + .FirstOrDefault(i => predicate(i)) + .NotNull(noMatch); + } +#pragma warning restore 1591 +} diff --git a/Source/FunicularSwitch.Generators.Templates/ResultTypeAttributes.cs b/Source/FunicularSwitch.Generators.Templates/ResultTypeAttributes.cs index 8dddcd08..ba6d29a4 100644 --- a/Source/FunicularSwitch.Generators.Templates/ResultTypeAttributes.cs +++ b/Source/FunicularSwitch.Generators.Templates/ResultTypeAttributes.cs @@ -4,10 +4,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/FunicularSwitch.Generators.Templates/ResultTypeWithMerge.cs b/Source/FunicularSwitch.Generators.Templates/ResultTypeWithMerge.cs index 036e3ce0..89c3e739 100644 --- a/Source/FunicularSwitch.Generators.Templates/ResultTypeWithMerge.cs +++ b/Source/FunicularSwitch.Generators.Templates/ResultTypeWithMerge.cs @@ -1,198 +1,198 @@ -#nullable enable -using global::System.Linq; -//additional using directives - -namespace FunicularSwitch.Generators.Templates -{ -#pragma warning disable 1591 - public abstract partial class MyResult - { - //generated aggregate methods - } - - public static partial class MyResultExtension - { - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult> Map(this global::System.Collections.Generic.IEnumerable> results, - [global::JetBrains.Annotations.InstantHandle]global::System.Func map) => +#nullable enable +using global::System.Linq; +//additional using directives + +namespace FunicularSwitch.Generators.Templates +{ +#pragma warning disable 1591 + public abstract partial class MyResult + { + //generated aggregate methods + } + + public static partial class MyResultExtension + { + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult> Map(this global::System.Collections.Generic.IEnumerable> results, + [global::JetBrains.Annotations.InstantHandle] global::System.Func map) => results.Select(r => r.Map(map)).Aggregate(); - - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult> Bind(this global::System.Collections.Generic.IEnumerable> results, - [global::JetBrains.Annotations.InstantHandle]global::System.Func> bind) => + + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult> Bind(this global::System.Collections.Generic.IEnumerable> results, + [global::JetBrains.Annotations.InstantHandle] global::System.Func> bind) => results.Select(r => r.Bind(bind)).Aggregate(); - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult> Bind(this MyResult result, - [global::JetBrains.Annotations.InstantHandle]global::System.Func>> bindMany) => + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult> Bind(this MyResult result, + [global::JetBrains.Annotations.InstantHandle] global::System.Func>> bindMany) => result.Map(ok => bindMany(ok).Aggregate()).Flatten(); - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult Bind(this global::System.Collections.Generic.IEnumerable> results, - [global::JetBrains.Annotations.InstantHandle]global::System.Func, MyResult> bind) => + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult Bind(this global::System.Collections.Generic.IEnumerable> results, + [global::JetBrains.Annotations.InstantHandle] global::System.Func, MyResult> bind) => results.Aggregate().Bind(bind); - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult> Aggregate(this global::System.Collections.Generic.IEnumerable> results) - { - var isError = false; - MyError aggregated = default!; - var oks = new global::System.Collections.Generic.List(); - foreach (var result in results) - { - result.Match( - ok => oks.Add(ok), - error => - { - aggregated = !isError ? error : MergeErrors(aggregated, error); - isError = true; - } - ); - } - - return isError - ? MyResult.Error>(aggregated) - : MyResult.Ok>(oks); + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult> Aggregate(this global::System.Collections.Generic.IEnumerable> results) + { + var isError = false; + MyError aggregated = default!; + var oks = new global::System.Collections.Generic.List(); + foreach (var result in results) + { + result.Match( + ok => oks.Add(ok), + error => + { + aggregated = !isError ? error : MergeErrors(aggregated, error); + isError = true; + } + ); + } + + return isError + ? MyResult.Error>(aggregated) + : MyResult.Ok>(oks); } - - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task>> Aggregate( - this global::System.Threading.Tasks.Task>> results) - => (await results.ConfigureAwait(false)) + + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task>> Aggregate( + this global::System.Threading.Tasks.Task>> results) + => (await results.ConfigureAwait(false)) .Aggregate(); - - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task>> Aggregate( - this global::System.Collections.Generic.IEnumerable>> results) - => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) + + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task>> Aggregate( + this global::System.Collections.Generic.IEnumerable>> results) + => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) .Aggregate(); - - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task>> AggregateMany( - this global::System.Collections.Generic.IEnumerable>>> results) - => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) - .SelectMany(e => e) + + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task>> AggregateMany( + this global::System.Collections.Generic.IEnumerable>>> results) + => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) + .SelectMany(e => e) .Aggregate(); - //generated aggregate extension methods - - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult FirstOk(this global::System.Collections.Generic.IEnumerable> results, [global::JetBrains.Annotations.InstantHandle]global::System.Func onEmpty) - { - var errors = new global::System.Collections.Generic.List(); - foreach (var result in results) - { - if (result is MyResult.Error_ e) - errors.Add(e.Details); - else - return result; - } - - if (!errors.Any()) - errors.Add(onEmpty()); - - return MyResult.Error(MergeErrors(errors)); + //generated aggregate extension methods + + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult FirstOk(this global::System.Collections.Generic.IEnumerable> results, [global::JetBrains.Annotations.InstantHandle] global::System.Func onEmpty) + { + var errors = new global::System.Collections.Generic.List(); + foreach (var result in results) + { + if (result is MyResult.Error_ e) + errors.Add(e.Details); + else + return result; + } + + if (!errors.Any()) + errors.Add(onEmpty()); + + return MyResult.Error(MergeErrors(errors)); } - - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task>> Aggregate( - this global::System.Collections.Generic.IEnumerable>> results, - int maxDegreeOfParallelism) - => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) + + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task>> Aggregate( + this global::System.Collections.Generic.IEnumerable>> results, + int maxDegreeOfParallelism) + => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) .Aggregate(); - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task>> AggregateMany( - this global::System.Collections.Generic.IEnumerable>>> results, - int maxDegreeOfParallelism) - => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) - .SelectMany(e => e) - .Aggregate(); - - static async global::System.Threading.Tasks.Task SelectAsync(this global::System.Collections.Generic.IEnumerable items, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)]global::System.Func> selector, int maxDegreeOfParallelism) - { - using (var throttler = new global::System.Threading.SemaphoreSlim(maxDegreeOfParallelism, maxDegreeOfParallelism)) - { - return await global::System.Threading.Tasks.Task.WhenAll(items.Select(async item => - { - // ReSharper disable once AccessToDisposedClosure - await throttler.WaitAsync().ConfigureAwait(false); - try - { - return await selector(item).ConfigureAwait(false); - } - finally - { - // ReSharper disable once AccessToDisposedClosure - throttler.Release(); - } - })).ConfigureAwait(false); - } + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task>> AggregateMany( + this global::System.Collections.Generic.IEnumerable>>> results, + int maxDegreeOfParallelism) + => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) + .SelectMany(e => e) + .Aggregate(); + + static async global::System.Threading.Tasks.Task SelectAsync(this global::System.Collections.Generic.IEnumerable items, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> selector, int maxDegreeOfParallelism) + { + using (var throttler = new global::System.Threading.SemaphoreSlim(maxDegreeOfParallelism, maxDegreeOfParallelism)) + { + return await global::System.Threading.Tasks.Task.WhenAll(items.Select(async item => + { + // ReSharper disable once AccessToDisposedClosure + await throttler.WaitAsync().ConfigureAwait(false); + try + { + return await selector(item).ConfigureAwait(false); + } + finally + { + // ReSharper disable once AccessToDisposedClosure + throttler.Release(); + } + })).ConfigureAwait(false); + } } - - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult> AllOk(this global::System.Collections.Generic.IEnumerable candidates, [global::JetBrains.Annotations.InstantHandle]global::System.Func> validate) => - candidates - .Select(c => c.Validate(validate)) + + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult> AllOk(this global::System.Collections.Generic.IEnumerable candidates, [global::JetBrains.Annotations.InstantHandle] global::System.Func> validate) => + candidates + .Select(c => c.Validate(validate)) .Aggregate(); - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult> AllOk(this global::System.Collections.Generic.IEnumerable> candidates, - [global::JetBrains.Annotations.InstantHandle]global::System.Func> validate) => - candidates + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult> AllOk(this global::System.Collections.Generic.IEnumerable> candidates, + [global::JetBrains.Annotations.InstantHandle] global::System.Func> validate) => + candidates .Bind(items => items.AllOk(validate)); - - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult Validate(this MyResult item, [global::JetBrains.Annotations.InstantHandle]global::System.Func> validate) => item.Bind(i => i.Validate(validate)); - - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult Validate(this T item, [global::JetBrains.Annotations.InstantHandle]global::System.Func> validate) - { - try - { - var errors = validate(item).ToList(); - return errors.Count > 0 ? MyResult.Error(MergeErrors(errors)) : item; - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } + + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult Validate(this MyResult item, [global::JetBrains.Annotations.InstantHandle] global::System.Func> validate) => item.Bind(i => i.Validate(validate)); + + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult Validate(this T item, [global::JetBrains.Annotations.InstantHandle] global::System.Func> validate) + { + try + { + var errors = validate(item).ToList(); + return errors.Count > 0 ? MyResult.Error(MergeErrors(errors)) : item; + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } } - - [global::JetBrains.Annotations.MustUseReturnValue] - public static MyResult FirstOk(this global::System.Collections.Generic.IEnumerable candidates, [global::JetBrains.Annotations.InstantHandle]global::System.Func> validate, [global::JetBrains.Annotations.InstantHandle]global::System.Func onEmpty) => - candidates - .Select(r => r.Validate(validate)) - .FirstOk(onEmpty); - - #region helpers - - static MyError MergeErrors(global::System.Collections.Generic.IEnumerable errors) - { - var first = true; - MyError aggregated = default!; - foreach (var myError in errors) - { - if (first) - { - aggregated = myError; - first = false; - } - else - { - aggregated = MergeErrors(aggregated, myError); - } - } - - return aggregated; - } - - static MyError MergeErrors(MyError aggregated, MyError error) => aggregated.Merge__MemberOrExtensionMethod(error); - - #endregion - } -#pragma warning restore 1591 + + [global::JetBrains.Annotations.MustUseReturnValue] + public static MyResult FirstOk(this global::System.Collections.Generic.IEnumerable candidates, [global::JetBrains.Annotations.InstantHandle] global::System.Func> validate, [global::JetBrains.Annotations.InstantHandle] global::System.Func onEmpty) => + candidates + .Select(r => r.Validate(validate)) + .FirstOk(onEmpty); + + #region helpers + + static MyError MergeErrors(global::System.Collections.Generic.IEnumerable errors) + { + var first = true; + MyError aggregated = default!; + foreach (var myError in errors) + { + if (first) + { + aggregated = myError; + first = false; + } + else + { + aggregated = MergeErrors(aggregated, myError); + } + } + + return aggregated; + } + + static MyError MergeErrors(MyError aggregated, MyError error) => aggregated.Merge__MemberOrExtensionMethod(error); + + #endregion + } +#pragma warning restore 1591 } \ No newline at end of file diff --git a/Source/FunicularSwitch.Generators/Diagnostics.cs b/Source/FunicularSwitch.Generators/Diagnostics.cs index b9f616b0..0a8ffda5 100644 --- a/Source/FunicularSwitch.Generators/Diagnostics.cs +++ b/Source/FunicularSwitch.Generators/Diagnostics.cs @@ -11,14 +11,14 @@ public static Diagnostic InvalidResultTypeAttributeUsage(string message, Locatio Create(location, id: "FUN0001", title: "Invalid attribute usage", - messageFormat: $"{message} - Please use ResultType attribute with typeof expression like [ResultType(typeof(MyError))].", + messageFormat: $"{message} - Please use ResultType attribute with typeof expression like [ResultType(typeof(MyError))].", severity: DiagnosticSeverity.Error); public static Diagnostic InvalidMergeMethod(string message, Location location) => Create(location, "FUN0002", "Invalid merge method", - $"{message} - Please implement merge as member of error type or static extension method with signature TError -> TError -> TError.", + $"{message} - Please implement merge as member of error type or static extension method with signature TError -> TError -> TError.", DiagnosticSeverity.Error); public static Diagnostic AmbiguousMergeMethods(IEnumerable methodNames) => @@ -47,16 +47,16 @@ public static Diagnostic CaseIndexNotSet(string message, Location location) => messageFormat: message, severity: DiagnosticSeverity.Warning); public static Diagnostic UnionTypeIsNotAccessible(string message, Location location) => - Create(location, - id: "FUN0007", - title: "Union type is not accessible", - messageFormat: message, severity: DiagnosticSeverity.Error); + Create(location, + id: "FUN0007", + title: "Union type is not accessible", + messageFormat: message, severity: DiagnosticSeverity.Error); public static Diagnostic ResultTypeInGlobalNamespace(string message, Location location) => - Create(location, - id: "FUN0008", - title: "Result type in global namespace", - messageFormat: message, severity: DiagnosticSeverity.Error); + Create(location, + id: "FUN0008", + title: "Result type in global namespace", + messageFormat: message, severity: DiagnosticSeverity.Error); public static Diagnostic EnumTypeIsNotAccessible(string message, Location location) => Create(location, @@ -65,18 +65,18 @@ public static Diagnostic EnumTypeIsNotAccessible(string message, Location locati messageFormat: message, severity: DiagnosticSeverity.Error); public static Diagnostic InvalidExceptionToErrorMethod(string message, Location location) => - Create(location, - "FUN0010", - "Invalid exception to error method", - $"{message} - Please implement method for generic error type as static method with signature Exception -> TError.", - DiagnosticSeverity.Error); + Create(location, + "FUN0010", + "Invalid exception to error method", + $"{message} - Please implement method for generic error type as static method with signature Exception -> TError.", + DiagnosticSeverity.Error); public static Diagnostic InvalidUnionTypeAttributeUsage(string message, Location location) => - Create(location, - id: "FUN0011", - title: "Invalid attribute usage", - messageFormat: $"{message} - Valid UnionType attribute usages: [UnionType], [UnionType(StaticFactoryMethods = false)], [UnionType(CaseOder = CaseOrder.AsDeclared, StaticFactoryMethods = true)].", - severity: DiagnosticSeverity.Error); + Create(location, + id: "FUN0011", + title: "Invalid attribute usage", + messageFormat: $"{message} - Valid UnionType attribute usages: [UnionType], [UnionType(StaticFactoryMethods = false)], [UnionType(CaseOder = CaseOrder.AsDeclared, StaticFactoryMethods = true)].", + severity: DiagnosticSeverity.Error); // TODO: this should be in another class/category with own id range public static Diagnostic MonadTransformerNoAttribute(INamedTypeSymbol type) => @@ -85,35 +85,35 @@ public static Diagnostic MonadTransformerNoAttribute(INamedTypeSymbol type) => title: "Missing monad transformer attribute", messageFormat: $"{type} is missing the MonadTransformer attribute - Add MonadTransformer attribute to type", severity: DiagnosticSeverity.Error); - + public static Diagnostic MissingReturnMethod(INamedTypeSymbol type) => Create(type.Locations.FirstOrDefault(), id: "FUN0013", title: "Missing return method", messageFormat: $"{type} is missing a return method - Add return method (A -> M) to type", severity: DiagnosticSeverity.Error); - + public static Diagnostic MissingBindMethod(INamedTypeSymbol type) => Create(type.Locations.FirstOrDefault(), id: "FUN0014", title: "Missing bind method", messageFormat: $"{type} is missing a bind method - Add bind method (M -> (A -> M) -> M) to type", severity: DiagnosticSeverity.Error); - + public static Diagnostic MissingBindTMethod(INamedTypeSymbol type) => Create(type.Locations.FirstOrDefault(), id: "FUN0015", title: "Missing transformer bind method", messageFormat: $"{type} is missing a transformer bind method - Add BindT method (Monad> -> (A -> Monad>) -> Monad>) to type", severity: DiagnosticSeverity.Error); - + public static Diagnostic ExperimentalGenerator(string name, Location location) => Create(location, id: "FUN0016", title: "Generator is experimental", messageFormat: $"Generator for {name} is considered experimental and might break", severity: DiagnosticSeverity.Info); - + static Diagnostic Create(Location? location, string id, string title, string messageFormat, DiagnosticSeverity severity = DiagnosticSeverity.Warning) => Diagnostic.Create( new( diff --git a/Source/FunicularSwitch.Generators/EnumType/EnumCaseOrder.cs b/Source/FunicularSwitch.Generators/EnumType/EnumCaseOrder.cs index 152ec655..00fe88be 100644 --- a/Source/FunicularSwitch.Generators/EnumType/EnumCaseOrder.cs +++ b/Source/FunicularSwitch.Generators/EnumType/EnumCaseOrder.cs @@ -2,6 +2,6 @@ enum EnumCaseOrder { - Alphabetic, - AsDeclared + Alphabetic, + AsDeclared } \ No newline at end of file diff --git a/Source/FunicularSwitch.Generators/EnumType/Generator.cs b/Source/FunicularSwitch.Generators/EnumType/Generator.cs index cc147e36..9e626206 100644 --- a/Source/FunicularSwitch.Generators/EnumType/Generator.cs +++ b/Source/FunicularSwitch.Generators/EnumType/Generator.cs @@ -7,14 +7,14 @@ namespace FunicularSwitch.Generators.EnumType; static class Generator { - private const string VoidMatchMethodName = "Switch"; - private const string MatchMethodName = "Match"; - - public static (string filename, string source) Emit( - EnumTypeSchema enumTypeSchema, - bool hasJetBrainsAnnotationsReference, - Action reportDiagnostic, - CancellationToken cancellationToken) + private const string VoidMatchMethodName = "Switch"; + private const string MatchMethodName = "Match"; + + public static (string filename, string source) Emit( + EnumTypeSchema enumTypeSchema, + bool hasJetBrainsAnnotationsReference, + Action reportDiagnostic, + CancellationToken cancellationToken) { var builder = new CSharpBuilder(); builder.WriteLine("#pragma warning disable 1591"); @@ -22,23 +22,23 @@ public static (string filename, string source) Emit( //builder.WriteLine($"//Generator runs: {RunCount.Increase(enumTypeSchema.FullTypeName)}"); void BlankLine() { - builder.WriteLine(""); + builder.WriteLine(""); } using (enumTypeSchema.Namespace != null ? builder.Namespace(enumTypeSchema.Namespace) : null) { using (builder.StaticPartialClass($"{enumTypeSchema.TypeName.Replace(".", "_")}MatchExtension", enumTypeSchema.IsInternal ? "internal" : "public")) { - var thisTaskParameter = ThisParameter(enumTypeSchema, $"global::System.Threading.Tasks.Task<{enumTypeSchema.FullTypeName}>"); - var caseParameters = enumTypeSchema.Cases.Select(c => c.ParameterName).ToSeparatedString(); - void WriteBodyForTaskExtension(string matchMethodName) => builder.WriteLine($"(await {thisTaskParameter.Name}.ConfigureAwait(false)).{matchMethodName}({caseParameters});"); - void WriteBodyForAsyncTaskExtension(string matchMethodName) => builder.WriteLine($"await (await {thisTaskParameter.Name}.ConfigureAwait(false)).{matchMethodName}({caseParameters}).ConfigureAwait(false);"); + var thisTaskParameter = ThisParameter(enumTypeSchema, $"global::System.Threading.Tasks.Task<{enumTypeSchema.FullTypeName}>"); + var caseParameters = enumTypeSchema.Cases.Select(c => c.ParameterName).ToSeparatedString(); + void WriteBodyForTaskExtension(string matchMethodName) => builder.WriteLine($"(await {thisTaskParameter.Name}.ConfigureAwait(false)).{matchMethodName}({caseParameters});"); + void WriteBodyForAsyncTaskExtension(string matchMethodName) => builder.WriteLine($"await (await {thisTaskParameter.Name}.ConfigureAwait(false)).{matchMethodName}({caseParameters}).ConfigureAwait(false);"); GenerateMatchMethod(builder, enumTypeSchema, "T", isAsync: false, hasJetBrainsAnnotationsReference); BlankLine(); GenerateMatchMethod(builder, enumTypeSchema, "T", isAsync: true, hasJetBrainsAnnotationsReference); BlankLine(); - + WriteMatchSignature(builder, enumTypeSchema, thisTaskParameter, "T", isAsync: true, hasJetBrainsAnnotationsReference, "T"); WriteBodyForTaskExtension(MatchMethodName); BlankLine(); @@ -71,14 +71,14 @@ static void GenerateMatchMethod(CSharpBuilder builder, EnumTypeSchema enumTypeSc builder.WriteLine($"{thisParameterName} switch"); using (builder.ScopeWithSemicolon()) { - foreach (var c in enumTypeSchema.Cases) - { - var call = $"{c.ParameterName}()"; - if (isAsync) - { - call = $"await {call}.ConfigureAwait(false)"; - } - builder.WriteLine($"{c.FullCaseName} => {call},"); + foreach (var c in enumTypeSchema.Cases) + { + var call = $"{c.ParameterName}()"; + if (isAsync) + { + call = $"await {call}.ConfigureAwait(false)"; + } + builder.WriteLine($"{c.FullCaseName} => {call},"); } builder.WriteLine( @@ -88,57 +88,57 @@ static void GenerateMatchMethod(CSharpBuilder builder, EnumTypeSchema enumTypeSc static void GenerateSwitchMethod(CSharpBuilder builder, EnumTypeSchema enumTypeSchema, bool isAsync, bool hasJetBrainsAnnotationsReference) { - var thisParameterType = enumTypeSchema.FullTypeName; - var thisParameter = ThisParameter(enumTypeSchema, thisParameterType); - var thisParameterName = thisParameter.Name; - WriteSwitchSignature(builder, enumTypeSchema, thisParameter, isAsync, hasJetBrainsAnnotationsReference); - using (builder.Scope()) - { - builder.WriteLine($"switch ({thisParameterName})"); - using (builder.Scope()) - { - foreach (var c in enumTypeSchema.Cases) - { - builder.WriteLine($"case {c.FullCaseName}:"); - using (builder.Indent()) - { - var call = $"{c.ParameterName}()"; - if (isAsync) - call = $"await {call}.ConfigureAwait(false)"; - builder.WriteLine($"{call};"); - builder.WriteLine("break;"); - } - } - - builder.WriteLine("default:"); - using (builder.Indent()) - { - builder.WriteLine($"throw new global::System.ArgumentException($\"Unknown enum value from {enumTypeSchema.FullTypeName}: {{{thisParameterName}.GetType().Name}}\");"); - } - } - } + var thisParameterType = enumTypeSchema.FullTypeName; + var thisParameter = ThisParameter(enumTypeSchema, thisParameterType); + var thisParameterName = thisParameter.Name; + WriteSwitchSignature(builder, enumTypeSchema, thisParameter, isAsync, hasJetBrainsAnnotationsReference); + using (builder.Scope()) + { + builder.WriteLine($"switch ({thisParameterName})"); + using (builder.Scope()) + { + foreach (var c in enumTypeSchema.Cases) + { + builder.WriteLine($"case {c.FullCaseName}:"); + using (builder.Indent()) + { + var call = $"{c.ParameterName}()"; + if (isAsync) + call = $"await {call}.ConfigureAwait(false)"; + builder.WriteLine($"{call};"); + builder.WriteLine("break;"); + } + } + + builder.WriteLine("default:"); + using (builder.Indent()) + { + builder.WriteLine($"throw new global::System.ArgumentException($\"Unknown enum value from {enumTypeSchema.FullTypeName}: {{{thisParameterName}.GetType().Name}}\");"); + } + } + } } static Parameter ThisParameter(EnumTypeSchema enumTypeSchema, string thisParameterType) => new($"this {thisParameterType}", enumTypeSchema.TypeName.ToParameterName()); static void WriteMatchSignature( - CSharpBuilder builder, - EnumTypeSchema enumTypeSchema, + CSharpBuilder builder, + EnumTypeSchema enumTypeSchema, Parameter thisParameter, - string returnType, - bool isAsync, - bool hasJetBrainsAnnotationsReference, - string? handlerReturnType = null) + string returnType, + bool isAsync, + bool hasJetBrainsAnnotationsReference, + string? handlerReturnType = null) { - var instantHandle = hasJetBrainsAnnotationsReference ? - isAsync ? Constants.Attributes.InstantHandleRequireAwait : Constants.Attributes.InstantHandle - : ""; - var modifiers = "public static"; - if (isAsync) - { - returnType = $"global::System.Threading.Tasks.Task<{returnType}>"; - modifiers = $"{modifiers} async"; - } + var instantHandle = hasJetBrainsAnnotationsReference ? + isAsync ? Constants.Attributes.InstantHandleRequireAwait : Constants.Attributes.InstantHandle + : ""; + var modifiers = "public static"; + if (isAsync) + { + returnType = $"global::System.Threading.Tasks.Task<{returnType}>"; + modifiers = $"{modifiers} async"; + } handlerReturnType ??= returnType; var handlerParameters = enumTypeSchema.Cases .Select(c => new Parameter($"{instantHandle}global::System.Func<{handlerReturnType}>", c.ParameterName)); @@ -153,27 +153,27 @@ static void WriteMatchSignature( } static void WriteSwitchSignature( - CSharpBuilder builder, - EnumTypeSchema enumTypeSchema, - Parameter thisParameter, - bool isAsync, - bool hasJetBrainsAnnotationsReference, - bool? asyncReturn = null, - bool lambda = false) + CSharpBuilder builder, + EnumTypeSchema enumTypeSchema, + Parameter thisParameter, + bool isAsync, + bool hasJetBrainsAnnotationsReference, + bool? asyncReturn = null, + bool lambda = false) { - var instantHandle = hasJetBrainsAnnotationsReference ? - isAsync ? Constants.Attributes.InstantHandleRequireAwait : Constants.Attributes.InstantHandle - : ""; - var returnType = asyncReturn ?? isAsync ? "async global::System.Threading.Tasks.Task" : "void"; + var instantHandle = hasJetBrainsAnnotationsReference ? + isAsync ? Constants.Attributes.InstantHandleRequireAwait : Constants.Attributes.InstantHandle + : ""; + var returnType = asyncReturn ?? isAsync ? "async global::System.Threading.Tasks.Task" : "void"; var handlerParameters = enumTypeSchema.Cases - .Select(c => new Parameter(isAsync ? $"{instantHandle}global::System.Func" : $"{instantHandle}global::System.Action", c.ParameterName)); + .Select(c => new Parameter(isAsync ? $"{instantHandle}global::System.Func" : $"{instantHandle}global::System.Action", c.ParameterName)); builder.WriteLine(Constants.Attributes.DebuggerStepThrough); builder.WriteMethodSignature( - modifiers: "public static", - returnType: returnType, - methodName: VoidMatchMethodName, parameters: new[] { thisParameter }.Concat(handlerParameters), + modifiers: "public static", + returnType: returnType, + methodName: VoidMatchMethodName, parameters: new[] { thisParameter }.Concat(handlerParameters), typeConstraints: [], - lambda: lambda); + lambda: lambda); } } \ No newline at end of file diff --git a/Source/FunicularSwitch.Generators/EnumType/Parser.cs b/Source/FunicularSwitch.Generators/EnumType/Parser.cs index a00debf8..d15095fb 100644 --- a/Source/FunicularSwitch.Generators/EnumType/Parser.cs +++ b/Source/FunicularSwitch.Generators/EnumType/Parser.cs @@ -66,8 +66,8 @@ public static EnumTypeSchema ToEnumTypeSchema(this EnumSymbolInfo symbolInfo) ? ExtensionAccessibility.Internal : symbolInfo.ExtensionAccessibility; - return new(fullNamespace, - enumSymbol.FullTypeName(), + return new(fullNamespace, + enumSymbol.FullTypeName(), fullTypeNameWithNamespace, OrderEnumCases(derivedTypes, symbolInfo.CaseOrder).ToImmutableArray(), extensionAccessibility == ExtensionAccessibility.Internal, diff --git a/Source/FunicularSwitch.Generators/EnumTypeGenerator.cs b/Source/FunicularSwitch.Generators/EnumTypeGenerator.cs index 1e5e0b20..a1385730 100644 --- a/Source/FunicularSwitch.Generators/EnumTypeGenerator.cs +++ b/Source/FunicularSwitch.Generators/EnumTypeGenerator.cs @@ -32,7 +32,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) .SelectMany(l => l) .Concat(t.Left.Right.SelectMany(l => l)) .Concat(t.Right.SelectMany(l => l))); - + var referencesJetBrainsAnnotationsAssembly = context.CompilationProvider .SelectMany((c, _) => c.SourceModule.ReferencedAssemblySymbols) .Where(a => a.Name == "JetBrains.Annotations") @@ -124,7 +124,7 @@ static IEnumerable GetSemanticTargetForGeneration(GeneratorAttri static IEnumerable GetSymbolInfosForExtendEnumTypeAttribute(AttributeData extendEnumTypesAttribute) { - if (extendEnumTypesAttribute.ConstructorArguments[0].Value + if (extendEnumTypesAttribute.ConstructorArguments[0].Value is not INamedTypeSymbol typeSymbol || typeSymbol.EnumUnderlyingType == null) yield break; @@ -145,7 +145,7 @@ static IEnumerable GetSymbolInfosForExtendEnumTypesAttribute(Att { var attributeSymbol = extendEnumTypesAttribute.AttributeClass!; - var enumFromAssembly = extendEnumTypesAttribute.ConstructorArguments.FirstOrDefault().Value + var enumFromAssembly = extendEnumTypesAttribute.ConstructorArguments.FirstOrDefault().Value is INamedTypeSymbol typeFromAssembly ? typeFromAssembly.ContainingAssembly : attributeSymbol.ContainingAssembly; diff --git a/Source/FunicularSwitch.Generators/ExtensionAccessibility.cs b/Source/FunicularSwitch.Generators/ExtensionAccessibility.cs index a52cd9d1..5de7eb41 100644 --- a/Source/FunicularSwitch.Generators/ExtensionAccessibility.cs +++ b/Source/FunicularSwitch.Generators/ExtensionAccessibility.cs @@ -2,6 +2,6 @@ enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } \ No newline at end of file diff --git a/Source/FunicularSwitch.Generators/Generation/Constants.cs b/Source/FunicularSwitch.Generators/Generation/Constants.cs index c3958762..7f16e513 100644 --- a/Source/FunicularSwitch.Generators/Generation/Constants.cs +++ b/Source/FunicularSwitch.Generators/Generation/Constants.cs @@ -2,19 +2,19 @@ namespace FunicularSwitch.Generators.Generation; internal static class Constants { - public const string ExperimentalGeneratorDocs = """ + public const string ExperimentalGeneratorDocs = """ This generator is still considered experimental and might break. Please open any issues you may find in the GitHub repo. - """; + """; - public static class Attributes - { - public const string InstantHandle = "[global::JetBrains.Annotations.InstantHandle]"; - public const string InstantHandleRequireAwait = "[global::JetBrains.Annotations.InstantHandle(RequireAwait = true)]"; - public const string MustUseReturnValue = "[global::JetBrains.Annotations.MustUseReturnValue]"; - public const string DebuggerStepThrough = "[global::System.Diagnostics.DebuggerStepThrough]"; - public const string Pure = "[global::System.Diagnostics.Contracts.PureAttribute]"; - } + public static class Attributes + { + public const string InstantHandle = "[global::JetBrains.Annotations.InstantHandle]"; + public const string InstantHandleRequireAwait = "[global::JetBrains.Annotations.InstantHandle(RequireAwait = true)]"; + public const string MustUseReturnValue = "[global::JetBrains.Annotations.MustUseReturnValue]"; + public const string DebuggerStepThrough = "[global::System.Diagnostics.DebuggerStepThrough]"; + public const string Pure = "[global::System.Diagnostics.Contracts.PureAttribute]"; + } } diff --git a/Source/FunicularSwitch.Generators/Generation/Indent.cs b/Source/FunicularSwitch.Generators/Generation/Indent.cs index 90d354b9..28ac3105 100644 --- a/Source/FunicularSwitch.Generators/Generation/Indent.cs +++ b/Source/FunicularSwitch.Generators/Generation/Indent.cs @@ -227,7 +227,7 @@ public static void Const(this CSharpBuilder tt, string typeName, string name, ob public static string ToParameterName(this string name) { - var typeNameWithoutOuter = name.Split('.').Last(); + var typeNameWithoutOuter = name.Split('.').Last(); var parameterName = typeNameWithoutOuter.FirstToLower(); return PrefixAtIfKeyword(parameterName); } @@ -247,6 +247,4 @@ public static string TrimBaseTypeName(this string value, string baseTypeName) } public static string ToMatchExtensionFilename(this string fullTypeName, EquatableArray typeParameters) => $"{fullTypeName.Replace(".", "")}{RoslynExtensions.FormatTypeParameterForFileName(typeParameters)}MatchExtension.g.cs"; - - } \ No newline at end of file diff --git a/Source/FunicularSwitch.Generators/Generation/MonadMethods.cs b/Source/FunicularSwitch.Generators/Generation/MonadMethods.cs index d1ac4b1a..8f276c9c 100644 --- a/Source/FunicularSwitch.Generators/Generation/MonadMethods.cs +++ b/Source/FunicularSwitch.Generators/Generation/MonadMethods.cs @@ -22,7 +22,7 @@ public static MethodGenerationInfo Create( .Concat(typeParameters).ToList(); return new MethodGenerationInfo( - genericTypeName([..extraTypeParameters, returnTypeParameter]), + genericTypeName([.. extraTypeParameters, returnTypeParameter]), allTypeParameters, parameters(extraTypeParameters), name, @@ -117,7 +117,7 @@ IEnumerable ForFnType(Func, TypeIn new ParameterGenerationInfo(Types.Func("A", fnReturnType(t)), "fn"), ], name, - t => $"{chainedMonad.BindMethod.Invoke([..t, "A", "B"], [$"(({chainedMonad.GenericTypeName([..t, "A"])}){p})", $"{Constants.Attributes.DebuggerStepThrough}(a) => fn(a)"])}" + t => $"{chainedMonad.BindMethod.Invoke([.. t, "A", "B"], [$"(({chainedMonad.GenericTypeName([.. t, "A"])}){p})", $"{Constants.Attributes.DebuggerStepThrough}(a) => fn(a)"])}" )); } @@ -142,7 +142,7 @@ IEnumerable ForFnType(Func, TypeIn new ParameterGenerationInfo(Types.Func("A", "B", "C"), "selector"), ], name, - t => $"{p}.SelectMany({Constants.Attributes.DebuggerStepThrough}(a) => (({genericTypeName([..t, "B"])})fn(a)).Map({Constants.Attributes.DebuggerStepThrough}(b) => selector(a, b)))" + t => $"{p}.SelectMany({Constants.Attributes.DebuggerStepThrough}(a) => (({genericTypeName([.. t, "B"])})fn(a)).Map({Constants.Attributes.DebuggerStepThrough}(b) => selector(a, b)))" )); } @@ -167,7 +167,7 @@ private static IEnumerable Map(string name, ConstructType new ParameterGenerationInfo(Types.Func("A", "B"), "fn"), ], name, - t => $"{p}.{monad.BindMethod.Name}({Constants.Attributes.DebuggerStepThrough}(a) => {monad.ReturnMethod.Invoke([..t, "B"], ["fn(a)"])})" + t => $"{p}.{monad.BindMethod.Name}({Constants.Attributes.DebuggerStepThrough}(a) => {monad.ReturnMethod.Invoke([.. t, "B"], ["fn(a)"])})" )); private static IEnumerable Return(ConstructType genericTypeName, MonadInfo chainedMonad) => @@ -178,6 +178,6 @@ private static IEnumerable Return(ConstructType genericTyp ["A"], _ => [new ParameterGenerationInfo("A", "a")], chainedMonad.ReturnMethod.Name, - t => chainedMonad.ReturnMethod.Invoke([..t, "A"], [p]) + t => chainedMonad.ReturnMethod.Invoke([.. t, "A"], [p]) )); } diff --git a/Source/FunicularSwitch.Generators/IsExternalInit.cs b/Source/FunicularSwitch.Generators/IsExternalInit.cs index e44d655d..273677de 100644 --- a/Source/FunicularSwitch.Generators/IsExternalInit.cs +++ b/Source/FunicularSwitch.Generators/IsExternalInit.cs @@ -4,4 +4,4 @@ namespace System.Runtime.CompilerServices; [EditorBrowsable(EditorBrowsableState.Never)] -internal class IsExternalInit{} \ No newline at end of file +internal class IsExternalInit { } \ No newline at end of file diff --git a/Source/FunicularSwitch.Generators/Parsing/MonadParser.cs b/Source/FunicularSwitch.Generators/Parsing/MonadParser.cs index f3255d95..ab2b7b40 100644 --- a/Source/FunicularSwitch.Generators/Parsing/MonadParser.cs +++ b/Source/FunicularSwitch.Generators/Parsing/MonadParser.cs @@ -121,7 +121,7 @@ bool IsReturnMethod(IMethodSymbol method) { if (method.TypeParameters.Length != 0) return false; if (method.Parameters.Length != 1) return false; - if (method.ReturnType is not INamedTypeSymbol {IsGenericType: true} genericReturnType) return false; + if (method.ReturnType is not INamedTypeSymbol { IsGenericType: true } genericReturnType) return false; if (!SymbolEqualityComparer.IncludeNullability.Equals(genericReturnType.ConstructUnboundGenericType(), genericMonadType.ConstructUnboundGenericType())) return false; if (genericReturnType.TypeArguments[^1].Name != genericMonadType.OriginalDefinition.TypeParameters[^1].Name) return false; return true; @@ -131,7 +131,7 @@ bool IsBindMethod(IMethodSymbol method) { if (method.TypeParameters.Length != 1) return false; if (method.Parameters.Length != 1) return false; - if (method.ReturnType is not INamedTypeSymbol {IsGenericType: true} genericReturnType) return false; + if (method.ReturnType is not INamedTypeSymbol { IsGenericType: true } genericReturnType) return false; if (!SymbolEqualityComparer.IncludeNullability.Equals(genericReturnType.ConstructUnboundGenericType(), genericMonadType.ConstructUnboundGenericType())) return false; if (genericReturnType.TypeArguments[^1].Name == genericMonadType.OriginalDefinition.TypeParameters[^1].Name) return false; return true; @@ -164,7 +164,7 @@ private static GenerationResult ResolveMonadDataFromStaticMonadType(I if (returnMethod is null) return new DiagnosticInfo(Diagnostics.MissingReturnMethod(staticMonadType)); - var genericMonadType = ((INamedTypeSymbol) returnMethod.ReturnType).ConstructUnboundGenericType(); + var genericMonadType = ((INamedTypeSymbol)returnMethod.ReturnType).ConstructUnboundGenericType(); var bindMethod = staticMonadType .GetMembers() @@ -186,7 +186,7 @@ bool IsStaticReturnMethod(IMethodSymbol method) bool IsGenericMonadType(ITypeSymbol type, ImmutableArray typeParameters) { - if (type is not INamedTypeSymbol {IsGenericType: true} genericType) return false; + if (type is not INamedTypeSymbol { IsGenericType: true } genericType) return false; if (!genericType.TypeArguments.SequenceEqual(typeParameters)) return false; return true; } @@ -195,7 +195,7 @@ static bool IsStaticBindMethod(INamedTypeSymbol genericMonadType, IMethodSymbol { if (method.TypeParameters.Length != genericMonadType.Arity + 1) return false; if (method.Parameters.Length != 2) return false; - if (method.ReturnType is not INamedTypeSymbol {IsGenericType: true} genericReturnType) return false; + if (method.ReturnType is not INamedTypeSymbol { IsGenericType: true } genericReturnType) return false; if (!SymbolEqualityComparer.IncludeNullability.Equals(genericReturnType.ConstructUnboundGenericType(), genericMonadType)) return false; if (genericReturnType.TypeArguments[^1].Name != method.TypeParameters[^1].Name) return false; return true; diff --git a/Source/FunicularSwitch.Generators/ResultType/Generator.cs b/Source/FunicularSwitch.Generators/ResultType/Generator.cs index 28af345c..fea64d51 100644 --- a/Source/FunicularSwitch.Generators/ResultType/Generator.cs +++ b/Source/FunicularSwitch.Generators/ResultType/Generator.cs @@ -84,8 +84,8 @@ string Replace(string code, IReadOnlyCollection additionalNamespaces, st {genericResultType}.Ok, {genericResultType}.Error); """); - - code = code.Replace("//createGenericResultConversionExtensions", + + code = code.Replace("//createGenericResultConversionExtensions", $""" public static {resultTypeName} To{resultTypeName}( @@ -147,7 +147,7 @@ string Replace(string code, IReadOnlyCollection additionalNamespaces, st static string GenerateAggregateExtensionMethods(int maxParameterCount, bool isValueType, bool referencesJetBrainsAnnotations) => Generate(maxParameterCount, i => MakeAggregateExtensionMethod(i, isValueType, referencesJetBrainsAnnotations)); static string GenerateAggregateMethods(int maxParameterCount, bool referencesJetBrainsAnnotations) => Generate(maxParameterCount, - i => GenerateAggregateMethod(i, referencesJetBrainsAnnotations)); + i => GenerateAggregateMethod(i, referencesJetBrainsAnnotations)); static string Generate(int maxParameterCount, Func generateMethods) => @@ -214,10 +214,10 @@ public static string GenerateAggregateMethod(int typeParameterCount, bool refere var typeParameters = Expand(i => $"T{i}"); var parameterDeclarations = Expand(i => $"MyResult r{i}"); var taskParameterDeclarations = Expand(i => $"global::System.Threading.Tasks.Task> r{i}"); - var parameters = Expand(i => $"r{i}"); + var parameters = Expand(i => $"r{i}"); var mustUseReturnValueAttribute = referencesJetBrainsAnnotations ? Constants.Attributes.MustUseReturnValue : ""; - return $@" + return $@" {mustUseReturnValueAttribute} public static MyResult<({typeParameters})> Aggregate<{typeParameters}>({parameterDeclarations}) => MyResultExtension.Aggregate({parameters}); diff --git a/Source/FunicularSwitch.Generators/ResultType/Parser.cs b/Source/FunicularSwitch.Generators/ResultType/Parser.cs index a65f5a98..e2f4d44c 100644 --- a/Source/FunicularSwitch.Generators/ResultType/Parser.cs +++ b/Source/FunicularSwitch.Generators/ResultType/Parser.cs @@ -27,7 +27,7 @@ public static GenerationResult GetResultTypeSchema( }); } - public static bool ReferencesFunicularSwitchGeneric(Compilation compilation) => + public static bool ReferencesFunicularSwitchGeneric(Compilation compilation) => compilation.ReferencedAssemblyNames.Any(r => r.Name.Equals("FunicularSwitch.Generic", StringComparison.InvariantCultureIgnoreCase)); public static Dictionary FindMergeMethods(Compilation compilation, Action reportDiagnostic) diff --git a/Source/FunicularSwitch.Generators/ResultType/ResultTypeSchema.cs b/Source/FunicularSwitch.Generators/ResultType/ResultTypeSchema.cs index fd864a3b..997cce70 100644 --- a/Source/FunicularSwitch.Generators/ResultType/ResultTypeSchema.cs +++ b/Source/FunicularSwitch.Generators/ResultType/ResultTypeSchema.cs @@ -9,7 +9,7 @@ sealed class ResultTypeSchema( ClassDeclarationSyntax resultType, INamedTypeSymbol? errorType) { - public SymbolWrapper? ErrorType { get; } = errorType != null ? new (errorType) : null; + public SymbolWrapper? ErrorType { get; } = errorType != null ? new(errorType) : null; public LocationInfo? ResultTypeLocation { get; } = LocationInfo.CreateFrom(resultType.GetLocation()); public bool IsInternal { get; } = !resultType.Modifiers.HasModifier(SyntaxKind.PublicKeyword); public QualifiedTypeName ResultTypeName { get; } = resultType.QualifiedName(); diff --git a/Source/FunicularSwitch.Generators/ResultTypeGenerator.cs b/Source/FunicularSwitch.Generators/ResultTypeGenerator.cs index 7f907c48..e51df154 100644 --- a/Source/FunicularSwitch.Generators/ResultTypeGenerator.cs +++ b/Source/FunicularSwitch.Generators/ResultTypeGenerator.cs @@ -109,7 +109,7 @@ bool referencesFunicularSwitchGeneric m.FullErrorTypeName == errorTypeSymbol.FullNameWithNamespace), exceptionToErrorMethod: resultTypeMethods.Value.exceptionToErrorMethods.FirstOrDefault(e => e.ErrorTypeName == errorTypeSymbol.FullNameWithNamespace), - reportDiagnostic: context.ReportDiagnostic, + reportDiagnostic: context.ReportDiagnostic, referencesFunicularSwitchGeneric: resultTypeMethods.Value.referencesFunicularSwitchGeneric, referencesJetBrainsAnnotations: hasJetBrainsAnnotationsReference, cancellationToken: context.CancellationToken); diff --git a/Source/FunicularSwitch.Generators/SymbolExtension.cs b/Source/FunicularSwitch.Generators/SymbolExtension.cs index 05eb3604..51fcfdbe 100644 --- a/Source/FunicularSwitch.Generators/SymbolExtension.cs +++ b/Source/FunicularSwitch.Generators/SymbolExtension.cs @@ -4,18 +4,18 @@ namespace FunicularSwitch.Generators; static class SymbolExtension { - public static Accessibility GetActualAccessibility(this INamedTypeSymbol t) - { - var actualAccessibility = Accessibility.Public; - var currentSymbol = t; + public static Accessibility GetActualAccessibility(this INamedTypeSymbol t) + { + var actualAccessibility = Accessibility.Public; + var currentSymbol = t; - do - { - if (actualAccessibility > currentSymbol.DeclaredAccessibility) - actualAccessibility = currentSymbol.DeclaredAccessibility; - currentSymbol = currentSymbol.ContainingType; - } while (currentSymbol != null); + do + { + if (actualAccessibility > currentSymbol.DeclaredAccessibility) + actualAccessibility = currentSymbol.DeclaredAccessibility; + currentSymbol = currentSymbol.ContainingType; + } while (currentSymbol != null); - return actualAccessibility; - } + return actualAccessibility; + } } \ No newline at end of file diff --git a/Source/FunicularSwitch.Generators/Transformer/Parser.cs b/Source/FunicularSwitch.Generators/Transformer/Parser.cs index a41619af..b290d6ac 100644 --- a/Source/FunicularSwitch.Generators/Transformer/Parser.cs +++ b/Source/FunicularSwitch.Generators/Transformer/Parser.cs @@ -17,16 +17,16 @@ public static GenerationResult GetTransformedMonadSchema( return from outerMonadData in MonadParser.ResolveMonadDataFromMonadType(outerMonadType, cancellationToken) - let transformerTypes = new[] {transformMonadAttribute.TransformerType} + let transformerTypes = new[] { transformMonadAttribute.TransformerType } .Concat(transformMonadAttribute.ExtraTransformerTypes) .ToList() from chainedMonads in transformerTypes .Aggregate( - (GenerationResult>) new[] {outerMonadData}, + (GenerationResult>)new[] { outerMonadData }, (acc, cur) => acc.Bind(acc_ => TransformMonad(acc_.Last(), cur, cancellationToken).Map>(transformMonad => - [..acc_, transformMonad]))) + [.. acc_, transformMonad]))) let chainedMonad = chainedMonads.Last() let implementations = chainedMonads .Take(chainedMonads.Count - 1) diff --git a/Source/FunicularSwitch.Generators/Transformer/TypeInfo.cs b/Source/FunicularSwitch.Generators/Transformer/TypeInfo.cs index 5b0cd627..6b0e7872 100644 --- a/Source/FunicularSwitch.Generators/Transformer/TypeInfo.cs +++ b/Source/FunicularSwitch.Generators/Transformer/TypeInfo.cs @@ -11,7 +11,7 @@ internal record TypeInfo( bool IsParameter, EquatableArray Parameters) { - public ConstructType Construct => parameters => this with {Parameters = parameters.ToImmutableArray()}; + public ConstructType Construct => parameters => this with { Parameters = parameters.ToImmutableArray() }; public static TypeInfo From(INamedTypeSymbol type) => new( type.FullTypeNameWithNamespace(), diff --git a/Source/FunicularSwitch.Generators/TransformerGenerator.cs b/Source/FunicularSwitch.Generators/TransformerGenerator.cs index c1da1cd9..a5c89e48 100644 --- a/Source/FunicularSwitch.Generators/TransformerGenerator.cs +++ b/Source/FunicularSwitch.Generators/TransformerGenerator.cs @@ -34,7 +34,7 @@ private static void Execute(SourceProductionContext context, GenerationResult Parse(GeneratorAttributeSyntaxContext syntaxContext, IReadOnlyList attributes, CancellationToken token) => Parser.GetTransformedMonadSchema( - (INamedTypeSymbol) syntaxContext.TargetSymbol, + (INamedTypeSymbol)syntaxContext.TargetSymbol, attributes.First(), token) .AddDiagnostics(new DiagnosticInfo(Diagnostics.ExperimentalGenerator(nameof(TransformMonadAttribute), syntaxContext.TargetNode.GetLocation()))); diff --git a/Source/FunicularSwitch.Generators/UnionType/Generator.cs b/Source/FunicularSwitch.Generators/UnionType/Generator.cs index 0ed6d5c4..442532a2 100644 --- a/Source/FunicularSwitch.Generators/UnionType/Generator.cs +++ b/Source/FunicularSwitch.Generators/UnionType/Generator.cs @@ -169,7 +169,7 @@ static void WritePartialWithStaticFactories(UnionTypeSchema unionTypeSchema, CSh else { builder.Content.Append(";"); - builder.Content.AppendLine(); + builder.Content.AppendLine(); } } } diff --git a/Source/FunicularSwitch.Generators/UnionType/UnionTypeSchema.cs b/Source/FunicularSwitch.Generators/UnionType/UnionTypeSchema.cs index dd0aff58..83d66774 100644 --- a/Source/FunicularSwitch.Generators/UnionType/UnionTypeSchema.cs +++ b/Source/FunicularSwitch.Generators/UnionType/UnionTypeSchema.cs @@ -4,38 +4,38 @@ namespace FunicularSwitch.Generators.UnionType; public sealed record UnionTypeSchema(string? Namespace, - string TypeName, - string FullTypeName, - string FullTypeNameWithTypeParameters, - EquatableArray Cases, - EquatableArray TypeParameters, - EquatableArray TypeConstraints, - bool IsInternal, - bool IsPartial, - UnionTypeTypeKind TypeKind, - EquatableArray Modifiers, - StaticFactoryMethodsInfo? StaticFactoryInfo); + string TypeName, + string FullTypeName, + string FullTypeNameWithTypeParameters, + EquatableArray Cases, + EquatableArray TypeParameters, + EquatableArray TypeConstraints, + bool IsInternal, + bool IsPartial, + UnionTypeTypeKind TypeKind, + EquatableArray Modifiers, + StaticFactoryMethodsInfo? StaticFactoryInfo); public enum UnionTypeTypeKind { - Class, - Record, - Interface + Class, + Record, + Interface } public record StaticFactoryMethodsInfo( - EquatableArray ExistingStaticMethods, + EquatableArray ExistingStaticMethods, EquatableArray ExistingStaticFields ); public sealed record DerivedType { - public string FullTypeName { get; } + public string FullTypeName { get; } public EquatableArray Constructors { get; } public EquatableArray RequiredMembers { get; } public string ParameterName { get; } - public string StaticFactoryMethodName { get; } - + public string StaticFactoryMethodName { get; } + public DerivedType(string fullTypeName, string parameterName, string staticFactoryMethodName, EquatableArray? constructors = null, EquatableArray? requiredMembers = null) { FullTypeName = fullTypeName; diff --git a/Source/FunicularSwitch.Generators/UnionTypeGenerator.cs b/Source/FunicularSwitch.Generators/UnionTypeGenerator.cs index 187fe50c..ed635104 100644 --- a/Source/FunicularSwitch.Generators/UnionTypeGenerator.cs +++ b/Source/FunicularSwitch.Generators/UnionTypeGenerator.cs @@ -32,13 +32,13 @@ public void Initialize(IncrementalGeneratorInitializationContext context) context.Attributes[0] ) ); - + var referencesJetBrainsAnnotationsAssembly = context.CompilationProvider .SelectMany((c, _) => c.SourceModule.ReferencedAssemblySymbols) .Where(a => a.Name == "JetBrains.Annotations") .Collect() .Select((a, _) => a.Length > 0); - + context.RegisterSourceOutput( unionTypeClasses.Combine(referencesJetBrainsAnnotationsAssembly), static (spc, source) => Execute(source.Left, source.Right, spc)); @@ -48,7 +48,7 @@ static void Execute(GenerationResult target, bool hasJetbrainsA { var (unionTypeSchema, errors, hasValue) = target; foreach (var error in errors) context.ReportDiagnostic(error); - + if (!hasValue || unionTypeSchema!.Cases.IsEmpty) return; var (filename, source) = Generator.Emit(unionTypeSchema, context.ReportDiagnostic, hasJetbrainsAnnotationsReference, context.CancellationToken); diff --git a/Source/FunicularSwitch.Generic.Assertions/GenericResultAssertions.cs b/Source/FunicularSwitch.Generic.Assertions/GenericResultAssertions.cs index b34cdb8b..c417e23b 100644 --- a/Source/FunicularSwitch.Generic.Assertions/GenericResultAssertions.cs +++ b/Source/FunicularSwitch.Generic.Assertions/GenericResultAssertions.cs @@ -18,7 +18,7 @@ public AndWhichConstraint, TOk> BeOk( return new AndWhichConstraint, TOk>(this, Subject.GetValueOrThrow()); } - + public AndWhichConstraint, TError> BeError( string because = "", params object[] becauseArgs) diff --git a/Source/FunicularSwitch.Generic/GenericResultOptionExtensions.cs b/Source/FunicularSwitch.Generic/GenericResultOptionExtensions.cs index 219ae37d..64b7b2a2 100644 --- a/Source/FunicularSwitch.Generic/GenericResultOptionExtensions.cs +++ b/Source/FunicularSwitch.Generic/GenericResultOptionExtensions.cs @@ -64,26 +64,26 @@ public static Task, TError>> UnboxResult( maybeResult.Match( static some => some.Map(Option.Some), static () => GenericResult, TError>.Ok(Option.None)); - + [Pure] public static async ValueTask, TError>> UnboxResult(ValueTask>> maybeResult) => (await maybeResult.ConfigureAwait(false)).Match( static some => some.Map(Option.Some), static () => GenericResult, TError>.Ok(Option.None)); - - + + [Pure] public static Option> UnboxOption(GenericResult, TError> result) => result.Match( static ok => ok.Map(GenericResult.Ok), static error => Option.Some(GenericResult.Error(error))); - + [Pure] public static Task>> UnboxOption(Task, TError>> result) => result.Match( static ok => ok.Map(GenericResult.Ok), static error => Option.Some(GenericResult.Error(error))); - + [Pure] public static ValueTask>> UnboxOption(ValueTask, TError>> result) => result.Match( diff --git a/Source/FunicularSwitch.Generic/GenericResultTaskExtensions.cs b/Source/FunicularSwitch.Generic/GenericResultTaskExtensions.cs index 5ea07740..aefdb88c 100644 --- a/Source/FunicularSwitch.Generic/GenericResultTaskExtensions.cs +++ b/Source/FunicularSwitch.Generic/GenericResultTaskExtensions.cs @@ -271,7 +271,7 @@ public static async Task> SelectMany> SelectMany( this Task> resultTask, diff --git a/Source/FunicularSwitch.Transformers/EnumerableT.cs b/Source/FunicularSwitch.Transformers/EnumerableT.cs index 33933fea..9e2d6003 100644 --- a/Source/FunicularSwitch.Transformers/EnumerableT.cs +++ b/Source/FunicularSwitch.Transformers/EnumerableT.cs @@ -14,10 +14,10 @@ public static IEnumerable Bind(this IEnumerable ma, Func> BindT(Monad> ma, Func>> fn) => - ma.Bind([DebuggerStepThrough](xs) => xs.Aggregate( + ma.Bind([DebuggerStepThrough] (xs) => xs.Aggregate( ma.Return>([]), - [DebuggerStepThrough](acc, cur) => - acc.Bind([DebuggerStepThrough](ys) => + [DebuggerStepThrough] (acc, cur) => + acc.Bind([DebuggerStepThrough] (ys) => fn(cur).Map(ys.Concat) ) ) diff --git a/Source/FunicularSwitch.Transformers/Monad.cs b/Source/FunicularSwitch.Transformers/Monad.cs index e75fe275..f6533400 100644 --- a/Source/FunicularSwitch.Transformers/Monad.cs +++ b/Source/FunicularSwitch.Transformers/Monad.cs @@ -18,5 +18,5 @@ public static class MonadExtension { [Pure] [DebuggerStepThrough] - public static Monad Map(this Monad ma, Func fn) => ma.Bind([DebuggerStepThrough](a) => ma.Return(fn(a))); + public static Monad Map(this Monad ma, Func fn) => ma.Bind([DebuggerStepThrough] (a) => ma.Return(fn(a))); } diff --git a/Source/FunicularSwitch.Transformers/OptionT.cs b/Source/FunicularSwitch.Transformers/OptionT.cs index a679b646..ea3e5d2f 100644 --- a/Source/FunicularSwitch.Transformers/OptionT.cs +++ b/Source/FunicularSwitch.Transformers/OptionT.cs @@ -9,5 +9,5 @@ public static class OptionT [Pure] [DebuggerStepThrough] public static Monad> BindT(Monad> ma, Func>> fn) => - ma.Bind([DebuggerStepThrough](aOption) => aOption.Match(fn, [DebuggerStepThrough]() => ma.Return(Option.None()))); + ma.Bind([DebuggerStepThrough] (aOption) => aOption.Match(fn, [DebuggerStepThrough] () => ma.Return(Option.None()))); } diff --git a/Source/FunicularSwitch.Transformers/ResultT.cs b/Source/FunicularSwitch.Transformers/ResultT.cs index 5ed1c517..da5303a5 100644 --- a/Source/FunicularSwitch.Transformers/ResultT.cs +++ b/Source/FunicularSwitch.Transformers/ResultT.cs @@ -9,5 +9,5 @@ public static class ResultT [Pure] [DebuggerStepThrough] public static Monad> BindT(Monad> ma, Func>> fn) => - ma.Bind([DebuggerStepThrough](aResult) => aResult.Match(fn, [DebuggerStepThrough](e) => ma.Return(Result.Error(e)))); + ma.Bind([DebuggerStepThrough] (aResult) => aResult.Match(fn, [DebuggerStepThrough] (e) => ma.Return(Result.Error(e)))); } diff --git a/Source/FunicularSwitch.sln b/Source/FunicularSwitch.sln index 5587dd4c..ff48460b 100644 --- a/Source/FunicularSwitch.sln +++ b/Source/FunicularSwitch.sln @@ -56,6 +56,7 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}" ProjectSection(SolutionItems) = preProject Directory.build.props = Directory.build.props + .editorconfig = .editorconfig EndProjectSection EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FunicularSwitch.Analyzers", "FunicularSwitch.Analyzers\FunicularSwitch.Analyzers.csproj", "{D653F8B4-FDF0-491C-AF18-13540DD60D03}" diff --git a/Source/FunicularSwitch/Extensions/EnumerableExtension.cs b/Source/FunicularSwitch/Extensions/EnumerableExtension.cs index a651070e..3ebc10ab 100644 --- a/Source/FunicularSwitch/Extensions/EnumerableExtension.cs +++ b/Source/FunicularSwitch/Extensions/EnumerableExtension.cs @@ -31,7 +31,7 @@ public static async Task> WhereAsync(this IEnumerable items /// public static async Task> WhereAsync(this IEnumerable items, Func> predicate, int maxDegreeOfParallelism) { - var context = await items.SelectAsync( async item => (item, await predicate(item).ConfigureAwait(false), maxDegreeOfParallelism)).ConfigureAwait(false); + var context = await items.SelectAsync(async item => (item, await predicate(item).ConfigureAwait(false), maxDegreeOfParallelism)).ConfigureAwait(false); return context.Where(item => item.Item2).Select(item => item.item); } @@ -127,9 +127,11 @@ public static IEnumerable Concat(this IEnumerable items, T item, params return items.Append(item).Concat(further); } - public static Option FirstOrNone(this IEnumerable source) + public static IEnumerable WhereSome(this IEnumerable> option) => option.SelectMany(o => o); + + public static Option FirstOrNone(this IEnumerable source) => source.FirstOrNone(static _ => true); - + public static Option FirstOrNone(this IEnumerable source, Func predicate) { foreach (var element in source) @@ -176,7 +178,7 @@ public static Option ElementAtOrNone(this IEnumerable list) { - return index < list.Count + return index < list.Count ? Option.Some(list[index]) : Option.None; } diff --git a/Source/FunicularSwitch/Extensions/TypeExtension.cs b/Source/FunicularSwitch/Extensions/TypeExtension.cs index 1020b087..96db40fa 100644 --- a/Source/FunicularSwitch/Extensions/TypeExtension.cs +++ b/Source/FunicularSwitch/Extensions/TypeExtension.cs @@ -16,7 +16,7 @@ public static string BeautifulName(this Type t) var index = t.Name.LastIndexOf("`", StringComparison.Ordinal); if (index < 0) return t.Name; - + sb.Append(t.Name.Substring(0, index)); sb.Append('<'); foreach (var type in t.GetGenericArguments()) diff --git a/Source/FunicularSwitch/FunicularSwitch.csproj b/Source/FunicularSwitch/FunicularSwitch.csproj index 7858bd3b..17598885 100644 --- a/Source/FunicularSwitch/FunicularSwitch.csproj +++ b/Source/FunicularSwitch/FunicularSwitch.csproj @@ -14,7 +14,7 @@ 6 - 4.1 + 5 $(MajorVersion).0.0 diff --git a/Source/FunicularSwitch/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.EnumTypeGenerator/Attributes.g.cs b/Source/FunicularSwitch/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.EnumTypeGenerator/Attributes.g.cs index f8adff12..3a2720d9 100644 --- a/Source/FunicularSwitch/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.EnumTypeGenerator/Attributes.g.cs +++ b/Source/FunicularSwitch/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.EnumTypeGenerator/Attributes.g.cs @@ -7,10 +7,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -23,16 +23,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -41,21 +41,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/FunicularSwitch/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/Attributes.g.cs b/Source/FunicularSwitch/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/Attributes.g.cs index 8dddcd08..ba6d29a4 100644 --- a/Source/FunicularSwitch/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/Attributes.g.cs +++ b/Source/FunicularSwitch/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/Attributes.g.cs @@ -4,10 +4,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/FunicularSwitch/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Result.g.cs b/Source/FunicularSwitch/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Result.g.cs index 4d8102bb..f13e67b4 100644 --- a/Source/FunicularSwitch/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Result.g.cs +++ b/Source/FunicularSwitch/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Result.g.cs @@ -1,610 +1,610 @@ -#nullable enable +#nullable enable using global::System.Linq; using System; -namespace FunicularSwitch +namespace FunicularSwitch { -#pragma warning disable 1591 - public abstract partial class Result - { - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static Result Error(String details) => new Result.Error_(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static ResultError Error(String details) => new(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static Result Ok(T value) => new Result.Ok_(value); - public bool IsError => GetType().GetGenericTypeDefinition() == typeof(Result<>.Error_); - public bool IsOk => !IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public abstract String? GetErrorOrDefault(); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result Try( global::System.Func action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - } - - public abstract partial class Result : Result, global::System.Collections.Generic.IEnumerable - { - - [global::System.Diagnostics.DebuggerNonUserCode] - - public new static Result Error(String message) => Error(message); - - [global::System.Diagnostics.DebuggerNonUserCode] - - public static Result Ok(T value) => Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator Result(T value) => Result.Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator Result(ResultError myResultError) => myResultError.WithOk(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator true(Result result) => result.IsOk; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator false(Result result) => result.IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !(Result result) => result.IsError; - - //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode - bool Equals(Result other) => this switch - { - Ok_ ok => ok.Equals((object)other), - Error_ error => error.Equals((object)other), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") - }; - - public override int GetHashCode() => this switch - { - Ok_ ok => ok.GetHashCode(), - Error_ error => error.GetHashCode(), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") - }; - - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != this.GetType()) return false; - return Equals((Result)obj); - } - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Result? left, Result? right) => Equals(left, right); - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Result? left, Result? right) => !Equals(left, right); - - [global::System.Diagnostics.DebuggerStepThrough] - public void Match( global::System.Action ok, global::System.Action? error = null) => Match( - v => - { - ok.Invoke(v); - return 42; - }, - err => - { - error?.Invoke(err); - return 42; - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T1 Match( global::System.Func ok, global::System.Func error) - { - return this switch - { - Ok_ okResult => ok(okResult.Value), - Error_ errorResult => error(errorResult.Details), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) - { - return this switch - { - Ok_ okResult => await ok(okResult.Value).ConfigureAwait(false), - Error_ errorResult => await error(errorResult.Details).ConfigureAwait(false), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => - Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); - - [global::System.Diagnostics.DebuggerStepThrough] - public async global::System.Threading.Tasks.Task Match( global::System.Func ok) - { - if (this is Ok_ okResult) await ok(okResult.Value).ConfigureAwait(false); - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public T Match( global::System.Func error) => Match(v => v, error); - - [global::System.Diagnostics.DebuggerStepThrough] - - public Result Bind( global::System.Func> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return bind(ok.Value); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return await bind(ok.Value).ConfigureAwait(false); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public Result Map( global::System.Func map) - { - switch (this) - { - case Ok_ ok: - try - { - return Result.Ok(map(ok.Value)); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Map( - global::System.Func> map) - { - switch (this) - { - case Ok_ ok: - try - { - return Result.Ok(await map(ok.Value).ConfigureAwait(false)); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - //createGenericResultConversions - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T? GetValueOrDefault() - => Match( - v => (T?)v, - _ => default - ); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T GetValueOrDefault( global::System.Func defaultValue) - => Match( - v => v, - _ => defaultValue() - ); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T GetValueOrDefault(T defaultValue) - => Match( - v => v, - _ => defaultValue - ); - - [global::System.Diagnostics.DebuggerStepThrough] - public T GetValueOrThrow() - => Match( - v => v, - details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); - - - public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); - - - public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); - global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); - - public sealed partial class Ok_ : Result - { - public T Value { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Ok_(T value) => Value = value; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override String? GetErrorOrDefault() => null; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Ok_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Ok_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); - } - - public sealed partial class Error_ : Result - { - public String Details { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Error_(String details) => Details = details; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public Result.Error_ Convert() => new Result.Error_(Details); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override String? GetErrorOrDefault() => Details; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Error_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return Equals(Details, other.Details); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Error_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); - } - } - - public readonly partial struct ResultError : global::System.IEquatable - { - readonly String _details; - - internal ResultError(String details) => _details = details; - - [global::System.Diagnostics.Contracts.Pure] - public Result WithOk() => Result.Error(_details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(ResultError other) => _details.Equals(other._details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) => obj is ResultError other && Equals(other); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => _details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(ResultError left, ResultError right) => left.Equals(right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(ResultError left, ResultError right) => !left.Equals(right); - } - - public static partial class ResultExtension - { - #region bind - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => (await result.ConfigureAwait(false)).Bind(bind); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func>> bind) - => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); - - #endregion - - #region map - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func map) - => (await result.ConfigureAwait(false)).Map(map); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => Bind(result, async v => Result.Ok(await bind(v).ConfigureAwait(false))); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result MapError(this Result result, global::System.Func mapError) - { - if (result is Result.Error_ e) - return Result.Error(mapError(e.Details)); - return result; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); - - #endregion - - #region match - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func> error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func ok, - global::System.Func error) - => (await result.ConfigureAwait(false)).Match(ok, error); - - #endregion - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static Result Flatten(this Result> result) => result.Bind(r => r); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result As(this Result result, global::System.Func errorTIsNotT1) => - result.Bind(r => - { - if (r is T1 converted) - return converted; - return Result.Error(errorTIsNotT1()); - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result As(this Result result, global::System.Func errorIsNotT1) => - result.As(errorIsNotT1); - - #region query-expression pattern - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result Select(this Result result, global::System.Func selector) => result.Map(selector); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result SelectMany(this Result result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this Result result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - - #endregion - - //createGenericResultConversionExtensions - } -} - -namespace FunicularSwitch.Extensions +#pragma warning disable 1591 + public abstract partial class Result + { + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static Result Error(String details) => new Result.Error_(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static ResultError Error(String details) => new(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static Result Ok(T value) => new Result.Ok_(value); + public bool IsError => GetType().GetGenericTypeDefinition() == typeof(Result<>.Error_); + public bool IsOk => !IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public abstract String? GetErrorOrDefault(); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result Try( global::System.Func action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + } + + public abstract partial class Result : Result, global::System.Collections.Generic.IEnumerable + { + + [global::System.Diagnostics.DebuggerNonUserCode] + + public new static Result Error(String message) => Error(message); + + [global::System.Diagnostics.DebuggerNonUserCode] + + public static Result Ok(T value) => Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator Result(T value) => Result.Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator Result(ResultError myResultError) => myResultError.WithOk(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator true(Result result) => result.IsOk; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator false(Result result) => result.IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !(Result result) => result.IsError; + + //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode + bool Equals(Result other) => this switch + { + Ok_ ok => ok.Equals((object)other), + Error_ error => error.Equals((object)other), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") + }; + + public override int GetHashCode() => this switch + { + Ok_ ok => ok.GetHashCode(), + Error_ error => error.GetHashCode(), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") + }; + + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((Result)obj); + } + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Result? left, Result? right) => Equals(left, right); + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Result? left, Result? right) => !Equals(left, right); + + [global::System.Diagnostics.DebuggerStepThrough] + public void Match( global::System.Action ok, global::System.Action? error = null) => Match( + v => + { + ok.Invoke(v); + return 42; + }, + err => + { + error?.Invoke(err); + return 42; + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T1 Match( global::System.Func ok, global::System.Func error) + { + return this switch + { + Ok_ okResult => ok(okResult.Value), + Error_ errorResult => error(errorResult.Details), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) + { + return this switch + { + Ok_ okResult => await ok(okResult.Value).ConfigureAwait(false), + Error_ errorResult => await error(errorResult.Details).ConfigureAwait(false), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => + Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); + + [global::System.Diagnostics.DebuggerStepThrough] + public async global::System.Threading.Tasks.Task Match( global::System.Func ok) + { + if (this is Ok_ okResult) await ok(okResult.Value).ConfigureAwait(false); + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public T Match( global::System.Func error) => Match(v => v, error); + + [global::System.Diagnostics.DebuggerStepThrough] + + public Result Bind( global::System.Func> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return bind(ok.Value); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return await bind(ok.Value).ConfigureAwait(false); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public Result Map( global::System.Func map) + { + switch (this) + { + case Ok_ ok: + try + { + return Result.Ok(map(ok.Value)); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Map( + global::System.Func> map) + { + switch (this) + { + case Ok_ ok: + try + { + return Result.Ok(await map(ok.Value).ConfigureAwait(false)); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + //createGenericResultConversions + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T? GetValueOrDefault() + => Match( + v => (T?)v, + _ => default + ); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T GetValueOrDefault( global::System.Func defaultValue) + => Match( + v => v, + _ => defaultValue() + ); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T GetValueOrDefault(T defaultValue) + => Match( + v => v, + _ => defaultValue + ); + + [global::System.Diagnostics.DebuggerStepThrough] + public T GetValueOrThrow() + => Match( + v => v, + details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); + + + public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); + + + public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); + global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); + + public sealed partial class Ok_ : Result + { + public T Value { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Ok_(T value) => Value = value; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override String? GetErrorOrDefault() => null; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Ok_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Ok_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); + } + + public sealed partial class Error_ : Result + { + public String Details { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Error_(String details) => Details = details; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public Result.Error_ Convert() => new Result.Error_(Details); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override String? GetErrorOrDefault() => Details; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Error_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Equals(Details, other.Details); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Error_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); + } + } + + public readonly partial struct ResultError : global::System.IEquatable + { + readonly String _details; + + internal ResultError(String details) => _details = details; + + [global::System.Diagnostics.Contracts.Pure] + public Result WithOk() => Result.Error(_details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(ResultError other) => _details.Equals(other._details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) => obj is ResultError other && Equals(other); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => _details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(ResultError left, ResultError right) => left.Equals(right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(ResultError left, ResultError right) => !left.Equals(right); + } + + public static partial class ResultExtension + { + #region bind + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => (await result.ConfigureAwait(false)).Bind(bind); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func>> bind) + => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); + + #endregion + + #region map + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func map) + => (await result.ConfigureAwait(false)).Map(map); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => Bind(result, async v => Result.Ok(await bind(v).ConfigureAwait(false))); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result MapError(this Result result, global::System.Func mapError) + { + if (result is Result.Error_ e) + return Result.Error(mapError(e.Details)); + return result; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); + + #endregion + + #region match + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func> error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func ok, + global::System.Func error) + => (await result.ConfigureAwait(false)).Match(ok, error); + + #endregion + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static Result Flatten(this Result> result) => result.Bind(r => r); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result As(this Result result, global::System.Func errorTIsNotT1) => + result.Bind(r => + { + if (r is T1 converted) + return converted; + return Result.Error(errorTIsNotT1()); + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result As(this Result result, global::System.Func errorIsNotT1) => + result.As(errorIsNotT1); + + #region query-expression pattern + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result Select(this Result result, global::System.Func selector) => result.Map(selector); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result SelectMany(this Result result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this Result result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + + #endregion + + //createGenericResultConversionExtensions + } +} + +namespace FunicularSwitch.Extensions { - public static partial class ResultExtension - { - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable items, - global::System.Func> choose, - global::System.Action onError) - => items - .Select(i => choose(i)) - .Choose(onError); - - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable> results, - global::System.Action onError) - => results - .Where(r => - r.Match(_ => true, error => - { - onError(error); - return false; - })) - .Select(r => r.GetValueOrThrow()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result As(this object? item, global::System.Func error) => - !(item is T t) ? Result.Error(error()) : t; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result NotNull(this T? item, global::System.Func error) => - item ?? Result.Error(error()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result NotNullOrEmpty(this string? s, global::System.Func error) - => string.IsNullOrEmpty(s) ? Result.Error(error()) : s!; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result NotNullOrWhiteSpace(this string? s, global::System.Func error) - => string.IsNullOrWhiteSpace(s) ? Result.Error(error()) : s!; - - - public static Result First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => - candidates - .FirstOrDefault(i => predicate(i)) - .NotNull(noMatch); - } -#pragma warning restore 1591 -} + public static partial class ResultExtension + { + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable items, + global::System.Func> choose, + global::System.Action onError) + => items + .Select(i => choose(i)) + .Choose(onError); + + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable> results, + global::System.Action onError) + => results + .Where(r => + r.Match(_ => true, error => + { + onError(error); + return false; + })) + .Select(r => r.GetValueOrThrow()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result As(this object? item, global::System.Func error) => + !(item is T t) ? Result.Error(error()) : t; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result NotNull(this T? item, global::System.Func error) => + item ?? Result.Error(error()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result NotNullOrEmpty(this string? s, global::System.Func error) + => string.IsNullOrEmpty(s) ? Result.Error(error()) : s!; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result NotNullOrWhiteSpace(this string? s, global::System.Func error) + => string.IsNullOrWhiteSpace(s) ? Result.Error(error()) : s!; + + + public static Result First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => + candidates + .FirstOrDefault(i => predicate(i)) + .NotNull(noMatch); + } +#pragma warning restore 1591 +} diff --git a/Source/FunicularSwitch/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.ResultWithMerge.g.cs b/Source/FunicularSwitch/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.ResultWithMerge.g.cs index 9d1bb872..b9d32c82 100644 --- a/Source/FunicularSwitch/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.ResultWithMerge.g.cs +++ b/Source/FunicularSwitch/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.ResultWithMerge.g.cs @@ -1,524 +1,524 @@ -#nullable enable -using global::System.Linq; -using System; - -namespace FunicularSwitch -{ -#pragma warning disable 1591 - public abstract partial class Result - { - - - - public static Result<(T1, T2)> Aggregate(Result r1, Result r2) => ResultExtension.Aggregate(r1, r2); - - - public static Result Aggregate(Result r1, Result r2, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2) => ResultExtension.Aggregate(r1, r2); - - +#nullable enable +using global::System.Linq; +using System; + +namespace FunicularSwitch +{ +#pragma warning disable 1591 + public abstract partial class Result + { + + + + public static Result<(T1, T2)> Aggregate(Result r1, Result r2) => ResultExtension.Aggregate(r1, r2); + + + public static Result Aggregate(Result r1, Result r2, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2) => ResultExtension.Aggregate(r1, r2); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, combine); - - - - public static Result<(T1, T2, T3)> Aggregate(Result r1, Result r2, Result r3) => ResultExtension.Aggregate(r1, r2, r3); - - - public static Result Aggregate(Result r1, Result r2, Result r3, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3) => ResultExtension.Aggregate(r1, r2, r3); - - + + + + public static Result<(T1, T2, T3)> Aggregate(Result r1, Result r2, Result r3) => ResultExtension.Aggregate(r1, r2, r3); + + + public static Result Aggregate(Result r1, Result r2, Result r3, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3) => ResultExtension.Aggregate(r1, r2, r3); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, combine); - - - - public static Result<(T1, T2, T3, T4)> Aggregate(Result r1, Result r2, Result r3, Result r4) => ResultExtension.Aggregate(r1, r2, r3, r4); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4) => ResultExtension.Aggregate(r1, r2, r3, r4); - - + + + + public static Result<(T1, T2, T3, T4)> Aggregate(Result r1, Result r2, Result r3, Result r4) => ResultExtension.Aggregate(r1, r2, r3, r4); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4) => ResultExtension.Aggregate(r1, r2, r3, r4); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, combine); - - - - public static Result<(T1, T2, T3, T4, T5)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5) => ResultExtension.Aggregate(r1, r2, r3, r4, r5); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5) => ResultExtension.Aggregate(r1, r2, r3, r4, r5); - - + + + + public static Result<(T1, T2, T3, T4, T5)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5) => ResultExtension.Aggregate(r1, r2, r3, r4, r5); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5) => ResultExtension.Aggregate(r1, r2, r3, r4, r5); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, combine); - - - - public static Result<(T1, T2, T3, T4, T5, T6)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6); - - + + + + public static Result<(T1, T2, T3, T4, T5, T6)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, combine); - - - - public static Result<(T1, T2, T3, T4, T5, T6, T7)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7); - - + + + + public static Result<(T1, T2, T3, T4, T5, T6, T7)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, combine); - - - - public static Result<(T1, T2, T3, T4, T5, T6, T7, T8)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8); - - + + + + public static Result<(T1, T2, T3, T4, T5, T6, T7, T8)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, combine); - - - - public static Result<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, combine); - } - - public static partial class ResultExtension - { - - public static Result> Map(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func map) => + + + + public static Result<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, combine); + } + + public static partial class ResultExtension + { + + public static Result> Map(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func map) => results.Select(r => r.Map(map)).Aggregate(); - - - public static Result> Bind(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func> bind) => + + + public static Result> Bind(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func> bind) => results.Select(r => r.Bind(bind)).Aggregate(); - - public static Result> Bind(this Result result, - global::System.Func>> bindMany) => + + public static Result> Bind(this Result result, + global::System.Func>> bindMany) => result.Map(ok => bindMany(ok).Aggregate()).Flatten(); - - public static Result Bind(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func, Result> bind) => + + public static Result Bind(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func, Result> bind) => results.Aggregate().Bind(bind); - - public static Result> Aggregate(this global::System.Collections.Generic.IEnumerable> results) - { - var isError = false; - String aggregated = default!; - var oks = new global::System.Collections.Generic.List(); - foreach (var result in results) - { - result.Match( - ok => oks.Add(ok), - error => - { - aggregated = !isError ? error : MergeErrors(aggregated, error); - isError = true; - } - ); - } - - return isError - ? Result.Error>(aggregated) - : Result.Ok>(oks); + + public static Result> Aggregate(this global::System.Collections.Generic.IEnumerable> results) + { + var isError = false; + String aggregated = default!; + var oks = new global::System.Collections.Generic.List(); + foreach (var result in results) + { + result.Match( + ok => oks.Add(ok), + error => + { + aggregated = !isError ? error : MergeErrors(aggregated, error); + isError = true; + } + ); + } + + return isError + ? Result.Error>(aggregated) + : Result.Ok>(oks); } - - - public static async global::System.Threading.Tasks.Task>> Aggregate( - this global::System.Threading.Tasks.Task>> results) - => (await results.ConfigureAwait(false)) + + + public static async global::System.Threading.Tasks.Task>> Aggregate( + this global::System.Threading.Tasks.Task>> results) + => (await results.ConfigureAwait(false)) .Aggregate(); - - - public static async global::System.Threading.Tasks.Task>> Aggregate( - this global::System.Collections.Generic.IEnumerable>> results) - => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) + + + public static async global::System.Threading.Tasks.Task>> Aggregate( + this global::System.Collections.Generic.IEnumerable>> results) + => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) .Aggregate(); - - - public static async global::System.Threading.Tasks.Task>> AggregateMany( - this global::System.Collections.Generic.IEnumerable>>> results) - => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) - .SelectMany(e => e) + + + public static async global::System.Threading.Tasks.Task>> AggregateMany( + this global::System.Collections.Generic.IEnumerable>>> results) + => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) + .SelectMany(e => e) .Aggregate(); - - - public static Result<(T1, T2)> Aggregate(this Result r1, Result r2) => - Aggregate(r1, r2, (v1, v2) => (v1, v2)); - - - public static Result Aggregate(this Result r1, Result r2, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2) - return combine(ok1.Value, ok2.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); - } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2) - => Aggregate(r1, r2, (v1, v2) => (v1, v2)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2); - return Aggregate(r1.Result, r2.Result, combine); - } - - - public static Result<(T1, T2, T3)> Aggregate(this Result r1, Result r2, Result r3) => - Aggregate(r1, r2, r3, (v1, v2, v3) => (v1, v2, v3)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3) - return combine(ok1.Value, ok2.Value, ok3.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); - } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3) - => Aggregate(r1, r2, r3, (v1, v2, v3) => (v1, v2, v3)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3); - return Aggregate(r1.Result, r2.Result, r3.Result, combine); - } - - - public static Result<(T1, T2, T3, T4)> Aggregate(this Result r1, Result r2, Result r3, Result r4) => - Aggregate(r1, r2, r3, r4, (v1, v2, v3, v4) => (v1, v2, v3, v4)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); - } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4) - => Aggregate(r1, r2, r3, r4, (v1, v2, v3, v4) => (v1, v2, v3, v4)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, combine); - } - - - public static Result<(T1, T2, T3, T4, T5)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5) => - Aggregate(r1, r2, r3, r4, r5, (v1, v2, v3, v4, v5) => (v1, v2, v3, v4, v5)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4, r5 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); - } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5) - => Aggregate(r1, r2, r3, r4, r5, (v1, v2, v3, v4, v5) => (v1, v2, v3, v4, v5)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, combine); - } - - - public static Result<(T1, T2, T3, T4, T5, T6)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6) => - Aggregate(r1, r2, r3, r4, r5, r6, (v1, v2, v3, v4, v5, v6) => (v1, v2, v3, v4, v5, v6)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); + + + public static Result<(T1, T2)> Aggregate(this Result r1, Result r2) => + Aggregate(r1, r2, (v1, v2) => (v1, v2)); + + + public static Result Aggregate(this Result r1, Result r2, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2) + return combine(ok1.Value, ok2.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2) + => Aggregate(r1, r2, (v1, v2) => (v1, v2)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2); + return Aggregate(r1.Result, r2.Result, combine); } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6) - => Aggregate(r1, r2, r3, r4, r5, r6, (v1, v2, v3, v4, v5, v6) => (v1, v2, v3, v4, v5, v6)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, combine); + + + public static Result<(T1, T2, T3)> Aggregate(this Result r1, Result r2, Result r3) => + Aggregate(r1, r2, r3, (v1, v2, v3) => (v1, v2, v3)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3) + return combine(ok1.Value, ok2.Value, ok3.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3) + => Aggregate(r1, r2, r3, (v1, v2, v3) => (v1, v2, v3)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3); + return Aggregate(r1.Result, r2.Result, r3.Result, combine); } - - - public static Result<(T1, T2, T3, T4, T5, T6, T7)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7) => - Aggregate(r1, r2, r3, r4, r5, r6, r7, (v1, v2, v3, v4, v5, v6, v7) => (v1, v2, v3, v4, v5, v6, v7)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); + + + public static Result<(T1, T2, T3, T4)> Aggregate(this Result r1, Result r2, Result r3, Result r4) => + Aggregate(r1, r2, r3, r4, (v1, v2, v3, v4) => (v1, v2, v3, v4)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4) + => Aggregate(r1, r2, r3, r4, (v1, v2, v3, v4) => (v1, v2, v3, v4)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, combine); } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7) - => Aggregate(r1, r2, r3, r4, r5, r6, r7, (v1, v2, v3, v4, v5, v6, v7) => (v1, v2, v3, v4, v5, v6, v7)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, combine); + + + public static Result<(T1, T2, T3, T4, T5)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5) => + Aggregate(r1, r2, r3, r4, r5, (v1, v2, v3, v4, v5) => (v1, v2, v3, v4, v5)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4, r5 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5) + => Aggregate(r1, r2, r3, r4, r5, (v1, v2, v3, v4, v5) => (v1, v2, v3, v4, v5)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, combine); } - - - public static Result<(T1, T2, T3, T4, T5, T6, T7, T8)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8) => - Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, (v1, v2, v3, v4, v5, v6, v7, v8) => (v1, v2, v3, v4, v5, v6, v7, v8)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7 && r8 is Result.Ok_ ok8) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value, ok8.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7, r8 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); + + + public static Result<(T1, T2, T3, T4, T5, T6)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6) => + Aggregate(r1, r2, r3, r4, r5, r6, (v1, v2, v3, v4, v5, v6) => (v1, v2, v3, v4, v5, v6)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6) + => Aggregate(r1, r2, r3, r4, r5, r6, (v1, v2, v3, v4, v5, v6) => (v1, v2, v3, v4, v5, v6)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, combine); } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8) - => Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, (v1, v2, v3, v4, v5, v6, v7, v8) => (v1, v2, v3, v4, v5, v6, v7, v8)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7, r8); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, r8.Result, combine); + + + public static Result<(T1, T2, T3, T4, T5, T6, T7)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7) => + Aggregate(r1, r2, r3, r4, r5, r6, r7, (v1, v2, v3, v4, v5, v6, v7) => (v1, v2, v3, v4, v5, v6, v7)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7) + => Aggregate(r1, r2, r3, r4, r5, r6, r7, (v1, v2, v3, v4, v5, v6, v7) => (v1, v2, v3, v4, v5, v6, v7)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, combine); } - - - public static Result<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9) => - Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, (v1, v2, v3, v4, v5, v6, v7, v8, v9) => (v1, v2, v3, v4, v5, v6, v7, v8, v9)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7 && r8 is Result.Ok_ ok8 && r9 is Result.Ok_ ok9) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value, ok8.Value, ok9.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7, r8, r9 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); + + + public static Result<(T1, T2, T3, T4, T5, T6, T7, T8)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8) => + Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, (v1, v2, v3, v4, v5, v6, v7, v8) => (v1, v2, v3, v4, v5, v6, v7, v8)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7 && r8 is Result.Ok_ ok8) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value, ok8.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7, r8 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8) + => Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, (v1, v2, v3, v4, v5, v6, v7, v8) => (v1, v2, v3, v4, v5, v6, v7, v8)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7, r8); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, r8.Result, combine); } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9) - => Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, (v1, v2, v3, v4, v5, v6, v7, v8, v9) => (v1, v2, v3, v4, v5, v6, v7, v8, v9)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7, r8, r9); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, r8.Result, r9.Result, combine); + + + public static Result<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9) => + Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, (v1, v2, v3, v4, v5, v6, v7, v8, v9) => (v1, v2, v3, v4, v5, v6, v7, v8, v9)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7 && r8 is Result.Ok_ ok8 && r9 is Result.Ok_ ok9) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value, ok8.Value, ok9.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7, r8, r9 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9) + => Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, (v1, v2, v3, v4, v5, v6, v7, v8, v9) => (v1, v2, v3, v4, v5, v6, v7, v8, v9)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7, r8, r9); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, r8.Result, r9.Result, combine); } - - public static Result FirstOk(this global::System.Collections.Generic.IEnumerable> results, global::System.Func onEmpty) - { - var errors = new global::System.Collections.Generic.List(); - foreach (var result in results) - { - if (result is Result.Error_ e) - errors.Add(e.Details); - else - return result; - } - - if (!errors.Any()) - errors.Add(onEmpty()); - - return Result.Error(MergeErrors(errors)); + + public static Result FirstOk(this global::System.Collections.Generic.IEnumerable> results, global::System.Func onEmpty) + { + var errors = new global::System.Collections.Generic.List(); + foreach (var result in results) + { + if (result is Result.Error_ e) + errors.Add(e.Details); + else + return result; + } + + if (!errors.Any()) + errors.Add(onEmpty()); + + return Result.Error(MergeErrors(errors)); } - - - public static async global::System.Threading.Tasks.Task>> Aggregate( - this global::System.Collections.Generic.IEnumerable>> results, - int maxDegreeOfParallelism) - => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) + + + public static async global::System.Threading.Tasks.Task>> Aggregate( + this global::System.Collections.Generic.IEnumerable>> results, + int maxDegreeOfParallelism) + => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) + .Aggregate(); + + + public static async global::System.Threading.Tasks.Task>> AggregateMany( + this global::System.Collections.Generic.IEnumerable>>> results, + int maxDegreeOfParallelism) + => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) + .SelectMany(e => e) .Aggregate(); - - public static async global::System.Threading.Tasks.Task>> AggregateMany( - this global::System.Collections.Generic.IEnumerable>>> results, - int maxDegreeOfParallelism) - => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) - .SelectMany(e => e) - .Aggregate(); - - static async global::System.Threading.Tasks.Task SelectAsync(this global::System.Collections.Generic.IEnumerable items, global::System.Func> selector, int maxDegreeOfParallelism) - { - using (var throttler = new global::System.Threading.SemaphoreSlim(maxDegreeOfParallelism, maxDegreeOfParallelism)) - { - return await global::System.Threading.Tasks.Task.WhenAll(items.Select(async item => - { - // ReSharper disable once AccessToDisposedClosure - await throttler.WaitAsync().ConfigureAwait(false); - try - { - return await selector(item).ConfigureAwait(false); - } - finally - { - // ReSharper disable once AccessToDisposedClosure - throttler.Release(); - } - })).ConfigureAwait(false); - } + static async global::System.Threading.Tasks.Task SelectAsync(this global::System.Collections.Generic.IEnumerable items, global::System.Func> selector, int maxDegreeOfParallelism) + { + using (var throttler = new global::System.Threading.SemaphoreSlim(maxDegreeOfParallelism, maxDegreeOfParallelism)) + { + return await global::System.Threading.Tasks.Task.WhenAll(items.Select(async item => + { + // ReSharper disable once AccessToDisposedClosure + await throttler.WaitAsync().ConfigureAwait(false); + try + { + return await selector(item).ConfigureAwait(false); + } + finally + { + // ReSharper disable once AccessToDisposedClosure + throttler.Release(); + } + })).ConfigureAwait(false); + } } - - - public static Result> AllOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate) => - candidates - .Select(c => c.Validate(validate)) + + + public static Result> AllOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate) => + candidates + .Select(c => c.Validate(validate)) .Aggregate(); - - public static Result> AllOk(this global::System.Collections.Generic.IEnumerable> candidates, - global::System.Func> validate) => - candidates + + public static Result> AllOk(this global::System.Collections.Generic.IEnumerable> candidates, + global::System.Func> validate) => + candidates .Bind(items => items.AllOk(validate)); - - - public static Result Validate(this Result item, global::System.Func> validate) => item.Bind(i => i.Validate(validate)); - - - public static Result Validate(this T item, global::System.Func> validate) - { - try - { - var errors = validate(item).ToList(); - return errors.Count > 0 ? Result.Error(MergeErrors(errors)) : item; - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } + + + public static Result Validate(this Result item, global::System.Func> validate) => item.Bind(i => i.Validate(validate)); + + + public static Result Validate(this T item, global::System.Func> validate) + { + try + { + var errors = validate(item).ToList(); + return errors.Count > 0 ? Result.Error(MergeErrors(errors)) : item; + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } } - - - public static Result FirstOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate, global::System.Func onEmpty) => - candidates - .Select(r => r.Validate(validate)) - .FirstOk(onEmpty); - - #region helpers - - static String MergeErrors(global::System.Collections.Generic.IEnumerable errors) - { - var first = true; - String aggregated = default!; - foreach (var myError in errors) - { - if (first) - { - aggregated = myError; - first = false; - } - else - { - aggregated = MergeErrors(aggregated, myError); - } - } - - return aggregated; - } - - static String MergeErrors(String aggregated, String error) => aggregated.JoinErrors(error); - - #endregion - } -#pragma warning restore 1591 + + + public static Result FirstOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate, global::System.Func onEmpty) => + candidates + .Select(r => r.Validate(validate)) + .FirstOk(onEmpty); + + #region helpers + + static String MergeErrors(global::System.Collections.Generic.IEnumerable errors) + { + var first = true; + String aggregated = default!; + foreach (var myError in errors) + { + if (first) + { + aggregated = myError; + first = false; + } + else + { + aggregated = MergeErrors(aggregated, myError); + } + } + + return aggregated; + } + + static String MergeErrors(String aggregated, String error) => aggregated.JoinErrors(error); + + #endregion + } +#pragma warning restore 1591 } \ No newline at end of file diff --git a/Source/FunicularSwitch/Option.cs b/Source/FunicularSwitch/Option.cs index 78cfdfce..37732941 100644 --- a/Source/FunicularSwitch/Option.cs +++ b/Source/FunicularSwitch/Option.cs @@ -22,7 +22,12 @@ public interface IOption bool IsNone(); } - public readonly struct Option : IEnumerable, IEquatable>, IOption + internal interface IInternalOption : IOption + { + object? Value { get; } + } + + public readonly struct Option : IEnumerable, IEquatable>, IInternalOption { public static readonly Option None = default; @@ -44,6 +49,8 @@ public interface IOption public bool IsNone() => !_isSome; + object? IInternalOption.Value => _value; + public Option Map(Func map) => Match(t => Option.Some(map(t)), Option.None); public Task> Map(Func> map) => Match( @@ -108,7 +115,7 @@ public async Task Match(Func> some, TResult n IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - public IEnumerator GetEnumerator() => Match(v => new[] { v }, Enumerable.Empty).GetEnumerator(); + public IEnumerator GetEnumerator() => Match(v => [v], Enumerable.Empty).GetEnumerator(); public T? GetValueOrDefault() => Match(v => (T?)v, () => default); @@ -116,7 +123,7 @@ public async Task Match(Func> some, TResult n public async Task GetValueOrDefault(Func> defaultValue) => _isSome ? _value : await defaultValue().ConfigureAwait(false); - + public async ValueTask GetValueOrDefault(Func> defaultValue) => _isSome ? _value : await defaultValue().ConfigureAwait(false); @@ -128,12 +135,12 @@ public T GetValueOrThrow(string? errorMessage = null) => : throw new InvalidOperationException(errorMessage ?? "Cannot access value of none option"); public Option Convert() => - Match(s => Option.Some((TOther)(object)s!), Option.None); + Match(s => Option.Some((TOther)(object)s!), Option.None); - public Option As() where TTarget : class => - Bind(item => (item as TTarget).ToOption()); + public Option As() where TTarget : class => + Bind(item => (item as TTarget).ToOption()); - public override string ToString() => Match(v => v?.ToString() ?? "", () => $"None {typeof(T).BeautifulName()}"); + public override string ToString() => Match(v => v?.ToString() ?? "", () => $"None {typeof(T).BeautifulName()}"); public bool Equals(Option other) => _isSome == other._isSome && EqualityComparer.Default.Equals(_value, other._value); @@ -165,17 +172,6 @@ public static Option Flatten(this Option> option) return option.Match(s => s, () => Option.None); } - public static Option ToOption(this T? item) where T : class => item ?? Option.None; - - public static Option ToOption(this T? item) where T : struct => - item.HasValue ? Option.Some(item.Value) : Option.None; - - public static T? ToNullable(this Option option) where T : struct => - option.Match(some => some, () => (T?)null); - - public static Option As(this object item) where TTarget : class => - (item as TTarget).ToOption(); - public static async Task Match(this Task> option, Func some, Func none) { var result = await option.ConfigureAwait(false); @@ -224,17 +220,9 @@ public static Option ToOption(this Result result, Action? logEr public static Result ToResult(this Option option, Func errorIfNone) => option.Match(s => Result.Ok(s), () => Result.Error(errorIfNone())); - public static Option ToOption(this T? value, Func hasValue) where T : class - => value is not null && hasValue(value) ? Option.Some(value) : Option.None(); - - public static Option ToOption(this T? value, Func hasValue) where T : struct - => value.HasValue && hasValue(value.Value) ? Option.Some(value.Value) : Option.None(); - public static Option NoneIfEmpty(this string? text) => text.ToOption(x => !string.IsNullOrEmpty(x)); - public static IEnumerable WhereSome(this IEnumerable> option) => option.SelectMany(o => o); - #region query-expression pattern public static Option Select(this Option result, Func selector) => result.Map(selector); @@ -258,6 +246,38 @@ public static Task> SelectMany(this Option result, Func Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + public static Option As(this object? item) + => item switch + { + TTarget target => Option.Some(target), + IInternalOption option => option.IsSome() && option.Value is TTarget target + ? Option.Some(target) + : Option.None, + _ => Option.None() + }; #endregion } + + public static class OptionStructExtensions + { + public static T? ToNullable(this Option option) where T : struct => + option.Match(some => some, () => (T?)null); + + public static Option ToOption(this T? item) where T : struct => + item.HasValue ? Option.Some(item.Value) : Option.None; + + public static Option ToOption(this T? value, Func hasValue) where T : struct + => value is not null && hasValue(value.Value) ? Option.Some(value.Value) : Option.None(); + + public static Option ToOption(this T value, Func hasValue) where T : struct + => hasValue(value) ? Option.Some(value) : Option.None(); + } + + public static class OptionClassExtensions + { + public static Option ToOption(this T? item) where T : class => item ?? Option.None; + + public static Option ToOption(this T? value, Func hasValue) where T : class + => value is not null && hasValue(value) ? Option.Some(value) : Option.None(); + } } \ No newline at end of file diff --git a/Source/FunicularSwitch/Result.cs b/Source/FunicularSwitch/Result.cs index 69084e57..537d7d30 100644 --- a/Source/FunicularSwitch/Result.cs +++ b/Source/FunicularSwitch/Result.cs @@ -7,9 +7,9 @@ namespace FunicularSwitch { - public abstract partial class Result - { - public static Result Try(Action action, Func formatError) + public abstract partial class Result + { + public static Result Try(Action action, Func formatError) { try { @@ -22,46 +22,46 @@ public static Result Try(Action action, Func formatErro } } - public static async Task> Try(Func action, Func formatError) - { - try - { - await action(); - return No.Thing; - } - catch (Exception e) - { - return Error(formatError(e)); - } - } - } + public static async Task> Try(Func action, Func formatError) + { + try + { + await action(); + return No.Thing; + } + catch (Exception e) + { + return Error(formatError(e)); + } + } + } - [ResultType(ErrorType = typeof(string))] - public abstract partial class Result; + [ResultType(ErrorType = typeof(string))] + public abstract partial class Result; - public static partial class ResultExtension + public static partial class ResultExtension { // ReSharper disable once FieldCanBeMadeReadOnly.Global public static string ErrorSeparator = Environment.NewLine; [MergeError] public static string JoinErrors(this string error1, string error2) => - string.Join(ErrorSeparator, error1, error2); + string.Join(ErrorSeparator, error1, error2); public static Result As(this Result result) => - result.Bind(r => - { - if (r is T1 converted) - return converted; - return Result.Error($"Could not convert '{r?.GetType().Name}' to type {typeof(T1)}"); - }); + result.Bind(r => + { + if (r is T1 converted) + return converted; + return Result.Error($"Could not convert '{r?.GetType().Name}' to type {typeof(T1)}"); + }); public static Result As(this Result result) => result.As(); public static Result First(this IEnumerable candidates, Func predicate, Func noMatch) => - candidates - .FirstOrDefault(predicate) - .NotNull(noMatch); - + candidates + .FirstOrDefault(predicate) + .NotNull(noMatch); + } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Analyzers.Tests/FunicularSwitch.Analyzers.Tests.csproj b/Source/Tests/FunicularSwitch.Analyzers.Tests/FunicularSwitch.Analyzers.Tests.csproj index f8664048..b2260ba1 100644 --- a/Source/Tests/FunicularSwitch.Analyzers.Tests/FunicularSwitch.Analyzers.Tests.csproj +++ b/Source/Tests/FunicularSwitch.Analyzers.Tests/FunicularSwitch.Analyzers.Tests.csproj @@ -1,7 +1,7 @@ - net9.0 + net10.0 enable false diff --git a/Source/Tests/FunicularSwitch.Analyzers.Tests/MatchNullAnalyzerTest.cs b/Source/Tests/FunicularSwitch.Analyzers.Tests/MatchNullAnalyzerTest.cs index 20375baa..ba833835 100644 --- a/Source/Tests/FunicularSwitch.Analyzers.Tests/MatchNullAnalyzerTest.cs +++ b/Source/Tests/FunicularSwitch.Analyzers.Tests/MatchNullAnalyzerTest.cs @@ -30,7 +30,7 @@ await Verify( new MatchNullCodeFixProvider(), diagnostic => diagnostic.Should().ContainSingle().Which.Id.Should().Be("FS0001")); } - + [Fact] public async Task MatchNullOption_WithNamedSome_IsRecognized_FixIsApplied() { @@ -53,7 +53,7 @@ await Verify( new MatchNullCodeFixProvider(), diagnostic => diagnostic.Should().ContainSingle().Which.Id.Should().Be("FS0001")); } - + [Fact] public async Task MatchNullOption_MatchOnNewLine_MapAndGetValueOrDefaultAlsoOnNewLines() { @@ -77,7 +77,7 @@ await Verify( new MatchNullCodeFixProvider(), diagnostic => diagnostic.Should().ContainSingle().Which.Id.Should().Be("FS0001")); } - + [Fact] public async Task MatchNullOption_WithNewLines_IsNormalized_FixIsApplied() { @@ -102,7 +102,7 @@ await Verify( new MatchNullCodeFixProvider(), diagnostic => diagnostic.Should().ContainSingle().Which.Id.Should().Be("FS0001")); } - + [Fact] public async Task MatchNullOption_WithDefault_IsRecognized_FixIsApplied() { @@ -125,7 +125,7 @@ await Verify( new MatchNullCodeFixProvider(), diagnostic => diagnostic.Should().ContainSingle().Which.Id.Should().Be("FS0001")); } - + [Fact] public async Task MatchNullResult_IsRecognized_FixIsApplied() { diff --git a/Source/Tests/FunicularSwitch.Analyzers.Tests/VerifyAnalyzer.cs b/Source/Tests/FunicularSwitch.Analyzers.Tests/VerifyAnalyzer.cs index b3952ee3..189fe817 100644 --- a/Source/Tests/FunicularSwitch.Analyzers.Tests/VerifyAnalyzer.cs +++ b/Source/Tests/FunicularSwitch.Analyzers.Tests/VerifyAnalyzer.cs @@ -21,7 +21,7 @@ public class VerifyAnalyzer : VerifyBase private static readonly MetadataReference CSharpSymbolsReference = MetadataReference.CreateFromFile(typeof(CSharpCompilation).Assembly.Location); private static readonly MetadataReference CodeAnalysisReference = MetadataReference.CreateFromFile(typeof(Compilation).Assembly.Location); private static readonly MetadataReference FunicularSwitchReference = MetadataReference.CreateFromFile(typeof(Unit).Assembly.Location); - + // ReSharper disable once ExplicitCallerInfoArgument protected VerifyAnalyzer([CallerFilePath] string sourceFile = "") : base(sourceFile: sourceFile) { @@ -37,11 +37,11 @@ protected async Task Verify( [CallerMemberName] string callingMethod = "") { const string TestProjectName = "Test"; - + var projectId = ProjectId.CreateNewId(debugName: TestProjectName); var fileName = "File.cs"; var documentId = DocumentId.CreateNewId(projectId, debugName: fileName); - + var solution = new AdhocWorkspace() .CurrentSolution .AddProject(projectId, TestProjectName, TestProjectName, LanguageNames.CSharp) @@ -78,7 +78,7 @@ protected async Task Verify( syntaxTree.Should().NotBeNull(); var updatedCode = syntaxTree.ToFullString(); await CheckCompilation(solution.RemoveDocument(documentId).AddDocument(documentId, fileName, SourceText.From(updatedCode)).GetProject(projectId)!); - + var settings = new VerifySettings(); settings.UseFileName($"{Path.GetFileNameWithoutExtension(this.sourceFile)}_{callingMethod}_{d.Id}_{index}.cs"); await Verify(updatedCode, settings) @@ -97,7 +97,7 @@ async Task CheckCompilation(Project p) return c; } } - + private static async Task ApplyFix(Document document, CodeAction codeAction) { var operations = await codeAction.GetOperationsAsync(CancellationToken.None); diff --git a/Source/Tests/FunicularSwitch.Analyzers.Tests/packages.lock.json b/Source/Tests/FunicularSwitch.Analyzers.Tests/packages.lock.json index 23e660ba..9d074ed0 100644 --- a/Source/Tests/FunicularSwitch.Analyzers.Tests/packages.lock.json +++ b/Source/Tests/FunicularSwitch.Analyzers.Tests/packages.lock.json @@ -1,372 +1,330 @@ -{ - "version": 1, - "dependencies": { - "net9.0": { - "AwesomeAssertions": { - "type": "Direct", - "requested": "[9.0.0, )", - "resolved": "9.0.0", - "contentHash": "7HpVy5wMgzBX9vmelhDdM6EshiaGuz8apnhPcWGAkrbc7rlgmtzEDNcUiTNIicTkurBY37JZYdDiNMWrnKdADA==" - }, - "Microsoft.NET.Test.Sdk": { - "type": "Direct", - "requested": "[17.6.2, )", - "resolved": "17.6.2", - "contentHash": "+WFxuoFVBG0aewD9Psw+1I90kaRToQgMIzK20xNVRO4QRWR9Bou3qGefrZvMZ/xTC+6bAmZppFbpPbiHDzrPyg==", - "dependencies": { - "Microsoft.CodeCoverage": "17.6.2", - "Microsoft.TestPlatform.TestHost": "17.6.2" - } - }, - "Verify.XunitV3": { - "type": "Direct", - "requested": "[30.4.0, )", - "resolved": "30.4.0", - "contentHash": "PSz6WHP5FtB65C1UCprkmgYWiqAC4OiLfEXxovZd9INhYKwCk0F4sPq856iG806gE8b9O8sp9VoySvgpnWPubQ==", - "dependencies": { - "Argon": "0.30.1", - "DiffEngine": "16.2.3", - "Microsoft.Bcl.AsyncInterfaces": "9.0.6", - "SimpleInfoName": "3.1.2", - "Verify": "30.4.0", - "xunit.v3.extensibility.core": "2.0.3" - } - }, - "xunit.runner.visualstudio": { - "type": "Direct", - "requested": "[3.1.1, )", - "resolved": "3.1.1", - "contentHash": "gNu2zhnuwjq5vQlU4S7yK/lfaKZDLmtcu+vTjnhfTlMAUYn+Hmgu8IIX0UCwWepYkk+Szx03DHx1bDnc9Fd+9w==" - }, - "xunit.v3": { - "type": "Direct", - "requested": "[2.0.3, )", - "resolved": "2.0.3", - "contentHash": "Zx2r5xnUgyAIhI3cvfKaAr7Mj0Enn0zFhaMoTJ2hRYEPJlqlllUkTUvBBYalZQvpXGub/UQWq1nY5erD7Zv1cA==", - "dependencies": { - "xunit.analyzers": "1.22.0", - "xunit.v3.assert": "[2.0.3]", - "xunit.v3.core": "[2.0.3]" - } - }, - "Argon": { - "type": "Transitive", - "resolved": "0.30.1", - "contentHash": "kjKnBzxJ1Xp4Sh9B7inrP1YjefXH4X8hV4/J5EoDKloog09Kp4KUVoJS8xxYfUbUzJ+Xe5PKZm3hj5pi4ZuCZw==" - }, - "DiffEngine": { - "type": "Transitive", - "resolved": "16.2.3", - "contentHash": "QWnG0MR3//Ss0G0N9mIfe1HLOrOIRZqau0AOiLt9Gm53ZQf/TLvzoccTkczEW5ACkbhRY5m+p+W7bzFVln2GDw==", - "dependencies": { - "EmptyFiles": "8.10.1", - "System.Management": "8.0.0" - } - }, - "EmptyFiles": { - "type": "Transitive", - "resolved": "8.10.1", - "contentHash": "vhLPAqdKuo2qjVkrJbCyacGXO9XTha7G1R5amw44m877FDR/gqFjCfdncj8VyHAC6eNqrCXgYTbHJGO5+l3TJg==" - }, - "Humanizer.Core": { - "type": "Transitive", - "resolved": "2.14.1", - "contentHash": "lQKvtaTDOXnoVJ20ibTuSIOf2i0uO0MPbDhd1jm238I+U/2ZnRENj0cktKZhtchBMtCUSRQ5v4xBCUbKNmyVMw==" - }, - "Microsoft.Bcl.AsyncInterfaces": { - "type": "Transitive", - "resolved": "9.0.6", - "contentHash": "82rLw487j5jBXEi2r3WvA/cagOhcRREVRtet6izzjDMY+i392W5oNSN2KCtuIvlTpyMONEUD0MIlGAgDdsvQ/w==" - }, - "Microsoft.CodeAnalysis.Common": { - "type": "Transitive", - "resolved": "4.11.0", - "contentHash": "djf8ujmqYImFgB04UGtcsEhHrzVqzHowS+EEl/Yunc5LdrYrZhGBWUTXoCF0NzYXJxtfuD+UVQarWpvrNc94Qg==", - "dependencies": { - "Microsoft.CodeAnalysis.Analyzers": "3.3.4", - "System.Collections.Immutable": "8.0.0", - "System.Reflection.Metadata": "8.0.0" - } - }, - "Microsoft.CodeAnalysis.CSharp": { - "type": "Transitive", - "resolved": "4.11.0", - "contentHash": "6XYi2EusI8JT4y2l/F3VVVS+ISoIX9nqHsZRaG6W5aFeJ5BEuBosHfT/ABb73FN0RZ1Z3cj2j7cL28SToJPXOw==", - "dependencies": { - "Microsoft.CodeAnalysis.Analyzers": "3.3.4", - "Microsoft.CodeAnalysis.Common": "[4.11.0]", - "System.Collections.Immutable": "8.0.0", - "System.Reflection.Metadata": "8.0.0" - } - }, - "Microsoft.CodeAnalysis.CSharp.Workspaces": { - "type": "Transitive", - "resolved": "4.11.0", - "contentHash": "/oRJPIMvzOfiunIegSF6FEa4VvBAUSXlbLDKxyzXuOZN9nLHg3fHuX6Mr9JZLNIupbe2xqQZEmfsPxgB01vCmg==", - "dependencies": { - "Humanizer.Core": "2.14.1", - "Microsoft.CodeAnalysis.Analyzers": "3.3.4", - "Microsoft.CodeAnalysis.CSharp": "[4.11.0]", - "Microsoft.CodeAnalysis.Common": "[4.11.0]", - "Microsoft.CodeAnalysis.Workspaces.Common": "[4.11.0]", - "System.Collections.Immutable": "8.0.0", - "System.Composition": "8.0.0", - "System.IO.Pipelines": "8.0.0", - "System.Reflection.Metadata": "8.0.0", - "System.Threading.Channels": "7.0.0" - } - }, - "Microsoft.CodeAnalysis.Workspaces.Common": { - "type": "Transitive", - "resolved": "4.11.0", - "contentHash": "UtwEt42V7/LnvAcschSlmUXRLEj0poX1H7QeFbY5bezcS+tJBCssAq8C7GfisgIA0qZPo2xzOrwKGBbg51CetA==", - "dependencies": { - "Humanizer.Core": "2.14.1", - "Microsoft.CodeAnalysis.Analyzers": "3.3.4", - "Microsoft.CodeAnalysis.Common": "[4.11.0]", - "System.Collections.Immutable": "8.0.0", - "System.Composition": "8.0.0", - "System.IO.Pipelines": "8.0.0", - "System.Reflection.Metadata": "8.0.0", - "System.Threading.Channels": "7.0.0" - } - }, - "Microsoft.CodeCoverage": { - "type": "Transitive", - "resolved": "17.6.2", - "contentHash": "t+DjPlq7GIxIkOK/jiTmNeiEMVkfVCynBEZyV+IMg2ro43NugKExng+7a04R1fBLi+d6voxjeDxL2ozdl+XyVg==" - }, - "Microsoft.Testing.Extensions.TrxReport.Abstractions": { - "type": "Transitive", - "resolved": "1.6.3", - "contentHash": "0MdowM+3IDVWE5VBzVe9NvxsE4caSbM3fO+jlWVzEBr/Vnc3BWx+uV/Ex0dLLpkxkeUKH2gGWTNLb39rw3DDqw==", - "dependencies": { - "Microsoft.Testing.Platform": "1.6.3" - } - }, - "Microsoft.Testing.Platform": { - "type": "Transitive", - "resolved": "1.6.3", - "contentHash": "DqMZukaPo+vKzColfqd1I5qZebfISZT6ND70AOem/dYQmHsaMN0xg/JG7E0e80rwfxL7wAA4ylSg8j6KJf1Tuw==" - }, - "Microsoft.Testing.Platform.MSBuild": { - "type": "Transitive", - "resolved": "1.6.3", - "contentHash": "PXSYI5Iae29GM5636zOL8PlQD1YyOa9cfzfYLR43hrLjjK7RDJgMTvgAet3oZLgDTvz6pbzABZvhx+S/W5m8YA==", - "dependencies": { - "Microsoft.Testing.Platform": "1.6.3" - } - }, - "Microsoft.TestPlatform.ObjectModel": { - "type": "Transitive", - "resolved": "17.6.2", - "contentHash": "I3S/oELDAe/ckFltBnPb0PV+gADbpc8n0yGTtYfTd7q1hd0cAZKn/FrYoegA/rGws8fEFyJD/2PB6uq+nIjWsg==", - "dependencies": { - "NuGet.Frameworks": "6.5.0", - "System.Reflection.Metadata": "1.6.0" - } - }, - "Microsoft.TestPlatform.TestHost": { - "type": "Transitive", - "resolved": "17.6.2", - "contentHash": "WszrF1CoG+Y2bA4WJFxlaPADQCWUuNP6TCxVRuScmP6DQsNvDDuOO9XiUxCA+wPmNOSyWVh9EGrPu7JoUpf2gA==", - "dependencies": { - "Microsoft.TestPlatform.ObjectModel": "17.6.2", - "Newtonsoft.Json": "13.0.1" - } - }, - "Newtonsoft.Json": { - "type": "Transitive", - "resolved": "13.0.1", - "contentHash": "ppPFpBcvxdsfUonNcvITKqLl3bqxWbDCZIzDWHzjpdAHRFfZe0Dw9HmA0+za13IdyrgJwpkDTDA9fHaxOrt20A==" - }, - "NuGet.Frameworks": { - "type": "Transitive", - "resolved": "6.5.0", - "contentHash": "QWINE2x3MbTODsWT1Gh71GaGb5icBz4chS8VYvTgsBnsi8esgN6wtHhydd7fvToWECYGq7T4cgBBDiKD/363fg==" - }, - "SimpleInfoName": { - "type": "Transitive", - "resolved": "3.1.2", - "contentHash": "/OoEZQxSW6DeTJ9nfrg8BLCOCWpxBiWHV4NkG3t+Xpe8tvzm7yCwKwxkhpauMl3fg9OjlIjJMKX61H6VavLkrw==" - }, - "System.CodeDom": { - "type": "Transitive", - "resolved": "8.0.0", - "contentHash": "WTlRjL6KWIMr/pAaq3rYqh0TJlzpouaQ/W1eelssHgtlwHAH25jXTkUphTYx9HaIIf7XA6qs/0+YhtLEQRkJ+Q==" - }, - "System.Collections.Immutable": { - "type": "Transitive", - "resolved": "8.0.0", - "contentHash": "AurL6Y5BA1WotzlEvVaIDpqzpIPvYnnldxru8oXJU2yFxFUy3+pNXjXd1ymO+RA0rq0+590Q8gaz2l3Sr7fmqg==" - }, - "System.Composition": { - "type": "Transitive", - "resolved": "8.0.0", - "contentHash": "E9oO9olNNxA39J8CxQwf7ceIPm+j/B/PhYpyK9M4LhN/OLLRw6u5fNInkhVqaWueMB9iXxYqnwqwgz+W91loIA==", - "dependencies": { - "System.Composition.AttributedModel": "8.0.0", - "System.Composition.Convention": "8.0.0", - "System.Composition.Hosting": "8.0.0", - "System.Composition.Runtime": "8.0.0", - "System.Composition.TypedParts": "8.0.0" - } - }, - "System.Composition.AttributedModel": { - "type": "Transitive", - "resolved": "8.0.0", - "contentHash": "NyElSuvmBMYdn2iPG0n29i7Igu0bq99izOP3MAtEwskY3OP9jqsavvVmPn9lesVaj/KT/o/QkNjA43dOJTsDQw==" - }, - "System.Composition.Convention": { - "type": "Transitive", - "resolved": "8.0.0", - "contentHash": "UuVkc1B3vQU/LzEbWLMZ1aYVssv4rpShzf8wPEyrUqoGNqdYKREmB8bXR73heOMKkwS6ZnPz3PjGODT2MenukQ==", - "dependencies": { - "System.Composition.AttributedModel": "8.0.0" - } - }, - "System.Composition.Hosting": { - "type": "Transitive", - "resolved": "8.0.0", - "contentHash": "qwbONqoxlazxcbiohvb3t1JWZgKIKcRdXS5uEeLbo5wtuBupIbAvdC3PYTAeBCZrZeERvrtAbhYHuuS43Zr1bQ==", - "dependencies": { - "System.Composition.Runtime": "8.0.0" - } - }, - "System.Composition.Runtime": { - "type": "Transitive", - "resolved": "8.0.0", - "contentHash": "G+kRyB5/6+3ucRRQz+DF4uSHGqpkK8Q4ilVdbt4zvxpmvLVZNmSkyFAQpJLcbOyVF85aomJx0m+TGMDVlwx7ZQ==" - }, - "System.Composition.TypedParts": { - "type": "Transitive", - "resolved": "8.0.0", - "contentHash": "DsSklhuA+Dsgo3ZZrar8hjBFvq1wa1grrkNCTt+6SoX3vq0Vy+HXJnVXrU/nNH1BjlGH684A7h4hJQHZd/u5mA==", - "dependencies": { - "System.Composition.AttributedModel": "8.0.0", - "System.Composition.Hosting": "8.0.0", - "System.Composition.Runtime": "8.0.0" - } - }, - "System.IO.Pipelines": { - "type": "Transitive", - "resolved": "8.0.0", - "contentHash": "FHNOatmUq0sqJOkTx+UF/9YK1f180cnW5FVqnQMvYUN0elp6wFzbtPSiqbo1/ru8ICp43JM1i7kKkk6GsNGHlA==" - }, - "System.Management": { - "type": "Transitive", - "resolved": "8.0.0", - "contentHash": "jrK22i5LRzxZCfGb+tGmke2VH7oE0DvcDlJ1HAKYU8cPmD8XnpUT0bYn2Gy98GEhGjtfbR/sxKTVb+dE770pfA==", - "dependencies": { - "System.CodeDom": "8.0.0" - } - }, - "System.Reflection.Metadata": { - "type": "Transitive", - "resolved": "8.0.0", - "contentHash": "ptvgrFh7PvWI8bcVqG5rsA/weWM09EnthFHR5SCnS6IN+P4mj6rE1lBDC4U8HL9/57htKAqy4KQ3bBj84cfYyQ==", - "dependencies": { - "System.Collections.Immutable": "8.0.0" - } - }, - "System.Threading.Channels": { - "type": "Transitive", - "resolved": "7.0.0", - "contentHash": "qmeeYNROMsONF6ndEZcIQ+VxR4Q/TX/7uIVLJqtwIWL7dDWeh0l1UIqgo4wYyjG//5lUNhwkLDSFl+pAWO6oiA==" - }, - "System.Threading.Tasks.Extensions": { - "type": "Transitive", - "resolved": "4.6.3", - "contentHash": "7sCiwilJLYbTZELaKnc7RecBBXWXA+xMLQWZKWawBxYjp6DBlSE3v9/UcvKBvr1vv2tTOhipiogM8rRmxlhrVA==" - }, - "Verify": { - "type": "Transitive", - "resolved": "30.4.0", - "contentHash": "ZoPOO21Dm3GS3V1fYeEcedCJdk5XS4X6mYBwQk+813QFkPLNr4/EvCDtGXH2NrFXtmWhu0WiIiiLMa0cZ7ynqQ==", - "dependencies": { - "Argon": "0.30.1", - "DiffEngine": "16.2.3", - "SimpleInfoName": "3.1.2" - } - }, - "xunit.analyzers": { - "type": "Transitive", - "resolved": "1.22.0", - "contentHash": "MXTmnlJLQPSJfgxibeFgpUP5sj+3649BWkf3WgeUhAVUrI3nXzWE+BGH4OPfWkG2Q5x0SzIKgegikNhnfMgaXQ==" - }, - "xunit.v3.assert": { - "type": "Transitive", - "resolved": "2.0.3", - "contentHash": "6uTT9zLyg7YtP8fAZqOFcCpeGXtsh6K1+QVHHJl/aoQObr2qHvhGMuZbm5UYf4z49ytpQt6NzP6Iq4uWEBBh4g==" - }, - "xunit.v3.common": { - "type": "Transitive", - "resolved": "2.0.3", - "contentHash": "u3kTB9Fqo6X8keccsW4Zkn87nQNxMNZGm1rjJjCC0yThKP77N8H52Fx6CaochjGeGzEc2w9CUDu7C2ygu61bYg==", - "dependencies": { - "Microsoft.Bcl.AsyncInterfaces": "6.0.0" - } - }, - "xunit.v3.core": { - "type": "Transitive", - "resolved": "2.0.3", - "contentHash": "KN2xOHNVGXI6AsPKatV9urqVs2YIGQ+2klY6HLys/oQauBVoS2W2+BRZz1rnwoJ+5aK+2+gfe1rLKi8iInT9Ow==", - "dependencies": { - "Microsoft.Testing.Platform.MSBuild": "1.6.3", - "xunit.v3.extensibility.core": "[2.0.3]", - "xunit.v3.runner.inproc.console": "[2.0.3]" - } - }, - "xunit.v3.extensibility.core": { - "type": "Transitive", - "resolved": "2.0.3", - "contentHash": "1uxfWR72veq0b5/3ghEA2zRAkdqmnA9ahCxWr1VEb2q6MBNKx6aHsAL85PyY172f1dsvUMqh7BSelaHsR5Df5w==", - "dependencies": { - "xunit.v3.common": "[2.0.3]" - } - }, - "xunit.v3.runner.common": { - "type": "Transitive", - "resolved": "2.0.3", - "contentHash": "Q+43ogqiJlWOG3l6Me7iNTm68CvVdX16OsHDoQcKCUEvWXHyiSRGBkcmMAT3OVENH0wS69QHi0q4mb3VsAbclA==", - "dependencies": { - "xunit.v3.common": "[2.0.3]" - } - }, - "xunit.v3.runner.inproc.console": { - "type": "Transitive", - "resolved": "2.0.3", - "contentHash": "S65IrpHbt9nHotyfs6MnjNsc8bZUMBLHHJcyky4ywDNKx+k07sjwaGb+otLQA4Z6FPCGIPrL7u0ThQm76FxBZw==", - "dependencies": { - "Microsoft.Testing.Extensions.TrxReport.Abstractions": "1.6.3", - "Microsoft.Testing.Platform": "1.6.3", - "xunit.v3.extensibility.core": "[2.0.3]", - "xunit.v3.runner.common": "[2.0.3]" - } - }, - "funicularswitch": { - "type": "Project", - "dependencies": { - "FunicularSwitch.Analyzers": "[1.0.0, )", - "System.Threading.Tasks.Extensions": "[4.6.3, )" - } - }, - "FunicularSwitch.Analyzers": { - "type": "Project" - }, - "FunicularSwitch.Analyzers.Analyzers": { - "type": "Project", - "dependencies": { - "Microsoft.CodeAnalysis.CSharp": "[4.11.0, )" - } - }, - "funicularswitch.analyzers.codefixproviders": { - "type": "Project", - "dependencies": { - "Microsoft.CodeAnalysis.CSharp": "[4.11.0, )", - "Microsoft.CodeAnalysis.CSharp.Workspaces": "[4.11.0, )" - } - } - } - } +{ + "version": 1, + "dependencies": { + "net10.0": { + "AwesomeAssertions": { + "type": "Direct", + "requested": "[9.0.0, )", + "resolved": "9.0.0", + "contentHash": "7HpVy5wMgzBX9vmelhDdM6EshiaGuz8apnhPcWGAkrbc7rlgmtzEDNcUiTNIicTkurBY37JZYdDiNMWrnKdADA==" + }, + "Microsoft.NET.Test.Sdk": { + "type": "Direct", + "requested": "[17.6.2, )", + "resolved": "17.6.2", + "contentHash": "+WFxuoFVBG0aewD9Psw+1I90kaRToQgMIzK20xNVRO4QRWR9Bou3qGefrZvMZ/xTC+6bAmZppFbpPbiHDzrPyg==", + "dependencies": { + "Microsoft.CodeCoverage": "17.6.2", + "Microsoft.TestPlatform.TestHost": "17.6.2" + } + }, + "Verify.XunitV3": { + "type": "Direct", + "requested": "[30.4.0, )", + "resolved": "30.4.0", + "contentHash": "PSz6WHP5FtB65C1UCprkmgYWiqAC4OiLfEXxovZd9INhYKwCk0F4sPq856iG806gE8b9O8sp9VoySvgpnWPubQ==", + "dependencies": { + "Argon": "0.30.1", + "DiffEngine": "16.2.3", + "Microsoft.Bcl.AsyncInterfaces": "9.0.6", + "SimpleInfoName": "3.1.2", + "Verify": "30.4.0", + "xunit.v3.extensibility.core": "2.0.3" + } + }, + "xunit.runner.visualstudio": { + "type": "Direct", + "requested": "[3.1.1, )", + "resolved": "3.1.1", + "contentHash": "gNu2zhnuwjq5vQlU4S7yK/lfaKZDLmtcu+vTjnhfTlMAUYn+Hmgu8IIX0UCwWepYkk+Szx03DHx1bDnc9Fd+9w==" + }, + "xunit.v3": { + "type": "Direct", + "requested": "[2.0.3, )", + "resolved": "2.0.3", + "contentHash": "Zx2r5xnUgyAIhI3cvfKaAr7Mj0Enn0zFhaMoTJ2hRYEPJlqlllUkTUvBBYalZQvpXGub/UQWq1nY5erD7Zv1cA==", + "dependencies": { + "xunit.analyzers": "1.22.0", + "xunit.v3.assert": "[2.0.3]", + "xunit.v3.core": "[2.0.3]" + } + }, + "Argon": { + "type": "Transitive", + "resolved": "0.30.1", + "contentHash": "kjKnBzxJ1Xp4Sh9B7inrP1YjefXH4X8hV4/J5EoDKloog09Kp4KUVoJS8xxYfUbUzJ+Xe5PKZm3hj5pi4ZuCZw==" + }, + "DiffEngine": { + "type": "Transitive", + "resolved": "16.2.3", + "contentHash": "QWnG0MR3//Ss0G0N9mIfe1HLOrOIRZqau0AOiLt9Gm53ZQf/TLvzoccTkczEW5ACkbhRY5m+p+W7bzFVln2GDw==", + "dependencies": { + "EmptyFiles": "8.10.1", + "System.Management": "8.0.0" + } + }, + "EmptyFiles": { + "type": "Transitive", + "resolved": "8.10.1", + "contentHash": "vhLPAqdKuo2qjVkrJbCyacGXO9XTha7G1R5amw44m877FDR/gqFjCfdncj8VyHAC6eNqrCXgYTbHJGO5+l3TJg==" + }, + "Humanizer.Core": { + "type": "Transitive", + "resolved": "2.14.1", + "contentHash": "lQKvtaTDOXnoVJ20ibTuSIOf2i0uO0MPbDhd1jm238I+U/2ZnRENj0cktKZhtchBMtCUSRQ5v4xBCUbKNmyVMw==" + }, + "Microsoft.Bcl.AsyncInterfaces": { + "type": "Transitive", + "resolved": "9.0.6", + "contentHash": "82rLw487j5jBXEi2r3WvA/cagOhcRREVRtet6izzjDMY+i392W5oNSN2KCtuIvlTpyMONEUD0MIlGAgDdsvQ/w==" + }, + "Microsoft.CodeAnalysis.Common": { + "type": "Transitive", + "resolved": "4.11.0", + "contentHash": "djf8ujmqYImFgB04UGtcsEhHrzVqzHowS+EEl/Yunc5LdrYrZhGBWUTXoCF0NzYXJxtfuD+UVQarWpvrNc94Qg==", + "dependencies": { + "Microsoft.CodeAnalysis.Analyzers": "3.3.4" + } + }, + "Microsoft.CodeAnalysis.CSharp": { + "type": "Transitive", + "resolved": "4.11.0", + "contentHash": "6XYi2EusI8JT4y2l/F3VVVS+ISoIX9nqHsZRaG6W5aFeJ5BEuBosHfT/ABb73FN0RZ1Z3cj2j7cL28SToJPXOw==", + "dependencies": { + "Microsoft.CodeAnalysis.Analyzers": "3.3.4", + "Microsoft.CodeAnalysis.Common": "[4.11.0]" + } + }, + "Microsoft.CodeAnalysis.CSharp.Workspaces": { + "type": "Transitive", + "resolved": "4.11.0", + "contentHash": "/oRJPIMvzOfiunIegSF6FEa4VvBAUSXlbLDKxyzXuOZN9nLHg3fHuX6Mr9JZLNIupbe2xqQZEmfsPxgB01vCmg==", + "dependencies": { + "Humanizer.Core": "2.14.1", + "Microsoft.CodeAnalysis.Analyzers": "3.3.4", + "Microsoft.CodeAnalysis.CSharp": "[4.11.0]", + "Microsoft.CodeAnalysis.Common": "[4.11.0]", + "Microsoft.CodeAnalysis.Workspaces.Common": "[4.11.0]", + "System.Composition": "8.0.0" + } + }, + "Microsoft.CodeAnalysis.Workspaces.Common": { + "type": "Transitive", + "resolved": "4.11.0", + "contentHash": "UtwEt42V7/LnvAcschSlmUXRLEj0poX1H7QeFbY5bezcS+tJBCssAq8C7GfisgIA0qZPo2xzOrwKGBbg51CetA==", + "dependencies": { + "Humanizer.Core": "2.14.1", + "Microsoft.CodeAnalysis.Analyzers": "3.3.4", + "Microsoft.CodeAnalysis.Common": "[4.11.0]", + "System.Composition": "8.0.0" + } + }, + "Microsoft.CodeCoverage": { + "type": "Transitive", + "resolved": "17.6.2", + "contentHash": "t+DjPlq7GIxIkOK/jiTmNeiEMVkfVCynBEZyV+IMg2ro43NugKExng+7a04R1fBLi+d6voxjeDxL2ozdl+XyVg==" + }, + "Microsoft.Testing.Extensions.TrxReport.Abstractions": { + "type": "Transitive", + "resolved": "1.6.3", + "contentHash": "0MdowM+3IDVWE5VBzVe9NvxsE4caSbM3fO+jlWVzEBr/Vnc3BWx+uV/Ex0dLLpkxkeUKH2gGWTNLb39rw3DDqw==", + "dependencies": { + "Microsoft.Testing.Platform": "1.6.3" + } + }, + "Microsoft.Testing.Platform": { + "type": "Transitive", + "resolved": "1.6.3", + "contentHash": "DqMZukaPo+vKzColfqd1I5qZebfISZT6ND70AOem/dYQmHsaMN0xg/JG7E0e80rwfxL7wAA4ylSg8j6KJf1Tuw==" + }, + "Microsoft.Testing.Platform.MSBuild": { + "type": "Transitive", + "resolved": "1.6.3", + "contentHash": "PXSYI5Iae29GM5636zOL8PlQD1YyOa9cfzfYLR43hrLjjK7RDJgMTvgAet3oZLgDTvz6pbzABZvhx+S/W5m8YA==", + "dependencies": { + "Microsoft.Testing.Platform": "1.6.3" + } + }, + "Microsoft.TestPlatform.ObjectModel": { + "type": "Transitive", + "resolved": "17.6.2", + "contentHash": "I3S/oELDAe/ckFltBnPb0PV+gADbpc8n0yGTtYfTd7q1hd0cAZKn/FrYoegA/rGws8fEFyJD/2PB6uq+nIjWsg==", + "dependencies": { + "NuGet.Frameworks": "6.5.0" + } + }, + "Microsoft.TestPlatform.TestHost": { + "type": "Transitive", + "resolved": "17.6.2", + "contentHash": "WszrF1CoG+Y2bA4WJFxlaPADQCWUuNP6TCxVRuScmP6DQsNvDDuOO9XiUxCA+wPmNOSyWVh9EGrPu7JoUpf2gA==", + "dependencies": { + "Microsoft.TestPlatform.ObjectModel": "17.6.2", + "Newtonsoft.Json": "13.0.1" + } + }, + "Newtonsoft.Json": { + "type": "Transitive", + "resolved": "13.0.1", + "contentHash": "ppPFpBcvxdsfUonNcvITKqLl3bqxWbDCZIzDWHzjpdAHRFfZe0Dw9HmA0+za13IdyrgJwpkDTDA9fHaxOrt20A==" + }, + "NuGet.Frameworks": { + "type": "Transitive", + "resolved": "6.5.0", + "contentHash": "QWINE2x3MbTODsWT1Gh71GaGb5icBz4chS8VYvTgsBnsi8esgN6wtHhydd7fvToWECYGq7T4cgBBDiKD/363fg==" + }, + "SimpleInfoName": { + "type": "Transitive", + "resolved": "3.1.2", + "contentHash": "/OoEZQxSW6DeTJ9nfrg8BLCOCWpxBiWHV4NkG3t+Xpe8tvzm7yCwKwxkhpauMl3fg9OjlIjJMKX61H6VavLkrw==" + }, + "System.CodeDom": { + "type": "Transitive", + "resolved": "8.0.0", + "contentHash": "WTlRjL6KWIMr/pAaq3rYqh0TJlzpouaQ/W1eelssHgtlwHAH25jXTkUphTYx9HaIIf7XA6qs/0+YhtLEQRkJ+Q==" + }, + "System.Composition": { + "type": "Transitive", + "resolved": "8.0.0", + "contentHash": "E9oO9olNNxA39J8CxQwf7ceIPm+j/B/PhYpyK9M4LhN/OLLRw6u5fNInkhVqaWueMB9iXxYqnwqwgz+W91loIA==", + "dependencies": { + "System.Composition.AttributedModel": "8.0.0", + "System.Composition.Convention": "8.0.0", + "System.Composition.Hosting": "8.0.0", + "System.Composition.Runtime": "8.0.0", + "System.Composition.TypedParts": "8.0.0" + } + }, + "System.Composition.AttributedModel": { + "type": "Transitive", + "resolved": "8.0.0", + "contentHash": "NyElSuvmBMYdn2iPG0n29i7Igu0bq99izOP3MAtEwskY3OP9jqsavvVmPn9lesVaj/KT/o/QkNjA43dOJTsDQw==" + }, + "System.Composition.Convention": { + "type": "Transitive", + "resolved": "8.0.0", + "contentHash": "UuVkc1B3vQU/LzEbWLMZ1aYVssv4rpShzf8wPEyrUqoGNqdYKREmB8bXR73heOMKkwS6ZnPz3PjGODT2MenukQ==", + "dependencies": { + "System.Composition.AttributedModel": "8.0.0" + } + }, + "System.Composition.Hosting": { + "type": "Transitive", + "resolved": "8.0.0", + "contentHash": "qwbONqoxlazxcbiohvb3t1JWZgKIKcRdXS5uEeLbo5wtuBupIbAvdC3PYTAeBCZrZeERvrtAbhYHuuS43Zr1bQ==", + "dependencies": { + "System.Composition.Runtime": "8.0.0" + } + }, + "System.Composition.Runtime": { + "type": "Transitive", + "resolved": "8.0.0", + "contentHash": "G+kRyB5/6+3ucRRQz+DF4uSHGqpkK8Q4ilVdbt4zvxpmvLVZNmSkyFAQpJLcbOyVF85aomJx0m+TGMDVlwx7ZQ==" + }, + "System.Composition.TypedParts": { + "type": "Transitive", + "resolved": "8.0.0", + "contentHash": "DsSklhuA+Dsgo3ZZrar8hjBFvq1wa1grrkNCTt+6SoX3vq0Vy+HXJnVXrU/nNH1BjlGH684A7h4hJQHZd/u5mA==", + "dependencies": { + "System.Composition.AttributedModel": "8.0.0", + "System.Composition.Hosting": "8.0.0", + "System.Composition.Runtime": "8.0.0" + } + }, + "System.Management": { + "type": "Transitive", + "resolved": "8.0.0", + "contentHash": "jrK22i5LRzxZCfGb+tGmke2VH7oE0DvcDlJ1HAKYU8cPmD8XnpUT0bYn2Gy98GEhGjtfbR/sxKTVb+dE770pfA==", + "dependencies": { + "System.CodeDom": "8.0.0" + } + }, + "Verify": { + "type": "Transitive", + "resolved": "30.4.0", + "contentHash": "ZoPOO21Dm3GS3V1fYeEcedCJdk5XS4X6mYBwQk+813QFkPLNr4/EvCDtGXH2NrFXtmWhu0WiIiiLMa0cZ7ynqQ==", + "dependencies": { + "Argon": "0.30.1", + "DiffEngine": "16.2.3", + "SimpleInfoName": "3.1.2" + } + }, + "xunit.analyzers": { + "type": "Transitive", + "resolved": "1.22.0", + "contentHash": "MXTmnlJLQPSJfgxibeFgpUP5sj+3649BWkf3WgeUhAVUrI3nXzWE+BGH4OPfWkG2Q5x0SzIKgegikNhnfMgaXQ==" + }, + "xunit.v3.assert": { + "type": "Transitive", + "resolved": "2.0.3", + "contentHash": "6uTT9zLyg7YtP8fAZqOFcCpeGXtsh6K1+QVHHJl/aoQObr2qHvhGMuZbm5UYf4z49ytpQt6NzP6Iq4uWEBBh4g==" + }, + "xunit.v3.common": { + "type": "Transitive", + "resolved": "2.0.3", + "contentHash": "u3kTB9Fqo6X8keccsW4Zkn87nQNxMNZGm1rjJjCC0yThKP77N8H52Fx6CaochjGeGzEc2w9CUDu7C2ygu61bYg==", + "dependencies": { + "Microsoft.Bcl.AsyncInterfaces": "6.0.0" + } + }, + "xunit.v3.core": { + "type": "Transitive", + "resolved": "2.0.3", + "contentHash": "KN2xOHNVGXI6AsPKatV9urqVs2YIGQ+2klY6HLys/oQauBVoS2W2+BRZz1rnwoJ+5aK+2+gfe1rLKi8iInT9Ow==", + "dependencies": { + "Microsoft.Testing.Platform.MSBuild": "1.6.3", + "xunit.v3.extensibility.core": "[2.0.3]", + "xunit.v3.runner.inproc.console": "[2.0.3]" + } + }, + "xunit.v3.extensibility.core": { + "type": "Transitive", + "resolved": "2.0.3", + "contentHash": "1uxfWR72veq0b5/3ghEA2zRAkdqmnA9ahCxWr1VEb2q6MBNKx6aHsAL85PyY172f1dsvUMqh7BSelaHsR5Df5w==", + "dependencies": { + "xunit.v3.common": "[2.0.3]" + } + }, + "xunit.v3.runner.common": { + "type": "Transitive", + "resolved": "2.0.3", + "contentHash": "Q+43ogqiJlWOG3l6Me7iNTm68CvVdX16OsHDoQcKCUEvWXHyiSRGBkcmMAT3OVENH0wS69QHi0q4mb3VsAbclA==", + "dependencies": { + "xunit.v3.common": "[2.0.3]" + } + }, + "xunit.v3.runner.inproc.console": { + "type": "Transitive", + "resolved": "2.0.3", + "contentHash": "S65IrpHbt9nHotyfs6MnjNsc8bZUMBLHHJcyky4ywDNKx+k07sjwaGb+otLQA4Z6FPCGIPrL7u0ThQm76FxBZw==", + "dependencies": { + "Microsoft.Testing.Extensions.TrxReport.Abstractions": "1.6.3", + "Microsoft.Testing.Platform": "1.6.3", + "xunit.v3.extensibility.core": "[2.0.3]", + "xunit.v3.runner.common": "[2.0.3]" + } + }, + "funicularswitch": { + "type": "Project", + "dependencies": { + "FunicularSwitch.Analyzers": "[1.0.0, )" + } + }, + "FunicularSwitch.Analyzers": { + "type": "Project" + }, + "FunicularSwitch.Analyzers.Analyzers": { + "type": "Project", + "dependencies": { + "Microsoft.CodeAnalysis.CSharp": "[4.11.0, )" + } + }, + "funicularswitch.analyzers.codefixproviders": { + "type": "Project", + "dependencies": { + "Microsoft.CodeAnalysis.CSharp": "[4.11.0, )", + "Microsoft.CodeAnalysis.CSharp.Workspaces": "[4.11.0, )" + } + } + } + } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.AwesomeAssertions.Test/VerifySourceGenerator.cs b/Source/Tests/FunicularSwitch.Generators.AwesomeAssertions.Test/VerifySourceGenerator.cs index e8b657e0..27d5eb44 100644 --- a/Source/Tests/FunicularSwitch.Generators.AwesomeAssertions.Test/VerifySourceGenerator.cs +++ b/Source/Tests/FunicularSwitch.Generators.AwesomeAssertions.Test/VerifySourceGenerator.cs @@ -31,7 +31,7 @@ protected Task Verify(IEnumerable assemblies, Action assemblies, Action assemblies, string? subfolder = null) => Verify( - assemblies, + assemblies, (compilation, _) => { var diagnostics = compilation.GetDiagnostics() diff --git a/Source/Tests/FunicularSwitch.Generators.Consumer.StandardMinLangVersion/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.EnumTypeGenerator/Attributes.g.cs b/Source/Tests/FunicularSwitch.Generators.Consumer.StandardMinLangVersion/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.EnumTypeGenerator/Attributes.g.cs index f8adff12..3a2720d9 100644 --- a/Source/Tests/FunicularSwitch.Generators.Consumer.StandardMinLangVersion/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.EnumTypeGenerator/Attributes.g.cs +++ b/Source/Tests/FunicularSwitch.Generators.Consumer.StandardMinLangVersion/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.EnumTypeGenerator/Attributes.g.cs @@ -7,10 +7,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -23,16 +23,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -41,21 +41,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Consumer.StandardMinLangVersion/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/Attributes.g.cs b/Source/Tests/FunicularSwitch.Generators.Consumer.StandardMinLangVersion/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/Attributes.g.cs index 8dddcd08..ba6d29a4 100644 --- a/Source/Tests/FunicularSwitch.Generators.Consumer.StandardMinLangVersion/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/Attributes.g.cs +++ b/Source/Tests/FunicularSwitch.Generators.Consumer.StandardMinLangVersion/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/Attributes.g.cs @@ -4,10 +4,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Consumer.StandardMinLangVersion/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/StandardMinLangVersion.Result.g.cs b/Source/Tests/FunicularSwitch.Generators.Consumer.StandardMinLangVersion/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/StandardMinLangVersion.Result.g.cs index 35b06547..a3f19d21 100644 --- a/Source/Tests/FunicularSwitch.Generators.Consumer.StandardMinLangVersion/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/StandardMinLangVersion.Result.g.cs +++ b/Source/Tests/FunicularSwitch.Generators.Consumer.StandardMinLangVersion/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/StandardMinLangVersion.Result.g.cs @@ -1,610 +1,610 @@ -#nullable enable +#nullable enable using global::System.Linq; using System; -namespace StandardMinLangVersion +namespace StandardMinLangVersion { -#pragma warning disable 1591 - public abstract partial class Result - { - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static Result Error(String details) => new Result.Error_(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static ResultError Error(String details) => new(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static Result Ok(T value) => new Result.Ok_(value); - public bool IsError => GetType().GetGenericTypeDefinition() == typeof(Result<>.Error_); - public bool IsOk => !IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public abstract String? GetErrorOrDefault(); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result Try( global::System.Func action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - } - - public abstract partial class Result : Result, global::System.Collections.Generic.IEnumerable - { - - [global::System.Diagnostics.DebuggerNonUserCode] - - public new static Result Error(String message) => Error(message); - - [global::System.Diagnostics.DebuggerNonUserCode] - - public static Result Ok(T value) => Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator Result(T value) => Result.Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator Result(ResultError myResultError) => myResultError.WithOk(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator true(Result result) => result.IsOk; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator false(Result result) => result.IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !(Result result) => result.IsError; - - //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode - bool Equals(Result other) => this switch - { - Ok_ ok => ok.Equals((object)other), - Error_ error => error.Equals((object)other), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") - }; - - public override int GetHashCode() => this switch - { - Ok_ ok => ok.GetHashCode(), - Error_ error => error.GetHashCode(), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") - }; - - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != this.GetType()) return false; - return Equals((Result)obj); - } - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Result? left, Result? right) => Equals(left, right); - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Result? left, Result? right) => !Equals(left, right); - - [global::System.Diagnostics.DebuggerStepThrough] - public void Match( global::System.Action ok, global::System.Action? error = null) => Match( - v => - { - ok.Invoke(v); - return 42; - }, - err => - { - error?.Invoke(err); - return 42; - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T1 Match( global::System.Func ok, global::System.Func error) - { - return this switch - { - Ok_ okResult => ok(okResult.Value), - Error_ errorResult => error(errorResult.Details), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) - { - return this switch - { - Ok_ okResult => await ok(okResult.Value).ConfigureAwait(false), - Error_ errorResult => await error(errorResult.Details).ConfigureAwait(false), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => - Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); - - [global::System.Diagnostics.DebuggerStepThrough] - public async global::System.Threading.Tasks.Task Match( global::System.Func ok) - { - if (this is Ok_ okResult) await ok(okResult.Value).ConfigureAwait(false); - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public T Match( global::System.Func error) => Match(v => v, error); - - [global::System.Diagnostics.DebuggerStepThrough] - - public Result Bind( global::System.Func> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return bind(ok.Value); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return await bind(ok.Value).ConfigureAwait(false); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public Result Map( global::System.Func map) - { - switch (this) - { - case Ok_ ok: - try - { - return Result.Ok(map(ok.Value)); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Map( - global::System.Func> map) - { - switch (this) - { - case Ok_ ok: - try - { - return Result.Ok(await map(ok.Value).ConfigureAwait(false)); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - //createGenericResultConversions - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T? GetValueOrDefault() - => Match( - v => (T?)v, - _ => default - ); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T GetValueOrDefault( global::System.Func defaultValue) - => Match( - v => v, - _ => defaultValue() - ); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T GetValueOrDefault(T defaultValue) - => Match( - v => v, - _ => defaultValue - ); - - [global::System.Diagnostics.DebuggerStepThrough] - public T GetValueOrThrow() - => Match( - v => v, - details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); - - - public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); - - - public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); - global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); - - public sealed partial class Ok_ : Result - { - public T Value { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Ok_(T value) => Value = value; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override String? GetErrorOrDefault() => null; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Ok_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Ok_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); - } - - public sealed partial class Error_ : Result - { - public String Details { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Error_(String details) => Details = details; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public Result.Error_ Convert() => new Result.Error_(Details); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override String? GetErrorOrDefault() => Details; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Error_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return Equals(Details, other.Details); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Error_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); - } - } - - public readonly partial struct ResultError : global::System.IEquatable - { - readonly String _details; - - internal ResultError(String details) => _details = details; - - [global::System.Diagnostics.Contracts.Pure] - public Result WithOk() => Result.Error(_details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(ResultError other) => _details.Equals(other._details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) => obj is ResultError other && Equals(other); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => _details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(ResultError left, ResultError right) => left.Equals(right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(ResultError left, ResultError right) => !left.Equals(right); - } - - public static partial class ResultExtension - { - #region bind - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => (await result.ConfigureAwait(false)).Bind(bind); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func>> bind) - => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); - - #endregion - - #region map - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func map) - => (await result.ConfigureAwait(false)).Map(map); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => Bind(result, async v => Result.Ok(await bind(v).ConfigureAwait(false))); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result MapError(this Result result, global::System.Func mapError) - { - if (result is Result.Error_ e) - return Result.Error(mapError(e.Details)); - return result; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); - - #endregion - - #region match - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func> error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func ok, - global::System.Func error) - => (await result.ConfigureAwait(false)).Match(ok, error); - - #endregion - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static Result Flatten(this Result> result) => result.Bind(r => r); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result As(this Result result, global::System.Func errorTIsNotT1) => - result.Bind(r => - { - if (r is T1 converted) - return converted; - return Result.Error(errorTIsNotT1()); - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result As(this Result result, global::System.Func errorIsNotT1) => - result.As(errorIsNotT1); - - #region query-expression pattern - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result Select(this Result result, global::System.Func selector) => result.Map(selector); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result SelectMany(this Result result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this Result result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - - #endregion - - //createGenericResultConversionExtensions - } -} - -namespace StandardMinLangVersion.Extensions +#pragma warning disable 1591 + public abstract partial class Result + { + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static Result Error(String details) => new Result.Error_(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static ResultError Error(String details) => new(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static Result Ok(T value) => new Result.Ok_(value); + public bool IsError => GetType().GetGenericTypeDefinition() == typeof(Result<>.Error_); + public bool IsOk => !IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public abstract String? GetErrorOrDefault(); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result Try( global::System.Func action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + } + + public abstract partial class Result : Result, global::System.Collections.Generic.IEnumerable + { + + [global::System.Diagnostics.DebuggerNonUserCode] + + public new static Result Error(String message) => Error(message); + + [global::System.Diagnostics.DebuggerNonUserCode] + + public static Result Ok(T value) => Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator Result(T value) => Result.Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator Result(ResultError myResultError) => myResultError.WithOk(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator true(Result result) => result.IsOk; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator false(Result result) => result.IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !(Result result) => result.IsError; + + //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode + bool Equals(Result other) => this switch + { + Ok_ ok => ok.Equals((object)other), + Error_ error => error.Equals((object)other), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") + }; + + public override int GetHashCode() => this switch + { + Ok_ ok => ok.GetHashCode(), + Error_ error => error.GetHashCode(), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") + }; + + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((Result)obj); + } + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Result? left, Result? right) => Equals(left, right); + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Result? left, Result? right) => !Equals(left, right); + + [global::System.Diagnostics.DebuggerStepThrough] + public void Match( global::System.Action ok, global::System.Action? error = null) => Match( + v => + { + ok.Invoke(v); + return 42; + }, + err => + { + error?.Invoke(err); + return 42; + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T1 Match( global::System.Func ok, global::System.Func error) + { + return this switch + { + Ok_ okResult => ok(okResult.Value), + Error_ errorResult => error(errorResult.Details), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) + { + return this switch + { + Ok_ okResult => await ok(okResult.Value).ConfigureAwait(false), + Error_ errorResult => await error(errorResult.Details).ConfigureAwait(false), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => + Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); + + [global::System.Diagnostics.DebuggerStepThrough] + public async global::System.Threading.Tasks.Task Match( global::System.Func ok) + { + if (this is Ok_ okResult) await ok(okResult.Value).ConfigureAwait(false); + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public T Match( global::System.Func error) => Match(v => v, error); + + [global::System.Diagnostics.DebuggerStepThrough] + + public Result Bind( global::System.Func> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return bind(ok.Value); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return await bind(ok.Value).ConfigureAwait(false); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public Result Map( global::System.Func map) + { + switch (this) + { + case Ok_ ok: + try + { + return Result.Ok(map(ok.Value)); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Map( + global::System.Func> map) + { + switch (this) + { + case Ok_ ok: + try + { + return Result.Ok(await map(ok.Value).ConfigureAwait(false)); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + //createGenericResultConversions + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T? GetValueOrDefault() + => Match( + v => (T?)v, + _ => default + ); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T GetValueOrDefault( global::System.Func defaultValue) + => Match( + v => v, + _ => defaultValue() + ); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T GetValueOrDefault(T defaultValue) + => Match( + v => v, + _ => defaultValue + ); + + [global::System.Diagnostics.DebuggerStepThrough] + public T GetValueOrThrow() + => Match( + v => v, + details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); + + + public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); + + + public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); + global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); + + public sealed partial class Ok_ : Result + { + public T Value { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Ok_(T value) => Value = value; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override String? GetErrorOrDefault() => null; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Ok_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Ok_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); + } + + public sealed partial class Error_ : Result + { + public String Details { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Error_(String details) => Details = details; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public Result.Error_ Convert() => new Result.Error_(Details); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override String? GetErrorOrDefault() => Details; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Error_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Equals(Details, other.Details); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Error_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); + } + } + + public readonly partial struct ResultError : global::System.IEquatable + { + readonly String _details; + + internal ResultError(String details) => _details = details; + + [global::System.Diagnostics.Contracts.Pure] + public Result WithOk() => Result.Error(_details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(ResultError other) => _details.Equals(other._details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) => obj is ResultError other && Equals(other); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => _details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(ResultError left, ResultError right) => left.Equals(right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(ResultError left, ResultError right) => !left.Equals(right); + } + + public static partial class ResultExtension + { + #region bind + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => (await result.ConfigureAwait(false)).Bind(bind); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func>> bind) + => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); + + #endregion + + #region map + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func map) + => (await result.ConfigureAwait(false)).Map(map); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => Bind(result, async v => Result.Ok(await bind(v).ConfigureAwait(false))); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result MapError(this Result result, global::System.Func mapError) + { + if (result is Result.Error_ e) + return Result.Error(mapError(e.Details)); + return result; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); + + #endregion + + #region match + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func> error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func ok, + global::System.Func error) + => (await result.ConfigureAwait(false)).Match(ok, error); + + #endregion + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static Result Flatten(this Result> result) => result.Bind(r => r); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result As(this Result result, global::System.Func errorTIsNotT1) => + result.Bind(r => + { + if (r is T1 converted) + return converted; + return Result.Error(errorTIsNotT1()); + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result As(this Result result, global::System.Func errorIsNotT1) => + result.As(errorIsNotT1); + + #region query-expression pattern + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result Select(this Result result, global::System.Func selector) => result.Map(selector); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result SelectMany(this Result result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this Result result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + + #endregion + + //createGenericResultConversionExtensions + } +} + +namespace StandardMinLangVersion.Extensions { - public static partial class ResultExtension - { - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable items, - global::System.Func> choose, - global::System.Action onError) - => items - .Select(i => choose(i)) - .Choose(onError); - - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable> results, - global::System.Action onError) - => results - .Where(r => - r.Match(_ => true, error => - { - onError(error); - return false; - })) - .Select(r => r.GetValueOrThrow()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result As(this object? item, global::System.Func error) => - !(item is T t) ? Result.Error(error()) : t; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result NotNull(this T? item, global::System.Func error) => - item ?? Result.Error(error()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result NotNullOrEmpty(this string? s, global::System.Func error) - => string.IsNullOrEmpty(s) ? Result.Error(error()) : s!; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result NotNullOrWhiteSpace(this string? s, global::System.Func error) - => string.IsNullOrWhiteSpace(s) ? Result.Error(error()) : s!; - - - public static Result First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => - candidates - .FirstOrDefault(i => predicate(i)) - .NotNull(noMatch); - } -#pragma warning restore 1591 -} + public static partial class ResultExtension + { + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable items, + global::System.Func> choose, + global::System.Action onError) + => items + .Select(i => choose(i)) + .Choose(onError); + + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable> results, + global::System.Action onError) + => results + .Where(r => + r.Match(_ => true, error => + { + onError(error); + return false; + })) + .Select(r => r.GetValueOrThrow()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result As(this object? item, global::System.Func error) => + !(item is T t) ? Result.Error(error()) : t; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result NotNull(this T? item, global::System.Func error) => + item ?? Result.Error(error()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result NotNullOrEmpty(this string? s, global::System.Func error) + => string.IsNullOrEmpty(s) ? Result.Error(error()) : s!; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result NotNullOrWhiteSpace(this string? s, global::System.Func error) + => string.IsNullOrWhiteSpace(s) ? Result.Error(error()) : s!; + + + public static Result First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => + candidates + .FirstOrDefault(i => predicate(i)) + .NotNull(noMatch); + } +#pragma warning restore 1591 +} diff --git a/Source/Tests/FunicularSwitch.Generators.Consumer.StandardMinLangVersion/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/StandardMinLangVersion.ResultWithMerge.g.cs b/Source/Tests/FunicularSwitch.Generators.Consumer.StandardMinLangVersion/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/StandardMinLangVersion.ResultWithMerge.g.cs index 051ccea8..dc5a9c0a 100644 --- a/Source/Tests/FunicularSwitch.Generators.Consumer.StandardMinLangVersion/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/StandardMinLangVersion.ResultWithMerge.g.cs +++ b/Source/Tests/FunicularSwitch.Generators.Consumer.StandardMinLangVersion/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/StandardMinLangVersion.ResultWithMerge.g.cs @@ -1,524 +1,524 @@ -#nullable enable -using global::System.Linq; -using System; - -namespace StandardMinLangVersion -{ -#pragma warning disable 1591 - public abstract partial class Result - { - - - - public static Result<(T1, T2)> Aggregate(Result r1, Result r2) => ResultExtension.Aggregate(r1, r2); - - - public static Result Aggregate(Result r1, Result r2, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2) => ResultExtension.Aggregate(r1, r2); - - +#nullable enable +using global::System.Linq; +using System; + +namespace StandardMinLangVersion +{ +#pragma warning disable 1591 + public abstract partial class Result + { + + + + public static Result<(T1, T2)> Aggregate(Result r1, Result r2) => ResultExtension.Aggregate(r1, r2); + + + public static Result Aggregate(Result r1, Result r2, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2) => ResultExtension.Aggregate(r1, r2); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, combine); - - - - public static Result<(T1, T2, T3)> Aggregate(Result r1, Result r2, Result r3) => ResultExtension.Aggregate(r1, r2, r3); - - - public static Result Aggregate(Result r1, Result r2, Result r3, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3) => ResultExtension.Aggregate(r1, r2, r3); - - + + + + public static Result<(T1, T2, T3)> Aggregate(Result r1, Result r2, Result r3) => ResultExtension.Aggregate(r1, r2, r3); + + + public static Result Aggregate(Result r1, Result r2, Result r3, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3) => ResultExtension.Aggregate(r1, r2, r3); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, combine); - - - - public static Result<(T1, T2, T3, T4)> Aggregate(Result r1, Result r2, Result r3, Result r4) => ResultExtension.Aggregate(r1, r2, r3, r4); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4) => ResultExtension.Aggregate(r1, r2, r3, r4); - - + + + + public static Result<(T1, T2, T3, T4)> Aggregate(Result r1, Result r2, Result r3, Result r4) => ResultExtension.Aggregate(r1, r2, r3, r4); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4) => ResultExtension.Aggregate(r1, r2, r3, r4); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, combine); - - - - public static Result<(T1, T2, T3, T4, T5)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5) => ResultExtension.Aggregate(r1, r2, r3, r4, r5); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5) => ResultExtension.Aggregate(r1, r2, r3, r4, r5); - - + + + + public static Result<(T1, T2, T3, T4, T5)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5) => ResultExtension.Aggregate(r1, r2, r3, r4, r5); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5) => ResultExtension.Aggregate(r1, r2, r3, r4, r5); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, combine); - - - - public static Result<(T1, T2, T3, T4, T5, T6)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6); - - + + + + public static Result<(T1, T2, T3, T4, T5, T6)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, combine); - - - - public static Result<(T1, T2, T3, T4, T5, T6, T7)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7); - - + + + + public static Result<(T1, T2, T3, T4, T5, T6, T7)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, combine); - - - - public static Result<(T1, T2, T3, T4, T5, T6, T7, T8)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8); - - + + + + public static Result<(T1, T2, T3, T4, T5, T6, T7, T8)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, combine); - - - - public static Result<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, combine); - } - - public static partial class ResultExtension - { - - public static Result> Map(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func map) => + + + + public static Result<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, combine); + } + + public static partial class ResultExtension + { + + public static Result> Map(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func map) => results.Select(r => r.Map(map)).Aggregate(); - - - public static Result> Bind(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func> bind) => + + + public static Result> Bind(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func> bind) => results.Select(r => r.Bind(bind)).Aggregate(); - - public static Result> Bind(this Result result, - global::System.Func>> bindMany) => + + public static Result> Bind(this Result result, + global::System.Func>> bindMany) => result.Map(ok => bindMany(ok).Aggregate()).Flatten(); - - public static Result Bind(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func, Result> bind) => + + public static Result Bind(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func, Result> bind) => results.Aggregate().Bind(bind); - - public static Result> Aggregate(this global::System.Collections.Generic.IEnumerable> results) - { - var isError = false; - String aggregated = default!; - var oks = new global::System.Collections.Generic.List(); - foreach (var result in results) - { - result.Match( - ok => oks.Add(ok), - error => - { - aggregated = !isError ? error : MergeErrors(aggregated, error); - isError = true; - } - ); - } - - return isError - ? Result.Error>(aggregated) - : Result.Ok>(oks); + + public static Result> Aggregate(this global::System.Collections.Generic.IEnumerable> results) + { + var isError = false; + String aggregated = default!; + var oks = new global::System.Collections.Generic.List(); + foreach (var result in results) + { + result.Match( + ok => oks.Add(ok), + error => + { + aggregated = !isError ? error : MergeErrors(aggregated, error); + isError = true; + } + ); + } + + return isError + ? Result.Error>(aggregated) + : Result.Ok>(oks); } - - - public static async global::System.Threading.Tasks.Task>> Aggregate( - this global::System.Threading.Tasks.Task>> results) - => (await results.ConfigureAwait(false)) + + + public static async global::System.Threading.Tasks.Task>> Aggregate( + this global::System.Threading.Tasks.Task>> results) + => (await results.ConfigureAwait(false)) .Aggregate(); - - - public static async global::System.Threading.Tasks.Task>> Aggregate( - this global::System.Collections.Generic.IEnumerable>> results) - => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) + + + public static async global::System.Threading.Tasks.Task>> Aggregate( + this global::System.Collections.Generic.IEnumerable>> results) + => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) .Aggregate(); - - - public static async global::System.Threading.Tasks.Task>> AggregateMany( - this global::System.Collections.Generic.IEnumerable>>> results) - => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) - .SelectMany(e => e) + + + public static async global::System.Threading.Tasks.Task>> AggregateMany( + this global::System.Collections.Generic.IEnumerable>>> results) + => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) + .SelectMany(e => e) .Aggregate(); - - - public static Result<(T1, T2)> Aggregate(this Result r1, Result r2) => - Aggregate(r1, r2, (v1, v2) => (v1, v2)); - - - public static Result Aggregate(this Result r1, Result r2, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2) - return combine(ok1.Value, ok2.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); - } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2) - => Aggregate(r1, r2, (v1, v2) => (v1, v2)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2); - return Aggregate(r1.Result, r2.Result, combine); - } - - - public static Result<(T1, T2, T3)> Aggregate(this Result r1, Result r2, Result r3) => - Aggregate(r1, r2, r3, (v1, v2, v3) => (v1, v2, v3)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3) - return combine(ok1.Value, ok2.Value, ok3.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); - } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3) - => Aggregate(r1, r2, r3, (v1, v2, v3) => (v1, v2, v3)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3); - return Aggregate(r1.Result, r2.Result, r3.Result, combine); - } - - - public static Result<(T1, T2, T3, T4)> Aggregate(this Result r1, Result r2, Result r3, Result r4) => - Aggregate(r1, r2, r3, r4, (v1, v2, v3, v4) => (v1, v2, v3, v4)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); - } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4) - => Aggregate(r1, r2, r3, r4, (v1, v2, v3, v4) => (v1, v2, v3, v4)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, combine); - } - - - public static Result<(T1, T2, T3, T4, T5)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5) => - Aggregate(r1, r2, r3, r4, r5, (v1, v2, v3, v4, v5) => (v1, v2, v3, v4, v5)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4, r5 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); - } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5) - => Aggregate(r1, r2, r3, r4, r5, (v1, v2, v3, v4, v5) => (v1, v2, v3, v4, v5)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, combine); - } - - - public static Result<(T1, T2, T3, T4, T5, T6)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6) => - Aggregate(r1, r2, r3, r4, r5, r6, (v1, v2, v3, v4, v5, v6) => (v1, v2, v3, v4, v5, v6)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); + + + public static Result<(T1, T2)> Aggregate(this Result r1, Result r2) => + Aggregate(r1, r2, (v1, v2) => (v1, v2)); + + + public static Result Aggregate(this Result r1, Result r2, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2) + return combine(ok1.Value, ok2.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2) + => Aggregate(r1, r2, (v1, v2) => (v1, v2)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2); + return Aggregate(r1.Result, r2.Result, combine); } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6) - => Aggregate(r1, r2, r3, r4, r5, r6, (v1, v2, v3, v4, v5, v6) => (v1, v2, v3, v4, v5, v6)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, combine); + + + public static Result<(T1, T2, T3)> Aggregate(this Result r1, Result r2, Result r3) => + Aggregate(r1, r2, r3, (v1, v2, v3) => (v1, v2, v3)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3) + return combine(ok1.Value, ok2.Value, ok3.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3) + => Aggregate(r1, r2, r3, (v1, v2, v3) => (v1, v2, v3)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3); + return Aggregate(r1.Result, r2.Result, r3.Result, combine); } - - - public static Result<(T1, T2, T3, T4, T5, T6, T7)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7) => - Aggregate(r1, r2, r3, r4, r5, r6, r7, (v1, v2, v3, v4, v5, v6, v7) => (v1, v2, v3, v4, v5, v6, v7)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); + + + public static Result<(T1, T2, T3, T4)> Aggregate(this Result r1, Result r2, Result r3, Result r4) => + Aggregate(r1, r2, r3, r4, (v1, v2, v3, v4) => (v1, v2, v3, v4)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4) + => Aggregate(r1, r2, r3, r4, (v1, v2, v3, v4) => (v1, v2, v3, v4)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, combine); } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7) - => Aggregate(r1, r2, r3, r4, r5, r6, r7, (v1, v2, v3, v4, v5, v6, v7) => (v1, v2, v3, v4, v5, v6, v7)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, combine); + + + public static Result<(T1, T2, T3, T4, T5)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5) => + Aggregate(r1, r2, r3, r4, r5, (v1, v2, v3, v4, v5) => (v1, v2, v3, v4, v5)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4, r5 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5) + => Aggregate(r1, r2, r3, r4, r5, (v1, v2, v3, v4, v5) => (v1, v2, v3, v4, v5)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, combine); } - - - public static Result<(T1, T2, T3, T4, T5, T6, T7, T8)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8) => - Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, (v1, v2, v3, v4, v5, v6, v7, v8) => (v1, v2, v3, v4, v5, v6, v7, v8)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7 && r8 is Result.Ok_ ok8) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value, ok8.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7, r8 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); + + + public static Result<(T1, T2, T3, T4, T5, T6)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6) => + Aggregate(r1, r2, r3, r4, r5, r6, (v1, v2, v3, v4, v5, v6) => (v1, v2, v3, v4, v5, v6)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6) + => Aggregate(r1, r2, r3, r4, r5, r6, (v1, v2, v3, v4, v5, v6) => (v1, v2, v3, v4, v5, v6)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, combine); } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8) - => Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, (v1, v2, v3, v4, v5, v6, v7, v8) => (v1, v2, v3, v4, v5, v6, v7, v8)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7, r8); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, r8.Result, combine); + + + public static Result<(T1, T2, T3, T4, T5, T6, T7)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7) => + Aggregate(r1, r2, r3, r4, r5, r6, r7, (v1, v2, v3, v4, v5, v6, v7) => (v1, v2, v3, v4, v5, v6, v7)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7) + => Aggregate(r1, r2, r3, r4, r5, r6, r7, (v1, v2, v3, v4, v5, v6, v7) => (v1, v2, v3, v4, v5, v6, v7)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, combine); } - - - public static Result<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9) => - Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, (v1, v2, v3, v4, v5, v6, v7, v8, v9) => (v1, v2, v3, v4, v5, v6, v7, v8, v9)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7 && r8 is Result.Ok_ ok8 && r9 is Result.Ok_ ok9) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value, ok8.Value, ok9.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7, r8, r9 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); + + + public static Result<(T1, T2, T3, T4, T5, T6, T7, T8)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8) => + Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, (v1, v2, v3, v4, v5, v6, v7, v8) => (v1, v2, v3, v4, v5, v6, v7, v8)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7 && r8 is Result.Ok_ ok8) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value, ok8.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7, r8 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8) + => Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, (v1, v2, v3, v4, v5, v6, v7, v8) => (v1, v2, v3, v4, v5, v6, v7, v8)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7, r8); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, r8.Result, combine); } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9) - => Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, (v1, v2, v3, v4, v5, v6, v7, v8, v9) => (v1, v2, v3, v4, v5, v6, v7, v8, v9)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7, r8, r9); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, r8.Result, r9.Result, combine); + + + public static Result<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9) => + Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, (v1, v2, v3, v4, v5, v6, v7, v8, v9) => (v1, v2, v3, v4, v5, v6, v7, v8, v9)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7 && r8 is Result.Ok_ ok8 && r9 is Result.Ok_ ok9) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value, ok8.Value, ok9.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7, r8, r9 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9) + => Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, (v1, v2, v3, v4, v5, v6, v7, v8, v9) => (v1, v2, v3, v4, v5, v6, v7, v8, v9)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7, r8, r9); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, r8.Result, r9.Result, combine); } - - public static Result FirstOk(this global::System.Collections.Generic.IEnumerable> results, global::System.Func onEmpty) - { - var errors = new global::System.Collections.Generic.List(); - foreach (var result in results) - { - if (result is Result.Error_ e) - errors.Add(e.Details); - else - return result; - } - - if (!errors.Any()) - errors.Add(onEmpty()); - - return Result.Error(MergeErrors(errors)); + + public static Result FirstOk(this global::System.Collections.Generic.IEnumerable> results, global::System.Func onEmpty) + { + var errors = new global::System.Collections.Generic.List(); + foreach (var result in results) + { + if (result is Result.Error_ e) + errors.Add(e.Details); + else + return result; + } + + if (!errors.Any()) + errors.Add(onEmpty()); + + return Result.Error(MergeErrors(errors)); } - - - public static async global::System.Threading.Tasks.Task>> Aggregate( - this global::System.Collections.Generic.IEnumerable>> results, - int maxDegreeOfParallelism) - => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) + + + public static async global::System.Threading.Tasks.Task>> Aggregate( + this global::System.Collections.Generic.IEnumerable>> results, + int maxDegreeOfParallelism) + => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) + .Aggregate(); + + + public static async global::System.Threading.Tasks.Task>> AggregateMany( + this global::System.Collections.Generic.IEnumerable>>> results, + int maxDegreeOfParallelism) + => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) + .SelectMany(e => e) .Aggregate(); - - public static async global::System.Threading.Tasks.Task>> AggregateMany( - this global::System.Collections.Generic.IEnumerable>>> results, - int maxDegreeOfParallelism) - => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) - .SelectMany(e => e) - .Aggregate(); - - static async global::System.Threading.Tasks.Task SelectAsync(this global::System.Collections.Generic.IEnumerable items, global::System.Func> selector, int maxDegreeOfParallelism) - { - using (var throttler = new global::System.Threading.SemaphoreSlim(maxDegreeOfParallelism, maxDegreeOfParallelism)) - { - return await global::System.Threading.Tasks.Task.WhenAll(items.Select(async item => - { - // ReSharper disable once AccessToDisposedClosure - await throttler.WaitAsync().ConfigureAwait(false); - try - { - return await selector(item).ConfigureAwait(false); - } - finally - { - // ReSharper disable once AccessToDisposedClosure - throttler.Release(); - } - })).ConfigureAwait(false); - } + static async global::System.Threading.Tasks.Task SelectAsync(this global::System.Collections.Generic.IEnumerable items, global::System.Func> selector, int maxDegreeOfParallelism) + { + using (var throttler = new global::System.Threading.SemaphoreSlim(maxDegreeOfParallelism, maxDegreeOfParallelism)) + { + return await global::System.Threading.Tasks.Task.WhenAll(items.Select(async item => + { + // ReSharper disable once AccessToDisposedClosure + await throttler.WaitAsync().ConfigureAwait(false); + try + { + return await selector(item).ConfigureAwait(false); + } + finally + { + // ReSharper disable once AccessToDisposedClosure + throttler.Release(); + } + })).ConfigureAwait(false); + } } - - - public static Result> AllOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate) => - candidates - .Select(c => c.Validate(validate)) + + + public static Result> AllOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate) => + candidates + .Select(c => c.Validate(validate)) .Aggregate(); - - public static Result> AllOk(this global::System.Collections.Generic.IEnumerable> candidates, - global::System.Func> validate) => - candidates + + public static Result> AllOk(this global::System.Collections.Generic.IEnumerable> candidates, + global::System.Func> validate) => + candidates .Bind(items => items.AllOk(validate)); - - - public static Result Validate(this Result item, global::System.Func> validate) => item.Bind(i => i.Validate(validate)); - - - public static Result Validate(this T item, global::System.Func> validate) - { - try - { - var errors = validate(item).ToList(); - return errors.Count > 0 ? Result.Error(MergeErrors(errors)) : item; - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } + + + public static Result Validate(this Result item, global::System.Func> validate) => item.Bind(i => i.Validate(validate)); + + + public static Result Validate(this T item, global::System.Func> validate) + { + try + { + var errors = validate(item).ToList(); + return errors.Count > 0 ? Result.Error(MergeErrors(errors)) : item; + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } } - - - public static Result FirstOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate, global::System.Func onEmpty) => - candidates - .Select(r => r.Validate(validate)) - .FirstOk(onEmpty); - - #region helpers - - static String MergeErrors(global::System.Collections.Generic.IEnumerable errors) - { - var first = true; - String aggregated = default!; - foreach (var myError in errors) - { - if (first) - { - aggregated = myError; - first = false; - } - else - { - aggregated = MergeErrors(aggregated, myError); - } - } - - return aggregated; - } - - static String MergeErrors(String aggregated, String error) => aggregated.MergeError(error); - - #endregion - } -#pragma warning restore 1591 + + + public static Result FirstOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate, global::System.Func onEmpty) => + candidates + .Select(r => r.Validate(validate)) + .FirstOk(onEmpty); + + #region helpers + + static String MergeErrors(global::System.Collections.Generic.IEnumerable errors) + { + var first = true; + String aggregated = default!; + foreach (var myError in errors) + { + if (first) + { + aggregated = myError; + first = false; + } + else + { + aggregated = MergeErrors(aggregated, myError); + } + } + + return aggregated; + } + + static String MergeErrors(String aggregated, String error) => aggregated.MergeError(error); + + #endregion + } +#pragma warning restore 1591 } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Consumer.StandardMinLangVersion/UnionTypeSpecs.cs b/Source/Tests/FunicularSwitch.Generators.Consumer.StandardMinLangVersion/UnionTypeSpecs.cs index fb968660..fc5e4bdb 100644 --- a/Source/Tests/FunicularSwitch.Generators.Consumer.StandardMinLangVersion/UnionTypeSpecs.cs +++ b/Source/Tests/FunicularSwitch.Generators.Consumer.StandardMinLangVersion/UnionTypeSpecs.cs @@ -33,8 +33,8 @@ public void ResultGenerator() [UnionType] public abstract partial class MyUnion { - public class Case1_ : MyUnion {} - public class MyUnionCase2 : MyUnion {} + public class Case1_ : MyUnion { } + public class MyUnionCase2 : MyUnion { } } [ResultType(ErrorType = typeof(string))] diff --git a/Source/Tests/FunicularSwitch.Generators.Consumer/EnumSpecs.cs b/Source/Tests/FunicularSwitch.Generators.Consumer/EnumSpecs.cs index 924a7fb8..6dd39cd8 100644 --- a/Source/Tests/FunicularSwitch.Generators.Consumer/EnumSpecs.cs +++ b/Source/Tests/FunicularSwitch.Generators.Consumer/EnumSpecs.cs @@ -13,91 +13,91 @@ namespace FunicularSwitch.Generators.Consumer; [TestClass] public class EnumSpecs { - [ExtendedEnum(CaseOrder = EnumCaseOrder.Alphabetic)] //direct EnumType attribute should have higher precedence compared to ExtendEnumTypes attribute, - //so case oder should be Alphabetic for Match methods of PlatformIdentifier - public enum PlatformIdentifier - { - LinuxDevice, - DeveloperMachine, - WindowsDevice - } + [ExtendedEnum(CaseOrder = EnumCaseOrder.Alphabetic)] //direct EnumType attribute should have higher precedence compared to ExtendEnumTypes attribute, + //so case oder should be Alphabetic for Match methods of PlatformIdentifier + public enum PlatformIdentifier + { + LinuxDevice, + DeveloperMachine, + WindowsDevice + } - [TestMethod] - public void EnumMatchWorks() - { - var p = PlatformIdentifier.DeveloperMachine; + [TestMethod] + public void EnumMatchWorks() + { + var p = PlatformIdentifier.DeveloperMachine; - var isGraphicalLinux = p.Match( - () => false, - () => true, - () => true - ); + var isGraphicalLinux = p.Match( + () => false, + () => true, + () => true + ); - Assert.IsFalse(isGraphicalLinux); - } + Assert.IsFalse(isGraphicalLinux); + } - [TestMethod] - public void EnumSwitchWorks() - { - var p = PlatformIdentifier.LinuxDevice; + [TestMethod] + public void EnumSwitchWorks() + { + var p = PlatformIdentifier.LinuxDevice; - p.Switch( - Assert.Fail, - () => { }, - Assert.Fail - ); - } + p.Switch( + Assert.Fail, + () => { }, + Assert.Fail + ); + } - [TestMethod] - public void MatchMethodsForSingleTypeInReferencedAssemblyWorks() - { - var kindName = DateTimeKind.Local.Match( - () => "local", - () => "unspecified", - () => "utc" - ); + [TestMethod] + public void MatchMethodsForSingleTypeInReferencedAssemblyWorks() + { + var kindName = DateTimeKind.Local.Match( + () => "local", + () => "unspecified", + () => "utc" + ); - kindName.Should().Be("local"); - } + kindName.Should().Be("local"); + } - [TestMethod] - public void MatchMethodsForAllTypesInReferencedAssemblyWorks() - { - var matchMode = RowMatchMode.PrimaryKey.Match( - () => "index", - () => "primaryKey" - ); + [TestMethod] + public void MatchMethodsForAllTypesInReferencedAssemblyWorks() + { + var matchMode = RowMatchMode.PrimaryKey.Match( + () => "index", + () => "primaryKey" + ); - matchMode.Should().Be("primaryKey"); - } + matchMode.Should().Be("primaryKey"); + } } //this one is here to make sure no match method is generated for non accessibly enum type public class Wrapper { - class PrivateEnumParent - { - public enum HiddenEnum - { - One, - Two - } - } + class PrivateEnumParent + { + public enum HiddenEnum + { + One, + Two + } + } } //this one is here to make sure accessibility for nested types is handled correctly class InternalEnumParent { - public enum InternalEnum - { - One, - Two - } + public enum InternalEnum + { + One, + Two + } } //no match methods should be generated here public enum DuplicateValues { - One = 1, - Two = 1 + One = 1, + Two = 1 } diff --git a/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.EnumTypeGenerator/Attributes.g.cs b/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.EnumTypeGenerator/Attributes.g.cs index f8adff12..3a2720d9 100644 --- a/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.EnumTypeGenerator/Attributes.g.cs +++ b/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.EnumTypeGenerator/Attributes.g.cs @@ -7,10 +7,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -23,16 +23,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -41,21 +41,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/Attributes.g.cs b/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/Attributes.g.cs index 8dddcd08..ba6d29a4 100644 --- a/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/Attributes.g.cs +++ b/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/Attributes.g.cs @@ -4,10 +4,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Generators.Consumer.OperationResult.g.cs b/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Generators.Consumer.OperationResult.g.cs index dffaeb07..3602a0fe 100644 --- a/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Generators.Consumer.OperationResult.g.cs +++ b/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Generators.Consumer.OperationResult.g.cs @@ -1,641 +1,641 @@ -#nullable enable +#nullable enable using global::System.Linq; using FunicularSwitch.Generic; -namespace FunicularSwitch.Generators.Consumer +namespace FunicularSwitch.Generators.Consumer { -#pragma warning disable 1591 - abstract partial class OperationResult - { - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResult Error(Error details) => new OperationResult.Error_(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResultError Error(Error details) => new(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResult Ok(T value) => new OperationResult.Ok_(value); - public bool IsError => GetType().GetGenericTypeDefinition() == typeof(OperationResult<>.Error_); - public bool IsOk => !IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public abstract Error? GetErrorOrDefault(); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult Try( global::System.Func action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - } - - abstract partial class OperationResult : OperationResult, global::System.Collections.Generic.IEnumerable - { - - [global::System.Diagnostics.DebuggerNonUserCode] - - public new static OperationResult Error(Error message) => Error(message); - - [global::System.Diagnostics.DebuggerNonUserCode] - - public static OperationResult Ok(T value) => Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator OperationResult(T value) => OperationResult.Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator OperationResult(OperationResultError myResultError) => myResultError.WithOk(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator true(OperationResult result) => result.IsOk; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator false(OperationResult result) => result.IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !(OperationResult result) => result.IsError; - - //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode - bool Equals(OperationResult other) => this switch - { - Ok_ ok => ok.Equals((object)other), - Error_ error => error.Equals((object)other), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") - }; - - public override int GetHashCode() => this switch - { - Ok_ ok => ok.GetHashCode(), - Error_ error => error.GetHashCode(), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") - }; - - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != this.GetType()) return false; - return Equals((OperationResult)obj); - } - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(OperationResult? left, OperationResult? right) => Equals(left, right); - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(OperationResult? left, OperationResult? right) => !Equals(left, right); - - [global::System.Diagnostics.DebuggerStepThrough] - public void Match( global::System.Action ok, global::System.Action? error = null) => Match( - v => - { - ok.Invoke(v); - return 42; - }, - err => - { - error?.Invoke(err); - return 42; - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T1 Match( global::System.Func ok, global::System.Func error) - { - return this switch - { - Ok_ okOperationResult => ok(okOperationResult.Value), - Error_ errorOperationResult => error(errorOperationResult.Details), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) - { - return this switch - { - Ok_ okOperationResult => await ok(okOperationResult.Value).ConfigureAwait(false), - Error_ errorOperationResult => await error(errorOperationResult.Details).ConfigureAwait(false), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => - Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); - - [global::System.Diagnostics.DebuggerStepThrough] - public async global::System.Threading.Tasks.Task Match( global::System.Func ok) - { - if (this is Ok_ okOperationResult) await ok(okOperationResult.Value).ConfigureAwait(false); - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public T Match( global::System.Func error) => Match(v => v, error); - - [global::System.Diagnostics.DebuggerStepThrough] - - public OperationResult Bind( global::System.Func> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return bind(ok.Value); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - return OperationResult.Error(FunicularSwitch.Generators.Consumer.Error.Generic(e)); - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return await bind(ok.Value).ConfigureAwait(false); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - return OperationResult.Error(FunicularSwitch.Generators.Consumer.Error.Generic(e)); - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public OperationResult Map( global::System.Func map) - { - switch (this) - { - case Ok_ ok: - try - { - return OperationResult.Ok(map(ok.Value)); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - return OperationResult.Error(FunicularSwitch.Generators.Consumer.Error.Generic(e)); - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Map( - global::System.Func> map) - { - switch (this) - { - case Ok_ ok: - try - { - return OperationResult.Ok(await map(ok.Value).ConfigureAwait(false)); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - return OperationResult.Error(FunicularSwitch.Generators.Consumer.Error.Generic(e)); - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - - public static implicit operator global::FunicularSwitch.Generic.GenericResult(OperationResult result) => - result.Match( - global::FunicularSwitch.Generic.GenericResult.Ok, - global::FunicularSwitch.Generic.GenericResult.Error); - - public static implicit operator OperationResult(global::FunicularSwitch.Generic.GenericResult result) => - result.Match>( - OperationResult.Ok, - OperationResult.Error); - - public global::FunicularSwitch.Generic.GenericResult ToGenericResult() => - Match( - global::FunicularSwitch.Generic.GenericResult.Ok, +#pragma warning disable 1591 + abstract partial class OperationResult + { + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResult Error(Error details) => new OperationResult.Error_(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResultError Error(Error details) => new(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResult Ok(T value) => new OperationResult.Ok_(value); + public bool IsError => GetType().GetGenericTypeDefinition() == typeof(OperationResult<>.Error_); + public bool IsOk => !IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public abstract Error? GetErrorOrDefault(); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult Try( global::System.Func action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + } + + abstract partial class OperationResult : OperationResult, global::System.Collections.Generic.IEnumerable + { + + [global::System.Diagnostics.DebuggerNonUserCode] + + public new static OperationResult Error(Error message) => Error(message); + + [global::System.Diagnostics.DebuggerNonUserCode] + + public static OperationResult Ok(T value) => Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator OperationResult(T value) => OperationResult.Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator OperationResult(OperationResultError myResultError) => myResultError.WithOk(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator true(OperationResult result) => result.IsOk; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator false(OperationResult result) => result.IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !(OperationResult result) => result.IsError; + + //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode + bool Equals(OperationResult other) => this switch + { + Ok_ ok => ok.Equals((object)other), + Error_ error => error.Equals((object)other), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") + }; + + public override int GetHashCode() => this switch + { + Ok_ ok => ok.GetHashCode(), + Error_ error => error.GetHashCode(), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") + }; + + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((OperationResult)obj); + } + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(OperationResult? left, OperationResult? right) => Equals(left, right); + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(OperationResult? left, OperationResult? right) => !Equals(left, right); + + [global::System.Diagnostics.DebuggerStepThrough] + public void Match( global::System.Action ok, global::System.Action? error = null) => Match( + v => + { + ok.Invoke(v); + return 42; + }, + err => + { + error?.Invoke(err); + return 42; + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T1 Match( global::System.Func ok, global::System.Func error) + { + return this switch + { + Ok_ okOperationResult => ok(okOperationResult.Value), + Error_ errorOperationResult => error(errorOperationResult.Details), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) + { + return this switch + { + Ok_ okOperationResult => await ok(okOperationResult.Value).ConfigureAwait(false), + Error_ errorOperationResult => await error(errorOperationResult.Details).ConfigureAwait(false), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => + Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); + + [global::System.Diagnostics.DebuggerStepThrough] + public async global::System.Threading.Tasks.Task Match( global::System.Func ok) + { + if (this is Ok_ okOperationResult) await ok(okOperationResult.Value).ConfigureAwait(false); + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public T Match( global::System.Func error) => Match(v => v, error); + + [global::System.Diagnostics.DebuggerStepThrough] + + public OperationResult Bind( global::System.Func> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return bind(ok.Value); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + return OperationResult.Error(FunicularSwitch.Generators.Consumer.Error.Generic(e)); + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return await bind(ok.Value).ConfigureAwait(false); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + return OperationResult.Error(FunicularSwitch.Generators.Consumer.Error.Generic(e)); + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public OperationResult Map( global::System.Func map) + { + switch (this) + { + case Ok_ ok: + try + { + return OperationResult.Ok(map(ok.Value)); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + return OperationResult.Error(FunicularSwitch.Generators.Consumer.Error.Generic(e)); + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Map( + global::System.Func> map) + { + switch (this) + { + case Ok_ ok: + try + { + return OperationResult.Ok(await map(ok.Value).ConfigureAwait(false)); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + return OperationResult.Error(FunicularSwitch.Generators.Consumer.Error.Generic(e)); + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + + public static implicit operator global::FunicularSwitch.Generic.GenericResult(OperationResult result) => + result.Match( + global::FunicularSwitch.Generic.GenericResult.Ok, + global::FunicularSwitch.Generic.GenericResult.Error); + + public static implicit operator OperationResult(global::FunicularSwitch.Generic.GenericResult result) => + result.Match>( + OperationResult.Ok, + OperationResult.Error); + + public global::FunicularSwitch.Generic.GenericResult ToGenericResult() => + Match( + global::FunicularSwitch.Generic.GenericResult.Ok, global::FunicularSwitch.Generic.GenericResult.Error); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T? GetValueOrDefault() - => Match( - v => (T?)v, - _ => default - ); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T GetValueOrDefault( global::System.Func defaultValue) - => Match( - v => v, - _ => defaultValue() - ); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T GetValueOrDefault(T defaultValue) - => Match( - v => v, - _ => defaultValue - ); - - [global::System.Diagnostics.DebuggerStepThrough] - public T GetValueOrThrow() - => Match( - v => v, - details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); - - - public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); - - - public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); - global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); - - public sealed partial class Ok_ : OperationResult - { - public T Value { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Ok_(T value) => Value = value; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override Error? GetErrorOrDefault() => null; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Ok_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Ok_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); - } - - public sealed partial class Error_ : OperationResult - { - public Error Details { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Error_(Error details) => Details = details; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public OperationResult.Error_ Convert() => new OperationResult.Error_(Details); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override Error? GetErrorOrDefault() => Details; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Error_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return Equals(Details, other.Details); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Error_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); - } - } - - readonly partial struct OperationResultError : global::System.IEquatable - { - readonly Error _details; - - internal OperationResultError(Error details) => _details = details; - - [global::System.Diagnostics.Contracts.Pure] - public OperationResult WithOk() => OperationResult.Error(_details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(OperationResultError other) => _details.Equals(other._details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) => obj is OperationResultError other && Equals(other); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => _details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(OperationResultError left, OperationResultError right) => left.Equals(right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(OperationResultError left, OperationResultError right) => !left.Equals(right); - } - - static partial class OperationResultExtension - { - #region bind - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => (await result.ConfigureAwait(false)).Bind(bind); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func>> bind) - => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); - - #endregion - - #region map - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func map) - => (await result.ConfigureAwait(false)).Map(map); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => Bind(result, async v => OperationResult.Ok(await bind(v).ConfigureAwait(false))); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult MapError(this OperationResult result, global::System.Func mapError) - { - if (result is OperationResult.Error_ e) - return OperationResult.Error(mapError(e.Details)); - return result; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); - - #endregion - - #region match - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func> error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func ok, - global::System.Func error) - => (await result.ConfigureAwait(false)).Match(ok, error); - - #endregion - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResult Flatten(this OperationResult> result) => result.Bind(r => r); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult As(this OperationResult result, global::System.Func errorTIsNotT1) => - result.Bind(r => - { - if (r is T1 converted) - return converted; - return OperationResult.Error(errorTIsNotT1()); - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult As(this OperationResult result, global::System.Func errorIsNotT1) => - result.As(errorIsNotT1); - - #region query-expression pattern - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult Select(this OperationResult result, global::System.Func selector) => result.Map(selector); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult SelectMany(this OperationResult result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this OperationResult result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - - #endregion - - - public static OperationResult ToOperationResult( - this global::FunicularSwitch.Generic.GenericResult result) => - result.Match>( - OperationResult.Ok, - OperationResult.Error); - - public static global::System.Threading.Tasks.Task> ToOperationResult( - this global::System.Threading.Tasks.Task> result) => - result.Match( - OperationResult.Ok, - OperationResult.Error); - - public static global::System.Threading.Tasks.Task> ToGenericResult( - this global::System.Threading.Tasks.Task> result) => - result.Match( - global::FunicularSwitch.Generic.GenericResult.Ok, + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T? GetValueOrDefault() + => Match( + v => (T?)v, + _ => default + ); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T GetValueOrDefault( global::System.Func defaultValue) + => Match( + v => v, + _ => defaultValue() + ); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T GetValueOrDefault(T defaultValue) + => Match( + v => v, + _ => defaultValue + ); + + [global::System.Diagnostics.DebuggerStepThrough] + public T GetValueOrThrow() + => Match( + v => v, + details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); + + + public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); + + + public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); + global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); + + public sealed partial class Ok_ : OperationResult + { + public T Value { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Ok_(T value) => Value = value; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override Error? GetErrorOrDefault() => null; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Ok_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Ok_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); + } + + public sealed partial class Error_ : OperationResult + { + public Error Details { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Error_(Error details) => Details = details; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public OperationResult.Error_ Convert() => new OperationResult.Error_(Details); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override Error? GetErrorOrDefault() => Details; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Error_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Equals(Details, other.Details); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Error_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); + } + } + + readonly partial struct OperationResultError : global::System.IEquatable + { + readonly Error _details; + + internal OperationResultError(Error details) => _details = details; + + [global::System.Diagnostics.Contracts.Pure] + public OperationResult WithOk() => OperationResult.Error(_details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(OperationResultError other) => _details.Equals(other._details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) => obj is OperationResultError other && Equals(other); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => _details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(OperationResultError left, OperationResultError right) => left.Equals(right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(OperationResultError left, OperationResultError right) => !left.Equals(right); + } + + static partial class OperationResultExtension + { + #region bind + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => (await result.ConfigureAwait(false)).Bind(bind); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func>> bind) + => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); + + #endregion + + #region map + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func map) + => (await result.ConfigureAwait(false)).Map(map); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => Bind(result, async v => OperationResult.Ok(await bind(v).ConfigureAwait(false))); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult MapError(this OperationResult result, global::System.Func mapError) + { + if (result is OperationResult.Error_ e) + return OperationResult.Error(mapError(e.Details)); + return result; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); + + #endregion + + #region match + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func> error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func ok, + global::System.Func error) + => (await result.ConfigureAwait(false)).Match(ok, error); + + #endregion + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResult Flatten(this OperationResult> result) => result.Bind(r => r); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult As(this OperationResult result, global::System.Func errorTIsNotT1) => + result.Bind(r => + { + if (r is T1 converted) + return converted; + return OperationResult.Error(errorTIsNotT1()); + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult As(this OperationResult result, global::System.Func errorIsNotT1) => + result.As(errorIsNotT1); + + #region query-expression pattern + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult Select(this OperationResult result, global::System.Func selector) => result.Map(selector); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult SelectMany(this OperationResult result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this OperationResult result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + + #endregion + + + public static OperationResult ToOperationResult( + this global::FunicularSwitch.Generic.GenericResult result) => + result.Match>( + OperationResult.Ok, + OperationResult.Error); + + public static global::System.Threading.Tasks.Task> ToOperationResult( + this global::System.Threading.Tasks.Task> result) => + result.Match( + OperationResult.Ok, + OperationResult.Error); + + public static global::System.Threading.Tasks.Task> ToGenericResult( + this global::System.Threading.Tasks.Task> result) => + result.Match( + global::FunicularSwitch.Generic.GenericResult.Ok, global::FunicularSwitch.Generic.GenericResult.Error); - } -} - -namespace FunicularSwitch.Generators.Consumer.Extensions + } +} + +namespace FunicularSwitch.Generators.Consumer.Extensions { - static partial class OperationResultExtension - { - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable items, - global::System.Func> choose, - global::System.Action onError) - => items - .Select(i => choose(i)) - .Choose(onError); - - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable> results, - global::System.Action onError) - => results - .Where(r => - r.Match(_ => true, error => - { - onError(error); - return false; - })) - .Select(r => r.GetValueOrThrow()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult As(this object? item, global::System.Func error) => - !(item is T t) ? OperationResult.Error(error()) : t; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult NotNull(this T? item, global::System.Func error) => - item ?? OperationResult.Error(error()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult NotNullOrEmpty(this string? s, global::System.Func error) - => string.IsNullOrEmpty(s) ? OperationResult.Error(error()) : s!; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult NotNullOrWhiteSpace(this string? s, global::System.Func error) - => string.IsNullOrWhiteSpace(s) ? OperationResult.Error(error()) : s!; - - - public static OperationResult First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => - candidates - .FirstOrDefault(i => predicate(i)) - .NotNull(noMatch); - } -#pragma warning restore 1591 -} + static partial class OperationResultExtension + { + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable items, + global::System.Func> choose, + global::System.Action onError) + => items + .Select(i => choose(i)) + .Choose(onError); + + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable> results, + global::System.Action onError) + => results + .Where(r => + r.Match(_ => true, error => + { + onError(error); + return false; + })) + .Select(r => r.GetValueOrThrow()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult As(this object? item, global::System.Func error) => + !(item is T t) ? OperationResult.Error(error()) : t; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult NotNull(this T? item, global::System.Func error) => + item ?? OperationResult.Error(error()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult NotNullOrEmpty(this string? s, global::System.Func error) + => string.IsNullOrEmpty(s) ? OperationResult.Error(error()) : s!; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult NotNullOrWhiteSpace(this string? s, global::System.Func error) + => string.IsNullOrWhiteSpace(s) ? OperationResult.Error(error()) : s!; + + + public static OperationResult First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => + candidates + .FirstOrDefault(i => predicate(i)) + .NotNull(noMatch); + } +#pragma warning restore 1591 +} diff --git a/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Generators.Consumer.OperationResultWithMerge.g.cs b/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Generators.Consumer.OperationResultWithMerge.g.cs index ff666e2e..d12a8d27 100644 --- a/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Generators.Consumer.OperationResultWithMerge.g.cs +++ b/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Generators.Consumer.OperationResultWithMerge.g.cs @@ -1,524 +1,524 @@ -#nullable enable -using global::System.Linq; -using FunicularSwitch.Generic; - -namespace FunicularSwitch.Generators.Consumer -{ -#pragma warning disable 1591 - abstract partial class OperationResult - { - - - - public static OperationResult<(T1, T2)> Aggregate(OperationResult r1, OperationResult r2) => OperationResultExtension.Aggregate(r1, r2); - - - public static OperationResult Aggregate(OperationResult r1, OperationResult r2, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2) => OperationResultExtension.Aggregate(r1, r2); - - +#nullable enable +using global::System.Linq; +using FunicularSwitch.Generic; + +namespace FunicularSwitch.Generators.Consumer +{ +#pragma warning disable 1591 + abstract partial class OperationResult + { + + + + public static OperationResult<(T1, T2)> Aggregate(OperationResult r1, OperationResult r2) => OperationResultExtension.Aggregate(r1, r2); + + + public static OperationResult Aggregate(OperationResult r1, OperationResult r2, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2) => OperationResultExtension.Aggregate(r1, r2); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, combine); - - - - public static OperationResult<(T1, T2, T3)> Aggregate(OperationResult r1, OperationResult r2, OperationResult r3) => OperationResultExtension.Aggregate(r1, r2, r3); - - - public static OperationResult Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, r3, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3) => OperationResultExtension.Aggregate(r1, r2, r3); - - + + + + public static OperationResult<(T1, T2, T3)> Aggregate(OperationResult r1, OperationResult r2, OperationResult r3) => OperationResultExtension.Aggregate(r1, r2, r3); + + + public static OperationResult Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, r3, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3) => OperationResultExtension.Aggregate(r1, r2, r3); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, r3, combine); - - - - public static OperationResult<(T1, T2, T3, T4)> Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4) => OperationResultExtension.Aggregate(r1, r2, r3, r4); - - - public static OperationResult Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, r3, r4, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4) => OperationResultExtension.Aggregate(r1, r2, r3, r4); - - + + + + public static OperationResult<(T1, T2, T3, T4)> Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4) => OperationResultExtension.Aggregate(r1, r2, r3, r4); + + + public static OperationResult Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, r3, r4, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4) => OperationResultExtension.Aggregate(r1, r2, r3, r4); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, r3, r4, combine); - - - - public static OperationResult<(T1, T2, T3, T4, T5)> Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5); - - - public static OperationResult Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5); - - + + + + public static OperationResult<(T1, T2, T3, T4, T5)> Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5); + + + public static OperationResult Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, combine); - - - - public static OperationResult<(T1, T2, T3, T4, T5, T6)> Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6); - - - public static OperationResult Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6); - - + + + + public static OperationResult<(T1, T2, T3, T4, T5, T6)> Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6); + + + public static OperationResult Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, combine); - - - - public static OperationResult<(T1, T2, T3, T4, T5, T6, T7)> Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7); - - - public static OperationResult Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7); - - + + + + public static OperationResult<(T1, T2, T3, T4, T5, T6, T7)> Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7); + + + public static OperationResult Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, combine); - - - - public static OperationResult<(T1, T2, T3, T4, T5, T6, T7, T8)> Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7, OperationResult r8) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8); - - - public static OperationResult Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7, OperationResult r8, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8); - - + + + + public static OperationResult<(T1, T2, T3, T4, T5, T6, T7, T8)> Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7, OperationResult r8) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8); + + + public static OperationResult Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7, OperationResult r8, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, combine); - - - - public static OperationResult<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7, OperationResult r8, OperationResult r9) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9); - - - public static OperationResult Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7, OperationResult r8, OperationResult r9, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, combine); - } - - static partial class OperationResultExtension - { - - public static OperationResult> Map(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func map) => + + + + public static OperationResult<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7, OperationResult r8, OperationResult r9) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9); + + + public static OperationResult Aggregate(OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7, OperationResult r8, OperationResult r9, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9, global::System.Func combine) => OperationResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, combine); + } + + static partial class OperationResultExtension + { + + public static OperationResult> Map(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func map) => results.Select(r => r.Map(map)).Aggregate(); - - - public static OperationResult> Bind(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func> bind) => + + + public static OperationResult> Bind(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func> bind) => results.Select(r => r.Bind(bind)).Aggregate(); - - public static OperationResult> Bind(this OperationResult result, - global::System.Func>> bindMany) => + + public static OperationResult> Bind(this OperationResult result, + global::System.Func>> bindMany) => result.Map(ok => bindMany(ok).Aggregate()).Flatten(); - - public static OperationResult Bind(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func, OperationResult> bind) => + + public static OperationResult Bind(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func, OperationResult> bind) => results.Aggregate().Bind(bind); - - public static OperationResult> Aggregate(this global::System.Collections.Generic.IEnumerable> results) - { - var isError = false; - Error aggregated = default!; - var oks = new global::System.Collections.Generic.List(); - foreach (var result in results) - { - result.Match( - ok => oks.Add(ok), - error => - { - aggregated = !isError ? error : MergeErrors(aggregated, error); - isError = true; - } - ); - } - - return isError - ? OperationResult.Error>(aggregated) - : OperationResult.Ok>(oks); + + public static OperationResult> Aggregate(this global::System.Collections.Generic.IEnumerable> results) + { + var isError = false; + Error aggregated = default!; + var oks = new global::System.Collections.Generic.List(); + foreach (var result in results) + { + result.Match( + ok => oks.Add(ok), + error => + { + aggregated = !isError ? error : MergeErrors(aggregated, error); + isError = true; + } + ); + } + + return isError + ? OperationResult.Error>(aggregated) + : OperationResult.Ok>(oks); } - - - public static async global::System.Threading.Tasks.Task>> Aggregate( - this global::System.Threading.Tasks.Task>> results) - => (await results.ConfigureAwait(false)) + + + public static async global::System.Threading.Tasks.Task>> Aggregate( + this global::System.Threading.Tasks.Task>> results) + => (await results.ConfigureAwait(false)) .Aggregate(); - - - public static async global::System.Threading.Tasks.Task>> Aggregate( - this global::System.Collections.Generic.IEnumerable>> results) - => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) + + + public static async global::System.Threading.Tasks.Task>> Aggregate( + this global::System.Collections.Generic.IEnumerable>> results) + => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) .Aggregate(); - - - public static async global::System.Threading.Tasks.Task>> AggregateMany( - this global::System.Collections.Generic.IEnumerable>>> results) - => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) - .SelectMany(e => e) + + + public static async global::System.Threading.Tasks.Task>> AggregateMany( + this global::System.Collections.Generic.IEnumerable>>> results) + => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) + .SelectMany(e => e) .Aggregate(); - - - public static OperationResult<(T1, T2)> Aggregate(this OperationResult r1, OperationResult r2) => - Aggregate(r1, r2, (v1, v2) => (v1, v2)); - - - public static OperationResult Aggregate(this OperationResult r1, OperationResult r2, global::System.Func combine) - { - if (r1 is OperationResult.Ok_ ok1 && r2 is OperationResult.Ok_ ok2) - return combine(ok1.Value, ok2.Value); - - return OperationResult.Error( - MergeErrors(new OperationResult[] { r1, r2 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); - } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2) - => Aggregate(r1, r2, (v1, v2) => (v1, v2)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2); - return Aggregate(r1.Result, r2.Result, combine); - } - - - public static OperationResult<(T1, T2, T3)> Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3) => - Aggregate(r1, r2, r3, (v1, v2, v3) => (v1, v2, v3)); - - - public static OperationResult Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, global::System.Func combine) - { - if (r1 is OperationResult.Ok_ ok1 && r2 is OperationResult.Ok_ ok2 && r3 is OperationResult.Ok_ ok3) - return combine(ok1.Value, ok2.Value, ok3.Value); - - return OperationResult.Error( - MergeErrors(new OperationResult[] { r1, r2, r3 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); - } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3) - => Aggregate(r1, r2, r3, (v1, v2, v3) => (v1, v2, v3)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3); - return Aggregate(r1.Result, r2.Result, r3.Result, combine); - } - - - public static OperationResult<(T1, T2, T3, T4)> Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4) => - Aggregate(r1, r2, r3, r4, (v1, v2, v3, v4) => (v1, v2, v3, v4)); - - - public static OperationResult Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, global::System.Func combine) - { - if (r1 is OperationResult.Ok_ ok1 && r2 is OperationResult.Ok_ ok2 && r3 is OperationResult.Ok_ ok3 && r4 is OperationResult.Ok_ ok4) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value); - - return OperationResult.Error( - MergeErrors(new OperationResult[] { r1, r2, r3, r4 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); - } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4) - => Aggregate(r1, r2, r3, r4, (v1, v2, v3, v4) => (v1, v2, v3, v4)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, combine); - } - - - public static OperationResult<(T1, T2, T3, T4, T5)> Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5) => - Aggregate(r1, r2, r3, r4, r5, (v1, v2, v3, v4, v5) => (v1, v2, v3, v4, v5)); - - - public static OperationResult Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, global::System.Func combine) - { - if (r1 is OperationResult.Ok_ ok1 && r2 is OperationResult.Ok_ ok2 && r3 is OperationResult.Ok_ ok3 && r4 is OperationResult.Ok_ ok4 && r5 is OperationResult.Ok_ ok5) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value); - - return OperationResult.Error( - MergeErrors(new OperationResult[] { r1, r2, r3, r4, r5 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); - } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5) - => Aggregate(r1, r2, r3, r4, r5, (v1, v2, v3, v4, v5) => (v1, v2, v3, v4, v5)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, combine); - } - - - public static OperationResult<(T1, T2, T3, T4, T5, T6)> Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6) => - Aggregate(r1, r2, r3, r4, r5, r6, (v1, v2, v3, v4, v5, v6) => (v1, v2, v3, v4, v5, v6)); - - - public static OperationResult Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, global::System.Func combine) - { - if (r1 is OperationResult.Ok_ ok1 && r2 is OperationResult.Ok_ ok2 && r3 is OperationResult.Ok_ ok3 && r4 is OperationResult.Ok_ ok4 && r5 is OperationResult.Ok_ ok5 && r6 is OperationResult.Ok_ ok6) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value); - - return OperationResult.Error( - MergeErrors(new OperationResult[] { r1, r2, r3, r4, r5, r6 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); + + + public static OperationResult<(T1, T2)> Aggregate(this OperationResult r1, OperationResult r2) => + Aggregate(r1, r2, (v1, v2) => (v1, v2)); + + + public static OperationResult Aggregate(this OperationResult r1, OperationResult r2, global::System.Func combine) + { + if (r1 is OperationResult.Ok_ ok1 && r2 is OperationResult.Ok_ ok2) + return combine(ok1.Value, ok2.Value); + + return OperationResult.Error( + MergeErrors(new OperationResult[] { r1, r2 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2) + => Aggregate(r1, r2, (v1, v2) => (v1, v2)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2); + return Aggregate(r1.Result, r2.Result, combine); } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6) - => Aggregate(r1, r2, r3, r4, r5, r6, (v1, v2, v3, v4, v5, v6) => (v1, v2, v3, v4, v5, v6)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, combine); + + + public static OperationResult<(T1, T2, T3)> Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3) => + Aggregate(r1, r2, r3, (v1, v2, v3) => (v1, v2, v3)); + + + public static OperationResult Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, global::System.Func combine) + { + if (r1 is OperationResult.Ok_ ok1 && r2 is OperationResult.Ok_ ok2 && r3 is OperationResult.Ok_ ok3) + return combine(ok1.Value, ok2.Value, ok3.Value); + + return OperationResult.Error( + MergeErrors(new OperationResult[] { r1, r2, r3 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3) + => Aggregate(r1, r2, r3, (v1, v2, v3) => (v1, v2, v3)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3); + return Aggregate(r1.Result, r2.Result, r3.Result, combine); } - - - public static OperationResult<(T1, T2, T3, T4, T5, T6, T7)> Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7) => - Aggregate(r1, r2, r3, r4, r5, r6, r7, (v1, v2, v3, v4, v5, v6, v7) => (v1, v2, v3, v4, v5, v6, v7)); - - - public static OperationResult Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7, global::System.Func combine) - { - if (r1 is OperationResult.Ok_ ok1 && r2 is OperationResult.Ok_ ok2 && r3 is OperationResult.Ok_ ok3 && r4 is OperationResult.Ok_ ok4 && r5 is OperationResult.Ok_ ok5 && r6 is OperationResult.Ok_ ok6 && r7 is OperationResult.Ok_ ok7) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value); - - return OperationResult.Error( - MergeErrors(new OperationResult[] { r1, r2, r3, r4, r5, r6, r7 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); + + + public static OperationResult<(T1, T2, T3, T4)> Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4) => + Aggregate(r1, r2, r3, r4, (v1, v2, v3, v4) => (v1, v2, v3, v4)); + + + public static OperationResult Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, global::System.Func combine) + { + if (r1 is OperationResult.Ok_ ok1 && r2 is OperationResult.Ok_ ok2 && r3 is OperationResult.Ok_ ok3 && r4 is OperationResult.Ok_ ok4) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value); + + return OperationResult.Error( + MergeErrors(new OperationResult[] { r1, r2, r3, r4 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4) + => Aggregate(r1, r2, r3, r4, (v1, v2, v3, v4) => (v1, v2, v3, v4)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, combine); } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7) - => Aggregate(r1, r2, r3, r4, r5, r6, r7, (v1, v2, v3, v4, v5, v6, v7) => (v1, v2, v3, v4, v5, v6, v7)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, combine); + + + public static OperationResult<(T1, T2, T3, T4, T5)> Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5) => + Aggregate(r1, r2, r3, r4, r5, (v1, v2, v3, v4, v5) => (v1, v2, v3, v4, v5)); + + + public static OperationResult Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, global::System.Func combine) + { + if (r1 is OperationResult.Ok_ ok1 && r2 is OperationResult.Ok_ ok2 && r3 is OperationResult.Ok_ ok3 && r4 is OperationResult.Ok_ ok4 && r5 is OperationResult.Ok_ ok5) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value); + + return OperationResult.Error( + MergeErrors(new OperationResult[] { r1, r2, r3, r4, r5 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5) + => Aggregate(r1, r2, r3, r4, r5, (v1, v2, v3, v4, v5) => (v1, v2, v3, v4, v5)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, combine); } - - - public static OperationResult<(T1, T2, T3, T4, T5, T6, T7, T8)> Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7, OperationResult r8) => - Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, (v1, v2, v3, v4, v5, v6, v7, v8) => (v1, v2, v3, v4, v5, v6, v7, v8)); - - - public static OperationResult Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7, OperationResult r8, global::System.Func combine) - { - if (r1 is OperationResult.Ok_ ok1 && r2 is OperationResult.Ok_ ok2 && r3 is OperationResult.Ok_ ok3 && r4 is OperationResult.Ok_ ok4 && r5 is OperationResult.Ok_ ok5 && r6 is OperationResult.Ok_ ok6 && r7 is OperationResult.Ok_ ok7 && r8 is OperationResult.Ok_ ok8) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value, ok8.Value); - - return OperationResult.Error( - MergeErrors(new OperationResult[] { r1, r2, r3, r4, r5, r6, r7, r8 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); + + + public static OperationResult<(T1, T2, T3, T4, T5, T6)> Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6) => + Aggregate(r1, r2, r3, r4, r5, r6, (v1, v2, v3, v4, v5, v6) => (v1, v2, v3, v4, v5, v6)); + + + public static OperationResult Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, global::System.Func combine) + { + if (r1 is OperationResult.Ok_ ok1 && r2 is OperationResult.Ok_ ok2 && r3 is OperationResult.Ok_ ok3 && r4 is OperationResult.Ok_ ok4 && r5 is OperationResult.Ok_ ok5 && r6 is OperationResult.Ok_ ok6) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value); + + return OperationResult.Error( + MergeErrors(new OperationResult[] { r1, r2, r3, r4, r5, r6 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6) + => Aggregate(r1, r2, r3, r4, r5, r6, (v1, v2, v3, v4, v5, v6) => (v1, v2, v3, v4, v5, v6)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, combine); } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8) - => Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, (v1, v2, v3, v4, v5, v6, v7, v8) => (v1, v2, v3, v4, v5, v6, v7, v8)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7, r8); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, r8.Result, combine); + + + public static OperationResult<(T1, T2, T3, T4, T5, T6, T7)> Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7) => + Aggregate(r1, r2, r3, r4, r5, r6, r7, (v1, v2, v3, v4, v5, v6, v7) => (v1, v2, v3, v4, v5, v6, v7)); + + + public static OperationResult Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7, global::System.Func combine) + { + if (r1 is OperationResult.Ok_ ok1 && r2 is OperationResult.Ok_ ok2 && r3 is OperationResult.Ok_ ok3 && r4 is OperationResult.Ok_ ok4 && r5 is OperationResult.Ok_ ok5 && r6 is OperationResult.Ok_ ok6 && r7 is OperationResult.Ok_ ok7) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value); + + return OperationResult.Error( + MergeErrors(new OperationResult[] { r1, r2, r3, r4, r5, r6, r7 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7) + => Aggregate(r1, r2, r3, r4, r5, r6, r7, (v1, v2, v3, v4, v5, v6, v7) => (v1, v2, v3, v4, v5, v6, v7)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, combine); } - - - public static OperationResult<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7, OperationResult r8, OperationResult r9) => - Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, (v1, v2, v3, v4, v5, v6, v7, v8, v9) => (v1, v2, v3, v4, v5, v6, v7, v8, v9)); - - - public static OperationResult Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7, OperationResult r8, OperationResult r9, global::System.Func combine) - { - if (r1 is OperationResult.Ok_ ok1 && r2 is OperationResult.Ok_ ok2 && r3 is OperationResult.Ok_ ok3 && r4 is OperationResult.Ok_ ok4 && r5 is OperationResult.Ok_ ok5 && r6 is OperationResult.Ok_ ok6 && r7 is OperationResult.Ok_ ok7 && r8 is OperationResult.Ok_ ok8 && r9 is OperationResult.Ok_ ok9) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value, ok8.Value, ok9.Value); - - return OperationResult.Error( - MergeErrors(new OperationResult[] { r1, r2, r3, r4, r5, r6, r7, r8, r9 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); + + + public static OperationResult<(T1, T2, T3, T4, T5, T6, T7, T8)> Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7, OperationResult r8) => + Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, (v1, v2, v3, v4, v5, v6, v7, v8) => (v1, v2, v3, v4, v5, v6, v7, v8)); + + + public static OperationResult Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7, OperationResult r8, global::System.Func combine) + { + if (r1 is OperationResult.Ok_ ok1 && r2 is OperationResult.Ok_ ok2 && r3 is OperationResult.Ok_ ok3 && r4 is OperationResult.Ok_ ok4 && r5 is OperationResult.Ok_ ok5 && r6 is OperationResult.Ok_ ok6 && r7 is OperationResult.Ok_ ok7 && r8 is OperationResult.Ok_ ok8) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value, ok8.Value); + + return OperationResult.Error( + MergeErrors(new OperationResult[] { r1, r2, r3, r4, r5, r6, r7, r8 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8) + => Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, (v1, v2, v3, v4, v5, v6, v7, v8) => (v1, v2, v3, v4, v5, v6, v7, v8)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7, r8); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, r8.Result, combine); } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9) - => Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, (v1, v2, v3, v4, v5, v6, v7, v8, v9) => (v1, v2, v3, v4, v5, v6, v7, v8, v9)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7, r8, r9); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, r8.Result, r9.Result, combine); + + + public static OperationResult<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7, OperationResult r8, OperationResult r9) => + Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, (v1, v2, v3, v4, v5, v6, v7, v8, v9) => (v1, v2, v3, v4, v5, v6, v7, v8, v9)); + + + public static OperationResult Aggregate(this OperationResult r1, OperationResult r2, OperationResult r3, OperationResult r4, OperationResult r5, OperationResult r6, OperationResult r7, OperationResult r8, OperationResult r9, global::System.Func combine) + { + if (r1 is OperationResult.Ok_ ok1 && r2 is OperationResult.Ok_ ok2 && r3 is OperationResult.Ok_ ok3 && r4 is OperationResult.Ok_ ok4 && r5 is OperationResult.Ok_ ok5 && r6 is OperationResult.Ok_ ok6 && r7 is OperationResult.Ok_ ok7 && r8 is OperationResult.Ok_ ok8 && r9 is OperationResult.Ok_ ok9) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value, ok8.Value, ok9.Value); + + return OperationResult.Error( + MergeErrors(new OperationResult[] { r1, r2, r3, r4, r5, r6, r7, r8, r9 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9) + => Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, (v1, v2, v3, v4, v5, v6, v7, v8, v9) => (v1, v2, v3, v4, v5, v6, v7, v8, v9)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7, r8, r9); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, r8.Result, r9.Result, combine); } - - public static OperationResult FirstOk(this global::System.Collections.Generic.IEnumerable> results, global::System.Func onEmpty) - { - var errors = new global::System.Collections.Generic.List(); - foreach (var result in results) - { - if (result is OperationResult.Error_ e) - errors.Add(e.Details); - else - return result; - } - - if (!errors.Any()) - errors.Add(onEmpty()); - - return OperationResult.Error(MergeErrors(errors)); + + public static OperationResult FirstOk(this global::System.Collections.Generic.IEnumerable> results, global::System.Func onEmpty) + { + var errors = new global::System.Collections.Generic.List(); + foreach (var result in results) + { + if (result is OperationResult.Error_ e) + errors.Add(e.Details); + else + return result; + } + + if (!errors.Any()) + errors.Add(onEmpty()); + + return OperationResult.Error(MergeErrors(errors)); } - - - public static async global::System.Threading.Tasks.Task>> Aggregate( - this global::System.Collections.Generic.IEnumerable>> results, - int maxDegreeOfParallelism) - => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) + + + public static async global::System.Threading.Tasks.Task>> Aggregate( + this global::System.Collections.Generic.IEnumerable>> results, + int maxDegreeOfParallelism) + => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) + .Aggregate(); + + + public static async global::System.Threading.Tasks.Task>> AggregateMany( + this global::System.Collections.Generic.IEnumerable>>> results, + int maxDegreeOfParallelism) + => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) + .SelectMany(e => e) .Aggregate(); - - public static async global::System.Threading.Tasks.Task>> AggregateMany( - this global::System.Collections.Generic.IEnumerable>>> results, - int maxDegreeOfParallelism) - => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) - .SelectMany(e => e) - .Aggregate(); - - static async global::System.Threading.Tasks.Task SelectAsync(this global::System.Collections.Generic.IEnumerable items, global::System.Func> selector, int maxDegreeOfParallelism) - { - using (var throttler = new global::System.Threading.SemaphoreSlim(maxDegreeOfParallelism, maxDegreeOfParallelism)) - { - return await global::System.Threading.Tasks.Task.WhenAll(items.Select(async item => - { - // ReSharper disable once AccessToDisposedClosure - await throttler.WaitAsync().ConfigureAwait(false); - try - { - return await selector(item).ConfigureAwait(false); - } - finally - { - // ReSharper disable once AccessToDisposedClosure - throttler.Release(); - } - })).ConfigureAwait(false); - } + static async global::System.Threading.Tasks.Task SelectAsync(this global::System.Collections.Generic.IEnumerable items, global::System.Func> selector, int maxDegreeOfParallelism) + { + using (var throttler = new global::System.Threading.SemaphoreSlim(maxDegreeOfParallelism, maxDegreeOfParallelism)) + { + return await global::System.Threading.Tasks.Task.WhenAll(items.Select(async item => + { + // ReSharper disable once AccessToDisposedClosure + await throttler.WaitAsync().ConfigureAwait(false); + try + { + return await selector(item).ConfigureAwait(false); + } + finally + { + // ReSharper disable once AccessToDisposedClosure + throttler.Release(); + } + })).ConfigureAwait(false); + } } - - - public static OperationResult> AllOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate) => - candidates - .Select(c => c.Validate(validate)) + + + public static OperationResult> AllOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate) => + candidates + .Select(c => c.Validate(validate)) .Aggregate(); - - public static OperationResult> AllOk(this global::System.Collections.Generic.IEnumerable> candidates, - global::System.Func> validate) => - candidates + + public static OperationResult> AllOk(this global::System.Collections.Generic.IEnumerable> candidates, + global::System.Func> validate) => + candidates .Bind(items => items.AllOk(validate)); - - - public static OperationResult Validate(this OperationResult item, global::System.Func> validate) => item.Bind(i => i.Validate(validate)); - - - public static OperationResult Validate(this T item, global::System.Func> validate) - { - try - { - var errors = validate(item).ToList(); - return errors.Count > 0 ? OperationResult.Error(MergeErrors(errors)) : item; - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - return OperationResult.Error(FunicularSwitch.Generators.Consumer.Error.Generic(e)); - } + + + public static OperationResult Validate(this OperationResult item, global::System.Func> validate) => item.Bind(i => i.Validate(validate)); + + + public static OperationResult Validate(this T item, global::System.Func> validate) + { + try + { + var errors = validate(item).ToList(); + return errors.Count > 0 ? OperationResult.Error(MergeErrors(errors)) : item; + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + return OperationResult.Error(FunicularSwitch.Generators.Consumer.Error.Generic(e)); + } } - - - public static OperationResult FirstOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate, global::System.Func onEmpty) => - candidates - .Select(r => r.Validate(validate)) - .FirstOk(onEmpty); - - #region helpers - - static Error MergeErrors(global::System.Collections.Generic.IEnumerable errors) - { - var first = true; - Error aggregated = default!; - foreach (var myError in errors) - { - if (first) - { - aggregated = myError; - first = false; - } - else - { - aggregated = MergeErrors(aggregated, myError); - } - } - - return aggregated; - } - - static Error MergeErrors(Error aggregated, Error error) => aggregated.MergeErrors(error); - - #endregion - } -#pragma warning restore 1591 + + + public static OperationResult FirstOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate, global::System.Func onEmpty) => + candidates + .Select(r => r.Validate(validate)) + .FirstOk(onEmpty); + + #region helpers + + static Error MergeErrors(global::System.Collections.Generic.IEnumerable errors) + { + var first = true; + Error aggregated = default!; + foreach (var myError in errors) + { + if (first) + { + aggregated = myError; + first = false; + } + else + { + aggregated = MergeErrors(aggregated, myError); + } + } + + return aggregated; + } + + static Error MergeErrors(Error aggregated, Error error) => aggregated.MergeErrors(error); + + #endregion + } +#pragma warning restore 1591 } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Generators.Consumer.Result.g.cs b/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Generators.Consumer.Result.g.cs index 0f1b9708..78de5590 100644 --- a/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Generators.Consumer.Result.g.cs +++ b/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Generators.Consumer.Result.g.cs @@ -1,642 +1,642 @@ -#nullable enable +#nullable enable using global::System.Linq; using System; using FunicularSwitch.Generic; -namespace FunicularSwitch.Generators.Consumer +namespace FunicularSwitch.Generators.Consumer { -#pragma warning disable 1591 - public abstract partial class Result - { - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static Result Error(String details) => new Result.Error_(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static ResultError Error(String details) => new(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static Result Ok(T value) => new Result.Ok_(value); - public bool IsError => GetType().GetGenericTypeDefinition() == typeof(Result<>.Error_); - public bool IsOk => !IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public abstract String? GetErrorOrDefault(); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result Try( global::System.Func action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - } - - public abstract partial class Result : Result, global::System.Collections.Generic.IEnumerable - { - - [global::System.Diagnostics.DebuggerNonUserCode] - - public new static Result Error(String message) => Error(message); - - [global::System.Diagnostics.DebuggerNonUserCode] - - public static Result Ok(T value) => Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator Result(T value) => Result.Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator Result(ResultError myResultError) => myResultError.WithOk(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator true(Result result) => result.IsOk; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator false(Result result) => result.IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !(Result result) => result.IsError; - - //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode - bool Equals(Result other) => this switch - { - Ok_ ok => ok.Equals((object)other), - Error_ error => error.Equals((object)other), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") - }; - - public override int GetHashCode() => this switch - { - Ok_ ok => ok.GetHashCode(), - Error_ error => error.GetHashCode(), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") - }; - - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != this.GetType()) return false; - return Equals((Result)obj); - } - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Result? left, Result? right) => Equals(left, right); - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Result? left, Result? right) => !Equals(left, right); - - [global::System.Diagnostics.DebuggerStepThrough] - public void Match( global::System.Action ok, global::System.Action? error = null) => Match( - v => - { - ok.Invoke(v); - return 42; - }, - err => - { - error?.Invoke(err); - return 42; - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T1 Match( global::System.Func ok, global::System.Func error) - { - return this switch - { - Ok_ okResult => ok(okResult.Value), - Error_ errorResult => error(errorResult.Details), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) - { - return this switch - { - Ok_ okResult => await ok(okResult.Value).ConfigureAwait(false), - Error_ errorResult => await error(errorResult.Details).ConfigureAwait(false), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => - Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); - - [global::System.Diagnostics.DebuggerStepThrough] - public async global::System.Threading.Tasks.Task Match( global::System.Func ok) - { - if (this is Ok_ okResult) await ok(okResult.Value).ConfigureAwait(false); - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public T Match( global::System.Func error) => Match(v => v, error); - - [global::System.Diagnostics.DebuggerStepThrough] - - public Result Bind( global::System.Func> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return bind(ok.Value); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - return Result.Error(FunicularSwitch.Generators.Consumer.ErrorExtension.UnexpectedToStringError(e)); - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return await bind(ok.Value).ConfigureAwait(false); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - return Result.Error(FunicularSwitch.Generators.Consumer.ErrorExtension.UnexpectedToStringError(e)); - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public Result Map( global::System.Func map) - { - switch (this) - { - case Ok_ ok: - try - { - return Result.Ok(map(ok.Value)); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - return Result.Error(FunicularSwitch.Generators.Consumer.ErrorExtension.UnexpectedToStringError(e)); - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Map( - global::System.Func> map) - { - switch (this) - { - case Ok_ ok: - try - { - return Result.Ok(await map(ok.Value).ConfigureAwait(false)); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - return Result.Error(FunicularSwitch.Generators.Consumer.ErrorExtension.UnexpectedToStringError(e)); - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - - public static implicit operator global::FunicularSwitch.Generic.GenericResult(Result result) => - result.Match( - global::FunicularSwitch.Generic.GenericResult.Ok, - global::FunicularSwitch.Generic.GenericResult.Error); - - public static implicit operator Result(global::FunicularSwitch.Generic.GenericResult result) => - result.Match>( - Result.Ok, - Result.Error); - - public global::FunicularSwitch.Generic.GenericResult ToGenericResult() => - Match( - global::FunicularSwitch.Generic.GenericResult.Ok, +#pragma warning disable 1591 + public abstract partial class Result + { + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static Result Error(String details) => new Result.Error_(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static ResultError Error(String details) => new(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static Result Ok(T value) => new Result.Ok_(value); + public bool IsError => GetType().GetGenericTypeDefinition() == typeof(Result<>.Error_); + public bool IsOk => !IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public abstract String? GetErrorOrDefault(); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result Try( global::System.Func action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + } + + public abstract partial class Result : Result, global::System.Collections.Generic.IEnumerable + { + + [global::System.Diagnostics.DebuggerNonUserCode] + + public new static Result Error(String message) => Error(message); + + [global::System.Diagnostics.DebuggerNonUserCode] + + public static Result Ok(T value) => Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator Result(T value) => Result.Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator Result(ResultError myResultError) => myResultError.WithOk(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator true(Result result) => result.IsOk; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator false(Result result) => result.IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !(Result result) => result.IsError; + + //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode + bool Equals(Result other) => this switch + { + Ok_ ok => ok.Equals((object)other), + Error_ error => error.Equals((object)other), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") + }; + + public override int GetHashCode() => this switch + { + Ok_ ok => ok.GetHashCode(), + Error_ error => error.GetHashCode(), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") + }; + + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((Result)obj); + } + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Result? left, Result? right) => Equals(left, right); + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Result? left, Result? right) => !Equals(left, right); + + [global::System.Diagnostics.DebuggerStepThrough] + public void Match( global::System.Action ok, global::System.Action? error = null) => Match( + v => + { + ok.Invoke(v); + return 42; + }, + err => + { + error?.Invoke(err); + return 42; + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T1 Match( global::System.Func ok, global::System.Func error) + { + return this switch + { + Ok_ okResult => ok(okResult.Value), + Error_ errorResult => error(errorResult.Details), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) + { + return this switch + { + Ok_ okResult => await ok(okResult.Value).ConfigureAwait(false), + Error_ errorResult => await error(errorResult.Details).ConfigureAwait(false), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => + Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); + + [global::System.Diagnostics.DebuggerStepThrough] + public async global::System.Threading.Tasks.Task Match( global::System.Func ok) + { + if (this is Ok_ okResult) await ok(okResult.Value).ConfigureAwait(false); + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public T Match( global::System.Func error) => Match(v => v, error); + + [global::System.Diagnostics.DebuggerStepThrough] + + public Result Bind( global::System.Func> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return bind(ok.Value); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + return Result.Error(FunicularSwitch.Generators.Consumer.ErrorExtension.UnexpectedToStringError(e)); + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return await bind(ok.Value).ConfigureAwait(false); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + return Result.Error(FunicularSwitch.Generators.Consumer.ErrorExtension.UnexpectedToStringError(e)); + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public Result Map( global::System.Func map) + { + switch (this) + { + case Ok_ ok: + try + { + return Result.Ok(map(ok.Value)); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + return Result.Error(FunicularSwitch.Generators.Consumer.ErrorExtension.UnexpectedToStringError(e)); + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Map( + global::System.Func> map) + { + switch (this) + { + case Ok_ ok: + try + { + return Result.Ok(await map(ok.Value).ConfigureAwait(false)); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + return Result.Error(FunicularSwitch.Generators.Consumer.ErrorExtension.UnexpectedToStringError(e)); + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + + public static implicit operator global::FunicularSwitch.Generic.GenericResult(Result result) => + result.Match( + global::FunicularSwitch.Generic.GenericResult.Ok, + global::FunicularSwitch.Generic.GenericResult.Error); + + public static implicit operator Result(global::FunicularSwitch.Generic.GenericResult result) => + result.Match>( + Result.Ok, + Result.Error); + + public global::FunicularSwitch.Generic.GenericResult ToGenericResult() => + Match( + global::FunicularSwitch.Generic.GenericResult.Ok, global::FunicularSwitch.Generic.GenericResult.Error); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T? GetValueOrDefault() - => Match( - v => (T?)v, - _ => default - ); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T GetValueOrDefault( global::System.Func defaultValue) - => Match( - v => v, - _ => defaultValue() - ); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T GetValueOrDefault(T defaultValue) - => Match( - v => v, - _ => defaultValue - ); - - [global::System.Diagnostics.DebuggerStepThrough] - public T GetValueOrThrow() - => Match( - v => v, - details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); - - - public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); - - - public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); - global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); - - public sealed partial class Ok_ : Result - { - public T Value { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Ok_(T value) => Value = value; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override String? GetErrorOrDefault() => null; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Ok_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Ok_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); - } - - public sealed partial class Error_ : Result - { - public String Details { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Error_(String details) => Details = details; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public Result.Error_ Convert() => new Result.Error_(Details); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override String? GetErrorOrDefault() => Details; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Error_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return Equals(Details, other.Details); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Error_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); - } - } - - public readonly partial struct ResultError : global::System.IEquatable - { - readonly String _details; - - internal ResultError(String details) => _details = details; - - [global::System.Diagnostics.Contracts.Pure] - public Result WithOk() => Result.Error(_details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(ResultError other) => _details.Equals(other._details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) => obj is ResultError other && Equals(other); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => _details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(ResultError left, ResultError right) => left.Equals(right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(ResultError left, ResultError right) => !left.Equals(right); - } - - public static partial class ResultExtension - { - #region bind - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => (await result.ConfigureAwait(false)).Bind(bind); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func>> bind) - => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); - - #endregion - - #region map - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func map) - => (await result.ConfigureAwait(false)).Map(map); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => Bind(result, async v => Result.Ok(await bind(v).ConfigureAwait(false))); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result MapError(this Result result, global::System.Func mapError) - { - if (result is Result.Error_ e) - return Result.Error(mapError(e.Details)); - return result; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); - - #endregion - - #region match - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func> error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func ok, - global::System.Func error) - => (await result.ConfigureAwait(false)).Match(ok, error); - - #endregion - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static Result Flatten(this Result> result) => result.Bind(r => r); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result As(this Result result, global::System.Func errorTIsNotT1) => - result.Bind(r => - { - if (r is T1 converted) - return converted; - return Result.Error(errorTIsNotT1()); - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result As(this Result result, global::System.Func errorIsNotT1) => - result.As(errorIsNotT1); - - #region query-expression pattern - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result Select(this Result result, global::System.Func selector) => result.Map(selector); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result SelectMany(this Result result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this Result result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - - #endregion - - - public static Result ToResult( - this global::FunicularSwitch.Generic.GenericResult result) => - result.Match>( - Result.Ok, - Result.Error); - - public static global::System.Threading.Tasks.Task> ToResult( - this global::System.Threading.Tasks.Task> result) => - result.Match( - Result.Ok, - Result.Error); - - public static global::System.Threading.Tasks.Task> ToGenericResult( - this global::System.Threading.Tasks.Task> result) => - result.Match( - global::FunicularSwitch.Generic.GenericResult.Ok, + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T? GetValueOrDefault() + => Match( + v => (T?)v, + _ => default + ); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T GetValueOrDefault( global::System.Func defaultValue) + => Match( + v => v, + _ => defaultValue() + ); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T GetValueOrDefault(T defaultValue) + => Match( + v => v, + _ => defaultValue + ); + + [global::System.Diagnostics.DebuggerStepThrough] + public T GetValueOrThrow() + => Match( + v => v, + details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); + + + public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); + + + public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); + global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); + + public sealed partial class Ok_ : Result + { + public T Value { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Ok_(T value) => Value = value; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override String? GetErrorOrDefault() => null; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Ok_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Ok_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); + } + + public sealed partial class Error_ : Result + { + public String Details { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Error_(String details) => Details = details; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public Result.Error_ Convert() => new Result.Error_(Details); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override String? GetErrorOrDefault() => Details; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Error_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Equals(Details, other.Details); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Error_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); + } + } + + public readonly partial struct ResultError : global::System.IEquatable + { + readonly String _details; + + internal ResultError(String details) => _details = details; + + [global::System.Diagnostics.Contracts.Pure] + public Result WithOk() => Result.Error(_details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(ResultError other) => _details.Equals(other._details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) => obj is ResultError other && Equals(other); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => _details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(ResultError left, ResultError right) => left.Equals(right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(ResultError left, ResultError right) => !left.Equals(right); + } + + public static partial class ResultExtension + { + #region bind + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => (await result.ConfigureAwait(false)).Bind(bind); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func>> bind) + => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); + + #endregion + + #region map + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func map) + => (await result.ConfigureAwait(false)).Map(map); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => Bind(result, async v => Result.Ok(await bind(v).ConfigureAwait(false))); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result MapError(this Result result, global::System.Func mapError) + { + if (result is Result.Error_ e) + return Result.Error(mapError(e.Details)); + return result; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); + + #endregion + + #region match + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func> error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func ok, + global::System.Func error) + => (await result.ConfigureAwait(false)).Match(ok, error); + + #endregion + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static Result Flatten(this Result> result) => result.Bind(r => r); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result As(this Result result, global::System.Func errorTIsNotT1) => + result.Bind(r => + { + if (r is T1 converted) + return converted; + return Result.Error(errorTIsNotT1()); + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result As(this Result result, global::System.Func errorIsNotT1) => + result.As(errorIsNotT1); + + #region query-expression pattern + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result Select(this Result result, global::System.Func selector) => result.Map(selector); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result SelectMany(this Result result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this Result result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + + #endregion + + + public static Result ToResult( + this global::FunicularSwitch.Generic.GenericResult result) => + result.Match>( + Result.Ok, + Result.Error); + + public static global::System.Threading.Tasks.Task> ToResult( + this global::System.Threading.Tasks.Task> result) => + result.Match( + Result.Ok, + Result.Error); + + public static global::System.Threading.Tasks.Task> ToGenericResult( + this global::System.Threading.Tasks.Task> result) => + result.Match( + global::FunicularSwitch.Generic.GenericResult.Ok, global::FunicularSwitch.Generic.GenericResult.Error); - } -} - -namespace FunicularSwitch.Generators.Consumer.Extensions + } +} + +namespace FunicularSwitch.Generators.Consumer.Extensions { - public static partial class ResultExtension - { - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable items, - global::System.Func> choose, - global::System.Action onError) - => items - .Select(i => choose(i)) - .Choose(onError); - - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable> results, - global::System.Action onError) - => results - .Where(r => - r.Match(_ => true, error => - { - onError(error); - return false; - })) - .Select(r => r.GetValueOrThrow()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result As(this object? item, global::System.Func error) => - !(item is T t) ? Result.Error(error()) : t; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result NotNull(this T? item, global::System.Func error) => - item ?? Result.Error(error()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result NotNullOrEmpty(this string? s, global::System.Func error) - => string.IsNullOrEmpty(s) ? Result.Error(error()) : s!; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result NotNullOrWhiteSpace(this string? s, global::System.Func error) - => string.IsNullOrWhiteSpace(s) ? Result.Error(error()) : s!; - - - public static Result First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => - candidates - .FirstOrDefault(i => predicate(i)) - .NotNull(noMatch); - } -#pragma warning restore 1591 -} + public static partial class ResultExtension + { + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable items, + global::System.Func> choose, + global::System.Action onError) + => items + .Select(i => choose(i)) + .Choose(onError); + + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable> results, + global::System.Action onError) + => results + .Where(r => + r.Match(_ => true, error => + { + onError(error); + return false; + })) + .Select(r => r.GetValueOrThrow()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result As(this object? item, global::System.Func error) => + !(item is T t) ? Result.Error(error()) : t; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result NotNull(this T? item, global::System.Func error) => + item ?? Result.Error(error()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result NotNullOrEmpty(this string? s, global::System.Func error) + => string.IsNullOrEmpty(s) ? Result.Error(error()) : s!; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result NotNullOrWhiteSpace(this string? s, global::System.Func error) + => string.IsNullOrWhiteSpace(s) ? Result.Error(error()) : s!; + + + public static Result First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => + candidates + .FirstOrDefault(i => predicate(i)) + .NotNull(noMatch); + } +#pragma warning restore 1591 +} diff --git a/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Generators.Consumer.ResultWithMerge.g.cs b/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Generators.Consumer.ResultWithMerge.g.cs index 7963c0fb..48416a7b 100644 --- a/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Generators.Consumer.ResultWithMerge.g.cs +++ b/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Generators.Consumer.ResultWithMerge.g.cs @@ -1,525 +1,525 @@ -#nullable enable -using global::System.Linq; +#nullable enable +using global::System.Linq; using System; -using FunicularSwitch.Generic; - -namespace FunicularSwitch.Generators.Consumer -{ -#pragma warning disable 1591 - public abstract partial class Result - { - - - - public static Result<(T1, T2)> Aggregate(Result r1, Result r2) => ResultExtension.Aggregate(r1, r2); - - - public static Result Aggregate(Result r1, Result r2, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2) => ResultExtension.Aggregate(r1, r2); - - +using FunicularSwitch.Generic; + +namespace FunicularSwitch.Generators.Consumer +{ +#pragma warning disable 1591 + public abstract partial class Result + { + + + + public static Result<(T1, T2)> Aggregate(Result r1, Result r2) => ResultExtension.Aggregate(r1, r2); + + + public static Result Aggregate(Result r1, Result r2, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2) => ResultExtension.Aggregate(r1, r2); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, combine); - - - - public static Result<(T1, T2, T3)> Aggregate(Result r1, Result r2, Result r3) => ResultExtension.Aggregate(r1, r2, r3); - - - public static Result Aggregate(Result r1, Result r2, Result r3, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3) => ResultExtension.Aggregate(r1, r2, r3); - - + + + + public static Result<(T1, T2, T3)> Aggregate(Result r1, Result r2, Result r3) => ResultExtension.Aggregate(r1, r2, r3); + + + public static Result Aggregate(Result r1, Result r2, Result r3, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3) => ResultExtension.Aggregate(r1, r2, r3); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, combine); - - - - public static Result<(T1, T2, T3, T4)> Aggregate(Result r1, Result r2, Result r3, Result r4) => ResultExtension.Aggregate(r1, r2, r3, r4); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4) => ResultExtension.Aggregate(r1, r2, r3, r4); - - + + + + public static Result<(T1, T2, T3, T4)> Aggregate(Result r1, Result r2, Result r3, Result r4) => ResultExtension.Aggregate(r1, r2, r3, r4); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4) => ResultExtension.Aggregate(r1, r2, r3, r4); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, combine); - - - - public static Result<(T1, T2, T3, T4, T5)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5) => ResultExtension.Aggregate(r1, r2, r3, r4, r5); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5) => ResultExtension.Aggregate(r1, r2, r3, r4, r5); - - + + + + public static Result<(T1, T2, T3, T4, T5)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5) => ResultExtension.Aggregate(r1, r2, r3, r4, r5); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5) => ResultExtension.Aggregate(r1, r2, r3, r4, r5); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, combine); - - - - public static Result<(T1, T2, T3, T4, T5, T6)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6); - - + + + + public static Result<(T1, T2, T3, T4, T5, T6)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, combine); - - - - public static Result<(T1, T2, T3, T4, T5, T6, T7)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7); - - + + + + public static Result<(T1, T2, T3, T4, T5, T6, T7)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, combine); - - - - public static Result<(T1, T2, T3, T4, T5, T6, T7, T8)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8); - - + + + + public static Result<(T1, T2, T3, T4, T5, T6, T7, T8)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, combine); - - - - public static Result<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, combine); - } - - public static partial class ResultExtension - { - - public static Result> Map(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func map) => + + + + public static Result<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, combine); + } + + public static partial class ResultExtension + { + + public static Result> Map(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func map) => results.Select(r => r.Map(map)).Aggregate(); - - - public static Result> Bind(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func> bind) => + + + public static Result> Bind(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func> bind) => results.Select(r => r.Bind(bind)).Aggregate(); - - public static Result> Bind(this Result result, - global::System.Func>> bindMany) => + + public static Result> Bind(this Result result, + global::System.Func>> bindMany) => result.Map(ok => bindMany(ok).Aggregate()).Flatten(); - - public static Result Bind(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func, Result> bind) => + + public static Result Bind(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func, Result> bind) => results.Aggregate().Bind(bind); - - public static Result> Aggregate(this global::System.Collections.Generic.IEnumerable> results) - { - var isError = false; - String aggregated = default!; - var oks = new global::System.Collections.Generic.List(); - foreach (var result in results) - { - result.Match( - ok => oks.Add(ok), - error => - { - aggregated = !isError ? error : MergeErrors(aggregated, error); - isError = true; - } - ); - } - - return isError - ? Result.Error>(aggregated) - : Result.Ok>(oks); + + public static Result> Aggregate(this global::System.Collections.Generic.IEnumerable> results) + { + var isError = false; + String aggregated = default!; + var oks = new global::System.Collections.Generic.List(); + foreach (var result in results) + { + result.Match( + ok => oks.Add(ok), + error => + { + aggregated = !isError ? error : MergeErrors(aggregated, error); + isError = true; + } + ); + } + + return isError + ? Result.Error>(aggregated) + : Result.Ok>(oks); } - - - public static async global::System.Threading.Tasks.Task>> Aggregate( - this global::System.Threading.Tasks.Task>> results) - => (await results.ConfigureAwait(false)) + + + public static async global::System.Threading.Tasks.Task>> Aggregate( + this global::System.Threading.Tasks.Task>> results) + => (await results.ConfigureAwait(false)) .Aggregate(); - - - public static async global::System.Threading.Tasks.Task>> Aggregate( - this global::System.Collections.Generic.IEnumerable>> results) - => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) + + + public static async global::System.Threading.Tasks.Task>> Aggregate( + this global::System.Collections.Generic.IEnumerable>> results) + => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) .Aggregate(); - - - public static async global::System.Threading.Tasks.Task>> AggregateMany( - this global::System.Collections.Generic.IEnumerable>>> results) - => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) - .SelectMany(e => e) + + + public static async global::System.Threading.Tasks.Task>> AggregateMany( + this global::System.Collections.Generic.IEnumerable>>> results) + => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) + .SelectMany(e => e) .Aggregate(); - - - public static Result<(T1, T2)> Aggregate(this Result r1, Result r2) => - Aggregate(r1, r2, (v1, v2) => (v1, v2)); - - - public static Result Aggregate(this Result r1, Result r2, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2) - return combine(ok1.Value, ok2.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); - } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2) - => Aggregate(r1, r2, (v1, v2) => (v1, v2)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2); - return Aggregate(r1.Result, r2.Result, combine); - } - - - public static Result<(T1, T2, T3)> Aggregate(this Result r1, Result r2, Result r3) => - Aggregate(r1, r2, r3, (v1, v2, v3) => (v1, v2, v3)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3) - return combine(ok1.Value, ok2.Value, ok3.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); - } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3) - => Aggregate(r1, r2, r3, (v1, v2, v3) => (v1, v2, v3)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3); - return Aggregate(r1.Result, r2.Result, r3.Result, combine); - } - - - public static Result<(T1, T2, T3, T4)> Aggregate(this Result r1, Result r2, Result r3, Result r4) => - Aggregate(r1, r2, r3, r4, (v1, v2, v3, v4) => (v1, v2, v3, v4)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); - } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4) - => Aggregate(r1, r2, r3, r4, (v1, v2, v3, v4) => (v1, v2, v3, v4)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, combine); - } - - - public static Result<(T1, T2, T3, T4, T5)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5) => - Aggregate(r1, r2, r3, r4, r5, (v1, v2, v3, v4, v5) => (v1, v2, v3, v4, v5)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4, r5 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); - } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5) - => Aggregate(r1, r2, r3, r4, r5, (v1, v2, v3, v4, v5) => (v1, v2, v3, v4, v5)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, combine); - } - - - public static Result<(T1, T2, T3, T4, T5, T6)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6) => - Aggregate(r1, r2, r3, r4, r5, r6, (v1, v2, v3, v4, v5, v6) => (v1, v2, v3, v4, v5, v6)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); + + + public static Result<(T1, T2)> Aggregate(this Result r1, Result r2) => + Aggregate(r1, r2, (v1, v2) => (v1, v2)); + + + public static Result Aggregate(this Result r1, Result r2, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2) + return combine(ok1.Value, ok2.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2) + => Aggregate(r1, r2, (v1, v2) => (v1, v2)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2); + return Aggregate(r1.Result, r2.Result, combine); } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6) - => Aggregate(r1, r2, r3, r4, r5, r6, (v1, v2, v3, v4, v5, v6) => (v1, v2, v3, v4, v5, v6)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, combine); + + + public static Result<(T1, T2, T3)> Aggregate(this Result r1, Result r2, Result r3) => + Aggregate(r1, r2, r3, (v1, v2, v3) => (v1, v2, v3)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3) + return combine(ok1.Value, ok2.Value, ok3.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3) + => Aggregate(r1, r2, r3, (v1, v2, v3) => (v1, v2, v3)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3); + return Aggregate(r1.Result, r2.Result, r3.Result, combine); } - - - public static Result<(T1, T2, T3, T4, T5, T6, T7)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7) => - Aggregate(r1, r2, r3, r4, r5, r6, r7, (v1, v2, v3, v4, v5, v6, v7) => (v1, v2, v3, v4, v5, v6, v7)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); + + + public static Result<(T1, T2, T3, T4)> Aggregate(this Result r1, Result r2, Result r3, Result r4) => + Aggregate(r1, r2, r3, r4, (v1, v2, v3, v4) => (v1, v2, v3, v4)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4) + => Aggregate(r1, r2, r3, r4, (v1, v2, v3, v4) => (v1, v2, v3, v4)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, combine); } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7) - => Aggregate(r1, r2, r3, r4, r5, r6, r7, (v1, v2, v3, v4, v5, v6, v7) => (v1, v2, v3, v4, v5, v6, v7)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, combine); + + + public static Result<(T1, T2, T3, T4, T5)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5) => + Aggregate(r1, r2, r3, r4, r5, (v1, v2, v3, v4, v5) => (v1, v2, v3, v4, v5)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4, r5 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5) + => Aggregate(r1, r2, r3, r4, r5, (v1, v2, v3, v4, v5) => (v1, v2, v3, v4, v5)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, combine); } - - - public static Result<(T1, T2, T3, T4, T5, T6, T7, T8)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8) => - Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, (v1, v2, v3, v4, v5, v6, v7, v8) => (v1, v2, v3, v4, v5, v6, v7, v8)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7 && r8 is Result.Ok_ ok8) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value, ok8.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7, r8 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); + + + public static Result<(T1, T2, T3, T4, T5, T6)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6) => + Aggregate(r1, r2, r3, r4, r5, r6, (v1, v2, v3, v4, v5, v6) => (v1, v2, v3, v4, v5, v6)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6) + => Aggregate(r1, r2, r3, r4, r5, r6, (v1, v2, v3, v4, v5, v6) => (v1, v2, v3, v4, v5, v6)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, combine); } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8) - => Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, (v1, v2, v3, v4, v5, v6, v7, v8) => (v1, v2, v3, v4, v5, v6, v7, v8)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7, r8); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, r8.Result, combine); + + + public static Result<(T1, T2, T3, T4, T5, T6, T7)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7) => + Aggregate(r1, r2, r3, r4, r5, r6, r7, (v1, v2, v3, v4, v5, v6, v7) => (v1, v2, v3, v4, v5, v6, v7)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7) + => Aggregate(r1, r2, r3, r4, r5, r6, r7, (v1, v2, v3, v4, v5, v6, v7) => (v1, v2, v3, v4, v5, v6, v7)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, combine); } - - - public static Result<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9) => - Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, (v1, v2, v3, v4, v5, v6, v7, v8, v9) => (v1, v2, v3, v4, v5, v6, v7, v8, v9)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7 && r8 is Result.Ok_ ok8 && r9 is Result.Ok_ ok9) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value, ok8.Value, ok9.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7, r8, r9 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); + + + public static Result<(T1, T2, T3, T4, T5, T6, T7, T8)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8) => + Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, (v1, v2, v3, v4, v5, v6, v7, v8) => (v1, v2, v3, v4, v5, v6, v7, v8)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7 && r8 is Result.Ok_ ok8) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value, ok8.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7, r8 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8) + => Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, (v1, v2, v3, v4, v5, v6, v7, v8) => (v1, v2, v3, v4, v5, v6, v7, v8)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7, r8); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, r8.Result, combine); } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9) - => Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, (v1, v2, v3, v4, v5, v6, v7, v8, v9) => (v1, v2, v3, v4, v5, v6, v7, v8, v9)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7, r8, r9); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, r8.Result, r9.Result, combine); + + + public static Result<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9) => + Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, (v1, v2, v3, v4, v5, v6, v7, v8, v9) => (v1, v2, v3, v4, v5, v6, v7, v8, v9)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7 && r8 is Result.Ok_ ok8 && r9 is Result.Ok_ ok9) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value, ok8.Value, ok9.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7, r8, r9 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9) + => Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, (v1, v2, v3, v4, v5, v6, v7, v8, v9) => (v1, v2, v3, v4, v5, v6, v7, v8, v9)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7, r8, r9); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, r8.Result, r9.Result, combine); } - - public static Result FirstOk(this global::System.Collections.Generic.IEnumerable> results, global::System.Func onEmpty) - { - var errors = new global::System.Collections.Generic.List(); - foreach (var result in results) - { - if (result is Result.Error_ e) - errors.Add(e.Details); - else - return result; - } - - if (!errors.Any()) - errors.Add(onEmpty()); - - return Result.Error(MergeErrors(errors)); + + public static Result FirstOk(this global::System.Collections.Generic.IEnumerable> results, global::System.Func onEmpty) + { + var errors = new global::System.Collections.Generic.List(); + foreach (var result in results) + { + if (result is Result.Error_ e) + errors.Add(e.Details); + else + return result; + } + + if (!errors.Any()) + errors.Add(onEmpty()); + + return Result.Error(MergeErrors(errors)); } - - - public static async global::System.Threading.Tasks.Task>> Aggregate( - this global::System.Collections.Generic.IEnumerable>> results, - int maxDegreeOfParallelism) - => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) + + + public static async global::System.Threading.Tasks.Task>> Aggregate( + this global::System.Collections.Generic.IEnumerable>> results, + int maxDegreeOfParallelism) + => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) + .Aggregate(); + + + public static async global::System.Threading.Tasks.Task>> AggregateMany( + this global::System.Collections.Generic.IEnumerable>>> results, + int maxDegreeOfParallelism) + => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) + .SelectMany(e => e) .Aggregate(); - - public static async global::System.Threading.Tasks.Task>> AggregateMany( - this global::System.Collections.Generic.IEnumerable>>> results, - int maxDegreeOfParallelism) - => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) - .SelectMany(e => e) - .Aggregate(); - - static async global::System.Threading.Tasks.Task SelectAsync(this global::System.Collections.Generic.IEnumerable items, global::System.Func> selector, int maxDegreeOfParallelism) - { - using (var throttler = new global::System.Threading.SemaphoreSlim(maxDegreeOfParallelism, maxDegreeOfParallelism)) - { - return await global::System.Threading.Tasks.Task.WhenAll(items.Select(async item => - { - // ReSharper disable once AccessToDisposedClosure - await throttler.WaitAsync().ConfigureAwait(false); - try - { - return await selector(item).ConfigureAwait(false); - } - finally - { - // ReSharper disable once AccessToDisposedClosure - throttler.Release(); - } - })).ConfigureAwait(false); - } + static async global::System.Threading.Tasks.Task SelectAsync(this global::System.Collections.Generic.IEnumerable items, global::System.Func> selector, int maxDegreeOfParallelism) + { + using (var throttler = new global::System.Threading.SemaphoreSlim(maxDegreeOfParallelism, maxDegreeOfParallelism)) + { + return await global::System.Threading.Tasks.Task.WhenAll(items.Select(async item => + { + // ReSharper disable once AccessToDisposedClosure + await throttler.WaitAsync().ConfigureAwait(false); + try + { + return await selector(item).ConfigureAwait(false); + } + finally + { + // ReSharper disable once AccessToDisposedClosure + throttler.Release(); + } + })).ConfigureAwait(false); + } } - - - public static Result> AllOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate) => - candidates - .Select(c => c.Validate(validate)) + + + public static Result> AllOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate) => + candidates + .Select(c => c.Validate(validate)) .Aggregate(); - - public static Result> AllOk(this global::System.Collections.Generic.IEnumerable> candidates, - global::System.Func> validate) => - candidates + + public static Result> AllOk(this global::System.Collections.Generic.IEnumerable> candidates, + global::System.Func> validate) => + candidates .Bind(items => items.AllOk(validate)); - - - public static Result Validate(this Result item, global::System.Func> validate) => item.Bind(i => i.Validate(validate)); - - - public static Result Validate(this T item, global::System.Func> validate) - { - try - { - var errors = validate(item).ToList(); - return errors.Count > 0 ? Result.Error(MergeErrors(errors)) : item; - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - return Result.Error(FunicularSwitch.Generators.Consumer.ErrorExtension.UnexpectedToStringError(e)); - } + + + public static Result Validate(this Result item, global::System.Func> validate) => item.Bind(i => i.Validate(validate)); + + + public static Result Validate(this T item, global::System.Func> validate) + { + try + { + var errors = validate(item).ToList(); + return errors.Count > 0 ? Result.Error(MergeErrors(errors)) : item; + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + return Result.Error(FunicularSwitch.Generators.Consumer.ErrorExtension.UnexpectedToStringError(e)); + } } - - - public static Result FirstOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate, global::System.Func onEmpty) => - candidates - .Select(r => r.Validate(validate)) - .FirstOk(onEmpty); - - #region helpers - - static String MergeErrors(global::System.Collections.Generic.IEnumerable errors) - { - var first = true; - String aggregated = default!; - foreach (var myError in errors) - { - if (first) - { - aggregated = myError; - first = false; - } - else - { - aggregated = MergeErrors(aggregated, myError); - } - } - - return aggregated; - } - - static String MergeErrors(String aggregated, String error) => aggregated.MergeErrors(error); - - #endregion - } -#pragma warning restore 1591 + + + public static Result FirstOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate, global::System.Func onEmpty) => + candidates + .Select(r => r.Validate(validate)) + .FirstOk(onEmpty); + + #region helpers + + static String MergeErrors(global::System.Collections.Generic.IEnumerable errors) + { + var first = true; + String aggregated = default!; + foreach (var myError in errors) + { + if (first) + { + aggregated = myError; + first = false; + } + else + { + aggregated = MergeErrors(aggregated, myError); + } + } + + return aggregated; + } + + static String MergeErrors(String aggregated, String error) => aggregated.MergeErrors(error); + + #endregion + } +#pragma warning restore 1591 } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Generators.Consumer.System.Result.g.cs b/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Generators.Consumer.System.Result.g.cs index 329f1842..d8d4fd4e 100644 --- a/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Generators.Consumer.System.Result.g.cs +++ b/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/FunicularSwitch.Generators.Consumer.System.Result.g.cs @@ -1,641 +1,641 @@ -#nullable enable +#nullable enable using global::System.Linq; using FunicularSwitch.Generic; -namespace FunicularSwitch.Generators.Consumer.System +namespace FunicularSwitch.Generators.Consumer.System { -#pragma warning disable 1591 - public abstract partial class Result - { - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static Result Error(Action details) => new Result.Error_(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static ResultError Error(Action details) => new(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static Result Ok(T value) => new Result.Ok_(value); - public bool IsError => GetType().GetGenericTypeDefinition() == typeof(Result<>.Error_); - public bool IsOk => !IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public abstract Action? GetErrorOrDefault(); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result Try( global::System.Func action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - } - - public abstract partial class Result : Result, global::System.Collections.Generic.IEnumerable - { - - [global::System.Diagnostics.DebuggerNonUserCode] - - public new static Result Error(Action message) => Error(message); - - [global::System.Diagnostics.DebuggerNonUserCode] - - public static Result Ok(T value) => Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator Result(T value) => Result.Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator Result(ResultError myResultError) => myResultError.WithOk(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator true(Result result) => result.IsOk; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator false(Result result) => result.IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !(Result result) => result.IsError; - - //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode - bool Equals(Result other) => this switch - { - Ok_ ok => ok.Equals((object)other), - Error_ error => error.Equals((object)other), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") - }; - - public override int GetHashCode() => this switch - { - Ok_ ok => ok.GetHashCode(), - Error_ error => error.GetHashCode(), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") - }; - - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != this.GetType()) return false; - return Equals((Result)obj); - } - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Result? left, Result? right) => Equals(left, right); - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Result? left, Result? right) => !Equals(left, right); - - [global::System.Diagnostics.DebuggerStepThrough] - public void Match( global::System.Action ok, global::System.Action? error = null) => Match( - v => - { - ok.Invoke(v); - return 42; - }, - err => - { - error?.Invoke(err); - return 42; - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T1 Match( global::System.Func ok, global::System.Func error) - { - return this switch - { - Ok_ okResult => ok(okResult.Value), - Error_ errorResult => error(errorResult.Details), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) - { - return this switch - { - Ok_ okResult => await ok(okResult.Value).ConfigureAwait(false), - Error_ errorResult => await error(errorResult.Details).ConfigureAwait(false), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => - Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); - - [global::System.Diagnostics.DebuggerStepThrough] - public async global::System.Threading.Tasks.Task Match( global::System.Func ok) - { - if (this is Ok_ okResult) await ok(okResult.Value).ConfigureAwait(false); - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public T Match( global::System.Func error) => Match(v => v, error); - - [global::System.Diagnostics.DebuggerStepThrough] - - public Result Bind( global::System.Func> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return bind(ok.Value); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return await bind(ok.Value).ConfigureAwait(false); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public Result Map( global::System.Func map) - { - switch (this) - { - case Ok_ ok: - try - { - return Result.Ok(map(ok.Value)); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Map( - global::System.Func> map) - { - switch (this) - { - case Ok_ ok: - try - { - return Result.Ok(await map(ok.Value).ConfigureAwait(false)); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - - public static implicit operator global::FunicularSwitch.Generic.GenericResult(Result result) => - result.Match( - global::FunicularSwitch.Generic.GenericResult.Ok, - global::FunicularSwitch.Generic.GenericResult.Error); - - public static implicit operator Result(global::FunicularSwitch.Generic.GenericResult result) => - result.Match>( - Result.Ok, - Result.Error); - - public global::FunicularSwitch.Generic.GenericResult ToGenericResult() => - Match( - global::FunicularSwitch.Generic.GenericResult.Ok, +#pragma warning disable 1591 + public abstract partial class Result + { + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static Result Error(Action details) => new Result.Error_(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static ResultError Error(Action details) => new(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static Result Ok(T value) => new Result.Ok_(value); + public bool IsError => GetType().GetGenericTypeDefinition() == typeof(Result<>.Error_); + public bool IsOk => !IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public abstract Action? GetErrorOrDefault(); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result Try( global::System.Func action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + } + + public abstract partial class Result : Result, global::System.Collections.Generic.IEnumerable + { + + [global::System.Diagnostics.DebuggerNonUserCode] + + public new static Result Error(Action message) => Error(message); + + [global::System.Diagnostics.DebuggerNonUserCode] + + public static Result Ok(T value) => Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator Result(T value) => Result.Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator Result(ResultError myResultError) => myResultError.WithOk(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator true(Result result) => result.IsOk; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator false(Result result) => result.IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !(Result result) => result.IsError; + + //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode + bool Equals(Result other) => this switch + { + Ok_ ok => ok.Equals((object)other), + Error_ error => error.Equals((object)other), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") + }; + + public override int GetHashCode() => this switch + { + Ok_ ok => ok.GetHashCode(), + Error_ error => error.GetHashCode(), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") + }; + + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((Result)obj); + } + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Result? left, Result? right) => Equals(left, right); + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Result? left, Result? right) => !Equals(left, right); + + [global::System.Diagnostics.DebuggerStepThrough] + public void Match( global::System.Action ok, global::System.Action? error = null) => Match( + v => + { + ok.Invoke(v); + return 42; + }, + err => + { + error?.Invoke(err); + return 42; + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T1 Match( global::System.Func ok, global::System.Func error) + { + return this switch + { + Ok_ okResult => ok(okResult.Value), + Error_ errorResult => error(errorResult.Details), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) + { + return this switch + { + Ok_ okResult => await ok(okResult.Value).ConfigureAwait(false), + Error_ errorResult => await error(errorResult.Details).ConfigureAwait(false), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => + Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); + + [global::System.Diagnostics.DebuggerStepThrough] + public async global::System.Threading.Tasks.Task Match( global::System.Func ok) + { + if (this is Ok_ okResult) await ok(okResult.Value).ConfigureAwait(false); + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public T Match( global::System.Func error) => Match(v => v, error); + + [global::System.Diagnostics.DebuggerStepThrough] + + public Result Bind( global::System.Func> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return bind(ok.Value); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return await bind(ok.Value).ConfigureAwait(false); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public Result Map( global::System.Func map) + { + switch (this) + { + case Ok_ ok: + try + { + return Result.Ok(map(ok.Value)); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Map( + global::System.Func> map) + { + switch (this) + { + case Ok_ ok: + try + { + return Result.Ok(await map(ok.Value).ConfigureAwait(false)); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + + public static implicit operator global::FunicularSwitch.Generic.GenericResult(Result result) => + result.Match( + global::FunicularSwitch.Generic.GenericResult.Ok, + global::FunicularSwitch.Generic.GenericResult.Error); + + public static implicit operator Result(global::FunicularSwitch.Generic.GenericResult result) => + result.Match>( + Result.Ok, + Result.Error); + + public global::FunicularSwitch.Generic.GenericResult ToGenericResult() => + Match( + global::FunicularSwitch.Generic.GenericResult.Ok, global::FunicularSwitch.Generic.GenericResult.Error); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T? GetValueOrDefault() - => Match( - v => (T?)v, - _ => default - ); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T GetValueOrDefault( global::System.Func defaultValue) - => Match( - v => v, - _ => defaultValue() - ); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T GetValueOrDefault(T defaultValue) - => Match( - v => v, - _ => defaultValue - ); - - [global::System.Diagnostics.DebuggerStepThrough] - public T GetValueOrThrow() - => Match( - v => v, - details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); - - - public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); - - - public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); - global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); - - public sealed partial class Ok_ : Result - { - public T Value { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Ok_(T value) => Value = value; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override Action? GetErrorOrDefault() => null; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Ok_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Ok_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); - } - - public sealed partial class Error_ : Result - { - public Action Details { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Error_(Action details) => Details = details; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public Result.Error_ Convert() => new Result.Error_(Details); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override Action? GetErrorOrDefault() => Details; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Error_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return Equals(Details, other.Details); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Error_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); - } - } - - public readonly partial struct ResultError : global::System.IEquatable - { - readonly Action _details; - - internal ResultError(Action details) => _details = details; - - [global::System.Diagnostics.Contracts.Pure] - public Result WithOk() => Result.Error(_details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(ResultError other) => _details.Equals(other._details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) => obj is ResultError other && Equals(other); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => _details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(ResultError left, ResultError right) => left.Equals(right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(ResultError left, ResultError right) => !left.Equals(right); - } - - public static partial class ResultExtension - { - #region bind - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => (await result.ConfigureAwait(false)).Bind(bind); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func>> bind) - => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); - - #endregion - - #region map - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func map) - => (await result.ConfigureAwait(false)).Map(map); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => Bind(result, async v => Result.Ok(await bind(v).ConfigureAwait(false))); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result MapError(this Result result, global::System.Func mapError) - { - if (result is Result.Error_ e) - return Result.Error(mapError(e.Details)); - return result; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); - - #endregion - - #region match - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func> error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func ok, - global::System.Func error) - => (await result.ConfigureAwait(false)).Match(ok, error); - - #endregion - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static Result Flatten(this Result> result) => result.Bind(r => r); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result As(this Result result, global::System.Func errorTIsNotT1) => - result.Bind(r => - { - if (r is T1 converted) - return converted; - return Result.Error(errorTIsNotT1()); - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result As(this Result result, global::System.Func errorIsNotT1) => - result.As(errorIsNotT1); - - #region query-expression pattern - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result Select(this Result result, global::System.Func selector) => result.Map(selector); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result SelectMany(this Result result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this Result result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - - #endregion - - - public static Result ToResult( - this global::FunicularSwitch.Generic.GenericResult result) => - result.Match>( - Result.Ok, - Result.Error); - - public static global::System.Threading.Tasks.Task> ToResult( - this global::System.Threading.Tasks.Task> result) => - result.Match( - Result.Ok, - Result.Error); - - public static global::System.Threading.Tasks.Task> ToGenericResult( - this global::System.Threading.Tasks.Task> result) => - result.Match( - global::FunicularSwitch.Generic.GenericResult.Ok, + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T? GetValueOrDefault() + => Match( + v => (T?)v, + _ => default + ); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T GetValueOrDefault( global::System.Func defaultValue) + => Match( + v => v, + _ => defaultValue() + ); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T GetValueOrDefault(T defaultValue) + => Match( + v => v, + _ => defaultValue + ); + + [global::System.Diagnostics.DebuggerStepThrough] + public T GetValueOrThrow() + => Match( + v => v, + details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); + + + public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); + + + public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); + global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); + + public sealed partial class Ok_ : Result + { + public T Value { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Ok_(T value) => Value = value; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override Action? GetErrorOrDefault() => null; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Ok_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Ok_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); + } + + public sealed partial class Error_ : Result + { + public Action Details { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Error_(Action details) => Details = details; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public Result.Error_ Convert() => new Result.Error_(Details); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override Action? GetErrorOrDefault() => Details; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Error_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Equals(Details, other.Details); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Error_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); + } + } + + public readonly partial struct ResultError : global::System.IEquatable + { + readonly Action _details; + + internal ResultError(Action details) => _details = details; + + [global::System.Diagnostics.Contracts.Pure] + public Result WithOk() => Result.Error(_details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(ResultError other) => _details.Equals(other._details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) => obj is ResultError other && Equals(other); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => _details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(ResultError left, ResultError right) => left.Equals(right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(ResultError left, ResultError right) => !left.Equals(right); + } + + public static partial class ResultExtension + { + #region bind + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => (await result.ConfigureAwait(false)).Bind(bind); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func>> bind) + => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); + + #endregion + + #region map + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func map) + => (await result.ConfigureAwait(false)).Map(map); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => Bind(result, async v => Result.Ok(await bind(v).ConfigureAwait(false))); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result MapError(this Result result, global::System.Func mapError) + { + if (result is Result.Error_ e) + return Result.Error(mapError(e.Details)); + return result; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); + + #endregion + + #region match + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func> error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func ok, + global::System.Func error) + => (await result.ConfigureAwait(false)).Match(ok, error); + + #endregion + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static Result Flatten(this Result> result) => result.Bind(r => r); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result As(this Result result, global::System.Func errorTIsNotT1) => + result.Bind(r => + { + if (r is T1 converted) + return converted; + return Result.Error(errorTIsNotT1()); + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result As(this Result result, global::System.Func errorIsNotT1) => + result.As(errorIsNotT1); + + #region query-expression pattern + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result Select(this Result result, global::System.Func selector) => result.Map(selector); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result SelectMany(this Result result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this Result result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + + #endregion + + + public static Result ToResult( + this global::FunicularSwitch.Generic.GenericResult result) => + result.Match>( + Result.Ok, + Result.Error); + + public static global::System.Threading.Tasks.Task> ToResult( + this global::System.Threading.Tasks.Task> result) => + result.Match( + Result.Ok, + Result.Error); + + public static global::System.Threading.Tasks.Task> ToGenericResult( + this global::System.Threading.Tasks.Task> result) => + result.Match( + global::FunicularSwitch.Generic.GenericResult.Ok, global::FunicularSwitch.Generic.GenericResult.Error); - } -} - -namespace FunicularSwitch.Generators.Consumer.System.Extensions + } +} + +namespace FunicularSwitch.Generators.Consumer.System.Extensions { - public static partial class ResultExtension - { - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable items, - global::System.Func> choose, - global::System.Action onError) - => items - .Select(i => choose(i)) - .Choose(onError); - - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable> results, - global::System.Action onError) - => results - .Where(r => - r.Match(_ => true, error => - { - onError(error); - return false; - })) - .Select(r => r.GetValueOrThrow()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result As(this object? item, global::System.Func error) => - !(item is T t) ? Result.Error(error()) : t; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result NotNull(this T? item, global::System.Func error) => - item ?? Result.Error(error()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result NotNullOrEmpty(this string? s, global::System.Func error) - => string.IsNullOrEmpty(s) ? Result.Error(error()) : s!; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result NotNullOrWhiteSpace(this string? s, global::System.Func error) - => string.IsNullOrWhiteSpace(s) ? Result.Error(error()) : s!; - - - public static Result First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => - candidates - .FirstOrDefault(i => predicate(i)) - .NotNull(noMatch); - } -#pragma warning restore 1591 -} + public static partial class ResultExtension + { + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable items, + global::System.Func> choose, + global::System.Action onError) + => items + .Select(i => choose(i)) + .Choose(onError); + + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable> results, + global::System.Action onError) + => results + .Where(r => + r.Match(_ => true, error => + { + onError(error); + return false; + })) + .Select(r => r.GetValueOrThrow()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result As(this object? item, global::System.Func error) => + !(item is T t) ? Result.Error(error()) : t; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result NotNull(this T? item, global::System.Func error) => + item ?? Result.Error(error()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result NotNullOrEmpty(this string? s, global::System.Func error) + => string.IsNullOrEmpty(s) ? Result.Error(error()) : s!; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result NotNullOrWhiteSpace(this string? s, global::System.Func error) + => string.IsNullOrWhiteSpace(s) ? Result.Error(error()) : s!; + + + public static Result First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => + candidates + .FirstOrDefault(i => predicate(i)) + .NotNull(noMatch); + } +#pragma warning restore 1591 +} diff --git a/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/MyNamespace.Result.g.cs b/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/MyNamespace.Result.g.cs index b9507b30..fc0bc0ff 100644 --- a/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/MyNamespace.Result.g.cs +++ b/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/MyNamespace.Result.g.cs @@ -1,642 +1,642 @@ -#nullable enable +#nullable enable using global::System.Linq; using MyNamespace2; using FunicularSwitch.Generic; -namespace MyNamespace +namespace MyNamespace { -#pragma warning disable 1591 - public abstract partial class Result - { - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static Result Error(ErrorInNamespaceWithDifferentResult details) => new Result.Error_(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static ResultError Error(ErrorInNamespaceWithDifferentResult details) => new(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static Result Ok(T value) => new Result.Ok_(value); - public bool IsError => GetType().GetGenericTypeDefinition() == typeof(Result<>.Error_); - public bool IsOk => !IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public abstract ErrorInNamespaceWithDifferentResult? GetErrorOrDefault(); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result Try( global::System.Func action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - } - - public abstract partial class Result : Result, global::System.Collections.Generic.IEnumerable - { - - [global::System.Diagnostics.DebuggerNonUserCode] - - public new static Result Error(ErrorInNamespaceWithDifferentResult message) => Error(message); - - [global::System.Diagnostics.DebuggerNonUserCode] - - public static Result Ok(T value) => Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator Result(T value) => Result.Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator Result(ResultError myResultError) => myResultError.WithOk(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator true(Result result) => result.IsOk; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator false(Result result) => result.IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !(Result result) => result.IsError; - - //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode - bool Equals(Result other) => this switch - { - Ok_ ok => ok.Equals((object)other), - Error_ error => error.Equals((object)other), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") - }; - - public override int GetHashCode() => this switch - { - Ok_ ok => ok.GetHashCode(), - Error_ error => error.GetHashCode(), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") - }; - - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != this.GetType()) return false; - return Equals((Result)obj); - } - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Result? left, Result? right) => Equals(left, right); - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Result? left, Result? right) => !Equals(left, right); - - [global::System.Diagnostics.DebuggerStepThrough] - public void Match( global::System.Action ok, global::System.Action? error = null) => Match( - v => - { - ok.Invoke(v); - return 42; - }, - err => - { - error?.Invoke(err); - return 42; - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T1 Match( global::System.Func ok, global::System.Func error) - { - return this switch - { - Ok_ okResult => ok(okResult.Value), - Error_ errorResult => error(errorResult.Details), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) - { - return this switch - { - Ok_ okResult => await ok(okResult.Value).ConfigureAwait(false), - Error_ errorResult => await error(errorResult.Details).ConfigureAwait(false), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => - Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); - - [global::System.Diagnostics.DebuggerStepThrough] - public async global::System.Threading.Tasks.Task Match( global::System.Func ok) - { - if (this is Ok_ okResult) await ok(okResult.Value).ConfigureAwait(false); - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public T Match( global::System.Func error) => Match(v => v, error); - - [global::System.Diagnostics.DebuggerStepThrough] - - public Result Bind( global::System.Func> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return bind(ok.Value); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return await bind(ok.Value).ConfigureAwait(false); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public Result Map( global::System.Func map) - { - switch (this) - { - case Ok_ ok: - try - { - return Result.Ok(map(ok.Value)); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Map( - global::System.Func> map) - { - switch (this) - { - case Ok_ ok: - try - { - return Result.Ok(await map(ok.Value).ConfigureAwait(false)); - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - - public static implicit operator global::FunicularSwitch.Generic.GenericResult(Result result) => - result.Match( - global::FunicularSwitch.Generic.GenericResult.Ok, - global::FunicularSwitch.Generic.GenericResult.Error); - - public static implicit operator Result(global::FunicularSwitch.Generic.GenericResult result) => - result.Match>( - Result.Ok, - Result.Error); - - public global::FunicularSwitch.Generic.GenericResult ToGenericResult() => - Match( - global::FunicularSwitch.Generic.GenericResult.Ok, +#pragma warning disable 1591 + public abstract partial class Result + { + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static Result Error(ErrorInNamespaceWithDifferentResult details) => new Result.Error_(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static ResultError Error(ErrorInNamespaceWithDifferentResult details) => new(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static Result Ok(T value) => new Result.Ok_(value); + public bool IsError => GetType().GetGenericTypeDefinition() == typeof(Result<>.Error_); + public bool IsOk => !IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public abstract ErrorInNamespaceWithDifferentResult? GetErrorOrDefault(); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result Try( global::System.Func action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + } + + public abstract partial class Result : Result, global::System.Collections.Generic.IEnumerable + { + + [global::System.Diagnostics.DebuggerNonUserCode] + + public new static Result Error(ErrorInNamespaceWithDifferentResult message) => Error(message); + + [global::System.Diagnostics.DebuggerNonUserCode] + + public static Result Ok(T value) => Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator Result(T value) => Result.Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator Result(ResultError myResultError) => myResultError.WithOk(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator true(Result result) => result.IsOk; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator false(Result result) => result.IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !(Result result) => result.IsError; + + //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode + bool Equals(Result other) => this switch + { + Ok_ ok => ok.Equals((object)other), + Error_ error => error.Equals((object)other), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") + }; + + public override int GetHashCode() => this switch + { + Ok_ ok => ok.GetHashCode(), + Error_ error => error.GetHashCode(), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") + }; + + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((Result)obj); + } + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Result? left, Result? right) => Equals(left, right); + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Result? left, Result? right) => !Equals(left, right); + + [global::System.Diagnostics.DebuggerStepThrough] + public void Match( global::System.Action ok, global::System.Action? error = null) => Match( + v => + { + ok.Invoke(v); + return 42; + }, + err => + { + error?.Invoke(err); + return 42; + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T1 Match( global::System.Func ok, global::System.Func error) + { + return this switch + { + Ok_ okResult => ok(okResult.Value), + Error_ errorResult => error(errorResult.Details), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) + { + return this switch + { + Ok_ okResult => await ok(okResult.Value).ConfigureAwait(false), + Error_ errorResult => await error(errorResult.Details).ConfigureAwait(false), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => + Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); + + [global::System.Diagnostics.DebuggerStepThrough] + public async global::System.Threading.Tasks.Task Match( global::System.Func ok) + { + if (this is Ok_ okResult) await ok(okResult.Value).ConfigureAwait(false); + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public T Match( global::System.Func error) => Match(v => v, error); + + [global::System.Diagnostics.DebuggerStepThrough] + + public Result Bind( global::System.Func> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return bind(ok.Value); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return await bind(ok.Value).ConfigureAwait(false); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public Result Map( global::System.Func map) + { + switch (this) + { + case Ok_ ok: + try + { + return Result.Ok(map(ok.Value)); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Map( + global::System.Func> map) + { + switch (this) + { + case Ok_ ok: + try + { + return Result.Ok(await map(ok.Value).ConfigureAwait(false)); + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + + public static implicit operator global::FunicularSwitch.Generic.GenericResult(Result result) => + result.Match( + global::FunicularSwitch.Generic.GenericResult.Ok, + global::FunicularSwitch.Generic.GenericResult.Error); + + public static implicit operator Result(global::FunicularSwitch.Generic.GenericResult result) => + result.Match>( + Result.Ok, + Result.Error); + + public global::FunicularSwitch.Generic.GenericResult ToGenericResult() => + Match( + global::FunicularSwitch.Generic.GenericResult.Ok, global::FunicularSwitch.Generic.GenericResult.Error); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T? GetValueOrDefault() - => Match( - v => (T?)v, - _ => default - ); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T GetValueOrDefault( global::System.Func defaultValue) - => Match( - v => v, - _ => defaultValue() - ); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T GetValueOrDefault(T defaultValue) - => Match( - v => v, - _ => defaultValue - ); - - [global::System.Diagnostics.DebuggerStepThrough] - public T GetValueOrThrow() - => Match( - v => v, - details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); - - - public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); - - - public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); - global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); - - public sealed partial class Ok_ : Result - { - public T Value { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Ok_(T value) => Value = value; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override ErrorInNamespaceWithDifferentResult? GetErrorOrDefault() => null; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Ok_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Ok_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); - } - - public sealed partial class Error_ : Result - { - public ErrorInNamespaceWithDifferentResult Details { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Error_(ErrorInNamespaceWithDifferentResult details) => Details = details; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public Result.Error_ Convert() => new Result.Error_(Details); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override ErrorInNamespaceWithDifferentResult? GetErrorOrDefault() => Details; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Error_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return Equals(Details, other.Details); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Error_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); - } - } - - public readonly partial struct ResultError : global::System.IEquatable - { - readonly ErrorInNamespaceWithDifferentResult _details; - - internal ResultError(ErrorInNamespaceWithDifferentResult details) => _details = details; - - [global::System.Diagnostics.Contracts.Pure] - public Result WithOk() => Result.Error(_details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(ResultError other) => _details.Equals(other._details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) => obj is ResultError other && Equals(other); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => _details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(ResultError left, ResultError right) => left.Equals(right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(ResultError left, ResultError right) => !left.Equals(right); - } - - public static partial class ResultExtension - { - #region bind - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => (await result.ConfigureAwait(false)).Bind(bind); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func>> bind) - => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); - - #endregion - - #region map - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func map) - => (await result.ConfigureAwait(false)).Map(map); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => Bind(result, async v => Result.Ok(await bind(v).ConfigureAwait(false))); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result MapError(this Result result, global::System.Func mapError) - { - if (result is Result.Error_ e) - return Result.Error(mapError(e.Details)); - return result; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); - - #endregion - - #region match - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func> error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func ok, - global::System.Func error) - => (await result.ConfigureAwait(false)).Match(ok, error); - - #endregion - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static Result Flatten(this Result> result) => result.Bind(r => r); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result As(this Result result, global::System.Func errorTIsNotT1) => - result.Bind(r => - { - if (r is T1 converted) - return converted; - return Result.Error(errorTIsNotT1()); - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result As(this Result result, global::System.Func errorIsNotT1) => - result.As(errorIsNotT1); - - #region query-expression pattern - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result Select(this Result result, global::System.Func selector) => result.Map(selector); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result SelectMany(this Result result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this Result result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - - #endregion - - - public static Result ToResult( - this global::FunicularSwitch.Generic.GenericResult result) => - result.Match>( - Result.Ok, - Result.Error); - - public static global::System.Threading.Tasks.Task> ToResult( - this global::System.Threading.Tasks.Task> result) => - result.Match( - Result.Ok, - Result.Error); - - public static global::System.Threading.Tasks.Task> ToGenericResult( - this global::System.Threading.Tasks.Task> result) => - result.Match( - global::FunicularSwitch.Generic.GenericResult.Ok, + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T? GetValueOrDefault() + => Match( + v => (T?)v, + _ => default + ); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T GetValueOrDefault( global::System.Func defaultValue) + => Match( + v => v, + _ => defaultValue() + ); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T GetValueOrDefault(T defaultValue) + => Match( + v => v, + _ => defaultValue + ); + + [global::System.Diagnostics.DebuggerStepThrough] + public T GetValueOrThrow() + => Match( + v => v, + details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); + + + public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); + + + public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); + global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); + + public sealed partial class Ok_ : Result + { + public T Value { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Ok_(T value) => Value = value; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override ErrorInNamespaceWithDifferentResult? GetErrorOrDefault() => null; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Ok_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Ok_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); + } + + public sealed partial class Error_ : Result + { + public ErrorInNamespaceWithDifferentResult Details { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Error_(ErrorInNamespaceWithDifferentResult details) => Details = details; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public Result.Error_ Convert() => new Result.Error_(Details); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override ErrorInNamespaceWithDifferentResult? GetErrorOrDefault() => Details; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Error_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Equals(Details, other.Details); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Error_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); + } + } + + public readonly partial struct ResultError : global::System.IEquatable + { + readonly ErrorInNamespaceWithDifferentResult _details; + + internal ResultError(ErrorInNamespaceWithDifferentResult details) => _details = details; + + [global::System.Diagnostics.Contracts.Pure] + public Result WithOk() => Result.Error(_details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(ResultError other) => _details.Equals(other._details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) => obj is ResultError other && Equals(other); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => _details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(ResultError left, ResultError right) => left.Equals(right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(ResultError left, ResultError right) => !left.Equals(right); + } + + public static partial class ResultExtension + { + #region bind + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => (await result.ConfigureAwait(false)).Bind(bind); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func>> bind) + => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); + + #endregion + + #region map + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func map) + => (await result.ConfigureAwait(false)).Map(map); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => Bind(result, async v => Result.Ok(await bind(v).ConfigureAwait(false))); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result MapError(this Result result, global::System.Func mapError) + { + if (result is Result.Error_ e) + return Result.Error(mapError(e.Details)); + return result; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); + + #endregion + + #region match + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func> error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func ok, + global::System.Func error) + => (await result.ConfigureAwait(false)).Match(ok, error); + + #endregion + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static Result Flatten(this Result> result) => result.Bind(r => r); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result As(this Result result, global::System.Func errorTIsNotT1) => + result.Bind(r => + { + if (r is T1 converted) + return converted; + return Result.Error(errorTIsNotT1()); + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result As(this Result result, global::System.Func errorIsNotT1) => + result.As(errorIsNotT1); + + #region query-expression pattern + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result Select(this Result result, global::System.Func selector) => result.Map(selector); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result SelectMany(this Result result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this Result result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + + #endregion + + + public static Result ToResult( + this global::FunicularSwitch.Generic.GenericResult result) => + result.Match>( + Result.Ok, + Result.Error); + + public static global::System.Threading.Tasks.Task> ToResult( + this global::System.Threading.Tasks.Task> result) => + result.Match( + Result.Ok, + Result.Error); + + public static global::System.Threading.Tasks.Task> ToGenericResult( + this global::System.Threading.Tasks.Task> result) => + result.Match( + global::FunicularSwitch.Generic.GenericResult.Ok, global::FunicularSwitch.Generic.GenericResult.Error); - } -} - -namespace MyNamespace.Extensions + } +} + +namespace MyNamespace.Extensions { - public static partial class ResultExtension - { - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable items, - global::System.Func> choose, - global::System.Action onError) - => items - .Select(i => choose(i)) - .Choose(onError); - - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable> results, - global::System.Action onError) - => results - .Where(r => - r.Match(_ => true, error => - { - onError(error); - return false; - })) - .Select(r => r.GetValueOrThrow()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result As(this object? item, global::System.Func error) => - !(item is T t) ? Result.Error(error()) : t; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result NotNull(this T? item, global::System.Func error) => - item ?? Result.Error(error()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result NotNullOrEmpty(this string? s, global::System.Func error) - => string.IsNullOrEmpty(s) ? Result.Error(error()) : s!; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result NotNullOrWhiteSpace(this string? s, global::System.Func error) - => string.IsNullOrWhiteSpace(s) ? Result.Error(error()) : s!; - - - public static Result First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => - candidates - .FirstOrDefault(i => predicate(i)) - .NotNull(noMatch); - } -#pragma warning restore 1591 -} + public static partial class ResultExtension + { + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable items, + global::System.Func> choose, + global::System.Action onError) + => items + .Select(i => choose(i)) + .Choose(onError); + + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable> results, + global::System.Action onError) + => results + .Where(r => + r.Match(_ => true, error => + { + onError(error); + return false; + })) + .Select(r => r.GetValueOrThrow()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result As(this object? item, global::System.Func error) => + !(item is T t) ? Result.Error(error()) : t; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result NotNull(this T? item, global::System.Func error) => + item ?? Result.Error(error()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result NotNullOrEmpty(this string? s, global::System.Func error) + => string.IsNullOrEmpty(s) ? Result.Error(error()) : s!; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result NotNullOrWhiteSpace(this string? s, global::System.Func error) + => string.IsNullOrWhiteSpace(s) ? Result.Error(error()) : s!; + + + public static Result First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => + candidates + .FirstOrDefault(i => predicate(i)) + .NotNull(noMatch); + } +#pragma warning restore 1591 +} diff --git a/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/MyNamespace.ResultWithMerge.g.cs b/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/MyNamespace.ResultWithMerge.g.cs index afd60a12..d2b4f1ed 100644 --- a/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/MyNamespace.ResultWithMerge.g.cs +++ b/Source/Tests/FunicularSwitch.Generators.Consumer/Generated/FunicularSwitch.Generators/FunicularSwitch.Generators.ResultTypeGenerator/MyNamespace.ResultWithMerge.g.cs @@ -1,525 +1,525 @@ -#nullable enable -using global::System.Linq; +#nullable enable +using global::System.Linq; using MyNamespace2; -using FunicularSwitch.Generic; - -namespace MyNamespace -{ -#pragma warning disable 1591 - public abstract partial class Result - { - - - - public static Result<(T1, T2)> Aggregate(Result r1, Result r2) => ResultExtension.Aggregate(r1, r2); - - - public static Result Aggregate(Result r1, Result r2, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2) => ResultExtension.Aggregate(r1, r2); - - +using FunicularSwitch.Generic; + +namespace MyNamespace +{ +#pragma warning disable 1591 + public abstract partial class Result + { + + + + public static Result<(T1, T2)> Aggregate(Result r1, Result r2) => ResultExtension.Aggregate(r1, r2); + + + public static Result Aggregate(Result r1, Result r2, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2) => ResultExtension.Aggregate(r1, r2); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, combine); - - - - public static Result<(T1, T2, T3)> Aggregate(Result r1, Result r2, Result r3) => ResultExtension.Aggregate(r1, r2, r3); - - - public static Result Aggregate(Result r1, Result r2, Result r3, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3) => ResultExtension.Aggregate(r1, r2, r3); - - + + + + public static Result<(T1, T2, T3)> Aggregate(Result r1, Result r2, Result r3) => ResultExtension.Aggregate(r1, r2, r3); + + + public static Result Aggregate(Result r1, Result r2, Result r3, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3) => ResultExtension.Aggregate(r1, r2, r3); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, combine); - - - - public static Result<(T1, T2, T3, T4)> Aggregate(Result r1, Result r2, Result r3, Result r4) => ResultExtension.Aggregate(r1, r2, r3, r4); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4) => ResultExtension.Aggregate(r1, r2, r3, r4); - - + + + + public static Result<(T1, T2, T3, T4)> Aggregate(Result r1, Result r2, Result r3, Result r4) => ResultExtension.Aggregate(r1, r2, r3, r4); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4) => ResultExtension.Aggregate(r1, r2, r3, r4); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, combine); - - - - public static Result<(T1, T2, T3, T4, T5)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5) => ResultExtension.Aggregate(r1, r2, r3, r4, r5); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5) => ResultExtension.Aggregate(r1, r2, r3, r4, r5); - - + + + + public static Result<(T1, T2, T3, T4, T5)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5) => ResultExtension.Aggregate(r1, r2, r3, r4, r5); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5) => ResultExtension.Aggregate(r1, r2, r3, r4, r5); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, combine); - - - - public static Result<(T1, T2, T3, T4, T5, T6)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6); - - + + + + public static Result<(T1, T2, T3, T4, T5, T6)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, combine); - - - - public static Result<(T1, T2, T3, T4, T5, T6, T7)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7); - - + + + + public static Result<(T1, T2, T3, T4, T5, T6, T7)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, combine); - - - - public static Result<(T1, T2, T3, T4, T5, T6, T7, T8)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8); - - + + + + public static Result<(T1, T2, T3, T4, T5, T6, T7, T8)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8); + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, combine); - - - - public static Result<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9); - - - public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, combine); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9); - - - public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, combine); - } - - public static partial class ResultExtension - { - - public static Result> Map(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func map) => + + + + public static Result<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9); + + + public static Result Aggregate(Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, combine); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9); + + + public static global::System.Threading.Tasks.Task> Aggregate(global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9, global::System.Func combine) => ResultExtension.Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, combine); + } + + public static partial class ResultExtension + { + + public static Result> Map(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func map) => results.Select(r => r.Map(map)).Aggregate(); - - - public static Result> Bind(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func> bind) => + + + public static Result> Bind(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func> bind) => results.Select(r => r.Bind(bind)).Aggregate(); - - public static Result> Bind(this Result result, - global::System.Func>> bindMany) => + + public static Result> Bind(this Result result, + global::System.Func>> bindMany) => result.Map(ok => bindMany(ok).Aggregate()).Flatten(); - - public static Result Bind(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func, Result> bind) => + + public static Result Bind(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func, Result> bind) => results.Aggregate().Bind(bind); - - public static Result> Aggregate(this global::System.Collections.Generic.IEnumerable> results) - { - var isError = false; - ErrorInNamespaceWithDifferentResult aggregated = default!; - var oks = new global::System.Collections.Generic.List(); - foreach (var result in results) - { - result.Match( - ok => oks.Add(ok), - error => - { - aggregated = !isError ? error : MergeErrors(aggregated, error); - isError = true; - } - ); - } - - return isError - ? Result.Error>(aggregated) - : Result.Ok>(oks); + + public static Result> Aggregate(this global::System.Collections.Generic.IEnumerable> results) + { + var isError = false; + ErrorInNamespaceWithDifferentResult aggregated = default!; + var oks = new global::System.Collections.Generic.List(); + foreach (var result in results) + { + result.Match( + ok => oks.Add(ok), + error => + { + aggregated = !isError ? error : MergeErrors(aggregated, error); + isError = true; + } + ); + } + + return isError + ? Result.Error>(aggregated) + : Result.Ok>(oks); } - - - public static async global::System.Threading.Tasks.Task>> Aggregate( - this global::System.Threading.Tasks.Task>> results) - => (await results.ConfigureAwait(false)) + + + public static async global::System.Threading.Tasks.Task>> Aggregate( + this global::System.Threading.Tasks.Task>> results) + => (await results.ConfigureAwait(false)) .Aggregate(); - - - public static async global::System.Threading.Tasks.Task>> Aggregate( - this global::System.Collections.Generic.IEnumerable>> results) - => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) + + + public static async global::System.Threading.Tasks.Task>> Aggregate( + this global::System.Collections.Generic.IEnumerable>> results) + => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) .Aggregate(); - - - public static async global::System.Threading.Tasks.Task>> AggregateMany( - this global::System.Collections.Generic.IEnumerable>>> results) - => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) - .SelectMany(e => e) + + + public static async global::System.Threading.Tasks.Task>> AggregateMany( + this global::System.Collections.Generic.IEnumerable>>> results) + => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) + .SelectMany(e => e) .Aggregate(); - - - public static Result<(T1, T2)> Aggregate(this Result r1, Result r2) => - Aggregate(r1, r2, (v1, v2) => (v1, v2)); - - - public static Result Aggregate(this Result r1, Result r2, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2) - return combine(ok1.Value, ok2.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); - } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2) - => Aggregate(r1, r2, (v1, v2) => (v1, v2)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2); - return Aggregate(r1.Result, r2.Result, combine); - } - - - public static Result<(T1, T2, T3)> Aggregate(this Result r1, Result r2, Result r3) => - Aggregate(r1, r2, r3, (v1, v2, v3) => (v1, v2, v3)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3) - return combine(ok1.Value, ok2.Value, ok3.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); - } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3) - => Aggregate(r1, r2, r3, (v1, v2, v3) => (v1, v2, v3)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3); - return Aggregate(r1.Result, r2.Result, r3.Result, combine); - } - - - public static Result<(T1, T2, T3, T4)> Aggregate(this Result r1, Result r2, Result r3, Result r4) => - Aggregate(r1, r2, r3, r4, (v1, v2, v3, v4) => (v1, v2, v3, v4)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); - } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4) - => Aggregate(r1, r2, r3, r4, (v1, v2, v3, v4) => (v1, v2, v3, v4)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, combine); - } - - - public static Result<(T1, T2, T3, T4, T5)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5) => - Aggregate(r1, r2, r3, r4, r5, (v1, v2, v3, v4, v5) => (v1, v2, v3, v4, v5)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4, r5 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); - } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5) - => Aggregate(r1, r2, r3, r4, r5, (v1, v2, v3, v4, v5) => (v1, v2, v3, v4, v5)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, combine); - } - - - public static Result<(T1, T2, T3, T4, T5, T6)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6) => - Aggregate(r1, r2, r3, r4, r5, r6, (v1, v2, v3, v4, v5, v6) => (v1, v2, v3, v4, v5, v6)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); + + + public static Result<(T1, T2)> Aggregate(this Result r1, Result r2) => + Aggregate(r1, r2, (v1, v2) => (v1, v2)); + + + public static Result Aggregate(this Result r1, Result r2, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2) + return combine(ok1.Value, ok2.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2) + => Aggregate(r1, r2, (v1, v2) => (v1, v2)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2); + return Aggregate(r1.Result, r2.Result, combine); } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6) - => Aggregate(r1, r2, r3, r4, r5, r6, (v1, v2, v3, v4, v5, v6) => (v1, v2, v3, v4, v5, v6)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, combine); + + + public static Result<(T1, T2, T3)> Aggregate(this Result r1, Result r2, Result r3) => + Aggregate(r1, r2, r3, (v1, v2, v3) => (v1, v2, v3)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3) + return combine(ok1.Value, ok2.Value, ok3.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3) + => Aggregate(r1, r2, r3, (v1, v2, v3) => (v1, v2, v3)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3); + return Aggregate(r1.Result, r2.Result, r3.Result, combine); } - - - public static Result<(T1, T2, T3, T4, T5, T6, T7)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7) => - Aggregate(r1, r2, r3, r4, r5, r6, r7, (v1, v2, v3, v4, v5, v6, v7) => (v1, v2, v3, v4, v5, v6, v7)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); + + + public static Result<(T1, T2, T3, T4)> Aggregate(this Result r1, Result r2, Result r3, Result r4) => + Aggregate(r1, r2, r3, r4, (v1, v2, v3, v4) => (v1, v2, v3, v4)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4) + => Aggregate(r1, r2, r3, r4, (v1, v2, v3, v4) => (v1, v2, v3, v4)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, combine); } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7) - => Aggregate(r1, r2, r3, r4, r5, r6, r7, (v1, v2, v3, v4, v5, v6, v7) => (v1, v2, v3, v4, v5, v6, v7)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, combine); + + + public static Result<(T1, T2, T3, T4, T5)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5) => + Aggregate(r1, r2, r3, r4, r5, (v1, v2, v3, v4, v5) => (v1, v2, v3, v4, v5)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4, r5 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5) + => Aggregate(r1, r2, r3, r4, r5, (v1, v2, v3, v4, v5) => (v1, v2, v3, v4, v5)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, combine); } - - - public static Result<(T1, T2, T3, T4, T5, T6, T7, T8)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8) => - Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, (v1, v2, v3, v4, v5, v6, v7, v8) => (v1, v2, v3, v4, v5, v6, v7, v8)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7 && r8 is Result.Ok_ ok8) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value, ok8.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7, r8 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); + + + public static Result<(T1, T2, T3, T4, T5, T6)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6) => + Aggregate(r1, r2, r3, r4, r5, r6, (v1, v2, v3, v4, v5, v6) => (v1, v2, v3, v4, v5, v6)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6) + => Aggregate(r1, r2, r3, r4, r5, r6, (v1, v2, v3, v4, v5, v6) => (v1, v2, v3, v4, v5, v6)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, combine); } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8) - => Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, (v1, v2, v3, v4, v5, v6, v7, v8) => (v1, v2, v3, v4, v5, v6, v7, v8)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7, r8); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, r8.Result, combine); + + + public static Result<(T1, T2, T3, T4, T5, T6, T7)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7) => + Aggregate(r1, r2, r3, r4, r5, r6, r7, (v1, v2, v3, v4, v5, v6, v7) => (v1, v2, v3, v4, v5, v6, v7)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7) + => Aggregate(r1, r2, r3, r4, r5, r6, r7, (v1, v2, v3, v4, v5, v6, v7) => (v1, v2, v3, v4, v5, v6, v7)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, combine); } - - - public static Result<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9) => - Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, (v1, v2, v3, v4, v5, v6, v7, v8, v9) => (v1, v2, v3, v4, v5, v6, v7, v8, v9)); - - - public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9, global::System.Func combine) - { - if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7 && r8 is Result.Ok_ ok8 && r9 is Result.Ok_ ok9) - return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value, ok8.Value, ok9.Value); - - return Result.Error( - MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7, r8, r9 } - .Where(r => r.IsError) - .Select(r => r.GetErrorOrDefault()!) - )!); + + + public static Result<(T1, T2, T3, T4, T5, T6, T7, T8)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8) => + Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, (v1, v2, v3, v4, v5, v6, v7, v8) => (v1, v2, v3, v4, v5, v6, v7, v8)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7 && r8 is Result.Ok_ ok8) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value, ok8.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7, r8 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8) + => Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, (v1, v2, v3, v4, v5, v6, v7, v8) => (v1, v2, v3, v4, v5, v6, v7, v8)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7, r8); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, r8.Result, combine); } - - - public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9) - => Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, (v1, v2, v3, v4, v5, v6, v7, v8, v9) => (v1, v2, v3, v4, v5, v6, v7, v8, v9)); - - - public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9, global::System.Func combine) - { - await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7, r8, r9); - return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, r8.Result, r9.Result, combine); + + + public static Result<(T1, T2, T3, T4, T5, T6, T7, T8, T9)> Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9) => + Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, (v1, v2, v3, v4, v5, v6, v7, v8, v9) => (v1, v2, v3, v4, v5, v6, v7, v8, v9)); + + + public static Result Aggregate(this Result r1, Result r2, Result r3, Result r4, Result r5, Result r6, Result r7, Result r8, Result r9, global::System.Func combine) + { + if (r1 is Result.Ok_ ok1 && r2 is Result.Ok_ ok2 && r3 is Result.Ok_ ok3 && r4 is Result.Ok_ ok4 && r5 is Result.Ok_ ok5 && r6 is Result.Ok_ ok6 && r7 is Result.Ok_ ok7 && r8 is Result.Ok_ ok8 && r9 is Result.Ok_ ok9) + return combine(ok1.Value, ok2.Value, ok3.Value, ok4.Value, ok5.Value, ok6.Value, ok7.Value, ok8.Value, ok9.Value); + + return Result.Error( + MergeErrors(new Result[] { r1, r2, r3, r4, r5, r6, r7, r8, r9 } + .Where(r => r.IsError) + .Select(r => r.GetErrorOrDefault()!) + )!); + } + + + public static global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9) + => Aggregate(r1, r2, r3, r4, r5, r6, r7, r8, r9, (v1, v2, v3, v4, v5, v6, v7, v8, v9) => (v1, v2, v3, v4, v5, v6, v7, v8, v9)); + + + public static async global::System.Threading.Tasks.Task> Aggregate(this global::System.Threading.Tasks.Task> r1, global::System.Threading.Tasks.Task> r2, global::System.Threading.Tasks.Task> r3, global::System.Threading.Tasks.Task> r4, global::System.Threading.Tasks.Task> r5, global::System.Threading.Tasks.Task> r6, global::System.Threading.Tasks.Task> r7, global::System.Threading.Tasks.Task> r8, global::System.Threading.Tasks.Task> r9, global::System.Func combine) + { + await global::System.Threading.Tasks.Task.WhenAll(r1, r2, r3, r4, r5, r6, r7, r8, r9); + return Aggregate(r1.Result, r2.Result, r3.Result, r4.Result, r5.Result, r6.Result, r7.Result, r8.Result, r9.Result, combine); } - - public static Result FirstOk(this global::System.Collections.Generic.IEnumerable> results, global::System.Func onEmpty) - { - var errors = new global::System.Collections.Generic.List(); - foreach (var result in results) - { - if (result is Result.Error_ e) - errors.Add(e.Details); - else - return result; - } - - if (!errors.Any()) - errors.Add(onEmpty()); - - return Result.Error(MergeErrors(errors)); + + public static Result FirstOk(this global::System.Collections.Generic.IEnumerable> results, global::System.Func onEmpty) + { + var errors = new global::System.Collections.Generic.List(); + foreach (var result in results) + { + if (result is Result.Error_ e) + errors.Add(e.Details); + else + return result; + } + + if (!errors.Any()) + errors.Add(onEmpty()); + + return Result.Error(MergeErrors(errors)); } - - - public static async global::System.Threading.Tasks.Task>> Aggregate( - this global::System.Collections.Generic.IEnumerable>> results, - int maxDegreeOfParallelism) - => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) + + + public static async global::System.Threading.Tasks.Task>> Aggregate( + this global::System.Collections.Generic.IEnumerable>> results, + int maxDegreeOfParallelism) + => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) + .Aggregate(); + + + public static async global::System.Threading.Tasks.Task>> AggregateMany( + this global::System.Collections.Generic.IEnumerable>>> results, + int maxDegreeOfParallelism) + => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) + .SelectMany(e => e) .Aggregate(); - - public static async global::System.Threading.Tasks.Task>> AggregateMany( - this global::System.Collections.Generic.IEnumerable>>> results, - int maxDegreeOfParallelism) - => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) - .SelectMany(e => e) - .Aggregate(); - - static async global::System.Threading.Tasks.Task SelectAsync(this global::System.Collections.Generic.IEnumerable items, global::System.Func> selector, int maxDegreeOfParallelism) - { - using (var throttler = new global::System.Threading.SemaphoreSlim(maxDegreeOfParallelism, maxDegreeOfParallelism)) - { - return await global::System.Threading.Tasks.Task.WhenAll(items.Select(async item => - { - // ReSharper disable once AccessToDisposedClosure - await throttler.WaitAsync().ConfigureAwait(false); - try - { - return await selector(item).ConfigureAwait(false); - } - finally - { - // ReSharper disable once AccessToDisposedClosure - throttler.Release(); - } - })).ConfigureAwait(false); - } + static async global::System.Threading.Tasks.Task SelectAsync(this global::System.Collections.Generic.IEnumerable items, global::System.Func> selector, int maxDegreeOfParallelism) + { + using (var throttler = new global::System.Threading.SemaphoreSlim(maxDegreeOfParallelism, maxDegreeOfParallelism)) + { + return await global::System.Threading.Tasks.Task.WhenAll(items.Select(async item => + { + // ReSharper disable once AccessToDisposedClosure + await throttler.WaitAsync().ConfigureAwait(false); + try + { + return await selector(item).ConfigureAwait(false); + } + finally + { + // ReSharper disable once AccessToDisposedClosure + throttler.Release(); + } + })).ConfigureAwait(false); + } } - - - public static Result> AllOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate) => - candidates - .Select(c => c.Validate(validate)) + + + public static Result> AllOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate) => + candidates + .Select(c => c.Validate(validate)) .Aggregate(); - - public static Result> AllOk(this global::System.Collections.Generic.IEnumerable> candidates, - global::System.Func> validate) => - candidates + + public static Result> AllOk(this global::System.Collections.Generic.IEnumerable> candidates, + global::System.Func> validate) => + candidates .Bind(items => items.AllOk(validate)); - - - public static Result Validate(this Result item, global::System.Func> validate) => item.Bind(i => i.Validate(validate)); - - - public static Result Validate(this T item, global::System.Func> validate) - { - try - { - var errors = validate(item).ToList(); - return errors.Count > 0 ? Result.Error(MergeErrors(errors)) : item; - } - // ReSharper disable once RedundantCatchClause -#pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) -#pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } + + + public static Result Validate(this Result item, global::System.Func> validate) => item.Bind(i => i.Validate(validate)); + + + public static Result Validate(this T item, global::System.Func> validate) + { + try + { + var errors = validate(item).ToList(); + return errors.Count > 0 ? Result.Error(MergeErrors(errors)) : item; + } + // ReSharper disable once RedundantCatchClause +#pragma warning disable CS0168 // Variable is declared but never used + catch (global::System.Exception e) +#pragma warning restore CS0168 // Variable is declared but never used + { + throw; //createGenericErrorResult + } } - - - public static Result FirstOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate, global::System.Func onEmpty) => - candidates - .Select(r => r.Validate(validate)) - .FirstOk(onEmpty); - - #region helpers - - static ErrorInNamespaceWithDifferentResult MergeErrors(global::System.Collections.Generic.IEnumerable errors) - { - var first = true; - ErrorInNamespaceWithDifferentResult aggregated = default!; - foreach (var myError in errors) - { - if (first) - { - aggregated = myError; - first = false; - } - else - { - aggregated = MergeErrors(aggregated, myError); - } - } - - return aggregated; - } - - static ErrorInNamespaceWithDifferentResult MergeErrors(ErrorInNamespaceWithDifferentResult aggregated, ErrorInNamespaceWithDifferentResult error) => aggregated.Merge(error); - - #endregion - } -#pragma warning restore 1591 + + + public static Result FirstOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate, global::System.Func onEmpty) => + candidates + .Select(r => r.Validate(validate)) + .FirstOk(onEmpty); + + #region helpers + + static ErrorInNamespaceWithDifferentResult MergeErrors(global::System.Collections.Generic.IEnumerable errors) + { + var first = true; + ErrorInNamespaceWithDifferentResult aggregated = default!; + foreach (var myError in errors) + { + if (first) + { + aggregated = myError; + first = false; + } + else + { + aggregated = MergeErrors(aggregated, myError); + } + } + + return aggregated; + } + + static ErrorInNamespaceWithDifferentResult MergeErrors(ErrorInNamespaceWithDifferentResult aggregated, ErrorInNamespaceWithDifferentResult error) => aggregated.Merge(error); + + #endregion + } +#pragma warning restore 1591 } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Consumer/GeneratorSpecs.cs b/Source/Tests/FunicularSwitch.Generators.Consumer/GeneratorSpecs.cs index 6a2892df..1898ffc0 100644 --- a/Source/Tests/FunicularSwitch.Generators.Consumer/GeneratorSpecs.cs +++ b/Source/Tests/FunicularSwitch.Generators.Consumer/GeneratorSpecs.cs @@ -20,9 +20,9 @@ static OperationResult Divide(decimal i, decimal divisor) => divisor == global::FunicularSwitch.Generic.GenericResult a = result; OperationResult d = a; - var b = result.ToGenericResult(); + var b = result.ToGenericResult(); var c = Task.FromResult(result).ToGenericResult(); - + var calc = result .Bind(i => Divide(i, 0)) .Map(i => (i * 2).ToString(CultureInfo.InvariantCulture)); diff --git a/Source/Tests/FunicularSwitch.Generators.Consumer/ResultSpecs.cs b/Source/Tests/FunicularSwitch.Generators.Consumer/ResultSpecs.cs index 327a7aaf..97bb0938 100644 --- a/Source/Tests/FunicularSwitch.Generators.Consumer/ResultSpecs.cs +++ b/Source/Tests/FunicularSwitch.Generators.Consumer/ResultSpecs.cs @@ -40,7 +40,7 @@ public void Equality() ); resultSet.Should().HaveCount(3); - resultSet.Should().BeEquivalentTo(new[]{Result.Ok(42), Result.Ok(23), Result.Error("42")}); + resultSet.Should().BeEquivalentTo(new[] { Result.Ok(42), Result.Ok(23), Result.Error("42") }); } [TestMethod] @@ -75,7 +75,7 @@ public void Map() [DebuggerNonUserCode] void Blubs(Action bla) { - Blubs2([DebuggerNonUserCode]() => bla()); + Blubs2([DebuggerNonUserCode] () => bla()); } [DebuggerNonUserCode] @@ -83,7 +83,7 @@ void Blubs2(Action bla) { bla(); } - + [TestMethod] public void Bind() { diff --git a/Source/Tests/FunicularSwitch.Generators.Consumer/TransformerSpecs.cs b/Source/Tests/FunicularSwitch.Generators.Consumer/TransformerSpecs.cs index 126fcfa2..8fbe6227 100644 --- a/Source/Tests/FunicularSwitch.Generators.Consumer/TransformerSpecs.cs +++ b/Source/Tests/FunicularSwitch.Generators.Consumer/TransformerSpecs.cs @@ -50,7 +50,7 @@ public void ResultOption_Bind_WithError() public void ResultOption_Bind_WithOkNone() { // Arrange - var value = (ResultOption) Result.Ok(Option.None()); + var value = (ResultOption)Result.Ok(Option.None()); var expected = new ResultOption(Result.Ok(Option.None())); // Act @@ -125,7 +125,7 @@ from z in Sqrt(y) static Writer> Sqrt(int a) => a < 0 ? WriterResult.Error($"sqrt({a}) -> Cannot get square root of negative number") - : WriterResult.Append((int) Math.Sqrt(a), v => $"sqrt({a}) = {v}"); + : WriterResult.Append((int)Math.Sqrt(a), v => $"sqrt({a}) = {v}"); static Writer> Div(int a, int b) => b == 0 @@ -153,7 +153,7 @@ public static partial class Writer public static Writer Bind(this Writer ma, Func> fn) { var tmp = fn(ma.Value); - var result = tmp with {Log = [..ma.Log, ..tmp.Log]}; + var result = tmp with { Log = [.. ma.Log, .. tmp.Log] }; return result; } diff --git a/Source/Tests/FunicularSwitch.Generators.Test/EnumTypeGeneratorSpecs.cs b/Source/Tests/FunicularSwitch.Generators.Test/EnumTypeGeneratorSpecs.cs index b59a191a..aef308c7 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/EnumTypeGeneratorSpecs.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/EnumTypeGeneratorSpecs.cs @@ -1,18 +1,16 @@ -using System.Threading.Tasks; using JetBrains.Annotations; -using Microsoft.VisualStudio.TestTools.UnitTesting; namespace FunicularSwitch.Generators.Test; [TestClass] public class Run_enum_match_method_generator : VerifySourceGenerator { - [TestMethod] - public Task For_enum_type() - { - var code = - /* lang=csharp */ - """ + [TestMethod] + public Task For_enum_type() + { + var code = + /* lang=csharp */ + """ using FunicularSwitch.Generators; [assembly: ExtendEnums(typeof(FunicularSwitch.Test.test), CaseOrder = EnumCaseOrder.Alphabetic, Accessibility = ExtensionAccessibility.Internal)] @@ -28,16 +26,16 @@ public enum test } """; - return Verify(code); - } - - - [TestMethod] - public Task For_enum_type_with_order() - { - var code = - /* lang=csharp */ - """ + return Verify(code); + } + + + [TestMethod] + public Task For_enum_type_with_order() + { + var code = + /* lang=csharp */ + """ using FunicularSwitch.Generators; namespace FunicularSwitch.Test; @@ -55,15 +53,15 @@ public enum test } """; - return Verify(code); - } - - [TestMethod] - public Task For_enum_type_embedded() - { - var code = - /* lang=csharp */ - """ + return Verify(code); + } + + [TestMethod] + public Task For_enum_type_embedded() + { + var code = + /* lang=csharp */ + """ using FunicularSwitch.Generators; namespace FunicularSwitch.Test; @@ -79,15 +77,15 @@ public enum test } """; - return Verify(code); - } - - [TestMethod] - public Task ForEnumType_WhenJetBrainsAnnotationsPackageIsReferenced() - { - var code = - /* lang=csharp */ - """ + return Verify(code); + } + + [TestMethod] + public Task ForEnumType_WhenJetBrainsAnnotationsPackageIsReferenced() + { + var code = + /* lang=csharp */ + """ using FunicularSwitch.Generators; namespace FunicularSwitch.Test; @@ -100,6 +98,6 @@ public enum test } """; - return Verify(code, additionalAssemblies: [typeof(InstantHandleAttribute).Assembly]); - } + return Verify(code, additionalAssemblies: [typeof(InstantHandleAttribute).Assembly]); + } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/ResultTypeGeneratorSpecs.cs b/Source/Tests/FunicularSwitch.Generators.Test/ResultTypeGeneratorSpecs.cs index f1ba229b..036af9b1 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/ResultTypeGeneratorSpecs.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/ResultTypeGeneratorSpecs.cs @@ -8,7 +8,7 @@ public class Run_result_type_generator : VerifySourceGenerator [TestMethod] public Task For_enum_error_type() { - var code = + var code = /* lang=csharp */ """ using FunicularSwitch.Generators; @@ -91,7 +91,7 @@ public static class MyErrorExtension [TestMethod] public Task For_result_type_without_namespace() { - var code = + var code = /* lang=csharp */ """ using FunicularSwitch.Generators; @@ -108,7 +108,7 @@ public enum MyError Unauthorized } """; - return Verify(code); + return Verify(code); } diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.ForEnumType_WhenJetBrainsAnnotationsPackageIsReferenced#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.ForEnumType_WhenJetBrainsAnnotationsPackageIsReferenced#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.ForEnumType_WhenJetBrainsAnnotationsPackageIsReferenced#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.ForEnumType_WhenJetBrainsAnnotationsPackageIsReferenced#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.ForEnumType_WhenJetBrainsAnnotationsPackageIsReferenced#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.ForEnumType_WhenJetBrainsAnnotationsPackageIsReferenced#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.ForEnumType_WhenJetBrainsAnnotationsPackageIsReferenced#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.ForEnumType_WhenJetBrainsAnnotationsPackageIsReferenced#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type_embedded#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type_embedded#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type_embedded#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type_embedded#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type_embedded#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type_embedded#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type_embedded#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type_embedded#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type_with_order#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type_with_order#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type_with_order#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type_with_order#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type_with_order#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type_with_order#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type_with_order#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_enum_match_method_generator.For_enum_type_with_order#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#FunicularSwitch.Test.OperationResult.g.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#FunicularSwitch.Test.OperationResult.g.verified.cs index 9464bee2..39d6a3e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#FunicularSwitch.Test.OperationResult.g.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#FunicularSwitch.Test.OperationResult.g.verified.cs @@ -7,605 +7,605 @@ namespace FunicularSwitch.Test { #pragma warning disable 1591 - public abstract partial class OperationResult - { - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResult Error(MyError details) => new OperationResult.Error_(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResultError Error(MyError details) => new(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResult Ok(T value) => new OperationResult.Ok_(value); - public bool IsError => GetType().GetGenericTypeDefinition() == typeof(OperationResult<>.Error_); - public bool IsOk => !IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public abstract MyError? GetErrorOrDefault(); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult Try( global::System.Func action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - } - - public abstract partial class OperationResult : OperationResult, global::System.Collections.Generic.IEnumerable - { - - [global::System.Diagnostics.DebuggerNonUserCode] - - public new static OperationResult Error(MyError message) => Error(message); - - [global::System.Diagnostics.DebuggerNonUserCode] - - public static OperationResult Ok(T value) => Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator OperationResult(T value) => OperationResult.Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator OperationResult(OperationResultError myResultError) => myResultError.WithOk(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator true(OperationResult result) => result.IsOk; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator false(OperationResult result) => result.IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !(OperationResult result) => result.IsError; - - //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode - bool Equals(OperationResult other) => this switch - { - Ok_ ok => ok.Equals((object)other), - Error_ error => error.Equals((object)other), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") - }; - - public override int GetHashCode() => this switch - { - Ok_ ok => ok.GetHashCode(), - Error_ error => error.GetHashCode(), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") - }; - - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != this.GetType()) return false; - return Equals((OperationResult)obj); - } - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(OperationResult? left, OperationResult? right) => Equals(left, right); - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(OperationResult? left, OperationResult? right) => !Equals(left, right); - - [global::System.Diagnostics.DebuggerStepThrough] - public void Match( global::System.Action ok, global::System.Action? error = null) => Match( - v => - { - ok.Invoke(v); - return 42; - }, - err => - { - error?.Invoke(err); - return 42; - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T1 Match( global::System.Func ok, global::System.Func error) - { - return this switch - { - Ok_ okOperationResult => ok(okOperationResult.Value), - Error_ errorOperationResult => error(errorOperationResult.Details), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) - { - return this switch - { - Ok_ okOperationResult => await ok(okOperationResult.Value).ConfigureAwait(false), - Error_ errorOperationResult => await error(errorOperationResult.Details).ConfigureAwait(false), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => - Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); - - [global::System.Diagnostics.DebuggerStepThrough] - public async global::System.Threading.Tasks.Task Match( global::System.Func ok) - { - if (this is Ok_ okOperationResult) await ok(okOperationResult.Value).ConfigureAwait(false); - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public T Match( global::System.Func error) => Match(v => v, error); - - [global::System.Diagnostics.DebuggerStepThrough] - - public OperationResult Bind( global::System.Func> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return bind(ok.Value); - } - // ReSharper disable once RedundantCatchClause + public abstract partial class OperationResult + { + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResult Error(MyError details) => new OperationResult.Error_(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResultError Error(MyError details) => new(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResult Ok(T value) => new OperationResult.Ok_(value); + public bool IsError => GetType().GetGenericTypeDefinition() == typeof(OperationResult<>.Error_); + public bool IsOk => !IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public abstract MyError? GetErrorOrDefault(); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult Try( global::System.Func action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + } + + public abstract partial class OperationResult : OperationResult, global::System.Collections.Generic.IEnumerable + { + + [global::System.Diagnostics.DebuggerNonUserCode] + + public new static OperationResult Error(MyError message) => Error(message); + + [global::System.Diagnostics.DebuggerNonUserCode] + + public static OperationResult Ok(T value) => Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator OperationResult(T value) => OperationResult.Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator OperationResult(OperationResultError myResultError) => myResultError.WithOk(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator true(OperationResult result) => result.IsOk; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator false(OperationResult result) => result.IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !(OperationResult result) => result.IsError; + + //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode + bool Equals(OperationResult other) => this switch + { + Ok_ ok => ok.Equals((object)other), + Error_ error => error.Equals((object)other), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") + }; + + public override int GetHashCode() => this switch + { + Ok_ ok => ok.GetHashCode(), + Error_ error => error.GetHashCode(), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") + }; + + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((OperationResult)obj); + } + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(OperationResult? left, OperationResult? right) => Equals(left, right); + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(OperationResult? left, OperationResult? right) => !Equals(left, right); + + [global::System.Diagnostics.DebuggerStepThrough] + public void Match( global::System.Action ok, global::System.Action? error = null) => Match( + v => + { + ok.Invoke(v); + return 42; + }, + err => + { + error?.Invoke(err); + return 42; + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T1 Match( global::System.Func ok, global::System.Func error) + { + return this switch + { + Ok_ okOperationResult => ok(okOperationResult.Value), + Error_ errorOperationResult => error(errorOperationResult.Details), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) + { + return this switch + { + Ok_ okOperationResult => await ok(okOperationResult.Value).ConfigureAwait(false), + Error_ errorOperationResult => await error(errorOperationResult.Details).ConfigureAwait(false), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => + Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); + + [global::System.Diagnostics.DebuggerStepThrough] + public async global::System.Threading.Tasks.Task Match( global::System.Func ok) + { + if (this is Ok_ okOperationResult) await ok(okOperationResult.Value).ConfigureAwait(false); + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public T Match( global::System.Func error) => Match(v => v, error); + + [global::System.Diagnostics.DebuggerStepThrough] + + public OperationResult Bind( global::System.Func> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return bind(ok.Value); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return await bind(ok.Value).ConfigureAwait(false); - } - // ReSharper disable once RedundantCatchClause + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return await bind(ok.Value).ConfigureAwait(false); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public OperationResult Map( global::System.Func map) - { - switch (this) - { - case Ok_ ok: - try - { - return OperationResult.Ok(map(ok.Value)); - } - // ReSharper disable once RedundantCatchClause + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public OperationResult Map( global::System.Func map) + { + switch (this) + { + case Ok_ ok: + try + { + return OperationResult.Ok(map(ok.Value)); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Map( - global::System.Func> map) - { - switch (this) - { - case Ok_ ok: - try - { - return OperationResult.Ok(await map(ok.Value).ConfigureAwait(false)); - } - // ReSharper disable once RedundantCatchClause + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Map( + global::System.Func> map) + { + switch (this) + { + case Ok_ ok: + try + { + return OperationResult.Ok(await map(ok.Value).ConfigureAwait(false)); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - //createGenericResultConversions - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T? GetValueOrDefault() - => Match( - v => (T?)v, - _ => default - ); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T GetValueOrDefault( global::System.Func defaultValue) - => Match( - v => v, - _ => defaultValue() - ); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T GetValueOrDefault(T defaultValue) - => Match( - v => v, - _ => defaultValue - ); - - [global::System.Diagnostics.DebuggerStepThrough] - public T GetValueOrThrow() - => Match( - v => v, - details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); - - - public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); - - - public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); - global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); - - public sealed partial class Ok_ : OperationResult - { - public T Value { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Ok_(T value) => Value = value; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override MyError? GetErrorOrDefault() => null; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Ok_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Ok_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); - } - - public sealed partial class Error_ : OperationResult - { - public MyError Details { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Error_(MyError details) => Details = details; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public OperationResult.Error_ Convert() => new OperationResult.Error_(Details); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override MyError? GetErrorOrDefault() => Details; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Error_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return Equals(Details, other.Details); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Error_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); - } - } - - public readonly partial struct OperationResultError : global::System.IEquatable - { - readonly MyError _details; - - internal OperationResultError(MyError details) => _details = details; - - [global::System.Diagnostics.Contracts.Pure] - public OperationResult WithOk() => OperationResult.Error(_details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(OperationResultError other) => _details.Equals(other._details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) => obj is OperationResultError other && Equals(other); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => _details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(OperationResultError left, OperationResultError right) => left.Equals(right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(OperationResultError left, OperationResultError right) => !left.Equals(right); - } - - public static partial class OperationResultExtension - { - #region bind - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => (await result.ConfigureAwait(false)).Bind(bind); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func>> bind) - => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); - - #endregion - - #region map - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func map) - => (await result.ConfigureAwait(false)).Map(map); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => Bind(result, async v => OperationResult.Ok(await bind(v).ConfigureAwait(false))); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult MapError(this OperationResult result, global::System.Func mapError) - { - if (result is OperationResult.Error_ e) - return OperationResult.Error(mapError(e.Details)); - return result; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); - - #endregion - - #region match - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func> error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func ok, - global::System.Func error) - => (await result.ConfigureAwait(false)).Match(ok, error); - - #endregion - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResult Flatten(this OperationResult> result) => result.Bind(r => r); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult As(this OperationResult result, global::System.Func errorTIsNotT1) => - result.Bind(r => - { - if (r is T1 converted) - return converted; - return OperationResult.Error(errorTIsNotT1()); - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult As(this OperationResult result, global::System.Func errorIsNotT1) => - result.As(errorIsNotT1); - - #region query-expression pattern - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult Select(this OperationResult result, global::System.Func selector) => result.Map(selector); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult SelectMany(this OperationResult result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this OperationResult result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - - #endregion - - //createGenericResultConversionExtensions - } + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + //createGenericResultConversions + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T? GetValueOrDefault() + => Match( + v => (T?)v, + _ => default + ); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T GetValueOrDefault( global::System.Func defaultValue) + => Match( + v => v, + _ => defaultValue() + ); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T GetValueOrDefault(T defaultValue) + => Match( + v => v, + _ => defaultValue + ); + + [global::System.Diagnostics.DebuggerStepThrough] + public T GetValueOrThrow() + => Match( + v => v, + details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); + + + public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); + + + public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); + global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); + + public sealed partial class Ok_ : OperationResult + { + public T Value { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Ok_(T value) => Value = value; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override MyError? GetErrorOrDefault() => null; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Ok_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Ok_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); + } + + public sealed partial class Error_ : OperationResult + { + public MyError Details { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Error_(MyError details) => Details = details; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public OperationResult.Error_ Convert() => new OperationResult.Error_(Details); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override MyError? GetErrorOrDefault() => Details; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Error_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Equals(Details, other.Details); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Error_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); + } + } + + public readonly partial struct OperationResultError : global::System.IEquatable + { + readonly MyError _details; + + internal OperationResultError(MyError details) => _details = details; + + [global::System.Diagnostics.Contracts.Pure] + public OperationResult WithOk() => OperationResult.Error(_details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(OperationResultError other) => _details.Equals(other._details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) => obj is OperationResultError other && Equals(other); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => _details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(OperationResultError left, OperationResultError right) => left.Equals(right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(OperationResultError left, OperationResultError right) => !left.Equals(right); + } + + public static partial class OperationResultExtension + { + #region bind + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => (await result.ConfigureAwait(false)).Bind(bind); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func>> bind) + => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); + + #endregion + + #region map + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func map) + => (await result.ConfigureAwait(false)).Map(map); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => Bind(result, async v => OperationResult.Ok(await bind(v).ConfigureAwait(false))); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult MapError(this OperationResult result, global::System.Func mapError) + { + if (result is OperationResult.Error_ e) + return OperationResult.Error(mapError(e.Details)); + return result; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); + + #endregion + + #region match + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func> error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func ok, + global::System.Func error) + => (await result.ConfigureAwait(false)).Match(ok, error); + + #endregion + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResult Flatten(this OperationResult> result) => result.Bind(r => r); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult As(this OperationResult result, global::System.Func errorTIsNotT1) => + result.Bind(r => + { + if (r is T1 converted) + return converted; + return OperationResult.Error(errorTIsNotT1()); + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult As(this OperationResult result, global::System.Func errorIsNotT1) => + result.As(errorIsNotT1); + + #region query-expression pattern + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult Select(this OperationResult result, global::System.Func selector) => result.Map(selector); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult SelectMany(this OperationResult result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this OperationResult result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + + #endregion + + //createGenericResultConversionExtensions + } } namespace FunicularSwitch.Test.Extensions { - public static partial class OperationResultExtension - { - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable items, - global::System.Func> choose, - global::System.Action onError) - => items - .Select(i => choose(i)) - .Choose(onError); - - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable> results, - global::System.Action onError) - => results - .Where(r => - r.Match(_ => true, error => - { - onError(error); - return false; - })) - .Select(r => r.GetValueOrThrow()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult As(this object? item, global::System.Func error) => - !(item is T t) ? OperationResult.Error(error()) : t; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult NotNull(this T? item, global::System.Func error) => - item ?? OperationResult.Error(error()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult NotNullOrEmpty(this string? s, global::System.Func error) - => string.IsNullOrEmpty(s) ? OperationResult.Error(error()) : s!; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult NotNullOrWhiteSpace(this string? s, global::System.Func error) - => string.IsNullOrWhiteSpace(s) ? OperationResult.Error(error()) : s!; - - - public static OperationResult First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => - candidates - .FirstOrDefault(i => predicate(i)) - .NotNull(noMatch); - } + public static partial class OperationResultExtension + { + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable items, + global::System.Func> choose, + global::System.Action onError) + => items + .Select(i => choose(i)) + .Choose(onError); + + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable> results, + global::System.Action onError) + => results + .Where(r => + r.Match(_ => true, error => + { + onError(error); + return false; + })) + .Select(r => r.GetValueOrThrow()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult As(this object? item, global::System.Func error) => + !(item is T t) ? OperationResult.Error(error()) : t; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult NotNull(this T? item, global::System.Func error) => + item ?? OperationResult.Error(error()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult NotNullOrEmpty(this string? s, global::System.Func error) + => string.IsNullOrEmpty(s) ? OperationResult.Error(error()) : s!; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult NotNullOrWhiteSpace(this string? s, global::System.Func error) + => string.IsNullOrWhiteSpace(s) ? OperationResult.Error(error()) : s!; + + + public static OperationResult First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => + candidates + .FirstOrDefault(i => predicate(i)) + .NotNull(noMatch); + } #pragma warning restore 1591 } diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#FunicularSwitch.Test.OperationResultWithMerge.g.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#FunicularSwitch.Test.OperationResultWithMerge.g.verified.cs index 43d69623..fb9a9f19 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#FunicularSwitch.Test.OperationResultWithMerge.g.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#FunicularSwitch.Test.OperationResultWithMerge.g.verified.cs @@ -116,28 +116,28 @@ public abstract partial class OperationResult public static partial class OperationResultExtension { - - public static OperationResult> Map(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func map) => + + public static OperationResult> Map(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func map) => results.Select(r => r.Map(map)).Aggregate(); - - public static OperationResult> Bind(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func> bind) => + + public static OperationResult> Bind(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func> bind) => results.Select(r => r.Bind(bind)).Aggregate(); - - public static OperationResult> Bind(this OperationResult result, - global::System.Func>> bindMany) => + + public static OperationResult> Bind(this OperationResult result, + global::System.Func>> bindMany) => result.Map(ok => bindMany(ok).Aggregate()).Flatten(); - - public static OperationResult Bind(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func, OperationResult> bind) => + + public static OperationResult Bind(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func, OperationResult> bind) => results.Aggregate().Bind(bind); - - public static OperationResult> Aggregate(this global::System.Collections.Generic.IEnumerable> results) + + public static OperationResult> Aggregate(this global::System.Collections.Generic.IEnumerable> results) { var isError = false; MyError aggregated = default!; @@ -160,25 +160,25 @@ public static OperationResult Bind(this global::System.Collections.Ge } - public static async global::System.Threading.Tasks.Task>> Aggregate( + public static async global::System.Threading.Tasks.Task>> Aggregate( this global::System.Threading.Tasks.Task>> results) => (await results.ConfigureAwait(false)) .Aggregate(); - - public static async global::System.Threading.Tasks.Task>> Aggregate( + + public static async global::System.Threading.Tasks.Task>> Aggregate( this global::System.Collections.Generic.IEnumerable>> results) => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) .Aggregate(); - - public static async global::System.Threading.Tasks.Task>> AggregateMany( + + public static async global::System.Threading.Tasks.Task>> AggregateMany( this global::System.Collections.Generic.IEnumerable>>> results) => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) .SelectMany(e => e) .Aggregate(); - + public static OperationResult<(T1, T2)> Aggregate(this OperationResult r1, OperationResult r2) => Aggregate(r1, r2, (v1, v2) => (v1, v2)); @@ -403,8 +403,8 @@ public static OperationResult Aggregate FirstOk(this global::System.Collections.Generic.IEnumerable> results, global::System.Func onEmpty) + + public static OperationResult FirstOk(this global::System.Collections.Generic.IEnumerable> results, global::System.Func onEmpty) { var errors = new global::System.Collections.Generic.List(); foreach (var result in results) @@ -422,21 +422,21 @@ public static OperationResult FirstOk(this global::System.Collections.Gene } - public static async global::System.Threading.Tasks.Task>> Aggregate( + public static async global::System.Threading.Tasks.Task>> Aggregate( this global::System.Collections.Generic.IEnumerable>> results, int maxDegreeOfParallelism) => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) .Aggregate(); - - public static async global::System.Threading.Tasks.Task>> AggregateMany( + + public static async global::System.Threading.Tasks.Task>> AggregateMany( this global::System.Collections.Generic.IEnumerable>>> results, int maxDegreeOfParallelism) => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) .SelectMany(e => e) .Aggregate(); - static async global::System.Threading.Tasks.Task SelectAsync(this global::System.Collections.Generic.IEnumerable items, global::System.Func> selector, int maxDegreeOfParallelism) + static async global::System.Threading.Tasks.Task SelectAsync(this global::System.Collections.Generic.IEnumerable items, global::System.Func> selector, int maxDegreeOfParallelism) { using (var throttler = new global::System.Threading.SemaphoreSlim(maxDegreeOfParallelism, maxDegreeOfParallelism)) { @@ -458,39 +458,39 @@ public static OperationResult FirstOk(this global::System.Collections.Gene } - public static OperationResult> AllOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate) => + public static OperationResult> AllOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate) => candidates .Select(c => c.Validate(validate)) .Aggregate(); - - public static OperationResult> AllOk(this global::System.Collections.Generic.IEnumerable> candidates, - global::System.Func> validate) => + + public static OperationResult> AllOk(this global::System.Collections.Generic.IEnumerable> candidates, + global::System.Func> validate) => candidates .Bind(items => items.AllOk(validate)); - - public static OperationResult Validate(this OperationResult item, global::System.Func> validate) => item.Bind(i => i.Validate(validate)); + + public static OperationResult Validate(this OperationResult item, global::System.Func> validate) => item.Bind(i => i.Validate(validate)); - - public static OperationResult Validate(this T item, global::System.Func> validate) + + public static OperationResult Validate(this T item, global::System.Func> validate) { - try - { - var errors = validate(item).ToList(); - return errors.Count > 0 ? OperationResult.Error(MergeErrors(errors)) : item; - } - // ReSharper disable once RedundantCatchClause + try + { + var errors = validate(item).ToList(); + return errors.Count > 0 ? OperationResult.Error(MergeErrors(errors)) : item; + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } + { + throw; //createGenericErrorResult + } } - public static OperationResult FirstOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate, global::System.Func onEmpty) => + public static OperationResult FirstOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate, global::System.Func onEmpty) => candidates .Select(r => r.Validate(validate)) .FirstOk(onEmpty); diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#FunicularSwitch.Test.Result.g.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#FunicularSwitch.Test.Result.g.verified.cs index b39cf5f4..913ff582 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#FunicularSwitch.Test.Result.g.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#FunicularSwitch.Test.Result.g.verified.cs @@ -7,605 +7,605 @@ namespace FunicularSwitch.Test { #pragma warning disable 1591 - public abstract partial class Result - { - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static Result Error(String details) => new Result.Error_(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static ResultError Error(String details) => new(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static Result Ok(T value) => new Result.Ok_(value); - public bool IsError => GetType().GetGenericTypeDefinition() == typeof(Result<>.Error_); - public bool IsOk => !IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public abstract String? GetErrorOrDefault(); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result Try( global::System.Func action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - } - - public abstract partial class Result : Result, global::System.Collections.Generic.IEnumerable - { - - [global::System.Diagnostics.DebuggerNonUserCode] - - public new static Result Error(String message) => Error(message); - - [global::System.Diagnostics.DebuggerNonUserCode] - - public static Result Ok(T value) => Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator Result(T value) => Result.Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator Result(ResultError myResultError) => myResultError.WithOk(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator true(Result result) => result.IsOk; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator false(Result result) => result.IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !(Result result) => result.IsError; - - //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode - bool Equals(Result other) => this switch - { - Ok_ ok => ok.Equals((object)other), - Error_ error => error.Equals((object)other), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") - }; - - public override int GetHashCode() => this switch - { - Ok_ ok => ok.GetHashCode(), - Error_ error => error.GetHashCode(), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") - }; - - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != this.GetType()) return false; - return Equals((Result)obj); - } - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Result? left, Result? right) => Equals(left, right); - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Result? left, Result? right) => !Equals(left, right); - - [global::System.Diagnostics.DebuggerStepThrough] - public void Match( global::System.Action ok, global::System.Action? error = null) => Match( - v => - { - ok.Invoke(v); - return 42; - }, - err => - { - error?.Invoke(err); - return 42; - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T1 Match( global::System.Func ok, global::System.Func error) - { - return this switch - { - Ok_ okResult => ok(okResult.Value), - Error_ errorResult => error(errorResult.Details), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) - { - return this switch - { - Ok_ okResult => await ok(okResult.Value).ConfigureAwait(false), - Error_ errorResult => await error(errorResult.Details).ConfigureAwait(false), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => - Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); - - [global::System.Diagnostics.DebuggerStepThrough] - public async global::System.Threading.Tasks.Task Match( global::System.Func ok) - { - if (this is Ok_ okResult) await ok(okResult.Value).ConfigureAwait(false); - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public T Match( global::System.Func error) => Match(v => v, error); - - [global::System.Diagnostics.DebuggerStepThrough] - - public Result Bind( global::System.Func> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return bind(ok.Value); - } - // ReSharper disable once RedundantCatchClause + public abstract partial class Result + { + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static Result Error(String details) => new Result.Error_(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static ResultError Error(String details) => new(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static Result Ok(T value) => new Result.Ok_(value); + public bool IsError => GetType().GetGenericTypeDefinition() == typeof(Result<>.Error_); + public bool IsOk => !IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public abstract String? GetErrorOrDefault(); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result Try( global::System.Func action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + } + + public abstract partial class Result : Result, global::System.Collections.Generic.IEnumerable + { + + [global::System.Diagnostics.DebuggerNonUserCode] + + public new static Result Error(String message) => Error(message); + + [global::System.Diagnostics.DebuggerNonUserCode] + + public static Result Ok(T value) => Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator Result(T value) => Result.Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator Result(ResultError myResultError) => myResultError.WithOk(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator true(Result result) => result.IsOk; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator false(Result result) => result.IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !(Result result) => result.IsError; + + //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode + bool Equals(Result other) => this switch + { + Ok_ ok => ok.Equals((object)other), + Error_ error => error.Equals((object)other), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") + }; + + public override int GetHashCode() => this switch + { + Ok_ ok => ok.GetHashCode(), + Error_ error => error.GetHashCode(), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(Result)}") + }; + + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((Result)obj); + } + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Result? left, Result? right) => Equals(left, right); + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Result? left, Result? right) => !Equals(left, right); + + [global::System.Diagnostics.DebuggerStepThrough] + public void Match( global::System.Action ok, global::System.Action? error = null) => Match( + v => + { + ok.Invoke(v); + return 42; + }, + err => + { + error?.Invoke(err); + return 42; + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T1 Match( global::System.Func ok, global::System.Func error) + { + return this switch + { + Ok_ okResult => ok(okResult.Value), + Error_ errorResult => error(errorResult.Details), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) + { + return this switch + { + Ok_ okResult => await ok(okResult.Value).ConfigureAwait(false), + Error_ errorResult => await error(errorResult.Details).ConfigureAwait(false), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => + Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); + + [global::System.Diagnostics.DebuggerStepThrough] + public async global::System.Threading.Tasks.Task Match( global::System.Func ok) + { + if (this is Ok_ okResult) await ok(okResult.Value).ConfigureAwait(false); + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public T Match( global::System.Func error) => Match(v => v, error); + + [global::System.Diagnostics.DebuggerStepThrough] + + public Result Bind( global::System.Func> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return bind(ok.Value); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return await bind(ok.Value).ConfigureAwait(false); - } - // ReSharper disable once RedundantCatchClause + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return await bind(ok.Value).ConfigureAwait(false); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public Result Map( global::System.Func map) - { - switch (this) - { - case Ok_ ok: - try - { - return Result.Ok(map(ok.Value)); - } - // ReSharper disable once RedundantCatchClause + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public Result Map( global::System.Func map) + { + switch (this) + { + case Ok_ ok: + try + { + return Result.Ok(map(ok.Value)); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Map( - global::System.Func> map) - { - switch (this) - { - case Ok_ ok: - try - { - return Result.Ok(await map(ok.Value).ConfigureAwait(false)); - } - // ReSharper disable once RedundantCatchClause + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Map( + global::System.Func> map) + { + switch (this) + { + case Ok_ ok: + try + { + return Result.Ok(await map(ok.Value).ConfigureAwait(false)); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - //createGenericResultConversions - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T? GetValueOrDefault() - => Match( - v => (T?)v, - _ => default - ); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T GetValueOrDefault( global::System.Func defaultValue) - => Match( - v => v, - _ => defaultValue() - ); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T GetValueOrDefault(T defaultValue) - => Match( - v => v, - _ => defaultValue - ); - - [global::System.Diagnostics.DebuggerStepThrough] - public T GetValueOrThrow() - => Match( - v => v, - details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); - - - public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); - - - public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); - global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); - - public sealed partial class Ok_ : Result - { - public T Value { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Ok_(T value) => Value = value; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override String? GetErrorOrDefault() => null; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Ok_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Ok_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); - } - - public sealed partial class Error_ : Result - { - public String Details { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Error_(String details) => Details = details; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public Result.Error_ Convert() => new Result.Error_(Details); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override String? GetErrorOrDefault() => Details; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Error_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return Equals(Details, other.Details); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Error_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); - } - } - - public readonly partial struct ResultError : global::System.IEquatable - { - readonly String _details; - - internal ResultError(String details) => _details = details; - - [global::System.Diagnostics.Contracts.Pure] - public Result WithOk() => Result.Error(_details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(ResultError other) => _details.Equals(other._details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) => obj is ResultError other && Equals(other); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => _details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(ResultError left, ResultError right) => left.Equals(right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(ResultError left, ResultError right) => !left.Equals(right); - } - - public static partial class ResultExtension - { - #region bind - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => (await result.ConfigureAwait(false)).Bind(bind); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func>> bind) - => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); - - #endregion - - #region map - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func map) - => (await result.ConfigureAwait(false)).Map(map); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => Bind(result, async v => Result.Ok(await bind(v).ConfigureAwait(false))); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result MapError(this Result result, global::System.Func mapError) - { - if (result is Result.Error_ e) - return Result.Error(mapError(e.Details)); - return result; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); - - #endregion - - #region match - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func> error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func ok, - global::System.Func error) - => (await result.ConfigureAwait(false)).Match(ok, error); - - #endregion - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static Result Flatten(this Result> result) => result.Bind(r => r); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result As(this Result result, global::System.Func errorTIsNotT1) => - result.Bind(r => - { - if (r is T1 converted) - return converted; - return Result.Error(errorTIsNotT1()); - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result As(this Result result, global::System.Func errorIsNotT1) => - result.As(errorIsNotT1); - - #region query-expression pattern - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result Select(this Result result, global::System.Func selector) => result.Map(selector); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result SelectMany(this Result result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this Result result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - - #endregion - - //createGenericResultConversionExtensions - } + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + //createGenericResultConversions + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T? GetValueOrDefault() + => Match( + v => (T?)v, + _ => default + ); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T GetValueOrDefault( global::System.Func defaultValue) + => Match( + v => v, + _ => defaultValue() + ); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T GetValueOrDefault(T defaultValue) + => Match( + v => v, + _ => defaultValue + ); + + [global::System.Diagnostics.DebuggerStepThrough] + public T GetValueOrThrow() + => Match( + v => v, + details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); + + + public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); + + + public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); + global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); + + public sealed partial class Ok_ : Result + { + public T Value { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Ok_(T value) => Value = value; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override String? GetErrorOrDefault() => null; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Ok_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Ok_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); + } + + public sealed partial class Error_ : Result + { + public String Details { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Error_(String details) => Details = details; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public Result.Error_ Convert() => new Result.Error_(Details); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override String? GetErrorOrDefault() => Details; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Error_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Equals(Details, other.Details); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Error_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); + } + } + + public readonly partial struct ResultError : global::System.IEquatable + { + readonly String _details; + + internal ResultError(String details) => _details = details; + + [global::System.Diagnostics.Contracts.Pure] + public Result WithOk() => Result.Error(_details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(ResultError other) => _details.Equals(other._details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) => obj is ResultError other && Equals(other); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => _details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(ResultError left, ResultError right) => left.Equals(right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(ResultError left, ResultError right) => !left.Equals(right); + } + + public static partial class ResultExtension + { + #region bind + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => (await result.ConfigureAwait(false)).Bind(bind); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func>> bind) + => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); + + #endregion + + #region map + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func map) + => (await result.ConfigureAwait(false)).Map(map); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => Bind(result, async v => Result.Ok(await bind(v).ConfigureAwait(false))); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result MapError(this Result result, global::System.Func mapError) + { + if (result is Result.Error_ e) + return Result.Error(mapError(e.Details)); + return result; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); + + #endregion + + #region match + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func> error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func ok, + global::System.Func error) + => (await result.ConfigureAwait(false)).Match(ok, error); + + #endregion + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static Result Flatten(this Result> result) => result.Bind(r => r); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result As(this Result result, global::System.Func errorTIsNotT1) => + result.Bind(r => + { + if (r is T1 converted) + return converted; + return Result.Error(errorTIsNotT1()); + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result As(this Result result, global::System.Func errorIsNotT1) => + result.As(errorIsNotT1); + + #region query-expression pattern + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result Select(this Result result, global::System.Func selector) => result.Map(selector); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result SelectMany(this Result result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this Result result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + + #endregion + + //createGenericResultConversionExtensions + } } namespace FunicularSwitch.Test.Extensions { - public static partial class ResultExtension - { - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable items, - global::System.Func> choose, - global::System.Action onError) - => items - .Select(i => choose(i)) - .Choose(onError); - - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable> results, - global::System.Action onError) - => results - .Where(r => - r.Match(_ => true, error => - { - onError(error); - return false; - })) - .Select(r => r.GetValueOrThrow()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result As(this object? item, global::System.Func error) => - !(item is T t) ? Result.Error(error()) : t; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result NotNull(this T? item, global::System.Func error) => - item ?? Result.Error(error()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result NotNullOrEmpty(this string? s, global::System.Func error) - => string.IsNullOrEmpty(s) ? Result.Error(error()) : s!; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static Result NotNullOrWhiteSpace(this string? s, global::System.Func error) - => string.IsNullOrWhiteSpace(s) ? Result.Error(error()) : s!; - - - public static Result First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => - candidates - .FirstOrDefault(i => predicate(i)) - .NotNull(noMatch); - } + public static partial class ResultExtension + { + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable items, + global::System.Func> choose, + global::System.Action onError) + => items + .Select(i => choose(i)) + .Choose(onError); + + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable> results, + global::System.Action onError) + => results + .Where(r => + r.Match(_ => true, error => + { + onError(error); + return false; + })) + .Select(r => r.GetValueOrThrow()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result As(this object? item, global::System.Func error) => + !(item is T t) ? Result.Error(error()) : t; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result NotNull(this T? item, global::System.Func error) => + item ?? Result.Error(error()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result NotNullOrEmpty(this string? s, global::System.Func error) + => string.IsNullOrEmpty(s) ? Result.Error(error()) : s!; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static Result NotNullOrWhiteSpace(this string? s, global::System.Func error) + => string.IsNullOrWhiteSpace(s) ? Result.Error(error()) : s!; + + + public static Result First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => + candidates + .FirstOrDefault(i => predicate(i)) + .NotNull(noMatch); + } #pragma warning restore 1591 } diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#FunicularSwitch.Test.ResultWithMerge.g.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#FunicularSwitch.Test.ResultWithMerge.g.verified.cs index 977e7900..8693e3e5 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#FunicularSwitch.Test.ResultWithMerge.g.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_enum_error_type#FunicularSwitch.Test.ResultWithMerge.g.verified.cs @@ -116,28 +116,28 @@ public abstract partial class Result public static partial class ResultExtension { - - public static Result> Map(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func map) => + + public static Result> Map(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func map) => results.Select(r => r.Map(map)).Aggregate(); - - public static Result> Bind(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func> bind) => + + public static Result> Bind(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func> bind) => results.Select(r => r.Bind(bind)).Aggregate(); - - public static Result> Bind(this Result result, - global::System.Func>> bindMany) => + + public static Result> Bind(this Result result, + global::System.Func>> bindMany) => result.Map(ok => bindMany(ok).Aggregate()).Flatten(); - - public static Result Bind(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func, Result> bind) => + + public static Result Bind(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func, Result> bind) => results.Aggregate().Bind(bind); - - public static Result> Aggregate(this global::System.Collections.Generic.IEnumerable> results) + + public static Result> Aggregate(this global::System.Collections.Generic.IEnumerable> results) { var isError = false; String aggregated = default!; @@ -160,25 +160,25 @@ public static Result Bind(this global::System.Collections.Generic.IEn } - public static async global::System.Threading.Tasks.Task>> Aggregate( + public static async global::System.Threading.Tasks.Task>> Aggregate( this global::System.Threading.Tasks.Task>> results) => (await results.ConfigureAwait(false)) .Aggregate(); - - public static async global::System.Threading.Tasks.Task>> Aggregate( + + public static async global::System.Threading.Tasks.Task>> Aggregate( this global::System.Collections.Generic.IEnumerable>> results) => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) .Aggregate(); - - public static async global::System.Threading.Tasks.Task>> AggregateMany( + + public static async global::System.Threading.Tasks.Task>> AggregateMany( this global::System.Collections.Generic.IEnumerable>>> results) => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) .SelectMany(e => e) .Aggregate(); - + public static Result<(T1, T2)> Aggregate(this Result r1, Result r2) => Aggregate(r1, r2, (v1, v2) => (v1, v2)); @@ -403,8 +403,8 @@ public static Result Aggregate FirstOk(this global::System.Collections.Generic.IEnumerable> results, global::System.Func onEmpty) + + public static Result FirstOk(this global::System.Collections.Generic.IEnumerable> results, global::System.Func onEmpty) { var errors = new global::System.Collections.Generic.List(); foreach (var result in results) @@ -422,21 +422,21 @@ public static Result FirstOk(this global::System.Collections.Generic.IEnum } - public static async global::System.Threading.Tasks.Task>> Aggregate( + public static async global::System.Threading.Tasks.Task>> Aggregate( this global::System.Collections.Generic.IEnumerable>> results, int maxDegreeOfParallelism) => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) .Aggregate(); - - public static async global::System.Threading.Tasks.Task>> AggregateMany( + + public static async global::System.Threading.Tasks.Task>> AggregateMany( this global::System.Collections.Generic.IEnumerable>>> results, int maxDegreeOfParallelism) => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) .SelectMany(e => e) .Aggregate(); - static async global::System.Threading.Tasks.Task SelectAsync(this global::System.Collections.Generic.IEnumerable items, global::System.Func> selector, int maxDegreeOfParallelism) + static async global::System.Threading.Tasks.Task SelectAsync(this global::System.Collections.Generic.IEnumerable items, global::System.Func> selector, int maxDegreeOfParallelism) { using (var throttler = new global::System.Threading.SemaphoreSlim(maxDegreeOfParallelism, maxDegreeOfParallelism)) { @@ -458,39 +458,39 @@ public static Result FirstOk(this global::System.Collections.Generic.IEnum } - public static Result> AllOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate) => + public static Result> AllOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate) => candidates .Select(c => c.Validate(validate)) .Aggregate(); - - public static Result> AllOk(this global::System.Collections.Generic.IEnumerable> candidates, - global::System.Func> validate) => + + public static Result> AllOk(this global::System.Collections.Generic.IEnumerable> candidates, + global::System.Func> validate) => candidates .Bind(items => items.AllOk(validate)); - - public static Result Validate(this Result item, global::System.Func> validate) => item.Bind(i => i.Validate(validate)); + + public static Result Validate(this Result item, global::System.Func> validate) => item.Bind(i => i.Validate(validate)); - - public static Result Validate(this T item, global::System.Func> validate) + + public static Result Validate(this T item, global::System.Func> validate) { - try - { - var errors = validate(item).ToList(); - return errors.Count > 0 ? Result.Error(MergeErrors(errors)) : item; - } - // ReSharper disable once RedundantCatchClause + try + { + var errors = validate(item).ToList(); + return errors.Count > 0 ? Result.Error(MergeErrors(errors)) : item; + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } + { + throw; //createGenericErrorResult + } } - public static Result FirstOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate, global::System.Func onEmpty) => + public static Result FirstOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate, global::System.Func onEmpty) => candidates .Select(r => r.Validate(validate)) .FirstOk(onEmpty); diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_in_different_namespace#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_in_different_namespace#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_in_different_namespace#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_in_different_namespace#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_in_different_namespace#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_in_different_namespace#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_in_different_namespace#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_in_different_namespace#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_in_different_namespace#FunicularSwitch.Test.OperationResult.g.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_in_different_namespace#FunicularSwitch.Test.OperationResult.g.verified.cs index 424c61ca..c961929d 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_in_different_namespace#FunicularSwitch.Test.OperationResult.g.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_in_different_namespace#FunicularSwitch.Test.OperationResult.g.verified.cs @@ -7,605 +7,605 @@ namespace FunicularSwitch.Test { #pragma warning disable 1591 - public abstract partial class OperationResult - { - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResult Error(MyError details) => new OperationResult.Error_(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResultError Error(MyError details) => new(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResult Ok(T value) => new OperationResult.Ok_(value); - public bool IsError => GetType().GetGenericTypeDefinition() == typeof(OperationResult<>.Error_); - public bool IsOk => !IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public abstract MyError? GetErrorOrDefault(); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult Try( global::System.Func action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - } - - public abstract partial class OperationResult : OperationResult, global::System.Collections.Generic.IEnumerable - { - - [global::System.Diagnostics.DebuggerNonUserCode] - - public new static OperationResult Error(MyError message) => Error(message); - - [global::System.Diagnostics.DebuggerNonUserCode] - - public static OperationResult Ok(T value) => Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator OperationResult(T value) => OperationResult.Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator OperationResult(OperationResultError myResultError) => myResultError.WithOk(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator true(OperationResult result) => result.IsOk; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator false(OperationResult result) => result.IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !(OperationResult result) => result.IsError; - - //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode - bool Equals(OperationResult other) => this switch - { - Ok_ ok => ok.Equals((object)other), - Error_ error => error.Equals((object)other), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") - }; - - public override int GetHashCode() => this switch - { - Ok_ ok => ok.GetHashCode(), - Error_ error => error.GetHashCode(), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") - }; - - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != this.GetType()) return false; - return Equals((OperationResult)obj); - } - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(OperationResult? left, OperationResult? right) => Equals(left, right); - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(OperationResult? left, OperationResult? right) => !Equals(left, right); - - [global::System.Diagnostics.DebuggerStepThrough] - public void Match( global::System.Action ok, global::System.Action? error = null) => Match( - v => - { - ok.Invoke(v); - return 42; - }, - err => - { - error?.Invoke(err); - return 42; - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T1 Match( global::System.Func ok, global::System.Func error) - { - return this switch - { - Ok_ okOperationResult => ok(okOperationResult.Value), - Error_ errorOperationResult => error(errorOperationResult.Details), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) - { - return this switch - { - Ok_ okOperationResult => await ok(okOperationResult.Value).ConfigureAwait(false), - Error_ errorOperationResult => await error(errorOperationResult.Details).ConfigureAwait(false), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => - Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); - - [global::System.Diagnostics.DebuggerStepThrough] - public async global::System.Threading.Tasks.Task Match( global::System.Func ok) - { - if (this is Ok_ okOperationResult) await ok(okOperationResult.Value).ConfigureAwait(false); - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public T Match( global::System.Func error) => Match(v => v, error); - - [global::System.Diagnostics.DebuggerStepThrough] - - public OperationResult Bind( global::System.Func> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return bind(ok.Value); - } - // ReSharper disable once RedundantCatchClause + public abstract partial class OperationResult + { + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResult Error(MyError details) => new OperationResult.Error_(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResultError Error(MyError details) => new(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResult Ok(T value) => new OperationResult.Ok_(value); + public bool IsError => GetType().GetGenericTypeDefinition() == typeof(OperationResult<>.Error_); + public bool IsOk => !IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public abstract MyError? GetErrorOrDefault(); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult Try( global::System.Func action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + } + + public abstract partial class OperationResult : OperationResult, global::System.Collections.Generic.IEnumerable + { + + [global::System.Diagnostics.DebuggerNonUserCode] + + public new static OperationResult Error(MyError message) => Error(message); + + [global::System.Diagnostics.DebuggerNonUserCode] + + public static OperationResult Ok(T value) => Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator OperationResult(T value) => OperationResult.Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator OperationResult(OperationResultError myResultError) => myResultError.WithOk(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator true(OperationResult result) => result.IsOk; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator false(OperationResult result) => result.IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !(OperationResult result) => result.IsError; + + //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode + bool Equals(OperationResult other) => this switch + { + Ok_ ok => ok.Equals((object)other), + Error_ error => error.Equals((object)other), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") + }; + + public override int GetHashCode() => this switch + { + Ok_ ok => ok.GetHashCode(), + Error_ error => error.GetHashCode(), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") + }; + + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((OperationResult)obj); + } + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(OperationResult? left, OperationResult? right) => Equals(left, right); + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(OperationResult? left, OperationResult? right) => !Equals(left, right); + + [global::System.Diagnostics.DebuggerStepThrough] + public void Match( global::System.Action ok, global::System.Action? error = null) => Match( + v => + { + ok.Invoke(v); + return 42; + }, + err => + { + error?.Invoke(err); + return 42; + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T1 Match( global::System.Func ok, global::System.Func error) + { + return this switch + { + Ok_ okOperationResult => ok(okOperationResult.Value), + Error_ errorOperationResult => error(errorOperationResult.Details), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) + { + return this switch + { + Ok_ okOperationResult => await ok(okOperationResult.Value).ConfigureAwait(false), + Error_ errorOperationResult => await error(errorOperationResult.Details).ConfigureAwait(false), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => + Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); + + [global::System.Diagnostics.DebuggerStepThrough] + public async global::System.Threading.Tasks.Task Match( global::System.Func ok) + { + if (this is Ok_ okOperationResult) await ok(okOperationResult.Value).ConfigureAwait(false); + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public T Match( global::System.Func error) => Match(v => v, error); + + [global::System.Diagnostics.DebuggerStepThrough] + + public OperationResult Bind( global::System.Func> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return bind(ok.Value); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return await bind(ok.Value).ConfigureAwait(false); - } - // ReSharper disable once RedundantCatchClause + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return await bind(ok.Value).ConfigureAwait(false); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public OperationResult Map( global::System.Func map) - { - switch (this) - { - case Ok_ ok: - try - { - return OperationResult.Ok(map(ok.Value)); - } - // ReSharper disable once RedundantCatchClause + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public OperationResult Map( global::System.Func map) + { + switch (this) + { + case Ok_ ok: + try + { + return OperationResult.Ok(map(ok.Value)); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Map( - global::System.Func> map) - { - switch (this) - { - case Ok_ ok: - try - { - return OperationResult.Ok(await map(ok.Value).ConfigureAwait(false)); - } - // ReSharper disable once RedundantCatchClause + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Map( + global::System.Func> map) + { + switch (this) + { + case Ok_ ok: + try + { + return OperationResult.Ok(await map(ok.Value).ConfigureAwait(false)); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - //createGenericResultConversions - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T? GetValueOrDefault() - => Match( - v => (T?)v, - _ => default - ); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T GetValueOrDefault( global::System.Func defaultValue) - => Match( - v => v, - _ => defaultValue() - ); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T GetValueOrDefault(T defaultValue) - => Match( - v => v, - _ => defaultValue - ); - - [global::System.Diagnostics.DebuggerStepThrough] - public T GetValueOrThrow() - => Match( - v => v, - details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); - - - public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); - - - public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); - global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); - - public sealed partial class Ok_ : OperationResult - { - public T Value { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Ok_(T value) => Value = value; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override MyError? GetErrorOrDefault() => null; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Ok_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Ok_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); - } - - public sealed partial class Error_ : OperationResult - { - public MyError Details { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Error_(MyError details) => Details = details; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public OperationResult.Error_ Convert() => new OperationResult.Error_(Details); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override MyError? GetErrorOrDefault() => Details; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Error_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return Equals(Details, other.Details); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Error_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); - } - } - - public readonly partial struct OperationResultError : global::System.IEquatable - { - readonly MyError _details; - - internal OperationResultError(MyError details) => _details = details; - - [global::System.Diagnostics.Contracts.Pure] - public OperationResult WithOk() => OperationResult.Error(_details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(OperationResultError other) => _details.Equals(other._details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) => obj is OperationResultError other && Equals(other); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => _details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(OperationResultError left, OperationResultError right) => left.Equals(right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(OperationResultError left, OperationResultError right) => !left.Equals(right); - } - - public static partial class OperationResultExtension - { - #region bind - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => (await result.ConfigureAwait(false)).Bind(bind); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func>> bind) - => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); - - #endregion - - #region map - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func map) - => (await result.ConfigureAwait(false)).Map(map); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => Bind(result, async v => OperationResult.Ok(await bind(v).ConfigureAwait(false))); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult MapError(this OperationResult result, global::System.Func mapError) - { - if (result is OperationResult.Error_ e) - return OperationResult.Error(mapError(e.Details)); - return result; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); - - #endregion - - #region match - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func> error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func ok, - global::System.Func error) - => (await result.ConfigureAwait(false)).Match(ok, error); - - #endregion - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResult Flatten(this OperationResult> result) => result.Bind(r => r); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult As(this OperationResult result, global::System.Func errorTIsNotT1) => - result.Bind(r => - { - if (r is T1 converted) - return converted; - return OperationResult.Error(errorTIsNotT1()); - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult As(this OperationResult result, global::System.Func errorIsNotT1) => - result.As(errorIsNotT1); - - #region query-expression pattern - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult Select(this OperationResult result, global::System.Func selector) => result.Map(selector); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult SelectMany(this OperationResult result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this OperationResult result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - - #endregion - - //createGenericResultConversionExtensions - } + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + //createGenericResultConversions + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T? GetValueOrDefault() + => Match( + v => (T?)v, + _ => default + ); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T GetValueOrDefault( global::System.Func defaultValue) + => Match( + v => v, + _ => defaultValue() + ); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T GetValueOrDefault(T defaultValue) + => Match( + v => v, + _ => defaultValue + ); + + [global::System.Diagnostics.DebuggerStepThrough] + public T GetValueOrThrow() + => Match( + v => v, + details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); + + + public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); + + + public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); + global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); + + public sealed partial class Ok_ : OperationResult + { + public T Value { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Ok_(T value) => Value = value; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override MyError? GetErrorOrDefault() => null; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Ok_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Ok_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); + } + + public sealed partial class Error_ : OperationResult + { + public MyError Details { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Error_(MyError details) => Details = details; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public OperationResult.Error_ Convert() => new OperationResult.Error_(Details); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override MyError? GetErrorOrDefault() => Details; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Error_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Equals(Details, other.Details); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Error_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); + } + } + + public readonly partial struct OperationResultError : global::System.IEquatable + { + readonly MyError _details; + + internal OperationResultError(MyError details) => _details = details; + + [global::System.Diagnostics.Contracts.Pure] + public OperationResult WithOk() => OperationResult.Error(_details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(OperationResultError other) => _details.Equals(other._details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) => obj is OperationResultError other && Equals(other); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => _details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(OperationResultError left, OperationResultError right) => left.Equals(right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(OperationResultError left, OperationResultError right) => !left.Equals(right); + } + + public static partial class OperationResultExtension + { + #region bind + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => (await result.ConfigureAwait(false)).Bind(bind); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func>> bind) + => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); + + #endregion + + #region map + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func map) + => (await result.ConfigureAwait(false)).Map(map); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => Bind(result, async v => OperationResult.Ok(await bind(v).ConfigureAwait(false))); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult MapError(this OperationResult result, global::System.Func mapError) + { + if (result is OperationResult.Error_ e) + return OperationResult.Error(mapError(e.Details)); + return result; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); + + #endregion + + #region match + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func> error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func ok, + global::System.Func error) + => (await result.ConfigureAwait(false)).Match(ok, error); + + #endregion + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResult Flatten(this OperationResult> result) => result.Bind(r => r); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult As(this OperationResult result, global::System.Func errorTIsNotT1) => + result.Bind(r => + { + if (r is T1 converted) + return converted; + return OperationResult.Error(errorTIsNotT1()); + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult As(this OperationResult result, global::System.Func errorIsNotT1) => + result.As(errorIsNotT1); + + #region query-expression pattern + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult Select(this OperationResult result, global::System.Func selector) => result.Map(selector); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult SelectMany(this OperationResult result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this OperationResult result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + + #endregion + + //createGenericResultConversionExtensions + } } namespace FunicularSwitch.Test.Extensions { - public static partial class OperationResultExtension - { - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable items, - global::System.Func> choose, - global::System.Action onError) - => items - .Select(i => choose(i)) - .Choose(onError); - - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable> results, - global::System.Action onError) - => results - .Where(r => - r.Match(_ => true, error => - { - onError(error); - return false; - })) - .Select(r => r.GetValueOrThrow()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult As(this object? item, global::System.Func error) => - !(item is T t) ? OperationResult.Error(error()) : t; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult NotNull(this T? item, global::System.Func error) => - item ?? OperationResult.Error(error()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult NotNullOrEmpty(this string? s, global::System.Func error) - => string.IsNullOrEmpty(s) ? OperationResult.Error(error()) : s!; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult NotNullOrWhiteSpace(this string? s, global::System.Func error) - => string.IsNullOrWhiteSpace(s) ? OperationResult.Error(error()) : s!; - - - public static OperationResult First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => - candidates - .FirstOrDefault(i => predicate(i)) - .NotNull(noMatch); - } + public static partial class OperationResultExtension + { + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable items, + global::System.Func> choose, + global::System.Action onError) + => items + .Select(i => choose(i)) + .Choose(onError); + + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable> results, + global::System.Action onError) + => results + .Where(r => + r.Match(_ => true, error => + { + onError(error); + return false; + })) + .Select(r => r.GetValueOrThrow()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult As(this object? item, global::System.Func error) => + !(item is T t) ? OperationResult.Error(error()) : t; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult NotNull(this T? item, global::System.Func error) => + item ?? OperationResult.Error(error()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult NotNullOrEmpty(this string? s, global::System.Func error) + => string.IsNullOrEmpty(s) ? OperationResult.Error(error()) : s!; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult NotNullOrWhiteSpace(this string? s, global::System.Func error) + => string.IsNullOrWhiteSpace(s) ? OperationResult.Error(error()) : s!; + + + public static OperationResult First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => + candidates + .FirstOrDefault(i => predicate(i)) + .NotNull(noMatch); + } #pragma warning restore 1591 } diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_in_different_namespace#FunicularSwitch.Test.OperationResultWithMerge.g.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_in_different_namespace#FunicularSwitch.Test.OperationResultWithMerge.g.verified.cs index dacfd1ce..0ac28dc2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_in_different_namespace#FunicularSwitch.Test.OperationResultWithMerge.g.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_in_different_namespace#FunicularSwitch.Test.OperationResultWithMerge.g.verified.cs @@ -117,28 +117,28 @@ public abstract partial class OperationResult public static partial class OperationResultExtension { - - public static OperationResult> Map(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func map) => + + public static OperationResult> Map(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func map) => results.Select(r => r.Map(map)).Aggregate(); - - public static OperationResult> Bind(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func> bind) => + + public static OperationResult> Bind(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func> bind) => results.Select(r => r.Bind(bind)).Aggregate(); - - public static OperationResult> Bind(this OperationResult result, - global::System.Func>> bindMany) => + + public static OperationResult> Bind(this OperationResult result, + global::System.Func>> bindMany) => result.Map(ok => bindMany(ok).Aggregate()).Flatten(); - - public static OperationResult Bind(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func, OperationResult> bind) => + + public static OperationResult Bind(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func, OperationResult> bind) => results.Aggregate().Bind(bind); - - public static OperationResult> Aggregate(this global::System.Collections.Generic.IEnumerable> results) + + public static OperationResult> Aggregate(this global::System.Collections.Generic.IEnumerable> results) { var isError = false; MyError aggregated = default!; @@ -161,25 +161,25 @@ public static OperationResult Bind(this global::System.Collections.Ge } - public static async global::System.Threading.Tasks.Task>> Aggregate( + public static async global::System.Threading.Tasks.Task>> Aggregate( this global::System.Threading.Tasks.Task>> results) => (await results.ConfigureAwait(false)) .Aggregate(); - - public static async global::System.Threading.Tasks.Task>> Aggregate( + + public static async global::System.Threading.Tasks.Task>> Aggregate( this global::System.Collections.Generic.IEnumerable>> results) => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) .Aggregate(); - - public static async global::System.Threading.Tasks.Task>> AggregateMany( + + public static async global::System.Threading.Tasks.Task>> AggregateMany( this global::System.Collections.Generic.IEnumerable>>> results) => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) .SelectMany(e => e) .Aggregate(); - + public static OperationResult<(T1, T2)> Aggregate(this OperationResult r1, OperationResult r2) => Aggregate(r1, r2, (v1, v2) => (v1, v2)); @@ -404,8 +404,8 @@ public static OperationResult Aggregate FirstOk(this global::System.Collections.Generic.IEnumerable> results, global::System.Func onEmpty) + + public static OperationResult FirstOk(this global::System.Collections.Generic.IEnumerable> results, global::System.Func onEmpty) { var errors = new global::System.Collections.Generic.List(); foreach (var result in results) @@ -423,21 +423,21 @@ public static OperationResult FirstOk(this global::System.Collections.Gene } - public static async global::System.Threading.Tasks.Task>> Aggregate( + public static async global::System.Threading.Tasks.Task>> Aggregate( this global::System.Collections.Generic.IEnumerable>> results, int maxDegreeOfParallelism) => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) .Aggregate(); - - public static async global::System.Threading.Tasks.Task>> AggregateMany( + + public static async global::System.Threading.Tasks.Task>> AggregateMany( this global::System.Collections.Generic.IEnumerable>>> results, int maxDegreeOfParallelism) => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) .SelectMany(e => e) .Aggregate(); - static async global::System.Threading.Tasks.Task SelectAsync(this global::System.Collections.Generic.IEnumerable items, global::System.Func> selector, int maxDegreeOfParallelism) + static async global::System.Threading.Tasks.Task SelectAsync(this global::System.Collections.Generic.IEnumerable items, global::System.Func> selector, int maxDegreeOfParallelism) { using (var throttler = new global::System.Threading.SemaphoreSlim(maxDegreeOfParallelism, maxDegreeOfParallelism)) { @@ -459,39 +459,39 @@ public static OperationResult FirstOk(this global::System.Collections.Gene } - public static OperationResult> AllOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate) => + public static OperationResult> AllOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate) => candidates .Select(c => c.Validate(validate)) .Aggregate(); - - public static OperationResult> AllOk(this global::System.Collections.Generic.IEnumerable> candidates, - global::System.Func> validate) => + + public static OperationResult> AllOk(this global::System.Collections.Generic.IEnumerable> candidates, + global::System.Func> validate) => candidates .Bind(items => items.AllOk(validate)); - - public static OperationResult Validate(this OperationResult item, global::System.Func> validate) => item.Bind(i => i.Validate(validate)); + + public static OperationResult Validate(this OperationResult item, global::System.Func> validate) => item.Bind(i => i.Validate(validate)); - - public static OperationResult Validate(this T item, global::System.Func> validate) + + public static OperationResult Validate(this T item, global::System.Func> validate) { - try - { - var errors = validate(item).ToList(); - return errors.Count > 0 ? OperationResult.Error(MergeErrors(errors)) : item; - } - // ReSharper disable once RedundantCatchClause + try + { + var errors = validate(item).ToList(); + return errors.Count > 0 ? OperationResult.Error(MergeErrors(errors)) : item; + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } + { + throw; //createGenericErrorResult + } } - public static OperationResult FirstOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate, global::System.Func onEmpty) => + public static OperationResult FirstOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate, global::System.Func onEmpty) => candidates .Select(r => r.Validate(validate)) .FirstOk(onEmpty); diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_when_jetbrains_annotations_is_referenced#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_when_jetbrains_annotations_is_referenced#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_when_jetbrains_annotations_is_referenced#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_when_jetbrains_annotations_is_referenced#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_when_jetbrains_annotations_is_referenced#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_when_jetbrains_annotations_is_referenced#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_when_jetbrains_annotations_is_referenced#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_when_jetbrains_annotations_is_referenced#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_when_jetbrains_annotations_is_referenced#FunicularSwitch.Test.OperationResult.g.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_when_jetbrains_annotations_is_referenced#FunicularSwitch.Test.OperationResult.g.verified.cs index 892ce468..025b29ac 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_when_jetbrains_annotations_is_referenced#FunicularSwitch.Test.OperationResult.g.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_when_jetbrains_annotations_is_referenced#FunicularSwitch.Test.OperationResult.g.verified.cs @@ -7,605 +7,605 @@ namespace FunicularSwitch.Test { #pragma warning disable 1591 - public abstract partial class OperationResult - { - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResult Error(MyError details) => new OperationResult.Error_(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResultError Error(MyError details) => new(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResult Ok(T value) => new OperationResult.Ok_(value); - public bool IsError => GetType().GetGenericTypeDefinition() == typeof(OperationResult<>.Error_); - public bool IsOk => !IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public abstract MyError? GetErrorOrDefault(); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult Try([global::JetBrains.Annotations.InstantHandle] global::System.Func action, [global::JetBrains.Annotations.InstantHandle] global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task> Try([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> action, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult Try([global::JetBrains.Annotations.InstantHandle] global::System.Func> action, [global::JetBrains.Annotations.InstantHandle] global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task> Try([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func>> action, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - } - - public abstract partial class OperationResult : OperationResult, global::System.Collections.Generic.IEnumerable - { - - [global::System.Diagnostics.DebuggerNonUserCode] - [global::JetBrains.Annotations.MustUseReturnValue] - public new static OperationResult Error(MyError message) => Error(message); - - [global::System.Diagnostics.DebuggerNonUserCode] - [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult Ok(T value) => Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static implicit operator OperationResult(T value) => OperationResult.Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static implicit operator OperationResult(OperationResultError myResultError) => myResultError.WithOk(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator true(OperationResult result) => result.IsOk; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator false(OperationResult result) => result.IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !(OperationResult result) => result.IsError; - - //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode - bool Equals(OperationResult other) => this switch - { - Ok_ ok => ok.Equals((object)other), - Error_ error => error.Equals((object)other), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") - }; - - public override int GetHashCode() => this switch - { - Ok_ ok => ok.GetHashCode(), - Error_ error => error.GetHashCode(), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") - }; - - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != this.GetType()) return false; - return Equals((OperationResult)obj); - } - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(OperationResult? left, OperationResult? right) => Equals(left, right); - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(OperationResult? left, OperationResult? right) => !Equals(left, right); - - [global::System.Diagnostics.DebuggerStepThrough] - public void Match([global::JetBrains.Annotations.InstantHandle] global::System.Action ok, [global::JetBrains.Annotations.InstantHandle] global::System.Action? error = null) => Match( - v => - { - ok.Invoke(v); - return 42; - }, - err => - { - error?.Invoke(err); - return 42; - }); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public T1 Match([global::JetBrains.Annotations.InstantHandle] global::System.Func ok, [global::JetBrains.Annotations.InstantHandle] global::System.Func error) - { - return this switch - { - Ok_ okOperationResult => ok(okOperationResult.Value), - Error_ errorOperationResult => error(errorOperationResult.Details), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public async global::System.Threading.Tasks.Task Match([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> ok, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> error) - { - return this switch - { - Ok_ okOperationResult => await ok(okOperationResult.Value).ConfigureAwait(false), - Error_ errorOperationResult => await error(errorOperationResult.Details).ConfigureAwait(false), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public global::System.Threading.Tasks.Task Match([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> ok, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func error) => - Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); - - [global::System.Diagnostics.DebuggerStepThrough] - public async global::System.Threading.Tasks.Task Match([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func ok) - { - if (this is Ok_ okOperationResult) await ok(okOperationResult.Value).ConfigureAwait(false); - } - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public T Match([global::JetBrains.Annotations.InstantHandle] global::System.Func error) => Match(v => v, error); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public OperationResult Bind([global::JetBrains.Annotations.InstantHandle] global::System.Func> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return bind(ok.Value); - } - // ReSharper disable once RedundantCatchClause + public abstract partial class OperationResult + { + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResult Error(MyError details) => new OperationResult.Error_(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResultError Error(MyError details) => new(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResult Ok(T value) => new OperationResult.Ok_(value); + public bool IsError => GetType().GetGenericTypeDefinition() == typeof(OperationResult<>.Error_); + public bool IsOk => !IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public abstract MyError? GetErrorOrDefault(); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static OperationResult Try([global::JetBrains.Annotations.InstantHandle] global::System.Func action, [global::JetBrains.Annotations.InstantHandle] global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task> Try([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> action, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static OperationResult Try([global::JetBrains.Annotations.InstantHandle] global::System.Func> action, [global::JetBrains.Annotations.InstantHandle] global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task> Try([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func>> action, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + } + + public abstract partial class OperationResult : OperationResult, global::System.Collections.Generic.IEnumerable + { + + [global::System.Diagnostics.DebuggerNonUserCode] + [global::JetBrains.Annotations.MustUseReturnValue] + public new static OperationResult Error(MyError message) => Error(message); + + [global::System.Diagnostics.DebuggerNonUserCode] + [global::JetBrains.Annotations.MustUseReturnValue] + public static OperationResult Ok(T value) => Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static implicit operator OperationResult(T value) => OperationResult.Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static implicit operator OperationResult(OperationResultError myResultError) => myResultError.WithOk(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator true(OperationResult result) => result.IsOk; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator false(OperationResult result) => result.IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !(OperationResult result) => result.IsError; + + //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode + bool Equals(OperationResult other) => this switch + { + Ok_ ok => ok.Equals((object)other), + Error_ error => error.Equals((object)other), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") + }; + + public override int GetHashCode() => this switch + { + Ok_ ok => ok.GetHashCode(), + Error_ error => error.GetHashCode(), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") + }; + + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((OperationResult)obj); + } + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(OperationResult? left, OperationResult? right) => Equals(left, right); + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(OperationResult? left, OperationResult? right) => !Equals(left, right); + + [global::System.Diagnostics.DebuggerStepThrough] + public void Match([global::JetBrains.Annotations.InstantHandle] global::System.Action ok, [global::JetBrains.Annotations.InstantHandle] global::System.Action? error = null) => Match( + v => + { + ok.Invoke(v); + return 42; + }, + err => + { + error?.Invoke(err); + return 42; + }); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public T1 Match([global::JetBrains.Annotations.InstantHandle] global::System.Func ok, [global::JetBrains.Annotations.InstantHandle] global::System.Func error) + { + return this switch + { + Ok_ okOperationResult => ok(okOperationResult.Value), + Error_ errorOperationResult => error(errorOperationResult.Details), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public async global::System.Threading.Tasks.Task Match([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> ok, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> error) + { + return this switch + { + Ok_ okOperationResult => await ok(okOperationResult.Value).ConfigureAwait(false), + Error_ errorOperationResult => await error(errorOperationResult.Details).ConfigureAwait(false), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public global::System.Threading.Tasks.Task Match([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> ok, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func error) => + Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); + + [global::System.Diagnostics.DebuggerStepThrough] + public async global::System.Threading.Tasks.Task Match([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func ok) + { + if (this is Ok_ okOperationResult) await ok(okOperationResult.Value).ConfigureAwait(false); + } + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public T Match([global::JetBrains.Annotations.InstantHandle] global::System.Func error) => Match(v => v, error); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public OperationResult Bind([global::JetBrains.Annotations.InstantHandle] global::System.Func> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return bind(ok.Value); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public async global::System.Threading.Tasks.Task> Bind([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func>> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return await bind(ok.Value).ConfigureAwait(false); - } - // ReSharper disable once RedundantCatchClause + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public async global::System.Threading.Tasks.Task> Bind([global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func>> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return await bind(ok.Value).ConfigureAwait(false); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public OperationResult Map([global::JetBrains.Annotations.InstantHandle] global::System.Func map) - { - switch (this) - { - case Ok_ ok: - try - { - return OperationResult.Ok(map(ok.Value)); - } - // ReSharper disable once RedundantCatchClause + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public OperationResult Map([global::JetBrains.Annotations.InstantHandle] global::System.Func map) + { + switch (this) + { + case Ok_ ok: + try + { + return OperationResult.Ok(map(ok.Value)); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public async global::System.Threading.Tasks.Task> Map( - [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> map) - { - switch (this) - { - case Ok_ ok: - try - { - return OperationResult.Ok(await map(ok.Value).ConfigureAwait(false)); - } - // ReSharper disable once RedundantCatchClause + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public async global::System.Threading.Tasks.Task> Map( + [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> map) + { + switch (this) + { + case Ok_ ok: + try + { + return OperationResult.Ok(await map(ok.Value).ConfigureAwait(false)); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - //createGenericResultConversions - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T? GetValueOrDefault() - => Match( - v => (T?)v, - _ => default - ); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public T GetValueOrDefault([global::JetBrains.Annotations.InstantHandle] global::System.Func defaultValue) - => Match( - v => v, - _ => defaultValue() - ); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T GetValueOrDefault(T defaultValue) - => Match( - v => v, - _ => defaultValue - ); - - [global::System.Diagnostics.DebuggerStepThrough] - public T GetValueOrThrow() - => Match( - v => v, - details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); - - [global::JetBrains.Annotations.MustUseReturnValue] - public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); - - [global::JetBrains.Annotations.MustUseReturnValue] - public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); - global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); - - public sealed partial class Ok_ : OperationResult - { - public T Value { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Ok_(T value) => Value = value; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override MyError? GetErrorOrDefault() => null; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Ok_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Ok_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); - } - - public sealed partial class Error_ : OperationResult - { - public MyError Details { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Error_(MyError details) => Details = details; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public OperationResult.Error_ Convert() => new OperationResult.Error_(Details); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override MyError? GetErrorOrDefault() => Details; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Error_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return Equals(Details, other.Details); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Error_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); - } - } - - public readonly partial struct OperationResultError : global::System.IEquatable - { - readonly MyError _details; - - internal OperationResultError(MyError details) => _details = details; - - [global::System.Diagnostics.Contracts.Pure] - public OperationResult WithOk() => OperationResult.Error(_details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(OperationResultError other) => _details.Equals(other._details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) => obj is OperationResultError other && Equals(other); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => _details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(OperationResultError left, OperationResultError right) => left.Equals(right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(OperationResultError left, OperationResultError right) => !left.Equals(right); - } - - public static partial class OperationResultExtension - { - #region bind - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> bind) - => (await result.ConfigureAwait(false)).Bind(bind); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func>> bind) - => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); - - #endregion - - #region map - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func map) - => (await result.ConfigureAwait(false)).Map(map); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> bind) - => Bind(result, async v => OperationResult.Ok(await bind(v).ConfigureAwait(false))); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult MapError(this OperationResult result, [global::JetBrains.Annotations.InstantHandle] global::System.Func mapError) - { - if (result is OperationResult.Error_ e) - return OperationResult.Error(mapError(e.Details)); - return result; - } - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); - - #endregion - - #region match - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> ok, - [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> ok, - [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func ok, - [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func error) - => (await result.ConfigureAwait(false)).Match(ok, error); - - #endregion - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResult Flatten(this OperationResult> result) => result.Bind(r => r); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult As(this OperationResult result, [global::JetBrains.Annotations.InstantHandle] global::System.Func errorTIsNotT1) => - result.Bind(r => - { - if (r is T1 converted) - return converted; - return OperationResult.Error(errorTIsNotT1()); - }); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult As(this OperationResult result, [global::JetBrains.Annotations.InstantHandle] global::System.Func errorIsNotT1) => - result.As(errorIsNotT1); - - #region query-expression pattern - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult Select(this OperationResult result, global::System.Func selector) => result.Map(selector); - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult SelectMany(this OperationResult result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static global::System.Threading.Tasks.Task> SelectMany(this OperationResult result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - - #endregion - - //createGenericResultConversionExtensions - } + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + //createGenericResultConversions + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T? GetValueOrDefault() + => Match( + v => (T?)v, + _ => default + ); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public T GetValueOrDefault([global::JetBrains.Annotations.InstantHandle] global::System.Func defaultValue) + => Match( + v => v, + _ => defaultValue() + ); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T GetValueOrDefault(T defaultValue) + => Match( + v => v, + _ => defaultValue + ); + + [global::System.Diagnostics.DebuggerStepThrough] + public T GetValueOrThrow() + => Match( + v => v, + details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); + + [global::JetBrains.Annotations.MustUseReturnValue] + public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); + + [global::JetBrains.Annotations.MustUseReturnValue] + public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); + global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); + + public sealed partial class Ok_ : OperationResult + { + public T Value { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Ok_(T value) => Value = value; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override MyError? GetErrorOrDefault() => null; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Ok_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Ok_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); + } + + public sealed partial class Error_ : OperationResult + { + public MyError Details { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Error_(MyError details) => Details = details; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public OperationResult.Error_ Convert() => new OperationResult.Error_(Details); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override MyError? GetErrorOrDefault() => Details; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Error_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Equals(Details, other.Details); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Error_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); + } + } + + public readonly partial struct OperationResultError : global::System.IEquatable + { + readonly MyError _details; + + internal OperationResultError(MyError details) => _details = details; + + [global::System.Diagnostics.Contracts.Pure] + public OperationResult WithOk() => OperationResult.Error(_details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(OperationResultError other) => _details.Equals(other._details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) => obj is OperationResultError other && Equals(other); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => _details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(OperationResultError left, OperationResultError right) => left.Equals(right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(OperationResultError left, OperationResultError right) => !left.Equals(right); + } + + public static partial class OperationResultExtension + { + #region bind + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> bind) + => (await result.ConfigureAwait(false)).Bind(bind); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func>> bind) + => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); + + #endregion + + #region map + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func map) + => (await result.ConfigureAwait(false)).Map(map); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> bind) + => Bind(result, async v => OperationResult.Ok(await bind(v).ConfigureAwait(false))); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static OperationResult MapError(this OperationResult result, [global::JetBrains.Annotations.InstantHandle] global::System.Func mapError) + { + if (result is OperationResult.Error_ e) + return OperationResult.Error(mapError(e.Details)); + return result; + } + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); + + #endregion + + #region match + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> ok, + [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> ok, + [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func ok, + [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func error) + => (await result.ConfigureAwait(false)).Match(ok, error); + + #endregion + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResult Flatten(this OperationResult> result) => result.Bind(r => r); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static OperationResult As(this OperationResult result, [global::JetBrains.Annotations.InstantHandle] global::System.Func errorTIsNotT1) => + result.Bind(r => + { + if (r is T1 converted) + return converted; + return OperationResult.Error(errorTIsNotT1()); + }); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static OperationResult As(this OperationResult result, [global::JetBrains.Annotations.InstantHandle] global::System.Func errorIsNotT1) => + result.As(errorIsNotT1); + + #region query-expression pattern + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static OperationResult Select(this OperationResult result, global::System.Func selector) => result.Map(selector); + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static OperationResult SelectMany(this OperationResult result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static global::System.Threading.Tasks.Task> SelectMany(this OperationResult result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + + #endregion + + //createGenericResultConversionExtensions + } } namespace FunicularSwitch.Test.Extensions { - public static partial class OperationResultExtension - { - [global::JetBrains.Annotations.MustUseReturnValue] - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable items, - global::System.Func> choose, - global::System.Action onError) - => items - .Select(i => choose(i)) - .Choose(onError); - - [global::JetBrains.Annotations.MustUseReturnValue] - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable> results, - global::System.Action onError) - => results - .Where(r => - r.Match(_ => true, error => - { - onError(error); - return false; - })) - .Select(r => r.GetValueOrThrow()); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult As(this object? item, [global::JetBrains.Annotations.InstantHandle] global::System.Func error) => - !(item is T t) ? OperationResult.Error(error()) : t; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult NotNull(this T? item, [global::JetBrains.Annotations.InstantHandle] global::System.Func error) => - item ?? OperationResult.Error(error()); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult NotNullOrEmpty(this string? s, [global::JetBrains.Annotations.InstantHandle] global::System.Func error) - => string.IsNullOrEmpty(s) ? OperationResult.Error(error()) : s!; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult NotNullOrWhiteSpace(this string? s, [global::JetBrains.Annotations.InstantHandle] global::System.Func error) - => string.IsNullOrWhiteSpace(s) ? OperationResult.Error(error()) : s!; - - [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult First(this global::System.Collections.Generic.IEnumerable candidates, [global::JetBrains.Annotations.InstantHandle] global::System.Func predicate, [global::JetBrains.Annotations.InstantHandle] global::System.Func noMatch) => - candidates - .FirstOrDefault(i => predicate(i)) - .NotNull(noMatch); - } + public static partial class OperationResultExtension + { + [global::JetBrains.Annotations.MustUseReturnValue] + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable items, + global::System.Func> choose, + global::System.Action onError) + => items + .Select(i => choose(i)) + .Choose(onError); + + [global::JetBrains.Annotations.MustUseReturnValue] + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable> results, + global::System.Action onError) + => results + .Where(r => + r.Match(_ => true, error => + { + onError(error); + return false; + })) + .Select(r => r.GetValueOrThrow()); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static OperationResult As(this object? item, [global::JetBrains.Annotations.InstantHandle] global::System.Func error) => + !(item is T t) ? OperationResult.Error(error()) : t; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static OperationResult NotNull(this T? item, [global::JetBrains.Annotations.InstantHandle] global::System.Func error) => + item ?? OperationResult.Error(error()); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static OperationResult NotNullOrEmpty(this string? s, [global::JetBrains.Annotations.InstantHandle] global::System.Func error) + => string.IsNullOrEmpty(s) ? OperationResult.Error(error()) : s!; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::JetBrains.Annotations.MustUseReturnValue] + public static OperationResult NotNullOrWhiteSpace(this string? s, [global::JetBrains.Annotations.InstantHandle] global::System.Func error) + => string.IsNullOrWhiteSpace(s) ? OperationResult.Error(error()) : s!; + + [global::JetBrains.Annotations.MustUseReturnValue] + public static OperationResult First(this global::System.Collections.Generic.IEnumerable candidates, [global::JetBrains.Annotations.InstantHandle] global::System.Func predicate, [global::JetBrains.Annotations.InstantHandle] global::System.Func noMatch) => + candidates + .FirstOrDefault(i => predicate(i)) + .NotNull(noMatch); + } #pragma warning restore 1591 } diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_when_jetbrains_annotations_is_referenced#FunicularSwitch.Test.OperationResultWithMerge.g.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_when_jetbrains_annotations_is_referenced#FunicularSwitch.Test.OperationResultWithMerge.g.verified.cs index 9e590558..1307463b 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_when_jetbrains_annotations_is_referenced#FunicularSwitch.Test.OperationResultWithMerge.g.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_when_jetbrains_annotations_is_referenced#FunicularSwitch.Test.OperationResultWithMerge.g.verified.cs @@ -116,28 +116,28 @@ public abstract partial class OperationResult public static partial class OperationResultExtension { - [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult> Map(this global::System.Collections.Generic.IEnumerable> results, - [global::JetBrains.Annotations.InstantHandle]global::System.Func map) => + [global::JetBrains.Annotations.MustUseReturnValue] + public static OperationResult> Map(this global::System.Collections.Generic.IEnumerable> results, + [global::JetBrains.Annotations.InstantHandle] global::System.Func map) => results.Select(r => r.Map(map)).Aggregate(); - [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult> Bind(this global::System.Collections.Generic.IEnumerable> results, - [global::JetBrains.Annotations.InstantHandle]global::System.Func> bind) => + [global::JetBrains.Annotations.MustUseReturnValue] + public static OperationResult> Bind(this global::System.Collections.Generic.IEnumerable> results, + [global::JetBrains.Annotations.InstantHandle] global::System.Func> bind) => results.Select(r => r.Bind(bind)).Aggregate(); - [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult> Bind(this OperationResult result, - [global::JetBrains.Annotations.InstantHandle]global::System.Func>> bindMany) => + [global::JetBrains.Annotations.MustUseReturnValue] + public static OperationResult> Bind(this OperationResult result, + [global::JetBrains.Annotations.InstantHandle] global::System.Func>> bindMany) => result.Map(ok => bindMany(ok).Aggregate()).Flatten(); - [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult Bind(this global::System.Collections.Generic.IEnumerable> results, - [global::JetBrains.Annotations.InstantHandle]global::System.Func, OperationResult> bind) => + [global::JetBrains.Annotations.MustUseReturnValue] + public static OperationResult Bind(this global::System.Collections.Generic.IEnumerable> results, + [global::JetBrains.Annotations.InstantHandle] global::System.Func, OperationResult> bind) => results.Aggregate().Bind(bind); - [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult> Aggregate(this global::System.Collections.Generic.IEnumerable> results) + [global::JetBrains.Annotations.MustUseReturnValue] + public static OperationResult> Aggregate(this global::System.Collections.Generic.IEnumerable> results) { var isError = false; MyError aggregated = default!; @@ -160,25 +160,25 @@ public static OperationResult Bind(this global::System.Collections.Ge } [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task>> Aggregate( + public static async global::System.Threading.Tasks.Task>> Aggregate( this global::System.Threading.Tasks.Task>> results) => (await results.ConfigureAwait(false)) .Aggregate(); - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task>> Aggregate( + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task>> Aggregate( this global::System.Collections.Generic.IEnumerable>> results) => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) .Aggregate(); - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task>> AggregateMany( + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task>> AggregateMany( this global::System.Collections.Generic.IEnumerable>>> results) => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) .SelectMany(e => e) .Aggregate(); - + [global::JetBrains.Annotations.MustUseReturnValue] public static OperationResult<(T1, T2)> Aggregate(this OperationResult r1, OperationResult r2) => Aggregate(r1, r2, (v1, v2) => (v1, v2)); @@ -403,8 +403,8 @@ public static OperationResult Aggregate FirstOk(this global::System.Collections.Generic.IEnumerable> results, [global::JetBrains.Annotations.InstantHandle]global::System.Func onEmpty) + [global::JetBrains.Annotations.MustUseReturnValue] + public static OperationResult FirstOk(this global::System.Collections.Generic.IEnumerable> results, [global::JetBrains.Annotations.InstantHandle] global::System.Func onEmpty) { var errors = new global::System.Collections.Generic.List(); foreach (var result in results) @@ -422,21 +422,21 @@ public static OperationResult FirstOk(this global::System.Collections.Gene } [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task>> Aggregate( + public static async global::System.Threading.Tasks.Task>> Aggregate( this global::System.Collections.Generic.IEnumerable>> results, int maxDegreeOfParallelism) => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) .Aggregate(); - [global::JetBrains.Annotations.MustUseReturnValue] - public static async global::System.Threading.Tasks.Task>> AggregateMany( + [global::JetBrains.Annotations.MustUseReturnValue] + public static async global::System.Threading.Tasks.Task>> AggregateMany( this global::System.Collections.Generic.IEnumerable>>> results, int maxDegreeOfParallelism) => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) .SelectMany(e => e) .Aggregate(); - static async global::System.Threading.Tasks.Task SelectAsync(this global::System.Collections.Generic.IEnumerable items, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)]global::System.Func> selector, int maxDegreeOfParallelism) + static async global::System.Threading.Tasks.Task SelectAsync(this global::System.Collections.Generic.IEnumerable items, [global::JetBrains.Annotations.InstantHandle(RequireAwait = true)] global::System.Func> selector, int maxDegreeOfParallelism) { using (var throttler = new global::System.Threading.SemaphoreSlim(maxDegreeOfParallelism, maxDegreeOfParallelism)) { @@ -458,39 +458,39 @@ public static OperationResult FirstOk(this global::System.Collections.Gene } [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult> AllOk(this global::System.Collections.Generic.IEnumerable candidates, [global::JetBrains.Annotations.InstantHandle]global::System.Func> validate) => + public static OperationResult> AllOk(this global::System.Collections.Generic.IEnumerable candidates, [global::JetBrains.Annotations.InstantHandle] global::System.Func> validate) => candidates .Select(c => c.Validate(validate)) .Aggregate(); - [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult> AllOk(this global::System.Collections.Generic.IEnumerable> candidates, - [global::JetBrains.Annotations.InstantHandle]global::System.Func> validate) => + [global::JetBrains.Annotations.MustUseReturnValue] + public static OperationResult> AllOk(this global::System.Collections.Generic.IEnumerable> candidates, + [global::JetBrains.Annotations.InstantHandle] global::System.Func> validate) => candidates .Bind(items => items.AllOk(validate)); - [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult Validate(this OperationResult item, [global::JetBrains.Annotations.InstantHandle]global::System.Func> validate) => item.Bind(i => i.Validate(validate)); + [global::JetBrains.Annotations.MustUseReturnValue] + public static OperationResult Validate(this OperationResult item, [global::JetBrains.Annotations.InstantHandle] global::System.Func> validate) => item.Bind(i => i.Validate(validate)); - [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult Validate(this T item, [global::JetBrains.Annotations.InstantHandle]global::System.Func> validate) + [global::JetBrains.Annotations.MustUseReturnValue] + public static OperationResult Validate(this T item, [global::JetBrains.Annotations.InstantHandle] global::System.Func> validate) { - try - { - var errors = validate(item).ToList(); - return errors.Count > 0 ? OperationResult.Error(MergeErrors(errors)) : item; - } - // ReSharper disable once RedundantCatchClause + try + { + var errors = validate(item).ToList(); + return errors.Count > 0 ? OperationResult.Error(MergeErrors(errors)) : item; + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } + { + throw; //createGenericErrorResult + } } [global::JetBrains.Annotations.MustUseReturnValue] - public static OperationResult FirstOk(this global::System.Collections.Generic.IEnumerable candidates, [global::JetBrains.Annotations.InstantHandle]global::System.Func> validate, [global::JetBrains.Annotations.InstantHandle]global::System.Func onEmpty) => + public static OperationResult FirstOk(this global::System.Collections.Generic.IEnumerable candidates, [global::JetBrains.Annotations.InstantHandle] global::System.Func> validate, [global::JetBrains.Annotations.InstantHandle] global::System.Func onEmpty) => candidates .Select(r => r.Validate(validate)) .FirstOk(onEmpty); diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_with_merge#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_with_merge#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_with_merge#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_with_merge#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_with_merge#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_with_merge#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_with_merge#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_with_merge#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_with_merge#FunicularSwitch.Test.OperationResult.g.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_with_merge#FunicularSwitch.Test.OperationResult.g.verified.cs index 6feb6de6..c4ccd7b6 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_with_merge#FunicularSwitch.Test.OperationResult.g.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_with_merge#FunicularSwitch.Test.OperationResult.g.verified.cs @@ -7,605 +7,605 @@ namespace FunicularSwitch.Test { #pragma warning disable 1591 - public abstract partial class OperationResult - { - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResult Error(MyError details) => new OperationResult.Error_(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResultError Error(MyError details) => new(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResult Ok(T value) => new OperationResult.Ok_(value); - public bool IsError => GetType().GetGenericTypeDefinition() == typeof(OperationResult<>.Error_); - public bool IsOk => !IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public abstract MyError? GetErrorOrDefault(); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult Try( global::System.Func action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - } - - public abstract partial class OperationResult : OperationResult, global::System.Collections.Generic.IEnumerable - { - - [global::System.Diagnostics.DebuggerNonUserCode] - - public new static OperationResult Error(MyError message) => Error(message); - - [global::System.Diagnostics.DebuggerNonUserCode] - - public static OperationResult Ok(T value) => Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator OperationResult(T value) => OperationResult.Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator OperationResult(OperationResultError myResultError) => myResultError.WithOk(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator true(OperationResult result) => result.IsOk; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator false(OperationResult result) => result.IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !(OperationResult result) => result.IsError; - - //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode - bool Equals(OperationResult other) => this switch - { - Ok_ ok => ok.Equals((object)other), - Error_ error => error.Equals((object)other), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") - }; - - public override int GetHashCode() => this switch - { - Ok_ ok => ok.GetHashCode(), - Error_ error => error.GetHashCode(), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") - }; - - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != this.GetType()) return false; - return Equals((OperationResult)obj); - } - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(OperationResult? left, OperationResult? right) => Equals(left, right); - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(OperationResult? left, OperationResult? right) => !Equals(left, right); - - [global::System.Diagnostics.DebuggerStepThrough] - public void Match( global::System.Action ok, global::System.Action? error = null) => Match( - v => - { - ok.Invoke(v); - return 42; - }, - err => - { - error?.Invoke(err); - return 42; - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T1 Match( global::System.Func ok, global::System.Func error) - { - return this switch - { - Ok_ okOperationResult => ok(okOperationResult.Value), - Error_ errorOperationResult => error(errorOperationResult.Details), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) - { - return this switch - { - Ok_ okOperationResult => await ok(okOperationResult.Value).ConfigureAwait(false), - Error_ errorOperationResult => await error(errorOperationResult.Details).ConfigureAwait(false), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => - Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); - - [global::System.Diagnostics.DebuggerStepThrough] - public async global::System.Threading.Tasks.Task Match( global::System.Func ok) - { - if (this is Ok_ okOperationResult) await ok(okOperationResult.Value).ConfigureAwait(false); - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public T Match( global::System.Func error) => Match(v => v, error); - - [global::System.Diagnostics.DebuggerStepThrough] - - public OperationResult Bind( global::System.Func> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return bind(ok.Value); - } - // ReSharper disable once RedundantCatchClause + public abstract partial class OperationResult + { + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResult Error(MyError details) => new OperationResult.Error_(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResultError Error(MyError details) => new(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResult Ok(T value) => new OperationResult.Ok_(value); + public bool IsError => GetType().GetGenericTypeDefinition() == typeof(OperationResult<>.Error_); + public bool IsOk => !IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public abstract MyError? GetErrorOrDefault(); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult Try( global::System.Func action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + } + + public abstract partial class OperationResult : OperationResult, global::System.Collections.Generic.IEnumerable + { + + [global::System.Diagnostics.DebuggerNonUserCode] + + public new static OperationResult Error(MyError message) => Error(message); + + [global::System.Diagnostics.DebuggerNonUserCode] + + public static OperationResult Ok(T value) => Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator OperationResult(T value) => OperationResult.Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator OperationResult(OperationResultError myResultError) => myResultError.WithOk(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator true(OperationResult result) => result.IsOk; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator false(OperationResult result) => result.IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !(OperationResult result) => result.IsError; + + //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode + bool Equals(OperationResult other) => this switch + { + Ok_ ok => ok.Equals((object)other), + Error_ error => error.Equals((object)other), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") + }; + + public override int GetHashCode() => this switch + { + Ok_ ok => ok.GetHashCode(), + Error_ error => error.GetHashCode(), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") + }; + + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((OperationResult)obj); + } + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(OperationResult? left, OperationResult? right) => Equals(left, right); + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(OperationResult? left, OperationResult? right) => !Equals(left, right); + + [global::System.Diagnostics.DebuggerStepThrough] + public void Match( global::System.Action ok, global::System.Action? error = null) => Match( + v => + { + ok.Invoke(v); + return 42; + }, + err => + { + error?.Invoke(err); + return 42; + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T1 Match( global::System.Func ok, global::System.Func error) + { + return this switch + { + Ok_ okOperationResult => ok(okOperationResult.Value), + Error_ errorOperationResult => error(errorOperationResult.Details), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) + { + return this switch + { + Ok_ okOperationResult => await ok(okOperationResult.Value).ConfigureAwait(false), + Error_ errorOperationResult => await error(errorOperationResult.Details).ConfigureAwait(false), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => + Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); + + [global::System.Diagnostics.DebuggerStepThrough] + public async global::System.Threading.Tasks.Task Match( global::System.Func ok) + { + if (this is Ok_ okOperationResult) await ok(okOperationResult.Value).ConfigureAwait(false); + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public T Match( global::System.Func error) => Match(v => v, error); + + [global::System.Diagnostics.DebuggerStepThrough] + + public OperationResult Bind( global::System.Func> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return bind(ok.Value); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - return OperationResult.Error(FunicularSwitch.Test.ErrorFactory.FromException(e)); - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return await bind(ok.Value).ConfigureAwait(false); - } - // ReSharper disable once RedundantCatchClause + { + return OperationResult.Error(FunicularSwitch.Test.ErrorFactory.FromException(e)); + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return await bind(ok.Value).ConfigureAwait(false); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - return OperationResult.Error(FunicularSwitch.Test.ErrorFactory.FromException(e)); - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public OperationResult Map( global::System.Func map) - { - switch (this) - { - case Ok_ ok: - try - { - return OperationResult.Ok(map(ok.Value)); - } - // ReSharper disable once RedundantCatchClause + { + return OperationResult.Error(FunicularSwitch.Test.ErrorFactory.FromException(e)); + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public OperationResult Map( global::System.Func map) + { + switch (this) + { + case Ok_ ok: + try + { + return OperationResult.Ok(map(ok.Value)); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - return OperationResult.Error(FunicularSwitch.Test.ErrorFactory.FromException(e)); - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Map( - global::System.Func> map) - { - switch (this) - { - case Ok_ ok: - try - { - return OperationResult.Ok(await map(ok.Value).ConfigureAwait(false)); - } - // ReSharper disable once RedundantCatchClause + { + return OperationResult.Error(FunicularSwitch.Test.ErrorFactory.FromException(e)); + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Map( + global::System.Func> map) + { + switch (this) + { + case Ok_ ok: + try + { + return OperationResult.Ok(await map(ok.Value).ConfigureAwait(false)); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - return OperationResult.Error(FunicularSwitch.Test.ErrorFactory.FromException(e)); - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - //createGenericResultConversions - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T? GetValueOrDefault() - => Match( - v => (T?)v, - _ => default - ); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T GetValueOrDefault( global::System.Func defaultValue) - => Match( - v => v, - _ => defaultValue() - ); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T GetValueOrDefault(T defaultValue) - => Match( - v => v, - _ => defaultValue - ); - - [global::System.Diagnostics.DebuggerStepThrough] - public T GetValueOrThrow() - => Match( - v => v, - details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); - - - public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); - - - public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); - global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); - - public sealed partial class Ok_ : OperationResult - { - public T Value { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Ok_(T value) => Value = value; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override MyError? GetErrorOrDefault() => null; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Ok_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Ok_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); - } - - public sealed partial class Error_ : OperationResult - { - public MyError Details { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Error_(MyError details) => Details = details; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public OperationResult.Error_ Convert() => new OperationResult.Error_(Details); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override MyError? GetErrorOrDefault() => Details; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Error_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return Equals(Details, other.Details); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Error_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); - } - } - - public readonly partial struct OperationResultError : global::System.IEquatable - { - readonly MyError _details; - - internal OperationResultError(MyError details) => _details = details; - - [global::System.Diagnostics.Contracts.Pure] - public OperationResult WithOk() => OperationResult.Error(_details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(OperationResultError other) => _details.Equals(other._details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) => obj is OperationResultError other && Equals(other); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => _details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(OperationResultError left, OperationResultError right) => left.Equals(right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(OperationResultError left, OperationResultError right) => !left.Equals(right); - } - - public static partial class OperationResultExtension - { - #region bind - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => (await result.ConfigureAwait(false)).Bind(bind); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func>> bind) - => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); - - #endregion - - #region map - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func map) - => (await result.ConfigureAwait(false)).Map(map); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => Bind(result, async v => OperationResult.Ok(await bind(v).ConfigureAwait(false))); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult MapError(this OperationResult result, global::System.Func mapError) - { - if (result is OperationResult.Error_ e) - return OperationResult.Error(mapError(e.Details)); - return result; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); - - #endregion - - #region match - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func> error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func ok, - global::System.Func error) - => (await result.ConfigureAwait(false)).Match(ok, error); - - #endregion - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResult Flatten(this OperationResult> result) => result.Bind(r => r); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult As(this OperationResult result, global::System.Func errorTIsNotT1) => - result.Bind(r => - { - if (r is T1 converted) - return converted; - return OperationResult.Error(errorTIsNotT1()); - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult As(this OperationResult result, global::System.Func errorIsNotT1) => - result.As(errorIsNotT1); - - #region query-expression pattern - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult Select(this OperationResult result, global::System.Func selector) => result.Map(selector); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult SelectMany(this OperationResult result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this OperationResult result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - - #endregion - - //createGenericResultConversionExtensions - } + { + return OperationResult.Error(FunicularSwitch.Test.ErrorFactory.FromException(e)); + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + //createGenericResultConversions + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T? GetValueOrDefault() + => Match( + v => (T?)v, + _ => default + ); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T GetValueOrDefault( global::System.Func defaultValue) + => Match( + v => v, + _ => defaultValue() + ); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T GetValueOrDefault(T defaultValue) + => Match( + v => v, + _ => defaultValue + ); + + [global::System.Diagnostics.DebuggerStepThrough] + public T GetValueOrThrow() + => Match( + v => v, + details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); + + + public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); + + + public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); + global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); + + public sealed partial class Ok_ : OperationResult + { + public T Value { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Ok_(T value) => Value = value; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override MyError? GetErrorOrDefault() => null; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Ok_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Ok_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); + } + + public sealed partial class Error_ : OperationResult + { + public MyError Details { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Error_(MyError details) => Details = details; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public OperationResult.Error_ Convert() => new OperationResult.Error_(Details); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override MyError? GetErrorOrDefault() => Details; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Error_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Equals(Details, other.Details); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Error_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); + } + } + + public readonly partial struct OperationResultError : global::System.IEquatable + { + readonly MyError _details; + + internal OperationResultError(MyError details) => _details = details; + + [global::System.Diagnostics.Contracts.Pure] + public OperationResult WithOk() => OperationResult.Error(_details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(OperationResultError other) => _details.Equals(other._details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) => obj is OperationResultError other && Equals(other); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => _details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(OperationResultError left, OperationResultError right) => left.Equals(right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(OperationResultError left, OperationResultError right) => !left.Equals(right); + } + + public static partial class OperationResultExtension + { + #region bind + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => (await result.ConfigureAwait(false)).Bind(bind); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func>> bind) + => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); + + #endregion + + #region map + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func map) + => (await result.ConfigureAwait(false)).Map(map); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => Bind(result, async v => OperationResult.Ok(await bind(v).ConfigureAwait(false))); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult MapError(this OperationResult result, global::System.Func mapError) + { + if (result is OperationResult.Error_ e) + return OperationResult.Error(mapError(e.Details)); + return result; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); + + #endregion + + #region match + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func> error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func ok, + global::System.Func error) + => (await result.ConfigureAwait(false)).Match(ok, error); + + #endregion + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResult Flatten(this OperationResult> result) => result.Bind(r => r); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult As(this OperationResult result, global::System.Func errorTIsNotT1) => + result.Bind(r => + { + if (r is T1 converted) + return converted; + return OperationResult.Error(errorTIsNotT1()); + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult As(this OperationResult result, global::System.Func errorIsNotT1) => + result.As(errorIsNotT1); + + #region query-expression pattern + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult Select(this OperationResult result, global::System.Func selector) => result.Map(selector); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult SelectMany(this OperationResult result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this OperationResult result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + + #endregion + + //createGenericResultConversionExtensions + } } namespace FunicularSwitch.Test.Extensions { - public static partial class OperationResultExtension - { - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable items, - global::System.Func> choose, - global::System.Action onError) - => items - .Select(i => choose(i)) - .Choose(onError); - - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable> results, - global::System.Action onError) - => results - .Where(r => - r.Match(_ => true, error => - { - onError(error); - return false; - })) - .Select(r => r.GetValueOrThrow()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult As(this object? item, global::System.Func error) => - !(item is T t) ? OperationResult.Error(error()) : t; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult NotNull(this T? item, global::System.Func error) => - item ?? OperationResult.Error(error()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult NotNullOrEmpty(this string? s, global::System.Func error) - => string.IsNullOrEmpty(s) ? OperationResult.Error(error()) : s!; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult NotNullOrWhiteSpace(this string? s, global::System.Func error) - => string.IsNullOrWhiteSpace(s) ? OperationResult.Error(error()) : s!; - - - public static OperationResult First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => - candidates - .FirstOrDefault(i => predicate(i)) - .NotNull(noMatch); - } + public static partial class OperationResultExtension + { + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable items, + global::System.Func> choose, + global::System.Action onError) + => items + .Select(i => choose(i)) + .Choose(onError); + + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable> results, + global::System.Action onError) + => results + .Where(r => + r.Match(_ => true, error => + { + onError(error); + return false; + })) + .Select(r => r.GetValueOrThrow()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult As(this object? item, global::System.Func error) => + !(item is T t) ? OperationResult.Error(error()) : t; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult NotNull(this T? item, global::System.Func error) => + item ?? OperationResult.Error(error()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult NotNullOrEmpty(this string? s, global::System.Func error) + => string.IsNullOrEmpty(s) ? OperationResult.Error(error()) : s!; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult NotNullOrWhiteSpace(this string? s, global::System.Func error) + => string.IsNullOrWhiteSpace(s) ? OperationResult.Error(error()) : s!; + + + public static OperationResult First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => + candidates + .FirstOrDefault(i => predicate(i)) + .NotNull(noMatch); + } #pragma warning restore 1591 } diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_with_merge#FunicularSwitch.Test.OperationResultWithMerge.g.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_with_merge#FunicularSwitch.Test.OperationResultWithMerge.g.verified.cs index 8742a912..83ccc539 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_with_merge#FunicularSwitch.Test.OperationResultWithMerge.g.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_error_type_with_merge#FunicularSwitch.Test.OperationResultWithMerge.g.verified.cs @@ -116,28 +116,28 @@ public abstract partial class OperationResult public static partial class OperationResultExtension { - - public static OperationResult> Map(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func map) => + + public static OperationResult> Map(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func map) => results.Select(r => r.Map(map)).Aggregate(); - - public static OperationResult> Bind(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func> bind) => + + public static OperationResult> Bind(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func> bind) => results.Select(r => r.Bind(bind)).Aggregate(); - - public static OperationResult> Bind(this OperationResult result, - global::System.Func>> bindMany) => + + public static OperationResult> Bind(this OperationResult result, + global::System.Func>> bindMany) => result.Map(ok => bindMany(ok).Aggregate()).Flatten(); - - public static OperationResult Bind(this global::System.Collections.Generic.IEnumerable> results, - global::System.Func, OperationResult> bind) => + + public static OperationResult Bind(this global::System.Collections.Generic.IEnumerable> results, + global::System.Func, OperationResult> bind) => results.Aggregate().Bind(bind); - - public static OperationResult> Aggregate(this global::System.Collections.Generic.IEnumerable> results) + + public static OperationResult> Aggregate(this global::System.Collections.Generic.IEnumerable> results) { var isError = false; MyError aggregated = default!; @@ -160,25 +160,25 @@ public static OperationResult Bind(this global::System.Collections.Ge } - public static async global::System.Threading.Tasks.Task>> Aggregate( + public static async global::System.Threading.Tasks.Task>> Aggregate( this global::System.Threading.Tasks.Task>> results) => (await results.ConfigureAwait(false)) .Aggregate(); - - public static async global::System.Threading.Tasks.Task>> Aggregate( + + public static async global::System.Threading.Tasks.Task>> Aggregate( this global::System.Collections.Generic.IEnumerable>> results) => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) .Aggregate(); - - public static async global::System.Threading.Tasks.Task>> AggregateMany( + + public static async global::System.Threading.Tasks.Task>> AggregateMany( this global::System.Collections.Generic.IEnumerable>>> results) => (await global::System.Threading.Tasks.Task.WhenAll(results.Select(e => e)).ConfigureAwait(false)) .SelectMany(e => e) .Aggregate(); - + public static OperationResult<(T1, T2)> Aggregate(this OperationResult r1, OperationResult r2) => Aggregate(r1, r2, (v1, v2) => (v1, v2)); @@ -403,8 +403,8 @@ public static OperationResult Aggregate FirstOk(this global::System.Collections.Generic.IEnumerable> results, global::System.Func onEmpty) + + public static OperationResult FirstOk(this global::System.Collections.Generic.IEnumerable> results, global::System.Func onEmpty) { var errors = new global::System.Collections.Generic.List(); foreach (var result in results) @@ -422,21 +422,21 @@ public static OperationResult FirstOk(this global::System.Collections.Gene } - public static async global::System.Threading.Tasks.Task>> Aggregate( + public static async global::System.Threading.Tasks.Task>> Aggregate( this global::System.Collections.Generic.IEnumerable>> results, int maxDegreeOfParallelism) => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) .Aggregate(); - - public static async global::System.Threading.Tasks.Task>> AggregateMany( + + public static async global::System.Threading.Tasks.Task>> AggregateMany( this global::System.Collections.Generic.IEnumerable>>> results, int maxDegreeOfParallelism) => (await results.SelectAsync(e => e, maxDegreeOfParallelism).ConfigureAwait(false)) .SelectMany(e => e) .Aggregate(); - static async global::System.Threading.Tasks.Task SelectAsync(this global::System.Collections.Generic.IEnumerable items, global::System.Func> selector, int maxDegreeOfParallelism) + static async global::System.Threading.Tasks.Task SelectAsync(this global::System.Collections.Generic.IEnumerable items, global::System.Func> selector, int maxDegreeOfParallelism) { using (var throttler = new global::System.Threading.SemaphoreSlim(maxDegreeOfParallelism, maxDegreeOfParallelism)) { @@ -458,39 +458,39 @@ public static OperationResult FirstOk(this global::System.Collections.Gene } - public static OperationResult> AllOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate) => + public static OperationResult> AllOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate) => candidates .Select(c => c.Validate(validate)) .Aggregate(); - - public static OperationResult> AllOk(this global::System.Collections.Generic.IEnumerable> candidates, - global::System.Func> validate) => + + public static OperationResult> AllOk(this global::System.Collections.Generic.IEnumerable> candidates, + global::System.Func> validate) => candidates .Bind(items => items.AllOk(validate)); - - public static OperationResult Validate(this OperationResult item, global::System.Func> validate) => item.Bind(i => i.Validate(validate)); + + public static OperationResult Validate(this OperationResult item, global::System.Func> validate) => item.Bind(i => i.Validate(validate)); - - public static OperationResult Validate(this T item, global::System.Func> validate) + + public static OperationResult Validate(this T item, global::System.Func> validate) { - try - { - var errors = validate(item).ToList(); - return errors.Count > 0 ? OperationResult.Error(MergeErrors(errors)) : item; - } - // ReSharper disable once RedundantCatchClause + try + { + var errors = validate(item).ToList(); + return errors.Count > 0 ? OperationResult.Error(MergeErrors(errors)) : item; + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - return OperationResult.Error(FunicularSwitch.Test.ErrorFactory.FromException(e)); - } + { + return OperationResult.Error(FunicularSwitch.Test.ErrorFactory.FromException(e)); + } } - public static OperationResult FirstOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate, global::System.Func onEmpty) => + public static OperationResult FirstOk(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func> validate, global::System.Func onEmpty) => candidates .Select(r => r.Validate(validate)) .FirstOk(onEmpty); diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_internal_result_type#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_internal_result_type#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_internal_result_type#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_internal_result_type#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_internal_result_type#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_internal_result_type#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_internal_result_type#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_internal_result_type#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_internal_result_type#FunicularSwitch.Test.OperationResult.g.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_internal_result_type#FunicularSwitch.Test.OperationResult.g.verified.cs index a7c15725..1049ead5 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_internal_result_type#FunicularSwitch.Test.OperationResult.g.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_internal_result_type#FunicularSwitch.Test.OperationResult.g.verified.cs @@ -7,605 +7,605 @@ namespace FunicularSwitch.Test { #pragma warning disable 1591 - abstract partial class OperationResult - { - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResult Error(MyError details) => new OperationResult.Error_(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResultError Error(MyError details) => new(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResult Ok(T value) => new OperationResult.Ok_(value); - public bool IsError => GetType().GetGenericTypeDefinition() == typeof(OperationResult<>.Error_); - public bool IsOk => !IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public abstract MyError? GetErrorOrDefault(); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult Try( global::System.Func action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - } - - abstract partial class OperationResult : OperationResult, global::System.Collections.Generic.IEnumerable - { - - [global::System.Diagnostics.DebuggerNonUserCode] - - public new static OperationResult Error(MyError message) => Error(message); - - [global::System.Diagnostics.DebuggerNonUserCode] - - public static OperationResult Ok(T value) => Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator OperationResult(T value) => OperationResult.Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator OperationResult(OperationResultError myResultError) => myResultError.WithOk(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator true(OperationResult result) => result.IsOk; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator false(OperationResult result) => result.IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !(OperationResult result) => result.IsError; - - //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode - bool Equals(OperationResult other) => this switch - { - Ok_ ok => ok.Equals((object)other), - Error_ error => error.Equals((object)other), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") - }; - - public override int GetHashCode() => this switch - { - Ok_ ok => ok.GetHashCode(), - Error_ error => error.GetHashCode(), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") - }; - - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != this.GetType()) return false; - return Equals((OperationResult)obj); - } - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(OperationResult? left, OperationResult? right) => Equals(left, right); - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(OperationResult? left, OperationResult? right) => !Equals(left, right); - - [global::System.Diagnostics.DebuggerStepThrough] - public void Match( global::System.Action ok, global::System.Action? error = null) => Match( - v => - { - ok.Invoke(v); - return 42; - }, - err => - { - error?.Invoke(err); - return 42; - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T1 Match( global::System.Func ok, global::System.Func error) - { - return this switch - { - Ok_ okOperationResult => ok(okOperationResult.Value), - Error_ errorOperationResult => error(errorOperationResult.Details), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) - { - return this switch - { - Ok_ okOperationResult => await ok(okOperationResult.Value).ConfigureAwait(false), - Error_ errorOperationResult => await error(errorOperationResult.Details).ConfigureAwait(false), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => - Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); - - [global::System.Diagnostics.DebuggerStepThrough] - public async global::System.Threading.Tasks.Task Match( global::System.Func ok) - { - if (this is Ok_ okOperationResult) await ok(okOperationResult.Value).ConfigureAwait(false); - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public T Match( global::System.Func error) => Match(v => v, error); - - [global::System.Diagnostics.DebuggerStepThrough] - - public OperationResult Bind( global::System.Func> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return bind(ok.Value); - } - // ReSharper disable once RedundantCatchClause + abstract partial class OperationResult + { + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResult Error(MyError details) => new OperationResult.Error_(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResultError Error(MyError details) => new(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResult Ok(T value) => new OperationResult.Ok_(value); + public bool IsError => GetType().GetGenericTypeDefinition() == typeof(OperationResult<>.Error_); + public bool IsOk => !IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public abstract MyError? GetErrorOrDefault(); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult Try( global::System.Func action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + } + + abstract partial class OperationResult : OperationResult, global::System.Collections.Generic.IEnumerable + { + + [global::System.Diagnostics.DebuggerNonUserCode] + + public new static OperationResult Error(MyError message) => Error(message); + + [global::System.Diagnostics.DebuggerNonUserCode] + + public static OperationResult Ok(T value) => Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator OperationResult(T value) => OperationResult.Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator OperationResult(OperationResultError myResultError) => myResultError.WithOk(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator true(OperationResult result) => result.IsOk; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator false(OperationResult result) => result.IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !(OperationResult result) => result.IsError; + + //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode + bool Equals(OperationResult other) => this switch + { + Ok_ ok => ok.Equals((object)other), + Error_ error => error.Equals((object)other), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") + }; + + public override int GetHashCode() => this switch + { + Ok_ ok => ok.GetHashCode(), + Error_ error => error.GetHashCode(), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(OperationResult)}") + }; + + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((OperationResult)obj); + } + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(OperationResult? left, OperationResult? right) => Equals(left, right); + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(OperationResult? left, OperationResult? right) => !Equals(left, right); + + [global::System.Diagnostics.DebuggerStepThrough] + public void Match( global::System.Action ok, global::System.Action? error = null) => Match( + v => + { + ok.Invoke(v); + return 42; + }, + err => + { + error?.Invoke(err); + return 42; + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T1 Match( global::System.Func ok, global::System.Func error) + { + return this switch + { + Ok_ okOperationResult => ok(okOperationResult.Value), + Error_ errorOperationResult => error(errorOperationResult.Details), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) + { + return this switch + { + Ok_ okOperationResult => await ok(okOperationResult.Value).ConfigureAwait(false), + Error_ errorOperationResult => await error(errorOperationResult.Details).ConfigureAwait(false), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => + Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); + + [global::System.Diagnostics.DebuggerStepThrough] + public async global::System.Threading.Tasks.Task Match( global::System.Func ok) + { + if (this is Ok_ okOperationResult) await ok(okOperationResult.Value).ConfigureAwait(false); + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public T Match( global::System.Func error) => Match(v => v, error); + + [global::System.Diagnostics.DebuggerStepThrough] + + public OperationResult Bind( global::System.Func> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return bind(ok.Value); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return await bind(ok.Value).ConfigureAwait(false); - } - // ReSharper disable once RedundantCatchClause + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return await bind(ok.Value).ConfigureAwait(false); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public OperationResult Map( global::System.Func map) - { - switch (this) - { - case Ok_ ok: - try - { - return OperationResult.Ok(map(ok.Value)); - } - // ReSharper disable once RedundantCatchClause + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public OperationResult Map( global::System.Func map) + { + switch (this) + { + case Ok_ ok: + try + { + return OperationResult.Ok(map(ok.Value)); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Map( - global::System.Func> map) - { - switch (this) - { - case Ok_ ok: - try - { - return OperationResult.Ok(await map(ok.Value).ConfigureAwait(false)); - } - // ReSharper disable once RedundantCatchClause + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Map( + global::System.Func> map) + { + switch (this) + { + case Ok_ ok: + try + { + return OperationResult.Ok(await map(ok.Value).ConfigureAwait(false)); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - //createGenericResultConversions - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T? GetValueOrDefault() - => Match( - v => (T?)v, - _ => default - ); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T GetValueOrDefault( global::System.Func defaultValue) - => Match( - v => v, - _ => defaultValue() - ); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T GetValueOrDefault(T defaultValue) - => Match( - v => v, - _ => defaultValue - ); - - [global::System.Diagnostics.DebuggerStepThrough] - public T GetValueOrThrow() - => Match( - v => v, - details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); - - - public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); - - - public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); - global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); - - public sealed partial class Ok_ : OperationResult - { - public T Value { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Ok_(T value) => Value = value; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override MyError? GetErrorOrDefault() => null; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Ok_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Ok_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); - } - - public sealed partial class Error_ : OperationResult - { - public MyError Details { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Error_(MyError details) => Details = details; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public OperationResult.Error_ Convert() => new OperationResult.Error_(Details); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override MyError? GetErrorOrDefault() => Details; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Error_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return Equals(Details, other.Details); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Error_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); - } - } - - readonly partial struct OperationResultError : global::System.IEquatable - { - readonly MyError _details; - - internal OperationResultError(MyError details) => _details = details; - - [global::System.Diagnostics.Contracts.Pure] - public OperationResult WithOk() => OperationResult.Error(_details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(OperationResultError other) => _details.Equals(other._details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) => obj is OperationResultError other && Equals(other); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => _details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(OperationResultError left, OperationResultError right) => left.Equals(right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(OperationResultError left, OperationResultError right) => !left.Equals(right); - } - - static partial class OperationResultExtension - { - #region bind - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => (await result.ConfigureAwait(false)).Bind(bind); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func>> bind) - => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); - - #endregion - - #region map - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func map) - => (await result.ConfigureAwait(false)).Map(map); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => Bind(result, async v => OperationResult.Ok(await bind(v).ConfigureAwait(false))); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult MapError(this OperationResult result, global::System.Func mapError) - { - if (result is OperationResult.Error_ e) - return OperationResult.Error(mapError(e.Details)); - return result; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); - - #endregion - - #region match - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func> error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func ok, - global::System.Func error) - => (await result.ConfigureAwait(false)).Match(ok, error); - - #endregion - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static OperationResult Flatten(this OperationResult> result) => result.Bind(r => r); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult As(this OperationResult result, global::System.Func errorTIsNotT1) => - result.Bind(r => - { - if (r is T1 converted) - return converted; - return OperationResult.Error(errorTIsNotT1()); - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult As(this OperationResult result, global::System.Func errorIsNotT1) => - result.As(errorIsNotT1); - - #region query-expression pattern - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult Select(this OperationResult result, global::System.Func selector) => result.Map(selector); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult SelectMany(this OperationResult result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this OperationResult result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - - #endregion - - //createGenericResultConversionExtensions - } + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + //createGenericResultConversions + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T? GetValueOrDefault() + => Match( + v => (T?)v, + _ => default + ); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T GetValueOrDefault( global::System.Func defaultValue) + => Match( + v => v, + _ => defaultValue() + ); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T GetValueOrDefault(T defaultValue) + => Match( + v => v, + _ => defaultValue + ); + + [global::System.Diagnostics.DebuggerStepThrough] + public T GetValueOrThrow() + => Match( + v => v, + details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); + + + public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); + + + public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); + global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); + + public sealed partial class Ok_ : OperationResult + { + public T Value { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Ok_(T value) => Value = value; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override MyError? GetErrorOrDefault() => null; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Ok_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Ok_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); + } + + public sealed partial class Error_ : OperationResult + { + public MyError Details { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Error_(MyError details) => Details = details; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public OperationResult.Error_ Convert() => new OperationResult.Error_(Details); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override MyError? GetErrorOrDefault() => Details; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Error_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Equals(Details, other.Details); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Error_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); + } + } + + readonly partial struct OperationResultError : global::System.IEquatable + { + readonly MyError _details; + + internal OperationResultError(MyError details) => _details = details; + + [global::System.Diagnostics.Contracts.Pure] + public OperationResult WithOk() => OperationResult.Error(_details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(OperationResultError other) => _details.Equals(other._details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) => obj is OperationResultError other && Equals(other); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => _details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(OperationResultError left, OperationResultError right) => left.Equals(right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(OperationResultError left, OperationResultError right) => !left.Equals(right); + } + + static partial class OperationResultExtension + { + #region bind + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => (await result.ConfigureAwait(false)).Bind(bind); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func>> bind) + => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); + + #endregion + + #region map + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func map) + => (await result.ConfigureAwait(false)).Map(map); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => Bind(result, async v => OperationResult.Ok(await bind(v).ConfigureAwait(false))); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult MapError(this OperationResult result, global::System.Func mapError) + { + if (result is OperationResult.Error_ e) + return OperationResult.Error(mapError(e.Details)); + return result; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); + + #endregion + + #region match + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func> error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func ok, + global::System.Func error) + => (await result.ConfigureAwait(false)).Match(ok, error); + + #endregion + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static OperationResult Flatten(this OperationResult> result) => result.Bind(r => r); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult As(this OperationResult result, global::System.Func errorTIsNotT1) => + result.Bind(r => + { + if (r is T1 converted) + return converted; + return OperationResult.Error(errorTIsNotT1()); + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult As(this OperationResult result, global::System.Func errorIsNotT1) => + result.As(errorIsNotT1); + + #region query-expression pattern + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult Select(this OperationResult result, global::System.Func selector) => result.Map(selector); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult SelectMany(this OperationResult result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this OperationResult result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + + #endregion + + //createGenericResultConversionExtensions + } } namespace FunicularSwitch.Test.Extensions { - static partial class OperationResultExtension - { - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable items, - global::System.Func> choose, - global::System.Action onError) - => items - .Select(i => choose(i)) - .Choose(onError); - - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable> results, - global::System.Action onError) - => results - .Where(r => - r.Match(_ => true, error => - { - onError(error); - return false; - })) - .Select(r => r.GetValueOrThrow()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult As(this object? item, global::System.Func error) => - !(item is T t) ? OperationResult.Error(error()) : t; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult NotNull(this T? item, global::System.Func error) => - item ?? OperationResult.Error(error()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult NotNullOrEmpty(this string? s, global::System.Func error) - => string.IsNullOrEmpty(s) ? OperationResult.Error(error()) : s!; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static OperationResult NotNullOrWhiteSpace(this string? s, global::System.Func error) - => string.IsNullOrWhiteSpace(s) ? OperationResult.Error(error()) : s!; - - - public static OperationResult First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => - candidates - .FirstOrDefault(i => predicate(i)) - .NotNull(noMatch); - } + static partial class OperationResultExtension + { + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable items, + global::System.Func> choose, + global::System.Action onError) + => items + .Select(i => choose(i)) + .Choose(onError); + + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable> results, + global::System.Action onError) + => results + .Where(r => + r.Match(_ => true, error => + { + onError(error); + return false; + })) + .Select(r => r.GetValueOrThrow()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult As(this object? item, global::System.Func error) => + !(item is T t) ? OperationResult.Error(error()) : t; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult NotNull(this T? item, global::System.Func error) => + item ?? OperationResult.Error(error()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult NotNullOrEmpty(this string? s, global::System.Func error) + => string.IsNullOrEmpty(s) ? OperationResult.Error(error()) : s!; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static OperationResult NotNullOrWhiteSpace(this string? s, global::System.Func error) + => string.IsNullOrWhiteSpace(s) ? OperationResult.Error(error()) : s!; + + + public static OperationResult First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => + candidates + .FirstOrDefault(i => predicate(i)) + .NotNull(noMatch); + } #pragma warning restore 1591 } diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_result_type_without_namespace#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_result_type_without_namespace#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_result_type_without_namespace#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_result_type_without_namespace#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_result_type_without_namespace#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_result_type_without_namespace#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_result_type_without_namespace#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_result_type_generator.For_result_type_without_namespace#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.ForUnionType_WhenReferencingJetBrainsAnnotationsPackage_AddsInstantHandleAttributes#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.ForUnionType_WhenReferencingJetBrainsAnnotationsPackage_AddsInstantHandleAttributes#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.ForUnionType_WhenReferencingJetBrainsAnnotationsPackage_AddsInstantHandleAttributes#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.ForUnionType_WhenReferencingJetBrainsAnnotationsPackage_AddsInstantHandleAttributes#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.ForUnionType_WhenReferencingJetBrainsAnnotationsPackage_AddsInstantHandleAttributes#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.ForUnionType_WhenReferencingJetBrainsAnnotationsPackage_AddsInstantHandleAttributes#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.ForUnionType_WhenReferencingJetBrainsAnnotationsPackage_AddsInstantHandleAttributes#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.ForUnionType_WhenReferencingJetBrainsAnnotationsPackage_AddsInstantHandleAttributes#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.ForUnionType_WithNullableParameters_StaticFactoriesHaveNullableParametersToo#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.ForUnionType_WithNullableParameters_StaticFactoriesHaveNullableParametersToo#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.ForUnionType_WithNullableParameters_StaticFactoriesHaveNullableParametersToo#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.ForUnionType_WithNullableParameters_StaticFactoriesHaveNullableParametersToo#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.ForUnionType_WithNullableParameters_StaticFactoriesHaveNullableParametersToo#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.ForUnionType_WithNullableParameters_StaticFactoriesHaveNullableParametersToo#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.ForUnionType_WithNullableParameters_StaticFactoriesHaveNullableParametersToo#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.ForUnionType_WithNullableParameters_StaticFactoriesHaveNullableParametersToo#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_empty_namespace#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_empty_namespace#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_empty_namespace#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_empty_namespace#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_empty_namespace#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_empty_namespace#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_empty_namespace#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_empty_namespace#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_explicitly_internal_union_type#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_explicitly_internal_union_type#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_explicitly_internal_union_type#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_explicitly_internal_union_type#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_explicitly_internal_union_type#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_explicitly_internal_union_type#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_explicitly_internal_union_type#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_explicitly_internal_union_type#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_implicitly_internal_union_type#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_implicitly_internal_union_type#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_implicitly_internal_union_type#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_implicitly_internal_union_type#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_implicitly_internal_union_type#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_implicitly_internal_union_type#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_implicitly_internal_union_type#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_implicitly_internal_union_type#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_inaccessible_type#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_inaccessible_type#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_inaccessible_type#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_inaccessible_type#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_inaccessible_type#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_inaccessible_type#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_inaccessible_type#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_inaccessible_type#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_interface_union_type#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_interface_union_type#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_interface_union_type#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_interface_union_type#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_interface_union_type#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_interface_union_type#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_interface_union_type#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_interface_union_type#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_nested_record_union_type#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_nested_record_union_type#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_nested_record_union_type#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_nested_record_union_type#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_nested_record_union_type#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_nested_record_union_type#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_nested_record_union_type#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_nested_record_union_type#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_partial_record_union_type#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_partial_record_union_type#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_partial_record_union_type#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_partial_record_union_type#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_partial_record_union_type#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_partial_record_union_type#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_partial_record_union_type#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_partial_record_union_type#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_as_declared_case_order#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_as_declared_case_order#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_as_declared_case_order#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_as_declared_case_order#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_as_declared_case_order#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_as_declared_case_order#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_as_declared_case_order#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_as_declared_case_order#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_explicit_case_order#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_explicit_case_order#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_explicit_case_order#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_explicit_case_order#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_explicit_case_order#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_explicit_case_order#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_explicit_case_order#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_explicit_case_order#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_multi_level_concrete_derived_types#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_multi_level_concrete_derived_types#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_multi_level_concrete_derived_types#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_multi_level_concrete_derived_types#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_multi_level_concrete_derived_types#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_multi_level_concrete_derived_types#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_multi_level_concrete_derived_types#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_record_union_type_with_multi_level_concrete_derived_types#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_switchyard_union_type#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_switchyard_union_type#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_switchyard_union_type#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_switchyard_union_type#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_switchyard_union_type#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_switchyard_union_type#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_switchyard_union_type#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_switchyard_union_type#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_with_generic_base_class#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_with_generic_base_class#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_with_generic_base_class#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_with_generic_base_class#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_with_generic_base_class#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_with_generic_base_class#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_with_generic_base_class#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_with_generic_base_class#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_with_generic_base_class_and_type_constraints#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_with_generic_base_class_and_type_constraints#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_with_generic_base_class_and_type_constraints#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_with_generic_base_class_and_type_constraints#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_with_generic_base_class_and_type_constraints#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_with_generic_base_class_and_type_constraints#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_with_generic_base_class_and_type_constraints#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_with_generic_base_class_and_type_constraints#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_without_derived_types#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_without_derived_types#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_without_derived_types#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_without_derived_types#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_without_derived_types#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_without_derived_types#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_without_derived_types#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.For_union_type_without_derived_types#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_interface_union_type#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_interface_union_type#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_interface_union_type#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_interface_union_type#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_interface_union_type#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_interface_union_type#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_interface_union_type#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_interface_union_type#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_nested_internal_union_type#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_nested_internal_union_type#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_nested_internal_union_type#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_nested_internal_union_type#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_nested_internal_union_type#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_nested_internal_union_type#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_nested_internal_union_type#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_nested_internal_union_type#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_type_with_required_properties#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_type_with_required_properties#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_type_with_required_properties#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_type_with_required_properties#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_type_with_required_properties#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_type_with_required_properties#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_type_with_required_properties#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Static_factories_for_type_with_required_properties#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Support_structs_derived_from_derived_interface#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Support_structs_derived_from_derived_interface#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Support_structs_derived_from_derived_interface#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Support_structs_derived_from_derived_interface#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Support_structs_derived_from_derived_interface#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Support_structs_derived_from_derived_interface#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Support_structs_derived_from_derived_interface#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Support_structs_derived_from_derived_interface#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Support_structs_derived_from_interface#Attributes.g.00.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Support_structs_derived_from_interface#Attributes.g.00.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Support_structs_derived_from_interface#Attributes.g.00.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Support_structs_derived_from_interface#Attributes.g.00.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Support_structs_derived_from_interface#Attributes.g.02.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Support_structs_derived_from_interface#Attributes.g.02.verified.cs index ad0ffe0d..bd2cbc72 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Support_structs_derived_from_interface#Attributes.g.02.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/Run_union_type_generator.Support_structs_derived_from_interface#Attributes.g.02.verified.cs @@ -8,10 +8,10 @@ namespace FunicularSwitch.Generators [AttributeUsage(AttributeTargets.Enum)] sealed class ExtendedEnumAttribute : Attribute { - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; } - + enum EnumCaseOrder { Alphabetic, @@ -24,16 +24,16 @@ enum EnumCaseOrder [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumsAttribute : Attribute { - public Type AssemblySpecifier { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public Type AssemblySpecifier { get; } + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); + public ExtendEnumsAttribute() => AssemblySpecifier = typeof(ExtendEnumsAttribute); - public ExtendEnumsAttribute(Type assemblySpecifier) - { - AssemblySpecifier = assemblySpecifier; - } + public ExtendEnumsAttribute(Type assemblySpecifier) + { + AssemblySpecifier = assemblySpecifier; + } } /// @@ -42,21 +42,21 @@ public ExtendEnumsAttribute(Type assemblySpecifier) [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] class ExtendEnumAttribute : Attribute { - public Type Type { get; } + public Type Type { get; } - public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; + public EnumCaseOrder CaseOrder { get; set; } = EnumCaseOrder.AsDeclared; - public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; + public ExtensionAccessibility Accessibility { get; set; } = ExtensionAccessibility.Public; - public ExtendEnumAttribute(Type type) - { - Type = type; - } + public ExtendEnumAttribute(Type type) + { + Type = type; + } } enum ExtensionAccessibility { - Internal, - Public + Internal, + Public } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/TransformerGeneratorInteropTest.TransformResultType#Attributes.g.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/TransformerGeneratorInteropTest.TransformResultType#Attributes.g.verified.cs index 3336ef53..607c56e2 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/TransformerGeneratorInteropTest.TransformResultType#Attributes.g.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/TransformerGeneratorInteropTest.TransformResultType#Attributes.g.verified.cs @@ -5,10 +5,10 @@ // ReSharper disable once CheckNamespace namespace FunicularSwitch.Generators { - /// - /// Mark an abstract partial type with a single generic argument with the ResultType attribute. - /// This type from now on has Ok | Error semantics with map and bind operations. - /// + /// + /// Mark an abstract partial type with a single generic argument with the ResultType attribute. + /// This type from now on has Ok | Error semantics with map and bind operations. + /// [AttributeUsage(AttributeTargets.Class, Inherited = false)] sealed class ResultTypeAttribute : Attribute { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/TransformerGeneratorInteropTest.TransformResultType#FunicularSwitch.Test.MonadA.g.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/TransformerGeneratorInteropTest.TransformResultType#FunicularSwitch.Test.MonadA.g.verified.cs index d15a2d57..f4011f7f 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/TransformerGeneratorInteropTest.TransformResultType#FunicularSwitch.Test.MonadA.g.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/TransformerGeneratorInteropTest.TransformResultType#FunicularSwitch.Test.MonadA.g.verified.cs @@ -7,605 +7,605 @@ namespace FunicularSwitch.Test { #pragma warning disable 1591 - public abstract partial class MonadA - { - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static MonadA Error(String details) => new MonadA.Error_(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static MonadAError Error(String details) => new(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static MonadA Ok(T value) => new MonadA.Ok_(value); - public bool IsError => GetType().GetGenericTypeDefinition() == typeof(MonadA<>.Error_); - public bool IsOk => !IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public abstract String? GetErrorOrDefault(); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static MonadA Try( global::System.Func action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static MonadA Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - } - - public abstract partial class MonadA : MonadA, global::System.Collections.Generic.IEnumerable - { - - [global::System.Diagnostics.DebuggerNonUserCode] - - public new static MonadA Error(String message) => Error(message); - - [global::System.Diagnostics.DebuggerNonUserCode] - - public static MonadA Ok(T value) => Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator MonadA(T value) => MonadA.Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator MonadA(MonadAError myResultError) => myResultError.WithOk(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator true(MonadA result) => result.IsOk; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator false(MonadA result) => result.IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !(MonadA result) => result.IsError; - - //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode - bool Equals(MonadA other) => this switch - { - Ok_ ok => ok.Equals((object)other), - Error_ error => error.Equals((object)other), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(MonadA)}") - }; - - public override int GetHashCode() => this switch - { - Ok_ ok => ok.GetHashCode(), - Error_ error => error.GetHashCode(), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(MonadA)}") - }; - - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != this.GetType()) return false; - return Equals((MonadA)obj); - } - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(MonadA? left, MonadA? right) => Equals(left, right); - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(MonadA? left, MonadA? right) => !Equals(left, right); - - [global::System.Diagnostics.DebuggerStepThrough] - public void Match( global::System.Action ok, global::System.Action? error = null) => Match( - v => - { - ok.Invoke(v); - return 42; - }, - err => - { - error?.Invoke(err); - return 42; - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T1 Match( global::System.Func ok, global::System.Func error) - { - return this switch - { - Ok_ okMonadA => ok(okMonadA.Value), - Error_ errorMonadA => error(errorMonadA.Details), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) - { - return this switch - { - Ok_ okMonadA => await ok(okMonadA.Value).ConfigureAwait(false), - Error_ errorMonadA => await error(errorMonadA.Details).ConfigureAwait(false), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => - Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); - - [global::System.Diagnostics.DebuggerStepThrough] - public async global::System.Threading.Tasks.Task Match( global::System.Func ok) - { - if (this is Ok_ okMonadA) await ok(okMonadA.Value).ConfigureAwait(false); - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public T Match( global::System.Func error) => Match(v => v, error); - - [global::System.Diagnostics.DebuggerStepThrough] - - public MonadA Bind( global::System.Func> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return bind(ok.Value); - } - // ReSharper disable once RedundantCatchClause + public abstract partial class MonadA + { + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static MonadA Error(String details) => new MonadA.Error_(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static MonadAError Error(String details) => new(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static MonadA Ok(T value) => new MonadA.Ok_(value); + public bool IsError => GetType().GetGenericTypeDefinition() == typeof(MonadA<>.Error_); + public bool IsOk => !IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public abstract String? GetErrorOrDefault(); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static MonadA Try( global::System.Func action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static MonadA Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + } + + public abstract partial class MonadA : MonadA, global::System.Collections.Generic.IEnumerable + { + + [global::System.Diagnostics.DebuggerNonUserCode] + + public new static MonadA Error(String message) => Error(message); + + [global::System.Diagnostics.DebuggerNonUserCode] + + public static MonadA Ok(T value) => Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator MonadA(T value) => MonadA.Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator MonadA(MonadAError myResultError) => myResultError.WithOk(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator true(MonadA result) => result.IsOk; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator false(MonadA result) => result.IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !(MonadA result) => result.IsError; + + //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode + bool Equals(MonadA other) => this switch + { + Ok_ ok => ok.Equals((object)other), + Error_ error => error.Equals((object)other), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(MonadA)}") + }; + + public override int GetHashCode() => this switch + { + Ok_ ok => ok.GetHashCode(), + Error_ error => error.GetHashCode(), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(MonadA)}") + }; + + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((MonadA)obj); + } + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(MonadA? left, MonadA? right) => Equals(left, right); + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(MonadA? left, MonadA? right) => !Equals(left, right); + + [global::System.Diagnostics.DebuggerStepThrough] + public void Match( global::System.Action ok, global::System.Action? error = null) => Match( + v => + { + ok.Invoke(v); + return 42; + }, + err => + { + error?.Invoke(err); + return 42; + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T1 Match( global::System.Func ok, global::System.Func error) + { + return this switch + { + Ok_ okMonadA => ok(okMonadA.Value), + Error_ errorMonadA => error(errorMonadA.Details), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) + { + return this switch + { + Ok_ okMonadA => await ok(okMonadA.Value).ConfigureAwait(false), + Error_ errorMonadA => await error(errorMonadA.Details).ConfigureAwait(false), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => + Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); + + [global::System.Diagnostics.DebuggerStepThrough] + public async global::System.Threading.Tasks.Task Match( global::System.Func ok) + { + if (this is Ok_ okMonadA) await ok(okMonadA.Value).ConfigureAwait(false); + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public T Match( global::System.Func error) => Match(v => v, error); + + [global::System.Diagnostics.DebuggerStepThrough] + + public MonadA Bind( global::System.Func> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return bind(ok.Value); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return await bind(ok.Value).ConfigureAwait(false); - } - // ReSharper disable once RedundantCatchClause + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return await bind(ok.Value).ConfigureAwait(false); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public MonadA Map( global::System.Func map) - { - switch (this) - { - case Ok_ ok: - try - { - return MonadA.Ok(map(ok.Value)); - } - // ReSharper disable once RedundantCatchClause + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public MonadA Map( global::System.Func map) + { + switch (this) + { + case Ok_ ok: + try + { + return MonadA.Ok(map(ok.Value)); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Map( - global::System.Func> map) - { - switch (this) - { - case Ok_ ok: - try - { - return MonadA.Ok(await map(ok.Value).ConfigureAwait(false)); - } - // ReSharper disable once RedundantCatchClause + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Map( + global::System.Func> map) + { + switch (this) + { + case Ok_ ok: + try + { + return MonadA.Ok(await map(ok.Value).ConfigureAwait(false)); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - //createGenericResultConversions - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T? GetValueOrDefault() - => Match( - v => (T?)v, - _ => default - ); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T GetValueOrDefault( global::System.Func defaultValue) - => Match( - v => v, - _ => defaultValue() - ); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T GetValueOrDefault(T defaultValue) - => Match( - v => v, - _ => defaultValue - ); - - [global::System.Diagnostics.DebuggerStepThrough] - public T GetValueOrThrow() - => Match( - v => v, - details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); - - - public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); - - - public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); - global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); - - public sealed partial class Ok_ : MonadA - { - public T Value { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Ok_(T value) => Value = value; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override String? GetErrorOrDefault() => null; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Ok_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Ok_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); - } - - public sealed partial class Error_ : MonadA - { - public String Details { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Error_(String details) => Details = details; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public MonadA.Error_ Convert() => new MonadA.Error_(Details); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override String? GetErrorOrDefault() => Details; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Error_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return Equals(Details, other.Details); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Error_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); - } - } - - public readonly partial struct MonadAError : global::System.IEquatable - { - readonly String _details; - - internal MonadAError(String details) => _details = details; - - [global::System.Diagnostics.Contracts.Pure] - public MonadA WithOk() => MonadA.Error(_details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(MonadAError other) => _details.Equals(other._details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) => obj is MonadAError other && Equals(other); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => _details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(MonadAError left, MonadAError right) => left.Equals(right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(MonadAError left, MonadAError right) => !left.Equals(right); - } - - public static partial class MonadAExtension - { - #region bind - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => (await result.ConfigureAwait(false)).Bind(bind); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func>> bind) - => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); - - #endregion - - #region map - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func map) - => (await result.ConfigureAwait(false)).Map(map); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => Bind(result, async v => MonadA.Ok(await bind(v).ConfigureAwait(false))); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static MonadA MapError(this MonadA result, global::System.Func mapError) - { - if (result is MonadA.Error_ e) - return MonadA.Error(mapError(e.Details)); - return result; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); - - #endregion - - #region match - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func> error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func ok, - global::System.Func error) - => (await result.ConfigureAwait(false)).Match(ok, error); - - #endregion - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static MonadA Flatten(this MonadA> result) => result.Bind(r => r); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static MonadA As(this MonadA result, global::System.Func errorTIsNotT1) => - result.Bind(r => - { - if (r is T1 converted) - return converted; - return MonadA.Error(errorTIsNotT1()); - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static MonadA As(this MonadA result, global::System.Func errorIsNotT1) => - result.As(errorIsNotT1); - - #region query-expression pattern - - [global::System.Diagnostics.DebuggerStepThrough] - - public static MonadA Select(this MonadA result, global::System.Func selector) => result.Map(selector); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static MonadA SelectMany(this MonadA result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this MonadA result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - - #endregion - - //createGenericResultConversionExtensions - } + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + //createGenericResultConversions + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T? GetValueOrDefault() + => Match( + v => (T?)v, + _ => default + ); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T GetValueOrDefault( global::System.Func defaultValue) + => Match( + v => v, + _ => defaultValue() + ); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T GetValueOrDefault(T defaultValue) + => Match( + v => v, + _ => defaultValue + ); + + [global::System.Diagnostics.DebuggerStepThrough] + public T GetValueOrThrow() + => Match( + v => v, + details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); + + + public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); + + + public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); + global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); + + public sealed partial class Ok_ : MonadA + { + public T Value { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Ok_(T value) => Value = value; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override String? GetErrorOrDefault() => null; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Ok_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Ok_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); + } + + public sealed partial class Error_ : MonadA + { + public String Details { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Error_(String details) => Details = details; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public MonadA.Error_ Convert() => new MonadA.Error_(Details); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override String? GetErrorOrDefault() => Details; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Error_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Equals(Details, other.Details); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Error_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); + } + } + + public readonly partial struct MonadAError : global::System.IEquatable + { + readonly String _details; + + internal MonadAError(String details) => _details = details; + + [global::System.Diagnostics.Contracts.Pure] + public MonadA WithOk() => MonadA.Error(_details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(MonadAError other) => _details.Equals(other._details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) => obj is MonadAError other && Equals(other); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => _details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(MonadAError left, MonadAError right) => left.Equals(right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(MonadAError left, MonadAError right) => !left.Equals(right); + } + + public static partial class MonadAExtension + { + #region bind + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => (await result.ConfigureAwait(false)).Bind(bind); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func>> bind) + => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); + + #endregion + + #region map + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func map) + => (await result.ConfigureAwait(false)).Map(map); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => Bind(result, async v => MonadA.Ok(await bind(v).ConfigureAwait(false))); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static MonadA MapError(this MonadA result, global::System.Func mapError) + { + if (result is MonadA.Error_ e) + return MonadA.Error(mapError(e.Details)); + return result; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); + + #endregion + + #region match + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func> error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func ok, + global::System.Func error) + => (await result.ConfigureAwait(false)).Match(ok, error); + + #endregion + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static MonadA Flatten(this MonadA> result) => result.Bind(r => r); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static MonadA As(this MonadA result, global::System.Func errorTIsNotT1) => + result.Bind(r => + { + if (r is T1 converted) + return converted; + return MonadA.Error(errorTIsNotT1()); + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static MonadA As(this MonadA result, global::System.Func errorIsNotT1) => + result.As(errorIsNotT1); + + #region query-expression pattern + + [global::System.Diagnostics.DebuggerStepThrough] + + public static MonadA Select(this MonadA result, global::System.Func selector) => result.Map(selector); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static MonadA SelectMany(this MonadA result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this MonadA result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + + #endregion + + //createGenericResultConversionExtensions + } } namespace FunicularSwitch.Test.Extensions { - public static partial class MonadAExtension - { - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable items, - global::System.Func> choose, - global::System.Action onError) - => items - .Select(i => choose(i)) - .Choose(onError); - - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable> results, - global::System.Action onError) - => results - .Where(r => - r.Match(_ => true, error => - { - onError(error); - return false; - })) - .Select(r => r.GetValueOrThrow()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static MonadA As(this object? item, global::System.Func error) => - !(item is T t) ? MonadA.Error(error()) : t; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static MonadA NotNull(this T? item, global::System.Func error) => - item ?? MonadA.Error(error()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static MonadA NotNullOrEmpty(this string? s, global::System.Func error) - => string.IsNullOrEmpty(s) ? MonadA.Error(error()) : s!; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static MonadA NotNullOrWhiteSpace(this string? s, global::System.Func error) - => string.IsNullOrWhiteSpace(s) ? MonadA.Error(error()) : s!; - - - public static MonadA First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => - candidates - .FirstOrDefault(i => predicate(i)) - .NotNull(noMatch); - } + public static partial class MonadAExtension + { + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable items, + global::System.Func> choose, + global::System.Action onError) + => items + .Select(i => choose(i)) + .Choose(onError); + + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable> results, + global::System.Action onError) + => results + .Where(r => + r.Match(_ => true, error => + { + onError(error); + return false; + })) + .Select(r => r.GetValueOrThrow()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static MonadA As(this object? item, global::System.Func error) => + !(item is T t) ? MonadA.Error(error()) : t; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static MonadA NotNull(this T? item, global::System.Func error) => + item ?? MonadA.Error(error()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static MonadA NotNullOrEmpty(this string? s, global::System.Func error) + => string.IsNullOrEmpty(s) ? MonadA.Error(error()) : s!; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static MonadA NotNullOrWhiteSpace(this string? s, global::System.Func error) + => string.IsNullOrWhiteSpace(s) ? MonadA.Error(error()) : s!; + + + public static MonadA First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => + candidates + .FirstOrDefault(i => predicate(i)) + .NotNull(noMatch); + } #pragma warning restore 1591 } diff --git a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/TransformerGeneratorInteropTest.TransformResultType#FunicularSwitch.Test.MonadB.g.verified.cs b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/TransformerGeneratorInteropTest.TransformResultType#FunicularSwitch.Test.MonadB.g.verified.cs index 910ac438..d8fbf57c 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/TransformerGeneratorInteropTest.TransformResultType#FunicularSwitch.Test.MonadB.g.verified.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/Snapshots/TransformerGeneratorInteropTest.TransformResultType#FunicularSwitch.Test.MonadB.g.verified.cs @@ -7,605 +7,605 @@ namespace FunicularSwitch.Test { #pragma warning disable 1591 - public abstract partial class MonadB - { - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static MonadB Error(Int32 details) => new MonadB.Error_(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static MonadBError Error(Int32 details) => new(details); - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static MonadB Ok(T value) => new MonadB.Ok_(value); - public bool IsError => GetType().GetGenericTypeDefinition() == typeof(MonadB<>.Error_); - public bool IsOk => !IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public abstract Int32? GetErrorOrDefault(); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static MonadB Try( global::System.Func action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static MonadB Try( global::System.Func> action, global::System.Func formatError) - { - try - { - return action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) - { - try - { - return await action(); - } - catch (global::System.Exception e) - { - return Error(formatError(e)); - } - } - } - - public abstract partial class MonadB : MonadB, global::System.Collections.Generic.IEnumerable - { - - [global::System.Diagnostics.DebuggerNonUserCode] - - public new static MonadB Error(Int32 message) => Error(message); - - [global::System.Diagnostics.DebuggerNonUserCode] - - public static MonadB Ok(T value) => Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator MonadB(T value) => MonadB.Ok(value); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static implicit operator MonadB(MonadBError myResultError) => myResultError.WithOk(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator true(MonadB result) => result.IsOk; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator false(MonadB result) => result.IsError; - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !(MonadB result) => result.IsError; - - //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode - bool Equals(MonadB other) => this switch - { - Ok_ ok => ok.Equals((object)other), - Error_ error => error.Equals((object)other), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(MonadB)}") - }; - - public override int GetHashCode() => this switch - { - Ok_ ok => ok.GetHashCode(), - Error_ error => error.GetHashCode(), - _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(MonadB)}") - }; - - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != this.GetType()) return false; - return Equals((MonadB)obj); - } - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(MonadB? left, MonadB? right) => Equals(left, right); - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(MonadB? left, MonadB? right) => !Equals(left, right); - - [global::System.Diagnostics.DebuggerStepThrough] - public void Match( global::System.Action ok, global::System.Action? error = null) => Match( - v => - { - ok.Invoke(v); - return 42; - }, - err => - { - error?.Invoke(err); - return 42; - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T1 Match( global::System.Func ok, global::System.Func error) - { - return this switch - { - Ok_ okMonadB => ok(okMonadB.Value), - Error_ errorMonadB => error(errorMonadB.Details), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) - { - return this switch - { - Ok_ okMonadB => await ok(okMonadB.Value).ConfigureAwait(false), - Error_ errorMonadB => await error(errorMonadB.Details).ConfigureAwait(false), - _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") - }; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => - Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); - - [global::System.Diagnostics.DebuggerStepThrough] - public async global::System.Threading.Tasks.Task Match( global::System.Func ok) - { - if (this is Ok_ okMonadB) await ok(okMonadB.Value).ConfigureAwait(false); - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public T Match( global::System.Func error) => Match(v => v, error); - - [global::System.Diagnostics.DebuggerStepThrough] - - public MonadB Bind( global::System.Func> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return bind(ok.Value); - } - // ReSharper disable once RedundantCatchClause + public abstract partial class MonadB + { + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static MonadB Error(Int32 details) => new MonadB.Error_(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static MonadBError Error(Int32 details) => new(details); + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static MonadB Ok(T value) => new MonadB.Ok_(value); + public bool IsError => GetType().GetGenericTypeDefinition() == typeof(MonadB<>.Error_); + public bool IsOk => !IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public abstract Int32? GetErrorOrDefault(); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static MonadB Try( global::System.Func action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static MonadB Try( global::System.Func> action, global::System.Func formatError) + { + try + { + return action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Try( global::System.Func>> action, global::System.Func formatError) + { + try + { + return await action(); + } + catch (global::System.Exception e) + { + return Error(formatError(e)); + } + } + } + + public abstract partial class MonadB : MonadB, global::System.Collections.Generic.IEnumerable + { + + [global::System.Diagnostics.DebuggerNonUserCode] + + public new static MonadB Error(Int32 message) => Error(message); + + [global::System.Diagnostics.DebuggerNonUserCode] + + public static MonadB Ok(T value) => Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator MonadB(T value) => MonadB.Ok(value); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static implicit operator MonadB(MonadBError myResultError) => myResultError.WithOk(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator true(MonadB result) => result.IsOk; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator false(MonadB result) => result.IsError; + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !(MonadB result) => result.IsError; + + //just here to suppress warning, never called because all subtypes (Ok_, Error_) implement Equals and GetHashCode + bool Equals(MonadB other) => this switch + { + Ok_ ok => ok.Equals((object)other), + Error_ error => error.Equals((object)other), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(MonadB)}") + }; + + public override int GetHashCode() => this switch + { + Ok_ ok => ok.GetHashCode(), + Error_ error => error.GetHashCode(), + _ => throw new global::System.InvalidOperationException($"Unexpected type derived from {nameof(MonadB)}") + }; + + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != this.GetType()) return false; + return Equals((MonadB)obj); + } + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(MonadB? left, MonadB? right) => Equals(left, right); + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(MonadB? left, MonadB? right) => !Equals(left, right); + + [global::System.Diagnostics.DebuggerStepThrough] + public void Match( global::System.Action ok, global::System.Action? error = null) => Match( + v => + { + ok.Invoke(v); + return 42; + }, + err => + { + error?.Invoke(err); + return 42; + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T1 Match( global::System.Func ok, global::System.Func error) + { + return this switch + { + Ok_ okMonadB => ok(okMonadB.Value), + Error_ errorMonadB => error(errorMonadB.Details), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func> error) + { + return this switch + { + Ok_ okMonadB => await ok(okMonadB.Value).ConfigureAwait(false), + Error_ errorMonadB => await error(errorMonadB.Details).ConfigureAwait(false), + _ => throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}") + }; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public global::System.Threading.Tasks.Task Match( global::System.Func> ok, global::System.Func error) => + Match(ok, e => global::System.Threading.Tasks.Task.FromResult(error(e))); + + [global::System.Diagnostics.DebuggerStepThrough] + public async global::System.Threading.Tasks.Task Match( global::System.Func ok) + { + if (this is Ok_ okMonadB) await ok(okMonadB.Value).ConfigureAwait(false); + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public T Match( global::System.Func error) => Match(v => v, error); + + [global::System.Diagnostics.DebuggerStepThrough] + + public MonadB Bind( global::System.Func> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return bind(ok.Value); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) - { - switch (this) - { - case Ok_ ok: - try - { - return await bind(ok.Value).ConfigureAwait(false); - } - // ReSharper disable once RedundantCatchClause + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Bind( global::System.Func>> bind) + { + switch (this) + { + case Ok_ ok: + try + { + return await bind(ok.Value).ConfigureAwait(false); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public MonadB Map( global::System.Func map) - { - switch (this) - { - case Ok_ ok: - try - { - return MonadB.Ok(map(ok.Value)); - } - // ReSharper disable once RedundantCatchClause + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public MonadB Map( global::System.Func map) + { + switch (this) + { + case Ok_ ok: + try + { + return MonadB.Ok(map(ok.Value)); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public async global::System.Threading.Tasks.Task> Map( - global::System.Func> map) - { - switch (this) - { - case Ok_ ok: - try - { - return MonadB.Ok(await map(ok.Value).ConfigureAwait(false)); - } - // ReSharper disable once RedundantCatchClause + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public async global::System.Threading.Tasks.Task> Map( + global::System.Func> map) + { + switch (this) + { + case Ok_ ok: + try + { + return MonadB.Ok(await map(ok.Value).ConfigureAwait(false)); + } + // ReSharper disable once RedundantCatchClause #pragma warning disable CS0168 // Variable is declared but never used - catch (global::System.Exception e) + catch (global::System.Exception e) #pragma warning restore CS0168 // Variable is declared but never used - { - throw; //createGenericErrorResult - } - case Error_ error: - return error.Convert(); - default: - throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); - } - } - - //createGenericResultConversions - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T? GetValueOrDefault() - => Match( - v => (T?)v, - _ => default - ); - - [global::System.Diagnostics.DebuggerStepThrough] - - public T GetValueOrDefault( global::System.Func defaultValue) - => Match( - v => v, - _ => defaultValue() - ); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public T GetValueOrDefault(T defaultValue) - => Match( - v => v, - _ => defaultValue - ); - - [global::System.Diagnostics.DebuggerStepThrough] - public T GetValueOrThrow() - => Match( - v => v, - details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); - - - public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); - - - public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); - global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); - - public sealed partial class Ok_ : MonadB - { - public T Value { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Ok_(T value) => Value = value; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override Int32? GetErrorOrDefault() => null; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Ok_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Ok_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); - } - - public sealed partial class Error_ : MonadB - { - public Int32 Details { get; } - - [global::System.Diagnostics.DebuggerStepThrough] - public Error_(Int32 details) => Details = details; - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public MonadB.Error_ Convert() => new MonadB.Error_(Details); - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public override Int32? GetErrorOrDefault() => Details; - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(Error_? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - return Equals(Details, other.Details); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - return obj is Error_ other && Equals(other); - } - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => Details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); - } - } - - public readonly partial struct MonadBError : global::System.IEquatable - { - readonly Int32 _details; - - internal MonadBError(Int32 details) => _details = details; - - [global::System.Diagnostics.Contracts.Pure] - public MonadB WithOk() => MonadB.Error(_details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public bool Equals(MonadBError other) => _details.Equals(other._details); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override bool Equals(object? obj) => obj is MonadBError other && Equals(other); - - [global::System.Diagnostics.Contracts.PureAttribute] - public override int GetHashCode() => _details.GetHashCode(); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator ==(MonadBError left, MonadBError right) => left.Equals(right); - - [global::System.Diagnostics.Contracts.PureAttribute] - public static bool operator !=(MonadBError left, MonadBError right) => !left.Equals(right); - } - - public static partial class MonadBExtension - { - #region bind - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => (await result.ConfigureAwait(false)).Bind(bind); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Bind( - this global::System.Threading.Tasks.Task> result, - global::System.Func>> bind) - => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); - - #endregion - - #region map - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func map) - => (await result.ConfigureAwait(false)).Map(map); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Map( - this global::System.Threading.Tasks.Task> result, - global::System.Func> bind) - => Bind(result, async v => MonadB.Ok(await bind(v).ConfigureAwait(false))); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static MonadB MapError(this MonadB result, global::System.Func mapError) - { - if (result is MonadB.Error_ e) - return MonadB.Error(mapError(e.Details)); - return result; - } - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); - - #endregion - - #region match - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func> error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func> ok, - global::System.Func error) - => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static async global::System.Threading.Tasks.Task Match( - this global::System.Threading.Tasks.Task> result, - global::System.Func ok, - global::System.Func error) - => (await result.ConfigureAwait(false)).Match(ok, error); - - #endregion - - [global::System.Diagnostics.DebuggerStepThrough] - [global::System.Diagnostics.Contracts.PureAttribute] - public static MonadB Flatten(this MonadB> result) => result.Bind(r => r); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static MonadB As(this MonadB result, global::System.Func errorTIsNotT1) => - result.Bind(r => - { - if (r is T1 converted) - return converted; - return MonadB.Error(errorTIsNotT1()); - }); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static MonadB As(this MonadB result, global::System.Func errorIsNotT1) => - result.As(errorIsNotT1); - - #region query-expression pattern - - [global::System.Diagnostics.DebuggerStepThrough] - - public static MonadB Select(this MonadB result, global::System.Func selector) => result.Map(selector); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static MonadB SelectMany(this MonadB result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - [global::System.Diagnostics.DebuggerStepThrough] - - public static global::System.Threading.Tasks.Task> SelectMany(this MonadB result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); - - #endregion - - //createGenericResultConversionExtensions - } + { + throw; //createGenericErrorResult + } + case Error_ error: + return error.Convert(); + default: + throw new global::System.InvalidOperationException($"Unexpected derived result type: {GetType()}"); + } + } + + //createGenericResultConversions + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T? GetValueOrDefault() + => Match( + v => (T?)v, + _ => default + ); + + [global::System.Diagnostics.DebuggerStepThrough] + + public T GetValueOrDefault( global::System.Func defaultValue) + => Match( + v => v, + _ => defaultValue() + ); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public T GetValueOrDefault(T defaultValue) + => Match( + v => v, + _ => defaultValue + ); + + [global::System.Diagnostics.DebuggerStepThrough] + public T GetValueOrThrow() + => Match( + v => v, + details => throw new global::System.InvalidOperationException($"Cannot access error result value. Error: {details}")); + + + public global::System.Collections.Generic.IEnumerator GetEnumerator() => Match(ok => new[] { ok }, _ => Enumerable.Empty()).GetEnumerator(); + + + public override string ToString() => Match(ok => $"Ok {ok?.ToString()}", error => $"Error {error}"); + global::System.Collections.IEnumerator global::System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); + + public sealed partial class Ok_ : MonadB + { + public T Value { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Ok_(T value) => Value = value; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override Int32? GetErrorOrDefault() => null; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Ok_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return global::System.Collections.Generic.EqualityComparer.Default.Equals(Value, other.Value); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Ok_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Value == null ? 0 : global::System.Collections.Generic.EqualityComparer.Default.GetHashCode(Value); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Ok_ left, Ok_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Ok_ left, Ok_ right) => !Equals(left, right); + } + + public sealed partial class Error_ : MonadB + { + public Int32 Details { get; } + + [global::System.Diagnostics.DebuggerStepThrough] + public Error_(Int32 details) => Details = details; + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public MonadB.Error_ Convert() => new MonadB.Error_(Details); + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public override Int32? GetErrorOrDefault() => Details; + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(Error_? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Equals(Details, other.Details); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + return obj is Error_ other && Equals(other); + } + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => Details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(Error_ left, Error_ right) => Equals(left, right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(Error_ left, Error_ right) => !Equals(left, right); + } + } + + public readonly partial struct MonadBError : global::System.IEquatable + { + readonly Int32 _details; + + internal MonadBError(Int32 details) => _details = details; + + [global::System.Diagnostics.Contracts.Pure] + public MonadB WithOk() => MonadB.Error(_details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public bool Equals(MonadBError other) => _details.Equals(other._details); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override bool Equals(object? obj) => obj is MonadBError other && Equals(other); + + [global::System.Diagnostics.Contracts.PureAttribute] + public override int GetHashCode() => _details.GetHashCode(); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator ==(MonadBError left, MonadBError right) => left.Equals(right); + + [global::System.Diagnostics.Contracts.PureAttribute] + public static bool operator !=(MonadBError left, MonadBError right) => !left.Equals(right); + } + + public static partial class MonadBExtension + { + #region bind + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => (await result.ConfigureAwait(false)).Bind(bind); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Bind( + this global::System.Threading.Tasks.Task> result, + global::System.Func>> bind) + => await (await result.ConfigureAwait(false)).Bind(bind).ConfigureAwait(false); + + #endregion + + #region map + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func map) + => (await result.ConfigureAwait(false)).Map(map); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Map( + this global::System.Threading.Tasks.Task> result, + global::System.Func> bind) + => Bind(result, async v => MonadB.Ok(await bind(v).ConfigureAwait(false))); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static MonadB MapError(this MonadB result, global::System.Func mapError) + { + if (result is MonadB.Error_ e) + return MonadB.Error(mapError(e.Details)); + return result; + } + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task> MapError(this global::System.Threading.Tasks.Task> result, global::System.Func mapError) => (await result.ConfigureAwait(false)).MapError(mapError); + + #endregion + + #region match + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func> error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func> ok, + global::System.Func error) + => await (await result.ConfigureAwait(false)).Match(ok, error).ConfigureAwait(false); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static async global::System.Threading.Tasks.Task Match( + this global::System.Threading.Tasks.Task> result, + global::System.Func ok, + global::System.Func error) + => (await result.ConfigureAwait(false)).Match(ok, error); + + #endregion + + [global::System.Diagnostics.DebuggerStepThrough] + [global::System.Diagnostics.Contracts.PureAttribute] + public static MonadB Flatten(this MonadB> result) => result.Bind(r => r); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static MonadB As(this MonadB result, global::System.Func errorTIsNotT1) => + result.Bind(r => + { + if (r is T1 converted) + return converted; + return MonadB.Error(errorTIsNotT1()); + }); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static MonadB As(this MonadB result, global::System.Func errorIsNotT1) => + result.As(errorIsNotT1); + + #region query-expression pattern + + [global::System.Diagnostics.DebuggerStepThrough] + + public static MonadB Select(this MonadB result, global::System.Func selector) => result.Map(selector); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> Select(this global::System.Threading.Tasks.Task> result, global::System.Func selector) => result.Map(selector); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static MonadB SelectMany(this MonadB result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this global::System.Threading.Tasks.Task> result, global::System.Func> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + [global::System.Diagnostics.DebuggerStepThrough] + + public static global::System.Threading.Tasks.Task> SelectMany(this MonadB result, global::System.Func>> selector, global::System.Func resultSelector) => result.Bind(t => selector(t).Map(t1 => resultSelector(t, t1))); + + #endregion + + //createGenericResultConversionExtensions + } } namespace FunicularSwitch.Test.Extensions { - public static partial class MonadBExtension - { - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable items, - global::System.Func> choose, - global::System.Action onError) - => items - .Select(i => choose(i)) - .Choose(onError); - - - public static global::System.Collections.Generic.IEnumerable Choose( - this global::System.Collections.Generic.IEnumerable> results, - global::System.Action onError) - => results - .Where(r => - r.Match(_ => true, error => - { - onError(error); - return false; - })) - .Select(r => r.GetValueOrThrow()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static MonadB As(this object? item, global::System.Func error) => - !(item is T t) ? MonadB.Error(error()) : t; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static MonadB NotNull(this T? item, global::System.Func error) => - item ?? MonadB.Error(error()); - - [global::System.Diagnostics.DebuggerStepThrough] - - public static MonadB NotNullOrEmpty(this string? s, global::System.Func error) - => string.IsNullOrEmpty(s) ? MonadB.Error(error()) : s!; - - [global::System.Diagnostics.DebuggerStepThrough] - - public static MonadB NotNullOrWhiteSpace(this string? s, global::System.Func error) - => string.IsNullOrWhiteSpace(s) ? MonadB.Error(error()) : s!; - - - public static MonadB First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => - candidates - .FirstOrDefault(i => predicate(i)) - .NotNull(noMatch); - } + public static partial class MonadBExtension + { + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable items, + global::System.Func> choose, + global::System.Action onError) + => items + .Select(i => choose(i)) + .Choose(onError); + + + public static global::System.Collections.Generic.IEnumerable Choose( + this global::System.Collections.Generic.IEnumerable> results, + global::System.Action onError) + => results + .Where(r => + r.Match(_ => true, error => + { + onError(error); + return false; + })) + .Select(r => r.GetValueOrThrow()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static MonadB As(this object? item, global::System.Func error) => + !(item is T t) ? MonadB.Error(error()) : t; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static MonadB NotNull(this T? item, global::System.Func error) => + item ?? MonadB.Error(error()); + + [global::System.Diagnostics.DebuggerStepThrough] + + public static MonadB NotNullOrEmpty(this string? s, global::System.Func error) + => string.IsNullOrEmpty(s) ? MonadB.Error(error()) : s!; + + [global::System.Diagnostics.DebuggerStepThrough] + + public static MonadB NotNullOrWhiteSpace(this string? s, global::System.Func error) + => string.IsNullOrWhiteSpace(s) ? MonadB.Error(error()) : s!; + + + public static MonadB First(this global::System.Collections.Generic.IEnumerable candidates, global::System.Func predicate, global::System.Func noMatch) => + candidates + .FirstOrDefault(i => predicate(i)) + .NotNull(noMatch); + } #pragma warning restore 1591 } diff --git a/Source/Tests/FunicularSwitch.Generators.Test/TransformerGeneratorTest.cs b/Source/Tests/FunicularSwitch.Generators.Test/TransformerGeneratorTest.cs index d50f2ec4..c9f72fbd 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/TransformerGeneratorTest.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/TransformerGeneratorTest.cs @@ -532,7 +532,7 @@ public class MonadBT return Verify(code); } - + [TestMethod] public Task MonadTransformerMissingBindTMethod() { diff --git a/Source/Tests/FunicularSwitch.Generators.Test/UnionTypeGeneratorSpecs.cs b/Source/Tests/FunicularSwitch.Generators.Test/UnionTypeGeneratorSpecs.cs index 1c3b2070..fbe42433 100644 --- a/Source/Tests/FunicularSwitch.Generators.Test/UnionTypeGeneratorSpecs.cs +++ b/Source/Tests/FunicularSwitch.Generators.Test/UnionTypeGeneratorSpecs.cs @@ -7,12 +7,12 @@ namespace FunicularSwitch.Generators.Test; [TestClass] public class Run_union_type_generator : VerifySourceGenerator { - [TestMethod] - public Task For_record_union_type() - { - var code = - /* lang=csharp */ - """ + [TestMethod] + public Task For_record_union_type() + { + var code = + /* lang=csharp */ + """ using FunicularSwitch.Generators; namespace FunicularSwitch.Test; @@ -25,15 +25,15 @@ public record Aaa : Base; public record Two : Base; """; - return Verify(code); - } + return Verify(code); + } - [TestMethod] - public Task For_record_union_type_with_multi_level_concrete_derived_types() - { - var code = - /* lang=csharp */ - """ + [TestMethod] + public Task For_record_union_type_with_multi_level_concrete_derived_types() + { + var code = + /* lang=csharp */ + """ using FunicularSwitch.Generators; namespace FunicularSwitch.Test; @@ -46,15 +46,15 @@ public record Bbb : BaseChild; public record Aaa : Base; """; - return Verify(code); - } + return Verify(code); + } - [TestMethod] - public Task For_record_union_type_with_explicit_case_order() - { - var code = - /* lang=csharp */ - """ + [TestMethod] + public Task For_record_union_type_with_explicit_case_order() + { + var code = + /* lang=csharp */ + """ using FunicularSwitch.Generators; namespace FunicularSwitch.Test; @@ -68,15 +68,15 @@ public record Eins : Base; public record Zwei : Base; """; - return Verify(code); - } + return Verify(code); + } - [TestMethod] - public Task For_record_union_type_with_as_declared_case_order() - { - var code = - /* lang=csharp */ - """ + [TestMethod] + public Task For_record_union_type_with_as_declared_case_order() + { + var code = + /* lang=csharp */ + """ using FunicularSwitch.Generators; namespace FunicularSwitch.Test; @@ -89,15 +89,15 @@ public record Aaa : Base; public record Two : Base; """; - return Verify(code); - } + return Verify(code); + } - [TestMethod] - public Task For_switchyard_union_type() - { - var code = - /* lang=csharp */ - """ + [TestMethod] + public Task For_switchyard_union_type() + { + var code = + /* lang=csharp */ + """ namespace FunicularSwitch.Test; [FunicularSwitch.Generators.UnionType(StaticFactoryMethods=false)] @@ -153,23 +153,23 @@ public override bool Equals(object? obj) } """; - return Verify(code); - } - - [TestMethod] - public void KeyWorkSpecs() - { - "string".IsAnyKeyWord().Should().BeTrue(); - "myparameter".IsAnyKeyWord().Should().BeFalse(); - "event".IsAnyKeyWord().Should().BeTrue(); - } - - [TestMethod] - public Task For_nested_record_union_type() - { - var code = - /* lang=csharp */ - """ + return Verify(code); + } + + [TestMethod] + public void KeyWorkSpecs() + { + "string".IsAnyKeyWord().Should().BeTrue(); + "myparameter".IsAnyKeyWord().Should().BeFalse(); + "event".IsAnyKeyWord().Should().BeTrue(); + } + + [TestMethod] + public Task For_nested_record_union_type() + { + var code = + /* lang=csharp */ + """ using FunicularSwitch.Generators; namespace FunicularSwitch.Test; @@ -185,15 +185,15 @@ public record Two : Base; } """; - return Verify(code); - } + return Verify(code); + } - [TestMethod] - public Task For_inaccessible_type() - { - var code = - /* lang=csharp */ - """ + [TestMethod] + public Task For_inaccessible_type() + { + var code = + /* lang=csharp */ + """ using FunicularSwitch.Generators; namespace FunicularSwitch.Test; @@ -209,15 +209,15 @@ record Two : Base; } """; - return Verify(code); - } + return Verify(code); + } - [TestMethod] - public Task For_empty_namespace() - { - var code = - /* lang=csharp */ - """ + [TestMethod] + public Task For_empty_namespace() + { + var code = + /* lang=csharp */ + """ using FunicularSwitch.Generators; public class Outer { @@ -230,15 +230,15 @@ public record Two : Base; } """; - return Verify(code); - } + return Verify(code); + } - [TestMethod] - public Task For_interface_union_type() - { - var code = - /* lang=csharp */ - """ + [TestMethod] + public Task For_interface_union_type() + { + var code = + /* lang=csharp */ + """ using FunicularSwitch.Generators; namespace FunicularSwitch.Test; @@ -250,15 +250,15 @@ public class One : IBase {} public record Two : IBase {} """; - return Verify(code); - } + return Verify(code); + } - [TestMethod] - public Task For_implicitly_internal_union_type() - { - var code = - /* lang=csharp */ - """ + [TestMethod] + public Task For_implicitly_internal_union_type() + { + var code = + /* lang=csharp */ + """ using FunicularSwitch.Generators; namespace FunicularSwitch.Test; @@ -270,15 +270,15 @@ record One : Base; record Two : Base; """; - return Verify(code); - } + return Verify(code); + } - [TestMethod] - public Task For_explicitly_internal_union_type() - { - var code = - /* lang=csharp */ - """ + [TestMethod] + public Task For_explicitly_internal_union_type() + { + var code = + /* lang=csharp */ + """ using FunicularSwitch.Generators; namespace FunicularSwitch.Test; @@ -290,15 +290,15 @@ internal record One : Base; record Two : Base; """; - return Verify(code); - } + return Verify(code); + } - [TestMethod] - public Task For_partial_record_union_type() - { - var code = - /* lang=csharp */ - """ + [TestMethod] + public Task For_partial_record_union_type() + { + var code = + /* lang=csharp */ + """ using FunicularSwitch.Generators; namespace FunicularSwitch.Test; @@ -364,15 +364,15 @@ class Consumer { } """; - return Verify(code); - } + return Verify(code); + } [TestMethod] public Task Static_factories_for_nested_internal_union_type() { var code = - /* lang=csharp */ - """ + /* lang=csharp */ + """ using FunicularSwitch.Generators; namespace FunicularSwitch.Test; @@ -393,12 +393,12 @@ public record NoSync_() : InitResult; } - [TestMethod] - public Task Static_factories_for_interface_union_type() - { - var code = - /* lang=csharp */ - """ + [TestMethod] + public Task Static_factories_for_interface_union_type() + { + var code = + /* lang=csharp */ + """ using FunicularSwitch.Generators; namespace FunicularSwitch.Test; @@ -410,15 +410,15 @@ public class One : IBase {} public record Two : IBase {} """; - return Verify(code); - } + return Verify(code); + } [TestMethod] public Task Static_factories_for_type_with_required_properties() { var code = - /* lang=csharp */ - """ + /* lang=csharp */ + """ using FunicularSwitch.Generators; namespace FunicularSwitch.Test; @@ -458,8 +458,8 @@ public Two(int bla, int strangeNameField, int strangeNameField2) : this(bla) public Task Support_structs_derived_from_interface() { var code = - /* lang=csharp */ - """ + /* lang=csharp */ + """ using FunicularSwitch.Generators; namespace FunicularSwitch.Test; @@ -484,8 +484,8 @@ public class Consumer public Task Support_structs_derived_from_derived_interface() { var code = - /* lang=csharp */ - """ + /* lang=csharp */ + """ using FunicularSwitch.Generators; namespace FunicularSwitch.Test; @@ -510,12 +510,12 @@ public class Consumer public readonly partial record struct X; - [TestMethod] - public Task For_union_type_without_derived_types() - { - var code = - /* lang=csharp */ - """ + [TestMethod] + public Task For_union_type_without_derived_types() + { + var code = + /* lang=csharp */ + """ using FunicularSwitch.Generators; namespace FunicularSwitch.Test; @@ -527,15 +527,15 @@ public abstract partial record NodeMessage(string NodeInstanceId) } """; - return Verify(code); - } + return Verify(code); + } - [TestMethod] - public Task For_union_type_with_generic_base_class() + [TestMethod] + public Task For_union_type_with_generic_base_class() { - var code = - /* lang=csharp */ - """ + var code = + /* lang=csharp */ + """ using FunicularSwitch.Generators; namespace FunicularSwitch.Test; @@ -552,12 +552,12 @@ public sealed record Deriving2_(string Value) : BaseType(Value); return Verify(code); } - [TestMethod] - public Task For_union_type_with_generic_base_class_and_type_constraints() + [TestMethod] + public Task For_union_type_with_generic_base_class_and_type_constraints() { var code = - /* lang=csharp */ - """ + /* lang=csharp */ + """ using System.Collections.Generic; using FunicularSwitch.Generators; @@ -581,8 +581,8 @@ public sealed record Deriving2_(string Value) : BaseType(Value); public Task ForUnionType_WithNullableParameters_StaticFactoriesHaveNullableParametersToo() { var code = - /* lang=csharp */ - """ + /* lang=csharp */ + """ using FunicularSwitch.Generators; namespace FunicularSwitch.Text; @@ -600,9 +600,9 @@ public sealed record DerivedType_(string? NullableReferenceType, int? NullableSt [TestMethod] public Task ForUnionType_WhenReferencingJetBrainsAnnotationsPackage_AddsInstantHandleAttributes() { - var code = - /* lang=csharp */ - """ + var code = + /* lang=csharp */ + """ using FunicularSwitch.Generators; [UnionType] @@ -612,6 +612,6 @@ public sealed record Derived_(int Number) : BaseType; } """; - return Verify(code, additionalAssemblies: [typeof(InstantHandleAttribute).Assembly]); + return Verify(code, additionalAssemblies: [typeof(InstantHandleAttribute).Assembly]); } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Test/EnumerableExtensionSpecs.cs b/Source/Tests/FunicularSwitch.Test/EnumerableExtensionSpecs.cs index 2dbe93ef..751f1efe 100644 --- a/Source/Tests/FunicularSwitch.Test/EnumerableExtensionSpecs.cs +++ b/Source/Tests/FunicularSwitch.Test/EnumerableExtensionSpecs.cs @@ -26,6 +26,14 @@ private static IEnumerable PureEnumerable(IEnumerable source) private static IEnumerable ListEnumerable(IEnumerable source) => source.ToList(); private static IEnumerable ListEnumerable(IEnumerable source) => source.ToList(); + [TestMethod] + public void WhereSome_CorrectCollectionReturned() + { + IEnumerable> target = [1, Option.None(), 2, Option.None(), 3]; + var result = target.WhereSome(); + result.Should().Equal([1, 2, 3]); + } + [TestMethod] public void FirstOrNone_Empty_ReturnsNone() { @@ -44,13 +52,13 @@ static void Assert(IEnumerable subject) result.Should().BeNone(); } } - + [TestMethod] public void FirstOrNone_OneElement_ReturnsSome() { // Given List subject = [1]; - + Assert(PureEnumerable(subject)); Assert(ListEnumerable(subject)); return; @@ -64,13 +72,13 @@ static void Assert(IEnumerable subject) result.Should().BeSome().Which.Should().Be(1); } } - + [TestMethod] public void FirstOrNone_Multiple_ReturnsFirst() { // Given List subject = [1, 2, 3, 4, 5, 6, 7, 8, 9]; - + Assert(PureEnumerable(subject)); Assert(ListEnumerable(subject)); return; @@ -84,13 +92,13 @@ static void Assert(IEnumerable subject) result.Should().BeSome().Which.Should().Be(1); } } - + [TestMethod] public void FirstOrNone_Predicate_ReturnsFirst() { // Given List subject = [1, 2, 3, 4, 5, 6, 7, 8, 9]; - + Assert(PureEnumerable(subject)); Assert(ListEnumerable(subject)); return; @@ -104,13 +112,13 @@ static void Assert(IEnumerable subject) result.Should().BeSome().Which.Should().Be(4); } } - + [TestMethod] public void LastOrNone_Empty_ReturnsNone() { // Given List subject = []; - + Assert(PureEnumerable(subject)); Assert(ListEnumerable(subject)); return; @@ -124,13 +132,13 @@ static void Assert(IEnumerable subject) result.Should().BeNone(); } } - + [TestMethod] public void LastOrNone_SingleElement_ReturnsElement() { // Given List subject = [0]; - + Assert(PureEnumerable(subject)); Assert(ListEnumerable(subject)); return; @@ -144,13 +152,13 @@ static void Assert(IEnumerable subject) result.Should().BeSome().Which.Should().Be(0); } } - + [TestMethod] public void LastOrNone_MultipleElements_ReturnsLastElement() { // Given List subject = [0, 1, 2, 3, 4]; - + Assert(PureEnumerable(subject)); Assert(ListEnumerable(subject)); return; @@ -164,13 +172,13 @@ static void Assert(IEnumerable subject) result.Should().BeSome().Which.Should().Be(4); } } - + [TestMethod] public void LastOrNone_NullStruct_ReturnsSome() { // Given List subject = [null]; - + Assert(PureEnumerable(subject)); Assert(ListEnumerable(subject)); return; @@ -183,14 +191,14 @@ static void Assert(IEnumerable subject) // Then result.Should().BeSome().Which.Should().BeNull(); } - } - + } + [TestMethod] public void LastOrNone_NullStructValue_ReturnsValue() { // Given List subject = [1, null, 2]; - + Assert(PureEnumerable(subject)); Assert(ListEnumerable(subject)); return; @@ -203,14 +211,14 @@ static void Assert(IEnumerable subject) // Then result.Should().BeSome().Which.Should().Be(2); } - } - + } + [TestMethod] public void LastOrNone_NullStructEmpty_ReturnsNone() { // Given List subject = []; - + Assert(PureEnumerable(subject)); Assert(ListEnumerable(subject)); return; @@ -224,13 +232,13 @@ static void Assert(IEnumerable subject) result.Should().BeNone(); } } - + [TestMethod] public void LastOrNone_Predicate_ReturnsCorrectValue() { // Given List subject = [1, 2, 3, 4, 5, 6]; - + Assert(PureEnumerable(subject)); Assert(ListEnumerable(subject)); return; @@ -244,13 +252,13 @@ static void Assert(IEnumerable subject) result.Should().BeSome().Which.Should().Be(4); } } - + [TestMethod] public void ElementAtOrNone_EmptyEnumerable_ReturnsNone() { // Given List subject = []; - + Assert(PureEnumerable(subject)); Assert(ListEnumerable(subject)); return; @@ -263,14 +271,14 @@ static void Assert(IEnumerable subject) // Then result.Should().BeNone(); } - } - + } + [TestMethod] public void ElementAtOrNone_MultipleValues_ReturnsSome() { // Given List subject = [1, 2, 3, 4, 5, 6, 7, 8]; - + Assert(PureEnumerable(subject)); Assert(ListEnumerable(subject)); return; @@ -284,13 +292,13 @@ static void Assert(IEnumerable subject) result.Should().BeSome().Which.Should().Be(4); } } - + [TestMethod] public void ElementAtOrNone_IndexOutOfRange_ReturnsNone() { // Given List subject = [1, 2, 3, 4, 5, 6, 7, 8]; - + Assert(PureEnumerable(subject)); Assert(ListEnumerable(subject)); return; @@ -318,7 +326,7 @@ static async Task Assert(IEnumerable subject) { // When var result = await subject.WhereAsync(x => Task.FromResult(x % 2 == 0)); - + // Then result.Should().Equal([2, 4, 6, 8, 10]); } @@ -337,7 +345,7 @@ static async Task Assert(IEnumerable subject) { // When var result = await subject.WhereAsync(x => Task.FromResult(x % 2 == 0), maxDegreeOfParallelism: 3); - + // Then result.Should().Equal([2, 4, 6, 8, 10]); } @@ -356,7 +364,7 @@ static async Task Assert(IEnumerable subject) { // When var result = await subject.WhereAsyncSequential(x => Task.FromResult(x % 2 == 0)); - + // Then result.Should().Equal([2, 4, 6, 8, 10]); } @@ -375,7 +383,7 @@ static async Task Assert(IEnumerable subject) { // When var result = await subject.SelectAsync(x => Task.FromResult(2 * x)); - + // Then result.Should().Equal([2, 4, 6, 8, 10]); } @@ -394,7 +402,7 @@ static async Task Assert(IEnumerable subject) { // When var result = await subject.SelectAsync(x => Task.FromResult(2 * x), maxDegreeOfParallelism: 3); - + // Then result.Should().Equal([2, 4, 6, 8, 10]); } @@ -413,7 +421,7 @@ static async Task Assert(IEnumerable subject) { // When var result = await subject.SelectAsyncSequential(x => Task.FromResult(2 * x)); - + // Then result.Should().Equal([2, 4, 6, 8, 10]); } @@ -424,7 +432,7 @@ public void Yield_TTBase() { // When var target = "Hi".Yield(); - + // Then target.Should().Equal([(object)"Hi"]); } @@ -434,12 +442,12 @@ public void Concat_OnlyOneExtraItem_ReturnsCorrectSequence() { // Given IEnumerable target = [1, 2, 3]; - + // When #pragma warning disable CS0618 // Type or member is obsolete var result = target.Concat(5); #pragma warning restore CS0618 // Type or member is obsolete - + // Then result.Should().Equal([1, 2, 3, 5]); } @@ -449,12 +457,12 @@ public void Concat_WithParams_ReturnsCorrectSequence() { // Given IEnumerable target = [1, 2, 3]; - + // When #pragma warning disable CS0618 // Type or member is obsolete var result = target.Concat(5, 6, 7, 8); #pragma warning restore CS0618 // Type or member is obsolete - + // Then result.Should().Equal([1, 2, 3, 5, 6, 7, 8]); } diff --git a/Source/Tests/FunicularSwitch.Test/FuncToActionSpecs.cs b/Source/Tests/FunicularSwitch.Test/FuncToActionSpecs.cs index 86a47c3d..2119d320 100644 --- a/Source/Tests/FunicularSwitch.Test/FuncToActionSpecs.cs +++ b/Source/Tests/FunicularSwitch.Test/FuncToActionSpecs.cs @@ -19,11 +19,11 @@ public void IgnoreReturn_T1_T2_FuncIsExecuted() numberPassed = x; return "Hi"; }; - + // When var action = func.IgnoreReturn(); action(1); - + // Then funcCalled.Should().BeTrue(); numberPassed.Should().Be(1); @@ -39,11 +39,11 @@ public void IgnoreReturn_T_FuncIsExecuted() funcCalled = true; return "Hello"; }; - + // When var action = func.IgnoreReturn(); action(); - + // Then funcCalled.Should().BeTrue(); } @@ -57,11 +57,11 @@ public void ToFunc_TNullable_ActionIsCalled() { actionCalled = true; }; - + // When var func = action.ToFunc(); var result = func(); - + // Then actionCalled.Should().BeTrue(); } @@ -77,11 +77,11 @@ public void ToFunc_Tint_ActionIsCalled() actionCalled = true; numberPassed = x; }; - + // When var func = action.ToFunc(); var result = func(23); - + // Then actionCalled.Should().BeTrue(); numberPassed.Should().Be(23); @@ -97,11 +97,11 @@ public async Task ToFunc_FuncTask_ActionIsCalled() actionCalled = true; return Task.CompletedTask; }; - + // When var func = asyncAction.ToFunc(); var result = await func(); - + // Then actionCalled.Should().BeTrue(); } @@ -118,11 +118,11 @@ public async Task ToFunc_Func_T_Task_ActionIsCalled() numberPassed = x; return Task.CompletedTask; }; - + // When var func = asyncAction.ToFunc(); var result = await func(12); - + // Then actionCalled.Should().BeTrue(); numberPassed.Should().Be(12); diff --git a/Source/Tests/FunicularSwitch.Test/ImplicitCastStudy.cs b/Source/Tests/FunicularSwitch.Test/ImplicitCastStudy.cs index 19fc306b..3f0b3711 100644 --- a/Source/Tests/FunicularSwitch.Test/ImplicitCastStudy.cs +++ b/Source/Tests/FunicularSwitch.Test/ImplicitCastStudy.cs @@ -3,35 +3,35 @@ namespace FunicularSwitch.Test; [TestClass] -public class ImplicitCastStudy +public class ImplicitCastStudy { -// Does not compile anymore because no auto conversion is possible -// [TestMethod] -// [Ignore] //this test fails. The implicit case is never called here due to a legacy compiler behaviour regarding Nullable -// public void ImplicitCastWithNullableStruct() -// { -// long? l = null; -// #pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type. -// Option converted = l; -// #pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type. -// converted.Should().NotBeNull(); -// } + // Does not compile anymore because no auto conversion is possible + // [TestMethod] + // [Ignore] //this test fails. The implicit case is never called here due to a legacy compiler behaviour regarding Nullable + // public void ImplicitCastWithNullableStruct() + // { + // long? l = null; + // #pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type. + // Option converted = l; + // #pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type. + // converted.Should().NotBeNull(); + // } - [TestMethod] - public void ImplicitCastWithClass() - { - object? l = null; + [TestMethod] + public void ImplicitCastWithClass() + { + object? l = null; #pragma warning disable CS8604 // Possible null reference argument. Option converted = l; #pragma warning restore CS8604 // Possible null reference argument. converted.Should().NotBeNull(); - } + } - [TestMethod] - public void ImplicitCastWithNullableStructToOptionOfNullable() - { - long? l = null; - Option converted = l; - converted.Should().NotBeNull(); - } + [TestMethod] + public void ImplicitCastWithNullableStructToOptionOfNullable() + { + long? l = null; + Option converted = l; + converted.Should().NotBeNull(); + } } \ No newline at end of file diff --git a/Source/Tests/FunicularSwitch.Test/OptionSpecs.cs b/Source/Tests/FunicularSwitch.Test/OptionSpecs.cs index 6d27b87e..e7525699 100644 --- a/Source/Tests/FunicularSwitch.Test/OptionSpecs.cs +++ b/Source/Tests/FunicularSwitch.Test/OptionSpecs.cs @@ -7,462 +7,460 @@ namespace FunicularSwitch.Test; [TestClass] public class OptionSpecs { - [TestMethod] - public void Match_ActionTAction_Some_SomeCalled() - { - // Given - var target = Some(5); - bool someCalled = false; - bool noneCalled = false; - - // When - target.Match( - some: _ => - { - someCalled = true; - }, - none: () => - { - noneCalled = true; - }); - - // Then - someCalled.Should().BeTrue(); - noneCalled.Should().BeFalse(); - } - - [TestMethod] - public void Match_ActionTAction_None_NoneCalled() - { - // Given - var target = None(); - bool someCalled = false; - bool noneCalled = false; - - // When - target.Match( - some: x => - { - someCalled = true; - }, - none: () => - { - noneCalled = true; - }); - - // Then - someCalled.Should().BeFalse(); - noneCalled.Should().BeTrue(); - } - - [TestMethod] - public async Task Match_FuncTTaskFuncTask_Some_SomeCalled() - { - // Given - var target = Some(5); - bool someCalled = false; - int numberPassed = -1; - bool noneCalled = false; - - // When - await target.Match( - some: x => - { - someCalled = true; - numberPassed = x; - return Task.CompletedTask; - }, none: () => - { - noneCalled = true; - return Task.CompletedTask; - }); - - // Then - someCalled.Should().BeTrue(); - numberPassed.Should().Be(5); - noneCalled.Should().BeFalse(); - } - - [TestMethod] - public async Task Match_FuncTTaskFuncTask_None_NoneCalled() - { - // Given - var target = None(); - bool someCalled = false; - int numberPassed = -1; - bool noneCalled = false; - - // When - await target.Match( - some: x => - { - someCalled = true; - numberPassed = x; - return Task.CompletedTask; - }, none: () => - { - noneCalled = true; - return Task.CompletedTask; - }); - - // Then - - // Then - someCalled.Should().BeFalse(); - numberPassed.Should().Be(-1); - noneCalled.Should().BeTrue(); - } - - [TestMethod] - public async Task Match_FuncTTaskTResultFuncTaskTResult_Some_SomeCalled() - { - // Given - var target = Some(5); - - // When - var result = await target.Match(some: x => Task.FromResult(x + 2), none: () => Task.FromResult(-4)); - - // Then - result.Should().Be(7); - } - - [TestMethod] - public async Task Match_FuncTTaskTResultFuncTaskTResult_None_NoneCalled() - { - // Given - var target = None(); - - // When - var result = await target.Match(some: x => Task.FromResult(x * 2), none: () => Task.FromResult(-3)); - - // Then - result.Should().Be(-3); - } - - [TestMethod] - public async Task Match_FuncTTaskTResultTResult_Some_SomeCalled() - { - // Given - var target = Some(17); - - // When - var result = await target.Match(some: x => Task.FromResult("Hi"), none: "Hello"); - - // Then - result.Should().Be("Hi"); - } - - [TestMethod] - public async Task Match_FuncTTaskTResultTResult_None_NoneCalled() - { - // Given - var target = None(); - - // When - var result = await target.Match(some: x => Task.FromResult("Hi"), none: "Hello"); - - // Then - result.Should().Be("Hello"); - } - - [TestMethod] - public async Task GetValueOrDefault_FuncTaskT_Some_GetsValue() - { - // Given - var target = Some(123); - - // When - var result = await target.GetValueOrDefault(defaultValue: () => Task.FromResult(3)); - - // Then - result.Should().Be(123); - } - - [TestMethod] - public async Task GetValueOrDefault_FuncTaskT_None_GetsDefault() - { - // Given - var target = None(); - - // When - var result = await target.GetValueOrDefault(defaultValue: () => Task.FromResult(37)); - - // Then - result.Should().Be(37); - } - - [TestMethod] - public async Task GetValueOrDefault_FuncValueTaskT_Some_GetsValue() - { - // Given - var target = Some(123); - - // When - var result = await target.GetValueOrDefault(defaultValue: () => ValueTask.FromResult(3)); - - // Then - result.Should().Be(123); - } - - [TestMethod] - public async Task GetValueOrDefault_FuncValueTaskT_None_GetsDefault() - { - // Given - var target = None(); - - // When - var result = await target.GetValueOrDefault(defaultValue: () => ValueTask.FromResult(37)); - - // Then - result.Should().Be(37); - } - - [TestMethod] - public void GetValueOrThrow_Some_GetsValue() - { - // Given - var target = Some(5); - var action = () => target.GetValueOrThrow("message"); - - // When - var result = action.Should().NotThrow().Which; - - // Then - result.Should().Be(5); - } - - [TestMethod] - public void GetValueOrThrow_None_Throws() - { - // Given - var target = None(); - var action = () => target.GetValueOrThrow("message"); - - // When - var result = action.Should().Throw().Which; - - // Then - result.Message.Should().Contain("message"); - } - - [TestMethod] - public void GetValueOrThrow_None_NoMessagePassed_ThrowsWithADefaultMessage() - { - // Given - var target = None(); - var action = () => target.GetValueOrThrow(); - - // When - var result = action.Should().Throw().Which; - - // Then - result.Message.Should().NotBeNullOrWhiteSpace(); - } - - [TestMethod] - public void ToString_Some_GetsSomeToString() - { - // Given - var target = Some(5); - - // When - var result = target.ToString(); - - // Then - result.Should().Be("5"); - } - - [TestMethod] - public void ToString_None_ReturnsNonePlusTypeString() - { - // Given - var target = None(); - - // When - var result = target.ToString(); - - // Then - result.Should().Be("None Int32"); - } - - [TestMethod] - public void GetHashCode_EqualObjects_ReturnsTheSameHashCode() - { - // Given - var left = Some(13); - var right = Some(13); - - // When - var equals = left.Equals(right); - var leftHashCode = left.GetHashCode(); - var rightHashCode = right.GetHashCode(); - - // Then - equals.Should().BeTrue(); - leftHashCode.Should().Be(rightHashCode); - } - - [TestMethod] - public void GetHashCode_EqualObjectsButDifferentType_ReturnsDifferentHashCodes() - { - // Given - var left = Some("Hi"); - var right = Some("Hi"); - - // When + [TestMethod] + public void Match_ActionTAction_Some_SomeCalled() + { + // Given + var target = Some(5); + bool someCalled = false; + bool noneCalled = false; + + // When + target.Match( + some: _ => + { + someCalled = true; + }, + none: () => + { + noneCalled = true; + }); + + // Then + someCalled.Should().BeTrue(); + noneCalled.Should().BeFalse(); + } + + [TestMethod] + public void Match_ActionTAction_None_NoneCalled() + { + // Given + var target = None(); + bool someCalled = false; + bool noneCalled = false; + + // When + target.Match( + some: _ => + { + someCalled = true; + }, + none: () => + { + noneCalled = true; + }); + + // Then + someCalled.Should().BeFalse(); + noneCalled.Should().BeTrue(); + } + + [TestMethod] + public async Task Match_FuncTTaskFuncTask_Some_SomeCalled() + { + // Given + var target = Some(5); + bool someCalled = false; + int numberPassed = -1; + bool noneCalled = false; + + // When + await target.Match( + some: x => + { + someCalled = true; + numberPassed = x; + return Task.CompletedTask; + }, + none: () => + { + noneCalled = true; + return Task.CompletedTask; + }); + + // Then + someCalled.Should().BeTrue(); + numberPassed.Should().Be(5); + noneCalled.Should().BeFalse(); + } + + [TestMethod] + public async Task Match_FuncTTaskFuncTask_None_NoneCalled() + { + // Given + var target = None(); + bool someCalled = false; + int numberPassed = -1; + bool noneCalled = false; + + // When + await target.Match( + some: x => + { + someCalled = true; + numberPassed = x; + return Task.CompletedTask; + }, none: () => + { + noneCalled = true; + return Task.CompletedTask; + }); + + // Then + + // Then + someCalled.Should().BeFalse(); + numberPassed.Should().Be(-1); + noneCalled.Should().BeTrue(); + } + + [TestMethod] + public async Task Match_FuncTTaskTResultFuncTaskTResult_Some_SomeCalled() + { + // Given + var target = Some(5); + + // When + var result = await target.Match(some: x => Task.FromResult(x + 2), none: () => Task.FromResult(-4)); + + // Then + result.Should().Be(7); + } + + [TestMethod] + public async Task Match_FuncTTaskTResultFuncTaskTResult_None_NoneCalled() + { + // Given + var target = None(); + + // When + var result = await target.Match(some: x => Task.FromResult(x * 2), none: () => Task.FromResult(-3)); + + // Then + result.Should().Be(-3); + } + + [TestMethod] + public async Task Match_FuncTTaskTResultTResult_Some_SomeCalled() + { + // Given + var target = Some(17); + + // When + var result = await target.Match(some: _ => Task.FromResult("Hi"), none: "Hello"); + + // Then + result.Should().Be("Hi"); + } + + [TestMethod] + public async Task Match_FuncTTaskTResultTResult_None_NoneCalled() + { + // Given + var target = None(); + + // When + var result = await target.Match(some: _ => Task.FromResult("Hi"), none: "Hello"); + + // Then + result.Should().Be("Hello"); + } + + [TestMethod] + public async Task GetValueOrDefault_FuncTaskT_Some_GetsValue() + { + // Given + var target = Some(123); + + // When + var result = await target.GetValueOrDefault(defaultValue: () => Task.FromResult(3)); + + // Then + result.Should().Be(123); + } + + [TestMethod] + public async Task GetValueOrDefault_FuncTaskT_None_GetsDefault() + { + // Given + var target = None(); + + // When + var result = await target.GetValueOrDefault(defaultValue: () => Task.FromResult(37)); + + // Then + result.Should().Be(37); + } + + [TestMethod] + public async Task GetValueOrDefault_FuncValueTaskT_Some_GetsValue() + { + // Given + var target = Some(123); + + // When + var result = await target.GetValueOrDefault(defaultValue: () => ValueTask.FromResult(3)); + + // Then + result.Should().Be(123); + } + + [TestMethod] + public async Task GetValueOrDefault_FuncValueTaskT_None_GetsDefault() + { + // Given + var target = None(); + + // When + var result = await target.GetValueOrDefault(defaultValue: () => ValueTask.FromResult(37)); + + // Then + result.Should().Be(37); + } + + [TestMethod] + public void GetValueOrThrow_Some_GetsValue() + { + // Given + var target = Some(5); + var action = () => target.GetValueOrThrow("message"); + + // When + var result = action.Should().NotThrow().Which; + + // Then + result.Should().Be(5); + } + + [TestMethod] + public void GetValueOrThrow_None_Throws() + { + // Given + var target = None(); + var action = () => target.GetValueOrThrow("message"); + + // When + var result = action.Should().Throw().Which; + + // Then + result.Message.Should().Contain("message"); + } + + [TestMethod] + public void GetValueOrThrow_None_NoMessagePassed_ThrowsWithADefaultMessage() + { + // Given + var target = None(); + var action = () => target.GetValueOrThrow(); + + // When + var result = action.Should().Throw().Which; + + // Then + result.Message.Should().NotBeNullOrWhiteSpace(); + } + + [TestMethod] + public void ToString_Some_GetsSomeToString() + { + // Given + var target = Some(5); + + // When + var result = target.ToString(); + + // Then + result.Should().Be("5"); + } + + [TestMethod] + public void ToString_None_ReturnsNonePlusTypeString() + { + // Given + var target = None(); + + // When + var result = target.ToString(); + + // Then + result.Should().Be("None Int32"); + } + + [TestMethod] + public void GetHashCode_EqualObjects_ReturnsTheSameHashCode() + { + // Given + var left = Some(13); + var right = Some(13); + + // When + var equals = left.Equals(right); + var leftHashCode = left.GetHashCode(); + var rightHashCode = right.GetHashCode(); + + // Then + equals.Should().BeTrue(); + leftHashCode.Should().Be(rightHashCode); + } + + [TestMethod] + public void GetHashCode_EqualObjectsButDifferentType_ReturnsDifferentHashCodes() + { + // Given + var left = Some("Hi"); + var right = Some("Hi"); + + // When // ReSharper disable once SuspiciousTypeConversion.Global var equals = left.Equals(right); - var leftHashCode = left.GetHashCode(); - var rightHashCode = right.GetHashCode(); - - // Then - equals.Should().BeFalse(); - leftHashCode.Should().NotBe(rightHashCode); - } - - [TestMethod] - public void GetHashCode_DifferentValuesWithSameType_ReturnsDifferentHashCodes() - { - // Given - var left = Some("Hi"); - var right = Some("Hello"); - - // When - var equals = left.Equals(right); - var leftHashCode = left.GetHashCode(); - var rightHashCode = right.GetHashCode(); - - // Then - equals.Should().BeFalse(); - leftHashCode.Should().NotBe(rightHashCode); - } - - [TestMethod] - public void NullCoalescingWithOptionBoolBehavesAsExpected() - { - bool? foo = null; - - var implicitTypedOption = foo ?? Option.None; - implicitTypedOption.GetType().Should().Be(typeof(Option)); - - Option option = foo ?? Option.None; - option.Equals(Option.None).Should().BeTrue(); - } - - [TestMethod] - public void ToOptionForNullable() - { - var none = ((bool?)null).ToOption(); - none.Equals(Option.None).Should().BeTrue(); - - var some = ((bool?)false).ToOption(); - some.Equals(Some(false)).Should().BeTrue(); - - var nullableNull = none.ToNullable(); - nullableNull.Should().BeNull(); - - var nullableWithValue = some.ToNullable(); - nullableWithValue.Should().Be(false); - } - - [TestMethod] - public void NullCoalescingWithResultBoolBehavesAsExpected() - { - bool? foo = null; - - var result = foo ?? Result.Error("Value is missing"); - if (result) - { - - } - result.Should().BeError().Subject.Should().Be("Value is missing"); - } - - [TestMethod] - public void QueryExpressionSelect() - { - Option subject = 42; - var result = - from r in subject - select r + 3; - result.Should().BeSome() - .Subject.Should().Be(45); - } - - [TestMethod] - public void QueryExpressionSelectMany() - { - Option some = 42; - var none = Option.None(); - - ( - from r in some - from r1 in none - select r1 - ).Should().BeNone(); - - ( - from r in none - from r1 in some - select r1 - ).Should().BeNone(); - - ( - from r in some - let x = r * 2 - from r1 in some - select x - ).Should().BeEquivalentTo(some.Map(r => r * 2)); - } - - [TestMethod] - public async Task QueryExpressionSelectManyAsync() - { - Task> someAsync = Task.FromResult(Option.Some(42)); - var noneAsync = Task.FromResult(Option.None()); - var some = Option.Some(1); - - (await ( - from r in someAsync - from r1 in noneAsync - select r1 - )).Should().BeEquivalentTo(await noneAsync); - - (await ( - from r in noneAsync - from r1 in someAsync - select r1 - )).Should().BeEquivalentTo(await noneAsync); - - (await ( - from r in someAsync - let x = r * 2 - from r1 in someAsync - select x - )).Should().BeEquivalentTo(await someAsync.Map(r => r * 2)); - - (await ( - from r in some - let x = r * 2 - from r1 in someAsync - select x - )).Should().BeEquivalentTo(some.Map(r => r * 2)); - - (await ( - from r in someAsync - let x = r * 2 - from r1 in some - select x - )).Should().BeEquivalentTo(await someAsync.Map(r => r * 2)); - } - - [TestMethod] - public void ShouldHaveDifferentHashCodeIfBothValuesAreDifferent() - { - var hashcode1 = Some(1).GetHashCode(); - var hashcode2 = Some(2).GetHashCode(); - hashcode1.Should().NotBe(hashcode2); - } + var leftHashCode = left.GetHashCode(); + var rightHashCode = right.GetHashCode(); + + // Then + equals.Should().BeFalse(); + leftHashCode.Should().NotBe(rightHashCode); + } + + [TestMethod] + public void GetHashCode_DifferentValuesWithSameType_ReturnsDifferentHashCodes() + { + // Given + var left = Some("Hi"); + var right = Some("Hello"); + + // When + var equals = left.Equals(right); + var leftHashCode = left.GetHashCode(); + var rightHashCode = right.GetHashCode(); + + // Then + equals.Should().BeFalse(); + leftHashCode.Should().NotBe(rightHashCode); + } + + [TestMethod] + public void NullCoalescingWithOptionBoolBehavesAsExpected() + { + bool? foo = null; + + var implicitTypedOption = foo ?? Option.None; + implicitTypedOption.GetType().Should().Be(typeof(Option)); + + Option option = foo ?? Option.None; + option.Equals(Option.None).Should().BeTrue(); + } + + [TestMethod] + public void ToOptionForNullable() + { + var none = ((bool?)null).ToOption(); + none.Equals(Option.None).Should().BeTrue(); + + var some = ((bool?)false).ToOption(); + some.Equals(Some(false)).Should().BeTrue(); + + var nullableNull = none.ToNullable(); + nullableNull.Should().BeNull(); + + var nullableWithValue = some.ToNullable(); + nullableWithValue.Should().Be(false); + } + + [TestMethod] + public void NullCoalescingWithResultBoolBehavesAsExpected() + { + bool? foo = null; + + var result = foo ?? Result.Error("Value is missing"); + + result.Should().BeError().Subject.Should().Be("Value is missing"); + } + + [TestMethod] + public void QueryExpressionSelect() + { + Option subject = 42; + var result = + from r in subject + select r + 3; + result.Should().BeSome() + .Subject.Should().Be(45); + } + + [TestMethod] + public void QueryExpressionSelectMany() + { + Option some = 42; + var none = Option.None(); + + ( + from r in some + from r1 in none + select r1 + ).Should().BeNone(); + + ( + from r in none + from r1 in some + select r1 + ).Should().BeNone(); + + ( + from r in some + let x = r * 2 + from r1 in some + select x + ).Should().BeEquivalentTo(some.Map(r => r * 2)); + } + + [TestMethod] + public async Task QueryExpressionSelectManyAsync() + { + Task> someAsync = Task.FromResult(Option.Some(42)); + var noneAsync = Task.FromResult(Option.None()); + var some = Option.Some(1); + + (await ( + from r in someAsync + from r1 in noneAsync + select r1 + )).Should().BeEquivalentTo(await noneAsync); + + (await ( + from r in noneAsync + from r1 in someAsync + select r1 + )).Should().BeEquivalentTo(await noneAsync); + + (await ( + from r in someAsync + let x = r * 2 + from r1 in someAsync + select x + )).Should().BeEquivalentTo(await someAsync.Map(r => r * 2)); + + (await ( + from r in some + let x = r * 2 + from r1 in someAsync + select x + )).Should().BeEquivalentTo(some.Map(r => r * 2)); + + (await ( + from r in someAsync + let x = r * 2 + from r1 in some + select x + )).Should().BeEquivalentTo(await someAsync.Map(r => r * 2)); + } + + [TestMethod] + public void ShouldHaveDifferentHashCodeIfBothValuesAreDifferent() + { + var hashcode1 = Some(1).GetHashCode(); + var hashcode2 = Some(2).GetHashCode(); + hashcode1.Should().NotBe(hashcode2); + } [TestMethod] public void ShouldNotBeEqualIfValuesDifferent() @@ -473,40 +471,40 @@ public void ShouldNotBeEqualIfValuesDifferent() (some1 == some2).Should().BeFalse(); (some1 != some2).Should().BeTrue(); } - - [TestMethod] - public void ShouldHaveSameHashCodeIfBothValueAreSame() - { - var hashcode1 = Some(1).GetHashCode(); - var hashcode2 = Some(1).GetHashCode(); - hashcode1.Should().Be(hashcode2); - } - - [TestMethod] - public void ShouldHaveDifferentHashCodeIfOneIsNone() - { - var hashcode1 = Some(1).GetHashCode(); - var hashcode2 = None().GetHashCode(); - hashcode1.Should().NotBe(hashcode2); - } - - [TestMethod] - public void ShouldBeEqualIfBothValuesAreEqual() - { - var some1 = Some(1); - var some2 = Some(1); - some1.Equals(some2).Should().BeTrue(); + + [TestMethod] + public void ShouldHaveSameHashCodeIfBothValueAreSame() + { + var hashcode1 = Some(1).GetHashCode(); + var hashcode2 = Some(1).GetHashCode(); + hashcode1.Should().Be(hashcode2); + } + + [TestMethod] + public void ShouldHaveDifferentHashCodeIfOneIsNone() + { + var hashcode1 = Some(1).GetHashCode(); + var hashcode2 = None().GetHashCode(); + hashcode1.Should().NotBe(hashcode2); + } + + [TestMethod] + public void ShouldBeEqualIfBothValuesAreEqual() + { + var some1 = Some(1); + var some2 = Some(1); + some1.Equals(some2).Should().BeTrue(); (some1 == some2).Should().BeTrue(); (some1 != some2).Should().BeFalse(); } - - [TestMethod] - public void ShouldNotBeEqualIfOneIsNone() - { - var some = Some(1); - var none = None(); - some.Equals(none).Should().BeFalse(); - } + + [TestMethod] + public void ShouldNotBeEqualIfOneIsNone() + { + var some = Some(1); + var none = None(); + some.Equals(none).Should().BeFalse(); + } [TestMethod] public void ShouldBeEqualIfBothAreNoneOfSameType() @@ -539,7 +537,7 @@ public void NullOptionsWork() { var nullOption = Option.Some(null); var nullOption2 = Option.Some(null); - nullOption.GetHashCode().Should().NotBe(nullOption2.GetHashCode()); + nullOption.GetHashCode().Should().NotBe(nullOption2.GetHashCode()); nullOption.Equals(Option.Some(new())).Should().BeFalse(); // ReSharper disable once SuspiciousTypeConversion.Global nullOption.Equals(nullOption2).Should().BeFalse(); @@ -558,196 +556,260 @@ public void IntermediateNoneOptionIsFine() [TestMethod] public void Flatten_SomeSome_Some() { - var optionOfOption = Some(Some(5)); - var flattened = optionOfOption.Flatten(); - flattened.Should().BeSome().Which.Should().Be(5); + var optionOfOption = Some(Some(5)); + var flattened = optionOfOption.Flatten(); + flattened.Should().BeSome().Which.Should().Be(5); } [TestMethod] public void Flatten_SomeNone_None() { - var optionOfNone = Some(None()); - var flattened = optionOfNone.Flatten(); - flattened.Should().BeNone(); + var optionOfNone = Some(None()); + var flattened = optionOfNone.Flatten(); + flattened.Should().BeNone(); } [TestMethod] public void Flatten_None_None() { - var none = Option.None>(); - var flattened = none.Flatten(); - flattened.Should().BeNone(); + var none = Option.None>(); + var flattened = none.Flatten(); + flattened.Should().BeNone(); } [TestMethod] public void As_Matches_IsExtracted() { - object boxedString = "Hi"; - var asString = boxedString.As(); - asString.Should().BeSome().Which.Should().Be("Hi"); + object boxedString = "Hi"; + var asString = boxedString.As(); + asString.Should().BeSome().Which.Should().Be("Hi"); + + object boxedInt = 5; + var asInt = boxedInt.As(); + asInt.Should().BeSome().Which.Should().Be(5); } [TestMethod] public void As_DoesNotMatch_None() { - object boxedString = "Hi"; - var asList = boxedString.As>(); - asList.Should().BeNone(); + object boxedString = "Hi"; + var asList = boxedString.As>(); + asList.Should().BeNone(); + + object boxedInt = 5; + var asDate = boxedInt.As(); + asDate.Should().BeNone(); + } + + [TestMethod] + public void As_MatchesObjectOptions() + { + var myDerivedExplicitOption = Some(new MyDerived()); + myDerivedExplicitOption.As().Should().BeSome().Which.Should().BeOfType(); + myDerivedExplicitOption.As().Should().BeSome().Which.Should().BeOfType(); + + object myDerivedOption = Some(new MyDerived()); + object myDerivedNullOption = Some((MyDerived?)null); + object myBaseOption = Some(new MyBase()); + object myBaseNullOption = Some((MyBase?)null); + object myIntOption = Some(5); + object myInt = 5; + + myDerivedOption.As().Should().BeSome().Which.Should().BeOfType(); + myDerivedOption.As().Should().BeSome().Which.Should().BeOfType(); + + myDerivedNullOption.As().Should().BeNone(); + myDerivedNullOption.As().Should().BeNone(); + + myBaseOption.As().Should().BeNone(); + myBaseNullOption.As().Should().BeNone(); + + myIntOption.As().Should().BeNone(); + myIntOption.As().Should().BeSome().Which.Should().Be(5); + + myIntOption.As().Should().BeSome().Which.Should().Be(5); + myInt.As().Should().BeSome().Which.Should().Be(5); + + var myNullableOption = Some((MyDerived?)null); + myNullableOption.As().Should().Be(((object)myNullableOption).As()); + } + + [TestMethod] + public void As_NestedOptions_UnpacksOneLevel() + { + var nestedOption = Some(Some(new MyDerived())); + object boxed = nestedOption; + + var asBase = nestedOption.As(); + var asFlatOption = nestedOption.As>(); + + var boxedAsBase = boxed.As(); + var boxedAsFlatOption = boxed.As>(); + + asBase.Should().BeNone(); + asFlatOption.Should().BeSome().Which.Should().BeSome().Which.Should().BeOfType(); + + boxedAsBase.Should().BeNone(); + boxedAsFlatOption.Should().BeSome().Which.Should().BeSome().Which.Should().BeOfType(); } [TestMethod] public void AsConvertOptionValue() { - var value = new MyDerived(); - var myBaseOption = Some(value); - myBaseOption.As().Should().BeSome().Which.Should().Be(value); - myBaseOption.As().Should().BeSome().Which.Should().Be(value); - myBaseOption.As().Should().BeNone(); + var value = new MyDerived(); + var myBaseOption = Some(value); + myBaseOption.As().Should().BeSome().Which.Should().Be(value); + myBaseOption.As().Should().BeSome().Which.Should().Be(value); + myBaseOption.As().Should().BeNone(); } class MyBase; - class MyDerived : MyBase; - class AnotherClass; + class MyDerived : MyBase; + class AnotherClass; [TestMethod] public async Task Match_TaskOptionT_FuncTTout_FuncTOut_Some_Matched() { - var target = Task.FromResult(Some(6)); - var matched = await target.Match( - some: number => number * 2, - none: () => 3); - matched.Should().Be(12); + var target = Task.FromResult(Some(6)); + var matched = await target.Match( + some: number => number * 2, + none: () => 3); + matched.Should().Be(12); } [TestMethod] public async Task Match_TaskOptionT_FuncTTout_FuncTOut_None_Matched() { - var target = Task.FromResult(None()); - var matched = await target.Match( - some: number => number * 2, - none: () => 3); - matched.Should().Be(3); + var target = Task.FromResult(None()); + var matched = await target.Match( + some: number => number * 2, + none: () => 3); + matched.Should().Be(3); } [TestMethod] public async Task Map_TaskOptionT_FuncTTaskTOut_Some_Mapped() { - var target = Task.FromResult(Some(13)); - var mapped = await target.Map(number => Task.FromResult(number + 5)); - mapped.Should().BeSome().Which.Should().Be(18); + var target = Task.FromResult(Some(13)); + var mapped = await target.Map(number => Task.FromResult(number + 5)); + mapped.Should().BeSome().Which.Should().Be(18); } [TestMethod] public async Task Map_TaskOptionT_FuncTTaskTOut_None_Mapped() { - var target = Task.FromResult(None()); - var mapped = await target.Map(number => Task.FromResult(number + 5)); - mapped.Should().BeNone(); + var target = Task.FromResult(None()); + var mapped = await target.Map(number => Task.FromResult(number + 5)); + mapped.Should().BeNone(); } [TestMethod] public async Task Bind_TaskOptionT_FuncTOptionTOut_Some_BoundToSome() { - var target = Task.FromResult(Some(89)); - var bound = await target.Bind(number => Some(number + 1)); - bound.Should().BeSome().Which.Should().Be(90); + var target = Task.FromResult(Some(89)); + var bound = await target.Bind(number => Some(number + 1)); + bound.Should().BeSome().Which.Should().Be(90); } [TestMethod] public async Task Bind_TaskOptionT_FuncTOptionTOut_Some_BoundToNone() { - var target = Task.FromResult(Some(89)); - var bound = await target.Bind(number => None()); - bound.Should().BeNone(); + var target = Task.FromResult(Some(89)); + var bound = await target.Bind(_ => None()); + bound.Should().BeNone(); } [TestMethod] public async Task Bind_TaskOptionT_FuncTOptionTOut_None_BoundToSome() { - var target = Task.FromResult(None()); - var bound = await target.Bind(number => Some(number + 1)); - bound.Should().BeNone(); + var target = Task.FromResult(None()); + var bound = await target.Bind(number => Some(number + 1)); + bound.Should().BeNone(); } [TestMethod] public async Task Bind_TaskOptionT_FuncTOptionTOut_None_BoundToNone() { - var target = Task.FromResult(None()); - var bound = await target.Bind(_ => None()); - bound.Should().BeNone(); + var target = Task.FromResult(None()); + var bound = await target.Bind(_ => None()); + bound.Should().BeNone(); } [TestMethod] public async Task Bind_TaskOptionT_FuncTTaskOptionTOut_Some_BoundToSome() { - var target = Task.FromResult(Some(14)); - var bound = await target.Bind(number => Task.FromResult(Some(number / 2))); - bound.Should().BeSome().Which.Should().Be(7); + var target = Task.FromResult(Some(14)); + var bound = await target.Bind(number => Task.FromResult(Some(number / 2))); + bound.Should().BeSome().Which.Should().Be(7); } [TestMethod] public async Task Bind_TaskOptionT_FuncTTaskOptionTOut_Some_BoundToNone() { - var target = Task.FromResult(Some(14)); - var bound = await target.Bind(_ => Task.FromResult(None())); - bound.Should().BeNone(); + var target = Task.FromResult(Some(14)); + var bound = await target.Bind(_ => Task.FromResult(None())); + bound.Should().BeNone(); } [TestMethod] public async Task Bind_TaskOptionT_FuncTTaskOptionTOut_None_BoundToSome() { - var target = Task.FromResult(None()); - var bound = await target.Bind(number => Task.FromResult(Some(number / 2))); - bound.Should().BeNone(); + var target = Task.FromResult(None()); + var bound = await target.Bind(number => Task.FromResult(Some(number / 2))); + bound.Should().BeNone(); } [TestMethod] public async Task Bind_TaskOptionT_FuncTTaskOptionTOut_None_BoundToNone() { - var target = Task.FromResult(None()); - var bound = await target.Bind(_ => Task.FromResult(None())); - bound.Should().BeNone(); + var target = Task.FromResult(None()); + var bound = await target.Bind(_ => Task.FromResult(None())); + bound.Should().BeNone(); } [TestMethod] public void Choose_PartiallySome_ExpectedCollectionIsReturned() { - var target = Enumerable.Range(0, 10); - var odds = target.Choose(i => i % 2 != 0 ? i * 10 : Option.None).ToList(); - odds.Should().Equal(10, 30, 50, 70, 90); + var target = Enumerable.Range(0, 10); + var odds = target.Choose(i => i % 2 != 0 ? i * 10 : Option.None).ToList(); + odds.Should().Equal(10, 30, 50, 70, 90); } [TestMethod] public void ToOption_HasValueFunc_Class() { - string? nullTarget = null; + string? nullTarget = null; // ReSharper disable once VariableCanBeNotNullable string? valueTarget = "Hi"; - nullTarget.ToOption(x => x == "Hi").Should().BeNone(); - nullTarget.ToOption(x => x == "Hi").Should().BeNone(); - valueTarget.ToOption(x => x == "Hello").Should().BeNone(); - valueTarget.ToOption(x => x == "Hi").Should().BeSome().Which.Should().Be("Hi"); + nullTarget.ToOption(x => x == "Hello").Should().BeNone(); + nullTarget.ToOption(x => x == "Hi").Should().BeNone(); + valueTarget.ToOption(x => x == "Hello").Should().BeNone(); + valueTarget.ToOption(x => x == "Hi").Should().BeSome().Which.Should().Be("Hi"); } [TestMethod] public void ToOption_HasValueFunc_Struct() { - int? nullTarget = null; - int? valueTarget = 8; + int? nullTarget = null; + nullTarget.ToOption(x => x == 5).Should().BeNone(); + nullTarget.ToOption(x => x == 8).Should().BeNone(); + + int? nullableValueTarget = 11; + nullableValueTarget.ToOption(x => x == 11).Should().BeSome().Which.Should().Be(11); + nullableValueTarget.ToOption(x => x == 5).Should().BeNone(); - nullTarget.ToOption(x => x == 8).Should().BeNone(); - nullTarget.ToOption(x => x == 8).Should().BeNone(); - valueTarget.ToOption(x => x == 5).Should().BeNone(); - valueTarget.ToOption(x => x == 8).Should().BeSome().Which.Should().Be(8); + int valueTarget = 8; + valueTarget.ToOption(x => x == 5).Should().BeNone(); + valueTarget.ToOption(x => x == 8).Should().BeSome().Which.Should().Be(8); } [TestMethod] public void NoneIfEmpty_Null_None() { - string? target = null; - var result = target.NoneIfEmpty(); - result.Should().BeNone(); + string? target = null; + var result = target.NoneIfEmpty(); + result.Should().BeNone(); } [TestMethod] @@ -755,8 +817,8 @@ public void NoneIfEmpty_Empty_None() { // ReSharper disable once VariableCanBeNotNullable string? target = ""; - var result = target.NoneIfEmpty(); - result.Should().BeNone(); + var result = target.NoneIfEmpty(); + result.Should().BeNone(); } [TestMethod] @@ -764,16 +826,8 @@ public void NoneIfEmpty_Text_Some() { // ReSharper disable once VariableCanBeNotNullable string? target = "Hi"; - var result = target.NoneIfEmpty(); - result.Should().BeSome().Which.Should().Be("Hi"); - } - - [TestMethod] - public void WhereSome_CorrectCollectionReturned() - { - IEnumerable> target = [1, None(), 2, None(), 3]; - var result = target.WhereSome(); - result.Should().Equal([1, 2, 3]); + var result = target.NoneIfEmpty(); + result.Should().BeSome().Which.Should().Be("Hi"); } class MyClass; diff --git a/Source/Tests/FunicularSwitch.Test/ResultSpecs.cs b/Source/Tests/FunicularSwitch.Test/ResultSpecs.cs index 564907bc..d5e87409 100644 --- a/Source/Tests/FunicularSwitch.Test/ResultSpecs.cs +++ b/Source/Tests/FunicularSwitch.Test/ResultSpecs.cs @@ -186,7 +186,7 @@ public async Task AsyncAggregateTest() .Aggregate(); result.Should().BeOk() - .Subject.Should().BeEquivalentTo(new[] { 0, 2, 4 }); + .Subject.Should().BeEquivalentTo([0, 2, 4]); } [TestMethod] @@ -249,15 +249,15 @@ select r1 from r in okAsync let x = r * 2 select x - )).Should().BeEquivalentTo(await okAsync.Map(r => r * 2)); - + )).Should().BeEquivalentTo(await okAsync.Map(r => r * 2)); + (await ( from r in ok from r1 in okAsync let x = r * r1 select x )).Should().BeEquivalentTo(await okAsync.Bind(r => ok.Map(r1 => r * r1))); - + (await ( from r in okAsync from r1 in ok @@ -281,14 +281,14 @@ void MyAction() [TestMethod] public async Task AsyncTryVoidReturn() { - async Task MyAction() - { - await Task.Delay(1); - throw new("broken"); - } - - var result = Result.Try(MyAction, ex => ex.Message); - (await result).Should().BeError(); + async Task MyAction() + { + await Task.Delay(1); + throw new("broken"); + } + + var result = Result.Try(MyAction, ex => ex.Message); + (await result).Should().BeError(); } [TestMethod] diff --git a/Source/Tests/FunicularSwitch.Test/TypeExtensionSpecs.cs b/Source/Tests/FunicularSwitch.Test/TypeExtensionSpecs.cs index 0d8b547a..eddc255c 100644 --- a/Source/Tests/FunicularSwitch.Test/TypeExtensionSpecs.cs +++ b/Source/Tests/FunicularSwitch.Test/TypeExtensionSpecs.cs @@ -12,10 +12,10 @@ public void BeautifulName_int() { // Given var type = typeof(int); - + // When var result = type.BeautifulName(); - + // Then result.Should().Be("Int32"); } @@ -25,10 +25,10 @@ public void BeautifulName_List_int() { // Given var type = typeof(List); - + // When var result = type.BeautifulName(); - + // Then result.Should().Be("List"); } @@ -38,10 +38,10 @@ public void BeautifulName_List_WithoutArgument() { // Given var type = typeof(List<>); - + // When var result = type.BeautifulName(); - + // Then result.Should().Contain("List"); } @@ -51,10 +51,10 @@ public void BeautifulName_FuncWithNestedTypes() { // Given var type = typeof(Func, Action>); - + // When var result = type.BeautifulName(); - + // Then result.Should().Be("Func,Action>"); } diff --git a/Source/Tutorial/Main.cs b/Source/Tutorial/Main.cs index df10ac26..ff5ff0c5 100644 --- a/Source/Tutorial/Main.cs +++ b/Source/Tutorial/Main.cs @@ -198,7 +198,7 @@ public async void Tutorial() public Result ParseInt(string input) { var success = int.TryParse(input, out var number); - return success + return success ? Result.Ok(number) : Result.Error("Input was no valid int!"); } @@ -208,8 +208,8 @@ public Result GetDataFromUnreliableSource(string data) Random rnd = new Random(); bool success = rnd.Next(0, 2) == 0; - return success - ? Result.Ok(data) + return success + ? Result.Ok(data) : Result.Error("Something went wrong!"); }