Compare commits
16 Commits
v1.0.0-rc3
...
v1.4.0
| Author | SHA1 | Date | |
|---|---|---|---|
| dd81fecdd6 | |||
| 9c9b734c51 | |||
| f2f0221f76 | |||
| e909eeae10 | |||
| 719b4e85f5 | |||
| 3d34a3021d | |||
| 57e83fbafa | |||
| c02fdc5492 | |||
| ccfa9d8295 | |||
| 127c5ba4eb | |||
| 3a8bf9394e | |||
| e21c35a08a | |||
| 5a2ae19a8e | |||
| 7aaacb0ac7 | |||
| b8ea74ec5b | |||
| 9ae185342b |
@@ -22,13 +22,17 @@ jobs:
|
|||||||
run: dotnet restore Railway/Railway.csproj
|
run: dotnet restore Railway/Railway.csproj
|
||||||
|
|
||||||
- name: Setup nuget source
|
- name: Setup nuget source
|
||||||
run: dotnet nuget add source --name gitea_registry https://gitea.jstdev.ru/api/packages/just/nuget/index.json
|
run: dotnet nuget add source --name gitea_registry ${{ vars.OUTPUT_NUGET_REGISTRY }}
|
||||||
|
|
||||||
- name: Create the package
|
- name: Create the package
|
||||||
env:
|
env:
|
||||||
RELEASE_VERSION: ${{ gitea.ref_name }}
|
RELEASE_VERSION: ${{ gitea.ref_name }}
|
||||||
run: dotnet pack --no-restore --configuration Release --output nupkgs Railway/Railway.csproj `echo $RELEASE_VERSION | sed -E 's|^(v([0-9]+(\.[0-9]+){2}))(-([a-z0-9]+)){1}|/p:ReleaseVersion=\2 /p:VersionSuffix=\5|; s|^(v([0-9]+(\.[0-9]+){2}))$|/p:ReleaseVersion=\2|'`
|
run: >
|
||||||
|
dotnet pack --no-restore --configuration Release --output nupkgs Railway/Railway.csproj
|
||||||
|
`echo $RELEASE_VERSION | sed -E 's|^(v([0-9]+(\.[0-9]+){2}))(-([a-z0-9]+)){1}|/p:ReleaseVersion=\2 /p:VersionSuffix=\5|; s|^(v([0-9]+(\.[0-9]+){2}))$|/p:ReleaseVersion=\2|'`
|
||||||
|
|
||||||
- name: Publish the package to Gitea
|
- name: Publish the package to Gitea
|
||||||
run: dotnet nuget push --source gitea_registry --api-key ${{ secrets.NUGET_PACKAGE_TOKEN }} nupkgs/*.nupkg
|
run: dotnet nuget push --source gitea_registry --api-key ${{ secrets.LOCAL_NUGET_PACKAGE_TOKEN }} nupkgs/*.nupkg
|
||||||
|
|
||||||
|
- name: Publish the package to NuGet.org
|
||||||
|
run: dotnet nuget push --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.NUGET_PACKAGE_TOKEN }} nupkgs/*.nupkg
|
||||||
|
|||||||
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -1,3 +1,4 @@
|
|||||||
{
|
{
|
||||||
"dotnet.defaultSolution": "Just.Railway.sln"
|
"dotnet.defaultSolution": "Just.Railway.sln",
|
||||||
|
"dotnetAcquisitionExtension.enableTelemetry": false
|
||||||
}
|
}
|
||||||
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
|||||||
Copyright (c) 2023 JustFixMe
|
Copyright (c) 2023-2024 JustFixMe
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
|||||||
115
README.md
115
README.md
@@ -4,6 +4,117 @@ This library uses features of C# to achieve railway-oriented programming.
|
|||||||
|
|
||||||
The desire is to make somewhat user-friendly experience while using result-object pattern.
|
The desire is to make somewhat user-friendly experience while using result-object pattern.
|
||||||
|
|
||||||
## Contents
|
## Features
|
||||||
|
|
||||||
_Coming soon..._
|
- Immutable ```Error``` class
|
||||||
|
- ```Result``` object
|
||||||
|
- A bunch of extensions to use result-object pattern with
|
||||||
|
- ```Try``` extensions to wrap function calls with result-object
|
||||||
|
- ```Ensure``` extensions to utilize result-object in validation scenarios
|
||||||
|
|
||||||
|
## Getting Started
|
||||||
|
|
||||||
|
### Install from NuGet.org
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# install the package using NuGet
|
||||||
|
dotnet add package Just.Railway
|
||||||
|
```
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
### Error
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
using Just.Railway;
|
||||||
|
Error expectedError = Error.New(type: "Some Error", message: "Some error detail");
|
||||||
|
Error exceptionalError = Error.New(new Exception("Some Exception"));
|
||||||
|
Error manyErrors = Error.Many(expectedError, exceptionalError);
|
||||||
|
// the same result while using .Append(..) or +
|
||||||
|
manyErrors = expectedError.Append(exceptionalError);
|
||||||
|
manyErrors = expectedError + exceptionalError;
|
||||||
|
```
|
||||||
|
|
||||||
|
> **Note**
|
||||||
|
> You can easily serialize/deserialize Error to and from JSON
|
||||||
|
|
||||||
|
### Result
|
||||||
|
|
||||||
|
#### As return value:
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
Result Foo()
|
||||||
|
{
|
||||||
|
// ...
|
||||||
|
if (SomeCondition())
|
||||||
|
return Result.Failure(Error.New("Some Error"));
|
||||||
|
// or just: return Error.New("Some Error");
|
||||||
|
// ...
|
||||||
|
return Result.Success();
|
||||||
|
}
|
||||||
|
|
||||||
|
Result<T> Bar()
|
||||||
|
{
|
||||||
|
T value;
|
||||||
|
// ...
|
||||||
|
if (SomeCondition())
|
||||||
|
return Error.New("Some Error");
|
||||||
|
// ...
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Consume Result object
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
Result<int> result = GetResult();
|
||||||
|
|
||||||
|
string value = result
|
||||||
|
.Append("new") // -> Result<(int, string)>
|
||||||
|
.Map((i, s) => $"{s} result {i}") // -> Result<string>
|
||||||
|
.Match(
|
||||||
|
onSuccess: x => x,
|
||||||
|
onFailure: err => err.ToString()
|
||||||
|
);
|
||||||
|
// value: "new result 1"
|
||||||
|
|
||||||
|
Result<int> GetResult() => Result.Success(1);
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Recover from failure
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
Result<string> failed = new NotImplementedException();
|
||||||
|
|
||||||
|
Result<string> result = failed.TryRecover(err => err.Type == "System.NotImplementedException"
|
||||||
|
? "recovered"
|
||||||
|
: err);
|
||||||
|
// result with value: "recovered"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Try
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
Result result = Try.Run(SomeAction);
|
||||||
|
// you can pass up to 5 arguments like this
|
||||||
|
result = Try.Run(SomeActionWithArguments, 1, 2.0, "3");
|
||||||
|
|
||||||
|
// you also can call functions
|
||||||
|
Result<int> resultWithValue = Try.Run(SomeFunction);
|
||||||
|
|
||||||
|
void SomeAction() {}
|
||||||
|
void SomeActionWithArguments(int a1, double a2, string? a3) {}
|
||||||
|
int SomeFunction() => 1;
|
||||||
|
```
|
||||||
|
|
||||||
|
### Ensure
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
int? value = GetValue();
|
||||||
|
Result<int> result = Ensure.That(value) // -> Ensure<int?>
|
||||||
|
.NotNull() // -> Ensure<int>
|
||||||
|
.Satisfies(i => i < 100)
|
||||||
|
.Result();
|
||||||
|
|
||||||
|
int? GetValue() => 1;
|
||||||
|
```
|
||||||
|
|||||||
@@ -1,209 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Collections.Immutable;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using Microsoft.CodeAnalysis;
|
|
||||||
|
|
||||||
namespace Just.Railway.SourceGen;
|
|
||||||
|
|
||||||
public sealed class EnsureExtensionExecutor : IGeneratorExecutor
|
|
||||||
{
|
|
||||||
public void Execute(SourceProductionContext context, Compilation source)
|
|
||||||
{
|
|
||||||
var methods = GenerateMethods();
|
|
||||||
var code = $$"""
|
|
||||||
#nullable enable
|
|
||||||
using System;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Diagnostics.Contracts;
|
|
||||||
using System.CodeDom.Compiler;
|
|
||||||
|
|
||||||
namespace Just.Railway;
|
|
||||||
|
|
||||||
public static partial class Ensure
|
|
||||||
{
|
|
||||||
{{methods}}
|
|
||||||
}
|
|
||||||
""";
|
|
||||||
|
|
||||||
context.AddSource("Ensure.Extensions.g.cs", code);
|
|
||||||
}
|
|
||||||
|
|
||||||
private string GenerateMethods()
|
|
||||||
{
|
|
||||||
List<(string ErrorParameterDecl, string ErrorValueExpr)> errorGenerationDefinitions =
|
|
||||||
[
|
|
||||||
("Error error = default!", "error"),
|
|
||||||
("ErrorFactory errorFactory", "errorFactory(ensure.ValueExpression)")
|
|
||||||
];
|
|
||||||
|
|
||||||
var sb = new StringBuilder();
|
|
||||||
|
|
||||||
sb.AppendLine($"#region Satisfies");
|
|
||||||
errorGenerationDefinitions.ForEach(def => GenerateSatisfiesExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
|
||||||
sb.AppendLine("#endregion");
|
|
||||||
|
|
||||||
sb.AppendLine($"#region NotNull");
|
|
||||||
errorGenerationDefinitions.ForEach(def => GenerateNotNullExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
|
||||||
sb.AppendLine("#endregion");
|
|
||||||
|
|
||||||
sb.AppendLine($"#region NotEmpty");
|
|
||||||
errorGenerationDefinitions.ForEach(def => GenerateNotEmptyExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
|
||||||
sb.AppendLine("#endregion");
|
|
||||||
|
|
||||||
|
|
||||||
return sb.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GenerateNotEmptyExtensions(StringBuilder sb, string errorParameterDecl, string errorValueExpr)
|
|
||||||
{
|
|
||||||
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} is empty.\")";
|
|
||||||
List<(string TemplateDef, string CollectionType, string NotEmptyTest)> typeOverloads =
|
|
||||||
[
|
|
||||||
("<T>", "IEnumerable<T>", "ensure.Value?.Any() == true"),
|
|
||||||
("<T>", "ICollection<T>", "ensure.Value?.Count > 0"),
|
|
||||||
("<T>", "IReadOnlyCollection<T>", "ensure.Value?.Count > 0"),
|
|
||||||
("<T>", "IList<T>", "ensure.Value?.Count > 0"),
|
|
||||||
("<T>", "IReadOnlyList<T>", "ensure.Value?.Count > 0"),
|
|
||||||
("<T>", "ISet<T>", "ensure.Value?.Count > 0"),
|
|
||||||
("<T>", "IReadOnlySet<T>", "ensure.Value?.Count > 0"),
|
|
||||||
("<TKey,TValue>", "IDictionary<TKey,TValue>", "ensure.Value?.Count > 0"),
|
|
||||||
("<TKey,TValue>", "IReadOnlyDictionary<TKey,TValue>", "ensure.Value?.Count > 0"),
|
|
||||||
("<T>", "T[]", "ensure.Value?.Length > 0"),
|
|
||||||
("<T>", "List<T>", "ensure.Value?.Count > 0"),
|
|
||||||
("<T>", "Queue<T>", "ensure.Value?.Count > 0"),
|
|
||||||
("<T>", "HashSet<T>", "ensure.Value?.Count > 0"),
|
|
||||||
("", "string", "!string.IsNullOrEmpty(ensure.Value)"),
|
|
||||||
];
|
|
||||||
|
|
||||||
typeOverloads.ForEach(def => sb.AppendLine($$"""
|
|
||||||
[PureAttribute]
|
|
||||||
[GeneratedCodeAttribute("{{nameof(EnsureExtensionExecutor)}}", "1.0.0.0")]
|
|
||||||
public static Ensure<{{def.CollectionType}}> NotEmpty{{def.TemplateDef}}(this in Ensure<{{def.CollectionType}}> ensure, {{errorParameterDecl}})
|
|
||||||
{
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => {{def.NotEmptyTest}}
|
|
||||||
? new(ensure.Value, ensure.ValueExpression)
|
|
||||||
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
"""));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GenerateNotNullExtensions(StringBuilder sb, string errorParameterDecl, string errorValueExpr)
|
|
||||||
{
|
|
||||||
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} is null.\")";
|
|
||||||
|
|
||||||
sb.AppendLine($$"""
|
|
||||||
[PureAttribute]
|
|
||||||
[GeneratedCodeAttribute("{{nameof(EnsureExtensionExecutor)}}", "1.0.0.0")]
|
|
||||||
public static Ensure<T> NotNull<T>(this in Ensure<T?> ensure, {{errorParameterDecl}})
|
|
||||||
where T : struct
|
|
||||||
{
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => ensure.Value.HasValue
|
|
||||||
? new(ensure.Value.Value, ensure.ValueExpression)
|
|
||||||
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
""");
|
|
||||||
sb.AppendLine($$"""
|
|
||||||
[PureAttribute]
|
|
||||||
[GeneratedCodeAttribute("{{nameof(EnsureExtensionExecutor)}}", "1.0.0.0")]
|
|
||||||
public static Ensure<T> NotNull<T>(this in Ensure<T?> ensure, {{errorParameterDecl}})
|
|
||||||
where T : notnull
|
|
||||||
{
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => ensure.Value is not null
|
|
||||||
? new(ensure.Value!, ensure.ValueExpression)
|
|
||||||
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
""");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GenerateSatisfiesExtensions(StringBuilder sb, string errorParameterDecl, string errorValueExpr)
|
|
||||||
{
|
|
||||||
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} does not satisfy the requirement.\")";
|
|
||||||
sb.AppendLine($$"""
|
|
||||||
[PureAttribute]
|
|
||||||
[GeneratedCodeAttribute("{{nameof(EnsureExtensionExecutor)}}", "1.0.0.0")]
|
|
||||||
public static Ensure<T> Satisfies<T>(this in Ensure<T> ensure, Func<T, bool> requirement, {{errorParameterDecl}})
|
|
||||||
{
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => requirement(ensure.Value)
|
|
||||||
? new(ensure.Value, ensure.ValueExpression)
|
|
||||||
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
""");
|
|
||||||
|
|
||||||
GenerateSatisfiesAsyncExtensions(sb, "Task", errorParameterDecl, errorValueExpr, defaultErrorExpr);
|
|
||||||
GenerateSatisfiesAsyncExtensions(sb, "ValueTask", errorParameterDecl, errorValueExpr, defaultErrorExpr);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void GenerateSatisfiesAsyncExtensions(StringBuilder sb, string taskType, string errorParameterDecl, string errorValueExpr, string defaultErrorExpr)
|
|
||||||
{
|
|
||||||
sb.AppendLine($$"""
|
|
||||||
[PureAttribute]
|
|
||||||
[GeneratedCodeAttribute("{{nameof(EnsureExtensionExecutor)}}", "1.0.0.0")]
|
|
||||||
public static async {{taskType}}<Ensure<T>> Satisfies<T>(this {{taskType}}<Ensure<T>> ensureTask, Func<T, bool> requirement, {{errorParameterDecl}})
|
|
||||||
{
|
|
||||||
var ensure = await ensureTask.ConfigureAwait(false);
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => requirement(ensure.Value)
|
|
||||||
? new(ensure.Value, ensure.ValueExpression)
|
|
||||||
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensureTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
""");
|
|
||||||
sb.AppendLine($$"""
|
|
||||||
[PureAttribute]
|
|
||||||
[GeneratedCodeAttribute("{{nameof(EnsureExtensionExecutor)}}", "1.0.0.0")]
|
|
||||||
public static async {{taskType}}<Ensure<T>> Satisfies<T>(this Ensure<T> ensure, Func<T, {{taskType}}<bool>> requirement, {{errorParameterDecl}})
|
|
||||||
{
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => await requirement(ensure.Value).ConfigureAwait(false)
|
|
||||||
? new(ensure.Value, ensure.ValueExpression)
|
|
||||||
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
""");
|
|
||||||
sb.AppendLine($$"""
|
|
||||||
[PureAttribute]
|
|
||||||
[GeneratedCodeAttribute("{{nameof(EnsureExtensionExecutor)}}", "1.0.0.0")]
|
|
||||||
public static async {{taskType}}<Ensure<T>> Satisfies<T>(this {{taskType}}<Ensure<T>> ensureTask, Func<T, {{taskType}}<bool>> requirement, {{errorParameterDecl}})
|
|
||||||
{
|
|
||||||
var ensure = await ensureTask.ConfigureAwait(false);
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => await requirement(ensure.Value).ConfigureAwait(false)
|
|
||||||
? new(ensure.Value, ensure.ValueExpression)
|
|
||||||
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensureTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
""");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
504
Railway.SourceGenerator/EnsureExtensionsExecutor.cs
Normal file
504
Railway.SourceGenerator/EnsureExtensionsExecutor.cs
Normal file
@@ -0,0 +1,504 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Just.Railway.SourceGen;
|
||||||
|
|
||||||
|
public sealed class EnsureExtensionsExecutor : IGeneratorExecutor
|
||||||
|
{
|
||||||
|
public void Execute(SourceProductionContext context, Compilation source)
|
||||||
|
{
|
||||||
|
var methods = GenerateMethods();
|
||||||
|
var code = $$"""
|
||||||
|
#nullable enable
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics.Contracts;
|
||||||
|
using System.CodeDom.Compiler;
|
||||||
|
|
||||||
|
namespace Just.Railway;
|
||||||
|
|
||||||
|
public static partial class Ensure
|
||||||
|
{
|
||||||
|
{{methods}}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
|
||||||
|
context.AddSource("Ensure.Extensions.g.cs", code);
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GenerateMethods()
|
||||||
|
{
|
||||||
|
List<(string ErrorParameterDecl, string ErrorValueExpr)> errorGenerationDefinitions =
|
||||||
|
[
|
||||||
|
("Error error = default!", "error"),
|
||||||
|
("ErrorFactory errorFactory", "errorFactory(ensure.ValueExpression)")
|
||||||
|
];
|
||||||
|
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
|
||||||
|
sb.AppendLine("#region Satisfies");
|
||||||
|
errorGenerationDefinitions.ForEach(def => GenerateSatisfiesExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
||||||
|
sb.AppendLine("#endregion");
|
||||||
|
|
||||||
|
sb.AppendLine("#region Null");
|
||||||
|
errorGenerationDefinitions.ForEach(def => GenerateNullExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
||||||
|
sb.AppendLine("#endregion");
|
||||||
|
|
||||||
|
sb.AppendLine("#region NotNull");
|
||||||
|
errorGenerationDefinitions.ForEach(def => GenerateNotNullExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
||||||
|
sb.AppendLine("#endregion");
|
||||||
|
|
||||||
|
sb.AppendLine("#region NotEmpty");
|
||||||
|
errorGenerationDefinitions.ForEach(def => GenerateNotEmptyExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
||||||
|
sb.AppendLine("#endregion");
|
||||||
|
|
||||||
|
sb.AppendLine("#region NotWhitespace");
|
||||||
|
errorGenerationDefinitions.ForEach(def => GenerateNotWhitespaceExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
||||||
|
sb.AppendLine("#endregion");
|
||||||
|
|
||||||
|
sb.AppendLine("#region True");
|
||||||
|
errorGenerationDefinitions.ForEach(def => GenerateTrueExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
||||||
|
sb.AppendLine("#endregion");
|
||||||
|
|
||||||
|
sb.AppendLine("#region False");
|
||||||
|
errorGenerationDefinitions.ForEach(def => GenerateFalseExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
||||||
|
sb.AppendLine("#endregion");
|
||||||
|
|
||||||
|
sb.AppendLine("#region EqualTo");
|
||||||
|
errorGenerationDefinitions.ForEach(def => GenerateEqualToExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
||||||
|
sb.AppendLine("#endregion");
|
||||||
|
|
||||||
|
sb.AppendLine("#region NotEqualTo");
|
||||||
|
errorGenerationDefinitions.ForEach(def => GenerateNotEqualToExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
||||||
|
sb.AppendLine("#endregion");
|
||||||
|
|
||||||
|
sb.AppendLine("#region LessThan");
|
||||||
|
errorGenerationDefinitions.ForEach(def => GenerateLessThanExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
||||||
|
sb.AppendLine("#endregion");
|
||||||
|
|
||||||
|
sb.AppendLine("#region GreaterThan");
|
||||||
|
errorGenerationDefinitions.ForEach(def => GenerateGreaterThanExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
||||||
|
sb.AppendLine("#endregion");
|
||||||
|
|
||||||
|
sb.AppendLine("#region LessThanOrEqualTo");
|
||||||
|
errorGenerationDefinitions.ForEach(def => GenerateLessThanOrEqualToExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
||||||
|
sb.AppendLine("#endregion");
|
||||||
|
|
||||||
|
sb.AppendLine("#region GreaterThanOrEqualTo");
|
||||||
|
errorGenerationDefinitions.ForEach(def => GenerateGreaterThanOrEqualToExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
||||||
|
sb.AppendLine("#endregion");
|
||||||
|
|
||||||
|
return sb.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateNotWhitespaceExtensions(StringBuilder sb, string errorParameterDecl, string errorValueExpr)
|
||||||
|
{
|
||||||
|
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} is empty or consists exclusively of white-space characters.\")";
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Ensure<string> NotWhitespace(this in Ensure<string> ensure, {{errorParameterDecl}})
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => string.IsNullOrWhiteSpace(ensure.Value)
|
||||||
|
? new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression)
|
||||||
|
: new(ensure.Value!, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateNotEmptyExtensions(StringBuilder sb, string errorParameterDecl, string errorValueExpr)
|
||||||
|
{
|
||||||
|
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} is empty.\")";
|
||||||
|
List<(string TemplateDef, string CollectionType, string NotEmptyTest)> typeOverloads =
|
||||||
|
[
|
||||||
|
("<T>", "IEnumerable<T>", "ensure.Value?.Any() == true"),
|
||||||
|
("<T>", "ICollection<T>", "ensure.Value?.Count > 0"),
|
||||||
|
("<T>", "IReadOnlyCollection<T>", "ensure.Value?.Count > 0"),
|
||||||
|
("<T>", "IList<T>", "ensure.Value?.Count > 0"),
|
||||||
|
("<T>", "IReadOnlyList<T>", "ensure.Value?.Count > 0"),
|
||||||
|
("<T>", "ISet<T>", "ensure.Value?.Count > 0"),
|
||||||
|
("<T>", "IReadOnlySet<T>", "ensure.Value?.Count > 0"),
|
||||||
|
("<TKey,TValue>", "IDictionary<TKey,TValue>", "ensure.Value?.Count > 0"),
|
||||||
|
("<TKey,TValue>", "IReadOnlyDictionary<TKey,TValue>", "ensure.Value?.Count > 0"),
|
||||||
|
("<T>", "T[]", "ensure.Value?.Length > 0"),
|
||||||
|
("<T>", "List<T>", "ensure.Value?.Count > 0"),
|
||||||
|
("<T>", "Queue<T>", "ensure.Value?.Count > 0"),
|
||||||
|
("<T>", "HashSet<T>", "ensure.Value?.Count > 0"),
|
||||||
|
("", "string", "!string.IsNullOrEmpty(ensure.Value)"),
|
||||||
|
];
|
||||||
|
|
||||||
|
typeOverloads.ForEach(def => sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Ensure<{{def.CollectionType}}> NotEmpty{{def.TemplateDef}}(this in Ensure<{{def.CollectionType}}> ensure, {{errorParameterDecl}})
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => {{def.NotEmptyTest}}
|
||||||
|
? new(ensure.Value, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
"""));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateNullExtensions(StringBuilder sb, string errorParameterDecl, string errorValueExpr)
|
||||||
|
{
|
||||||
|
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} is not null.\")";
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Ensure<T?> Null<T>(this in Ensure<T?> ensure, {{errorParameterDecl}})
|
||||||
|
where T : struct
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => !ensure.Value.HasValue
|
||||||
|
? new(default(T?)!, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Ensure<T?> Null<T>(this in Ensure<T?> ensure, {{errorParameterDecl}})
|
||||||
|
where T : class
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => ensure.Value is null
|
||||||
|
? new(default(T?)!, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateNotNullExtensions(StringBuilder sb, string errorParameterDecl, string errorValueExpr)
|
||||||
|
{
|
||||||
|
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} is null.\")";
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Ensure<T> NotNull<T>(this in Ensure<T?> ensure, {{errorParameterDecl}})
|
||||||
|
where T : struct
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => ensure.Value.HasValue
|
||||||
|
? new(ensure.Value.Value, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Ensure<T> NotNull<T>(this in Ensure<T?> ensure, {{errorParameterDecl}})
|
||||||
|
where T : notnull
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => ensure.Value is not null
|
||||||
|
? new(ensure.Value!, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateSatisfiesExtensions(StringBuilder sb, string errorParameterDecl, string errorValueExpr)
|
||||||
|
{
|
||||||
|
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} does not satisfy the requirement.\")";
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Ensure<T> Satisfies<T>(this in Ensure<T> ensure, Func<T, bool> requirement, {{errorParameterDecl}})
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => requirement(ensure.Value)
|
||||||
|
? new(ensure.Value, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
GenerateSatisfiesAsyncExtensions(sb, "Task", errorParameterDecl, errorValueExpr, defaultErrorExpr);
|
||||||
|
GenerateSatisfiesAsyncExtensions(sb, "ValueTask", errorParameterDecl, errorValueExpr, defaultErrorExpr);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateSatisfiesAsyncExtensions(StringBuilder sb, string taskType, string errorParameterDecl, string errorValueExpr, string defaultErrorExpr)
|
||||||
|
{
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<Ensure<T>> Satisfies<T>(this {{taskType}}<Ensure<T>> ensureTask, Func<T, bool> requirement, {{errorParameterDecl}})
|
||||||
|
{
|
||||||
|
var ensure = await ensureTask.ConfigureAwait(false);
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => requirement(ensure.Value)
|
||||||
|
? new(ensure.Value, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensureTask))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<Ensure<T>> Satisfies<T>(this Ensure<T> ensure, Func<T, {{taskType}}<bool>> requirement, {{errorParameterDecl}})
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => await requirement(ensure.Value).ConfigureAwait(false)
|
||||||
|
? new(ensure.Value, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<Ensure<T>> Satisfies<T>(this {{taskType}}<Ensure<T>> ensureTask, Func<T, {{taskType}}<bool>> requirement, {{errorParameterDecl}})
|
||||||
|
{
|
||||||
|
var ensure = await ensureTask.ConfigureAwait(false);
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => await requirement(ensure.Value).ConfigureAwait(false)
|
||||||
|
? new(ensure.Value, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensureTask))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateTrueExtensions(StringBuilder sb, string errorParameterDecl, string errorValueExpr)
|
||||||
|
{
|
||||||
|
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} is not true.\")";
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Ensure<bool> True(this in Ensure<bool> ensure, {{errorParameterDecl}})
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => ensure.Value == true
|
||||||
|
? new(ensure.Value!, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Ensure<bool> True(this in Ensure<bool?> ensure, {{errorParameterDecl}})
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => ensure.Value == true
|
||||||
|
? new(ensure.Value.Value, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateFalseExtensions(StringBuilder sb, string errorParameterDecl, string errorValueExpr)
|
||||||
|
{
|
||||||
|
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} is not false.\")";
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Ensure<bool> False(this in Ensure<bool> ensure, {{errorParameterDecl}})
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => ensure.Value == false
|
||||||
|
? new(ensure.Value!, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Ensure<bool> False(this in Ensure<bool?> ensure, {{errorParameterDecl}})
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => ensure.Value == false
|
||||||
|
? new(ensure.Value.Value, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateEqualToExtensions(StringBuilder sb, string errorParameterDecl, string errorValueExpr)
|
||||||
|
{
|
||||||
|
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} is not equal to requirement.\")";
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Ensure<T> EqualTo<T>(this in Ensure<T> ensure, T requirement, {{errorParameterDecl}})
|
||||||
|
where T : IEquatable<T>
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => ensure.Value.Equals(requirement)
|
||||||
|
? new(ensure.Value, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateNotEqualToExtensions(StringBuilder sb, string errorParameterDecl, string errorValueExpr)
|
||||||
|
{
|
||||||
|
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} is equal to requirement.\")";
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Ensure<T> NotEqualTo<T>(this in Ensure<T> ensure, T requirement, {{errorParameterDecl}})
|
||||||
|
where T : IEquatable<T>
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => !ensure.Value.Equals(requirement)
|
||||||
|
? new(ensure.Value, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateLessThanExtensions(StringBuilder sb, string errorParameterDecl, string errorValueExpr)
|
||||||
|
{
|
||||||
|
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} is not less than requirement.\")";
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Ensure<T> LessThan<T>(this in Ensure<T> ensure, T requirement, {{errorParameterDecl}})
|
||||||
|
where T : IComparable<T>
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => ensure.Value.CompareTo(requirement) < 0
|
||||||
|
? new(ensure.Value, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateGreaterThanExtensions(StringBuilder sb, string errorParameterDecl, string errorValueExpr)
|
||||||
|
{
|
||||||
|
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} is not greater than requirement.\")";
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Ensure<T> GreaterThan<T>(this in Ensure<T> ensure, T requirement, {{errorParameterDecl}})
|
||||||
|
where T : IComparable<T>
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => ensure.Value.CompareTo(requirement) > 0
|
||||||
|
? new(ensure.Value, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateLessThanOrEqualToExtensions(StringBuilder sb, string errorParameterDecl, string errorValueExpr)
|
||||||
|
{
|
||||||
|
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} is greater than requirement.\")";
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Ensure<T> LessThanOrEqualTo<T>(this in Ensure<T> ensure, T requirement, {{errorParameterDecl}})
|
||||||
|
where T : IComparable<T>
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => ensure.Value.CompareTo(requirement) <= 0
|
||||||
|
? new(ensure.Value, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void GenerateGreaterThanOrEqualToExtensions(StringBuilder sb, string errorParameterDecl, string errorValueExpr)
|
||||||
|
{
|
||||||
|
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} is less than requirement.\")";
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(EnsureExtensionsExecutor)}}", "1.0.0.0")]
|
||||||
|
public static Ensure<T> GreaterThanOrEqualTo<T>(this in Ensure<T> ensure, T requirement, {{errorParameterDecl}})
|
||||||
|
where T : IComparable<T>
|
||||||
|
{
|
||||||
|
return ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => ensure.Value.CompareTo(requirement) >= 0
|
||||||
|
? new(ensure.Value, ensure.ValueExpression)
|
||||||
|
: new({{errorValueExpr}} {{defaultErrorExpr}}, ensure.ValueExpression),
|
||||||
|
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,4 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Immutable;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
namespace Just.Railway.SourceGen;
|
namespace Just.Railway.SourceGen;
|
||||||
@@ -17,9 +13,11 @@ public class ExtensionsMethodGenerator : IIncrementalGenerator
|
|||||||
new ResultMapExecutor(),
|
new ResultMapExecutor(),
|
||||||
new ResultBindExecutor(),
|
new ResultBindExecutor(),
|
||||||
new ResultTapExecutor(),
|
new ResultTapExecutor(),
|
||||||
|
new ResultExtendExecutor(),
|
||||||
|
new ResultTryRecoverExecutor(),
|
||||||
new ResultAppendExecutor(),
|
new ResultAppendExecutor(),
|
||||||
new TryExtensionsExecutor(),
|
new TryExtensionsExecutor(),
|
||||||
new EnsureExtensionExecutor(),
|
new EnsureExtensionsExecutor(),
|
||||||
};
|
};
|
||||||
|
|
||||||
public void Initialize(IncrementalGeneratorInitializationContext context)
|
public void Initialize(IncrementalGeneratorInitializationContext context)
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
using System;
|
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
namespace Just.Railway.SourceGen;
|
namespace Just.Railway.SourceGen;
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
||||||
|
|
||||||
namespace Just.Railway.SourceGen;
|
namespace Just.Railway.SourceGen;
|
||||||
|
|
||||||
@@ -200,6 +198,48 @@ internal sealed class ResultAppendExecutor : ResultExtensionsExecutor
|
|||||||
string resultExpandedTypeDef = GenerateResultTypeDef(expandedTemplateArgNames);
|
string resultExpandedTypeDef = GenerateResultTypeDef(expandedTemplateArgNames);
|
||||||
string methodExpandedTemplateDecl = GenerateTemplateDecl(expandedTemplateArgNames);
|
string methodExpandedTemplateDecl = GenerateTemplateDecl(expandedTemplateArgNames);
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultAppendExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<{{resultExpandedTypeDef}}> Append{{methodExpandedTemplateDecl}}(this {{taskType}}<{{resultTypeDef}}> resultTask, TNext next)
|
||||||
|
{
|
||||||
|
var result = await resultTask.ConfigureAwait(false);
|
||||||
|
return result.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => Result.Success({{JoinArguments(resultValueExpansion, "next")}}),
|
||||||
|
ResultState.Error => result.Error!,
|
||||||
|
_ => throw new ResultNotInitializedException(nameof(result))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultAppendExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<{{resultExpandedTypeDef}}> Append{{methodExpandedTemplateDecl}}(this {{taskType}}<{{resultTypeDef}}> resultTask, Result<TNext> next)
|
||||||
|
{
|
||||||
|
var result = await resultTask.ConfigureAwait(false);
|
||||||
|
if ((result.State & next.State) == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(string.Join(';', GetBottom(result.State, next.State)));
|
||||||
|
}
|
||||||
|
|
||||||
|
Error? error = null;
|
||||||
|
if (result.IsFailure)
|
||||||
|
{
|
||||||
|
error += result.Error;
|
||||||
|
}
|
||||||
|
if (next.IsFailure)
|
||||||
|
{
|
||||||
|
error += next.Error;
|
||||||
|
}
|
||||||
|
|
||||||
|
return error is null
|
||||||
|
? Result.Success({{JoinArguments(resultValueExpansion, "next.Value")}})
|
||||||
|
: error;
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
sb.AppendLine($$"""
|
sb.AppendLine($$"""
|
||||||
[PureAttribute]
|
[PureAttribute]
|
||||||
[GeneratedCodeAttribute("{{nameof(ResultAppendExecutor)}}", "1.0.0.0")]
|
[GeneratedCodeAttribute("{{nameof(ResultAppendExecutor)}}", "1.0.0.0")]
|
||||||
@@ -420,12 +460,4 @@ internal sealed class ResultAppendExecutor : ResultExtensionsExecutor
|
|||||||
}
|
}
|
||||||
""");
|
""");
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static string JoinArguments(string arg1, string arg2) => (arg1, arg2) switch
|
|
||||||
{
|
|
||||||
("", "") => "",
|
|
||||||
(string arg, "") => arg,
|
|
||||||
("", string arg) => arg,
|
|
||||||
_ => $"{arg1}, {arg2}"
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|||||||
160
Railway.SourceGenerator/ResultExtendExecutor.cs
Normal file
160
Railway.SourceGenerator/ResultExtendExecutor.cs
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
using System.Collections.Immutable;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Just.Railway.SourceGen;
|
||||||
|
|
||||||
|
internal sealed class ResultExtendExecutor : ResultExtensionsExecutor
|
||||||
|
{
|
||||||
|
protected override string ExtensionType => "Extend";
|
||||||
|
|
||||||
|
protected override void GenerateMethodsForArgCount(StringBuilder sb, int argCount)
|
||||||
|
{
|
||||||
|
if (argCount == 0 || argCount == Constants.MaxResultTupleSize)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var templateArgNames = Enumerable.Range(1, argCount)
|
||||||
|
.Select(i => $"T{i}")
|
||||||
|
.ToImmutableArray();
|
||||||
|
|
||||||
|
var expandedTemplateArgNames = templateArgNames.Add("R");
|
||||||
|
string resultTypeDef = GenerateResultTypeDef(templateArgNames);
|
||||||
|
string resultValueExpansion = GenerateResultValueExpansion(templateArgNames);
|
||||||
|
string resultExpandedTypeDef = GenerateResultTypeDef(expandedTemplateArgNames);
|
||||||
|
string methodTemplateDecl = GenerateTemplateDecl(expandedTemplateArgNames);
|
||||||
|
string bindTemplateDecl = GenerateTemplateDecl(templateArgNames.Add("Result<R>"));
|
||||||
|
|
||||||
|
sb.AppendLine($"#region {resultTypeDef}");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultExtendExecutor)}}", "1.0.0.0")]
|
||||||
|
public static {{resultExpandedTypeDef}} Extend{{methodTemplateDecl}}(this in {{resultTypeDef}} result, Func{{bindTemplateDecl}} extensionFunc)
|
||||||
|
{
|
||||||
|
if (result.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(result));
|
||||||
|
}
|
||||||
|
else if (result.IsFailure)
|
||||||
|
{
|
||||||
|
return result.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
var extension = extensionFunc({{resultValueExpansion}});
|
||||||
|
if (extension.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(extensionFunc));
|
||||||
|
}
|
||||||
|
else if (extension.IsFailure)
|
||||||
|
{
|
||||||
|
return extension.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.Success({{JoinArguments(resultValueExpansion, "extension.Value")}});
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
GenerateAsyncMethods("Task", sb, templateArgNames, resultTypeDef, resultValueExpansion);
|
||||||
|
GenerateAsyncMethods("ValueTask", sb, templateArgNames, resultTypeDef, resultValueExpansion);
|
||||||
|
|
||||||
|
sb.AppendLine("#endregion");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void GenerateAsyncMethods(string taskType, StringBuilder sb, ImmutableArray<string> templateArgNames, string resultTypeDef, string resultValueExpansion)
|
||||||
|
{
|
||||||
|
var expandedTemplateArgNames = templateArgNames.Add("R");
|
||||||
|
string resultExpandedTypeDef = GenerateResultTypeDef(expandedTemplateArgNames);
|
||||||
|
string methodTemplateDecl = GenerateTemplateDecl(expandedTemplateArgNames);
|
||||||
|
string bindTemplateDecl = GenerateTemplateDecl(templateArgNames.Add("Result<R>"));
|
||||||
|
string asyncActionTemplateDecl = GenerateTemplateDecl(templateArgNames.Add($"{taskType}<Result<R>>"));
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultExtendExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<{{resultExpandedTypeDef}}> Extend{{methodTemplateDecl}}(this {{taskType}}<{{resultTypeDef}}> resultTask, Func{{bindTemplateDecl}} extensionFunc)
|
||||||
|
{
|
||||||
|
var result = await resultTask.ConfigureAwait(false);
|
||||||
|
if (result.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(resultTask));
|
||||||
|
}
|
||||||
|
else if (result.IsFailure)
|
||||||
|
{
|
||||||
|
return result.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
var extension = extensionFunc({{resultValueExpansion}});
|
||||||
|
if (extension.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(extensionFunc));
|
||||||
|
}
|
||||||
|
else if (extension.IsFailure)
|
||||||
|
{
|
||||||
|
return extension.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.Success({{JoinArguments(resultValueExpansion, "extension.Value")}});
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultExtendExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<{{resultExpandedTypeDef}}> Extend{{methodTemplateDecl}}(this {{resultTypeDef}} result, Func{{asyncActionTemplateDecl}} extensionFunc)
|
||||||
|
{
|
||||||
|
if (result.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(result));
|
||||||
|
}
|
||||||
|
else if (result.IsFailure)
|
||||||
|
{
|
||||||
|
return result.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
var extension = await extensionFunc({{resultValueExpansion}}).ConfigureAwait(false);
|
||||||
|
if (extension.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(extensionFunc));
|
||||||
|
}
|
||||||
|
else if (extension.IsFailure)
|
||||||
|
{
|
||||||
|
return extension.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.Success({{JoinArguments(resultValueExpansion, "extension.Value")}});
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultExtendExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<{{resultExpandedTypeDef}}> Extend{{methodTemplateDecl}}(this {{taskType}}<{{resultTypeDef}}> resultTask, Func{{asyncActionTemplateDecl}} extensionFunc)
|
||||||
|
{
|
||||||
|
var result = await resultTask.ConfigureAwait(false);
|
||||||
|
if (result.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(resultTask));
|
||||||
|
}
|
||||||
|
else if (result.IsFailure)
|
||||||
|
{
|
||||||
|
return result.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
var extension = await extensionFunc({{resultValueExpansion}}).ConfigureAwait(false);
|
||||||
|
if (extension.State == ResultState.Bottom)
|
||||||
|
{
|
||||||
|
throw new ResultNotInitializedException(nameof(extensionFunc));
|
||||||
|
}
|
||||||
|
else if (extension.IsFailure)
|
||||||
|
{
|
||||||
|
return extension.Error!;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.Success({{JoinArguments(resultValueExpansion, "extension.Value")}});
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -50,6 +50,13 @@ internal abstract class ResultExtensionsExecutor : IGeneratorExecutor
|
|||||||
1 => $"Result<{string.Join(", ", templateArgNames)}>",
|
1 => $"Result<{string.Join(", ", templateArgNames)}>",
|
||||||
_ => $"Result<({string.Join(", ", templateArgNames)})>",
|
_ => $"Result<({string.Join(", ", templateArgNames)})>",
|
||||||
};
|
};
|
||||||
|
protected static string JoinArguments(string arg1, string arg2) => (arg1, arg2) switch
|
||||||
|
{
|
||||||
|
("", "") => "",
|
||||||
|
(string arg, "") => arg,
|
||||||
|
("", string arg) => arg,
|
||||||
|
_ => $"{arg1}, {arg2}"
|
||||||
|
};
|
||||||
|
|
||||||
protected static string GenerateResultValueExpansion(ImmutableArray<string> templateArgNames)
|
protected static string GenerateResultValueExpansion(ImmutableArray<string> templateArgNames)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|||||||
85
Railway.SourceGenerator/ResultTryRecoverExecutor.cs
Normal file
85
Railway.SourceGenerator/ResultTryRecoverExecutor.cs
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
using System.Collections.Immutable;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Just.Railway.SourceGen;
|
||||||
|
|
||||||
|
internal sealed class ResultTryRecoverExecutor : ResultExtensionsExecutor
|
||||||
|
{
|
||||||
|
protected override string ExtensionType => "TryRecover";
|
||||||
|
protected override void GenerateMethodsForArgCount(StringBuilder sb, int argCount)
|
||||||
|
{
|
||||||
|
if (argCount > 1) return;
|
||||||
|
|
||||||
|
var templateArgNames = Enumerable.Repeat("T", argCount)
|
||||||
|
.ToImmutableArray();
|
||||||
|
|
||||||
|
string methodTemplateDecl = GenerateTemplateDecl(templateArgNames);
|
||||||
|
string resultTypeDef = GenerateResultTypeDef(templateArgNames);
|
||||||
|
|
||||||
|
sb.AppendLine($"#region {resultTypeDef}");
|
||||||
|
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultTryRecoverExecutor)}}", "1.0.0.0")]
|
||||||
|
public static {{resultTypeDef}} TryRecover{{methodTemplateDecl}}(this in {{resultTypeDef}} result, Func<Error, {{resultTypeDef}}> recover)
|
||||||
|
{
|
||||||
|
return result.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => ({{resultTypeDef}})result.Value,
|
||||||
|
ResultState.Error => recover(result.Error!),
|
||||||
|
_ => throw new ResultNotInitializedException(nameof(result))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
|
||||||
|
GenerateAsyncMethods("Task", sb, templateArgNames, resultTypeDef, methodTemplateDecl);
|
||||||
|
GenerateAsyncMethods("ValueTask", sb, templateArgNames, resultTypeDef, methodTemplateDecl);
|
||||||
|
|
||||||
|
sb.AppendLine("#endregion");
|
||||||
|
}
|
||||||
|
private static void GenerateAsyncMethods(string taskType, StringBuilder sb, ImmutableArray<string> templateArgNames, string resultTypeDef, string methodTemplateDecl)
|
||||||
|
{
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultTryRecoverExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<{{resultTypeDef}}> TryRecover{{methodTemplateDecl}}(this {{taskType}}<{{resultTypeDef}}> resultTask, Func<Error, {{resultTypeDef}}> recover)
|
||||||
|
{
|
||||||
|
var result = await resultTask.ConfigureAwait(false);
|
||||||
|
return result.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => ({{resultTypeDef}})result.Value,
|
||||||
|
ResultState.Error => recover(result.Error!),
|
||||||
|
_ => throw new ResultNotInitializedException(nameof(resultTask))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultTryRecoverExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<{{resultTypeDef}}> TryRecover{{methodTemplateDecl}}(this {{resultTypeDef}} result, Func<Error, {{taskType}}<{{resultTypeDef}}>> recover)
|
||||||
|
{
|
||||||
|
return result.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => ({{resultTypeDef}})result.Value,
|
||||||
|
ResultState.Error => await recover(result.Error!).ConfigureAwait(false),
|
||||||
|
_ => throw new ResultNotInitializedException(nameof(result))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
sb.AppendLine($$"""
|
||||||
|
[PureAttribute]
|
||||||
|
[GeneratedCodeAttribute("{{nameof(ResultTryRecoverExecutor)}}", "1.0.0.0")]
|
||||||
|
public static async {{taskType}}<{{resultTypeDef}}> TryRecover{{methodTemplateDecl}}(this {{taskType}}<{{resultTypeDef}}> resultTask, Func<Error, {{taskType}}<{{resultTypeDef}}>> recover)
|
||||||
|
{
|
||||||
|
var result = await resultTask.ConfigureAwait(false);
|
||||||
|
return result.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => ({{resultTypeDef}})result.Value,
|
||||||
|
ResultState.Error => await recover(result.Error!).ConfigureAwait(false),
|
||||||
|
_ => throw new ResultNotInitializedException(nameof(resultTask))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
""");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|||||||
@@ -8,44 +8,9 @@ public static partial class Ensure
|
|||||||
|
|
||||||
[Pure] public static Ensure<T> That<T>(T value, [CallerArgumentExpression(nameof(value))]string valueExpression = "") => new(value, valueExpression);
|
[Pure] public static Ensure<T> That<T>(T value, [CallerArgumentExpression(nameof(value))]string valueExpression = "") => new(value, valueExpression);
|
||||||
|
|
||||||
[Pure] public static Result<T> Result<T>(this in Ensure<T> ensure) => ensure.State switch
|
[Pure] public static Result<T> Result<T>(this in Ensure<T> ensure) => ensure;
|
||||||
{
|
[Pure] public static async Task<Result<T>> Result<T>(this Task<Ensure<T>> ensureTask) => await ensureTask.ConfigureAwait(false);
|
||||||
ResultState.Success => new(ensure.Value),
|
[Pure] public static async ValueTask<Result<T>> Result<T>(this ValueTask<Ensure<T>> ensureTask) => await ensureTask.ConfigureAwait(false);
|
||||||
ResultState.Error => new(ensure.Error!),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
|
||||||
};
|
|
||||||
[Pure] public static async Task<Result<T>> Result<T>(this Task<Ensure<T>> ensureTask)
|
|
||||||
{
|
|
||||||
var ensure = await ensureTask.ConfigureAwait(false);
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => new(ensure.Value),
|
|
||||||
ResultState.Error => new(ensure.Error!),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensureTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
[Pure] public static async ValueTask<Result<T>> Result<T>(this ValueTask<Ensure<T>> ensureTask)
|
|
||||||
{
|
|
||||||
var ensure = await ensureTask.ConfigureAwait(false);
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => new(ensure.Value),
|
|
||||||
ResultState.Error => new(ensure.Error!),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensureTask))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
[Pure] public static Ensure<string> NotWhitespace(this in Ensure<string> ensure, Error error = default!)
|
|
||||||
{
|
|
||||||
return ensure.State switch
|
|
||||||
{
|
|
||||||
ResultState.Success => string.IsNullOrWhiteSpace(ensure.Value)
|
|
||||||
? new(error ?? Error.New(DefaultErrorType, $"Value {{{ensure.ValueExpression}}} is empty or consists exclusively of white-space characters."), ensure.ValueExpression)
|
|
||||||
: new(ensure.Value, ensure.ValueExpression),
|
|
||||||
ResultState.Error => new(ensure.Error!, ensure.ValueExpression),
|
|
||||||
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly struct Ensure<T>
|
public readonly struct Ensure<T>
|
||||||
@@ -60,6 +25,7 @@ public readonly struct Ensure<T>
|
|||||||
Value = value;
|
Value = value;
|
||||||
ValueExpression = valueExpression;
|
ValueExpression = valueExpression;
|
||||||
State = ResultState.Success;
|
State = ResultState.Success;
|
||||||
|
Error = default;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal Ensure(Error error, string valueExpression)
|
internal Ensure(Error error, string valueExpression)
|
||||||
@@ -69,10 +35,31 @@ public readonly struct Ensure<T>
|
|||||||
Value = default!;
|
Value = default!;
|
||||||
State = ResultState.Error;
|
State = ResultState.Error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Pure]
|
||||||
|
public static implicit operator Result<T>(in Ensure<T> ensure) => ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => new(ensure.Value),
|
||||||
|
ResultState.Error => new(ensure.Error!),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
|
|
||||||
|
[Pure]
|
||||||
|
public static explicit operator Result(in Ensure<T> ensure) => ensure.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => new(null),
|
||||||
|
ResultState.Error => new(ensure.Error!),
|
||||||
|
_ => throw new EnsureNotInitializedException(nameof(ensure))
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class EnsureNotInitializedException(string variableName = "this") : InvalidOperationException("Ensure was not properly initialized.")
|
public class EnsureNotInitializedException : InvalidOperationException
|
||||||
{
|
{
|
||||||
public string VariableName { get; } = variableName;
|
public EnsureNotInitializedException(string variableName = "this")
|
||||||
|
: base("Ensure was not properly initialized.")
|
||||||
|
{
|
||||||
|
VariableName = variableName;
|
||||||
|
}
|
||||||
|
public string VariableName { get; }
|
||||||
}
|
}
|
||||||
|
|||||||
240
Railway/Error.cs
240
Railway/Error.cs
@@ -1,17 +1,12 @@
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Runtime.Serialization;
|
using System.Collections.Immutable;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace Just.Railway;
|
namespace Just.Railway;
|
||||||
|
|
||||||
[JsonPolymorphic(TypeDiscriminatorPropertyName = "$$err")]
|
[JsonConverter(typeof(ErrorJsonConverter))]
|
||||||
[JsonDerivedType(typeof(ExpectedError), typeDiscriminator: 0)]
|
|
||||||
[JsonDerivedType(typeof(ExceptionalError), typeDiscriminator: 1)]
|
|
||||||
[JsonDerivedType(typeof(ManyErrors))]
|
|
||||||
public abstract class Error : IEquatable<Error>, IComparable<Error>
|
public abstract class Error : IEquatable<Error>, IComparable<Error>
|
||||||
{
|
{
|
||||||
private IDictionary<string, object>? _extensionData;
|
|
||||||
|
|
||||||
protected internal Error(){}
|
protected internal Error(){}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -34,16 +29,22 @@ public abstract class Error : IEquatable<Error>, IComparable<Error>
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="message">Error detail</param>
|
/// <param name="message">Error detail</param>
|
||||||
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static Error New(string message) =>
|
public static Error New(string message, IEnumerable<KeyValuePair<string, string>>? extensionData = null) =>
|
||||||
new ExpectedError("error", message);
|
new ExpectedError("error", message)
|
||||||
|
{
|
||||||
|
ExtensionData = extensionData?.ToImmutableDictionary() ?? ImmutableDictionary<string, string>.Empty
|
||||||
|
};
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create an <see cref="ExpectedError"/>
|
/// Create an <see cref="ExpectedError"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="type">Error code</param>
|
/// <param name="type">Error code</param>
|
||||||
/// <param name="message">Error detail</param>
|
/// <param name="message">Error detail</param>
|
||||||
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static Error New(string type, string message) =>
|
public static Error New(string type, string message, IEnumerable<KeyValuePair<string, string>>? extensionData = null) =>
|
||||||
new ExpectedError(type, message);
|
new ExpectedError(type, message)
|
||||||
|
{
|
||||||
|
ExtensionData = extensionData?.ToImmutableDictionary() ?? ImmutableDictionary<string, string>.Empty
|
||||||
|
};
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Create a <see cref="ManyErrors"/>
|
/// Create a <see cref="ManyErrors"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -51,7 +52,7 @@ public abstract class Error : IEquatable<Error>, IComparable<Error>
|
|||||||
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static Error Many(Error error1, Error error2) => (error1, error2) switch
|
public static Error Many(Error error1, Error error2) => (error1, error2) switch
|
||||||
{
|
{
|
||||||
(null, null) => new ManyErrors(Enumerable.Empty<Error>()),
|
(null, null) => new ManyErrors(ImmutableArray<Error>.Empty),
|
||||||
(Error err, null) => err,
|
(Error err, null) => err,
|
||||||
(Error err, { IsEmpty: true }) => err,
|
(Error err, { IsEmpty: true }) => err,
|
||||||
(null, Error err) => err,
|
(null, Error err) => err,
|
||||||
@@ -77,33 +78,14 @@ public abstract class Error : IEquatable<Error>, IComparable<Error>
|
|||||||
|
|
||||||
[Pure] public abstract string Type { get; }
|
[Pure] public abstract string Type { get; }
|
||||||
[Pure] public abstract string Message { get; }
|
[Pure] public abstract string Message { get; }
|
||||||
[Pure, JsonExtensionData] public IDictionary<string, object> ExtensionData
|
|
||||||
{
|
|
||||||
get => _extensionData ??= new Dictionary<string, object>();
|
|
||||||
init => _extensionData = value ?? new Dictionary<string, object>();
|
|
||||||
}
|
|
||||||
[Pure] public object? this[string name]
|
|
||||||
{
|
|
||||||
get => _extensionData?.TryGetValue(name, out var val) == true ? val : null;
|
|
||||||
|
|
||||||
set
|
[Pure] public ImmutableDictionary<string, string> ExtensionData { get; internal init; } = ImmutableDictionary<string, string>.Empty;
|
||||||
{
|
[Pure] public string? this[string key] => ExtensionData.TryGetValue(key, out var value) == true ? value : null;
|
||||||
if (value is null)
|
|
||||||
{
|
|
||||||
_extensionData?.Remove(name);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_extensionData ??= new Dictionary<string, object>();
|
|
||||||
_extensionData[name] = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Pure, JsonIgnore] public abstract int Count { get; }
|
[Pure] public abstract int Count { get; }
|
||||||
[Pure, JsonIgnore] public abstract bool IsEmpty { get; }
|
[Pure] public abstract bool IsEmpty { get; }
|
||||||
[Pure, JsonIgnore] public abstract bool IsExpected { get; }
|
[Pure] public abstract bool IsExpected { get; }
|
||||||
[Pure, JsonIgnore] public abstract bool IsExeptional { get; }
|
[Pure] public abstract bool IsExeptional { get; }
|
||||||
|
|
||||||
[Pure] public Error Append(Error? next)
|
[Pure] public Error Append(Error? next)
|
||||||
{
|
{
|
||||||
@@ -155,17 +137,19 @@ public abstract class Error : IEquatable<Error>, IComparable<Error>
|
|||||||
return string.Compare(Message, other.Message);
|
return string.Compare(Message, other.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Pure] public override string ToString() => Message;
|
[Pure] public sealed override string ToString() => Message;
|
||||||
[Pure] public void Deconstruct(out string type, out string message)
|
[Pure] public void Deconstruct(out string type, out string message)
|
||||||
{
|
{
|
||||||
type = Type;
|
type = Type;
|
||||||
message = Message;
|
message = Message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)] internal virtual Error AccessUnsafe(int position) => this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[JsonConverter(typeof(ExpectedErrorJsonConverter))]
|
||||||
public sealed class ExpectedError : Error
|
public sealed class ExpectedError : Error
|
||||||
{
|
{
|
||||||
[JsonConstructor]
|
|
||||||
public ExpectedError(string type, string message)
|
public ExpectedError(string type, string message)
|
||||||
{
|
{
|
||||||
Type = type;
|
Type = type;
|
||||||
@@ -179,10 +163,10 @@ public sealed class ExpectedError : Error
|
|||||||
[Pure] public override string Type { get; }
|
[Pure] public override string Type { get; }
|
||||||
[Pure] public override string Message { get; }
|
[Pure] public override string Message { get; }
|
||||||
|
|
||||||
[Pure, JsonIgnore] public override int Count => 1;
|
[Pure] public override int Count => 1;
|
||||||
[Pure, JsonIgnore] public override bool IsEmpty => false;
|
[Pure] public override bool IsEmpty => false;
|
||||||
[Pure, JsonIgnore] public override bool IsExpected => true;
|
[Pure] public override bool IsExpected => true;
|
||||||
[Pure, JsonIgnore] public override bool IsExeptional => false;
|
[Pure] public override bool IsExeptional => false;
|
||||||
|
|
||||||
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public override IEnumerable<Error> ToEnumerable()
|
public override IEnumerable<Error> ToEnumerable()
|
||||||
@@ -191,24 +175,27 @@ public sealed class ExpectedError : Error
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[JsonConverter(typeof(ExceptionalErrorJsonConverter))]
|
||||||
public sealed class ExceptionalError : Error
|
public sealed class ExceptionalError : Error
|
||||||
{
|
{
|
||||||
internal readonly Exception? Exception;
|
internal readonly Exception? Exception;
|
||||||
|
|
||||||
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
internal static string ToErrorType(Type exceptionType) => exceptionType.FullName ?? exceptionType.Name;
|
||||||
|
|
||||||
internal ExceptionalError(Exception exception)
|
internal ExceptionalError(Exception exception)
|
||||||
: this(exception.GetType().Name, exception.Message)
|
: this(ToErrorType(exception.GetType()), exception.Message)
|
||||||
{
|
{
|
||||||
Exception = exception;
|
Exception = exception;
|
||||||
FillExtensionData(exception);
|
ExtensionData = ExtractExtensionData(exception);
|
||||||
}
|
}
|
||||||
internal ExceptionalError(string message, Exception exception)
|
internal ExceptionalError(string message, Exception exception)
|
||||||
: this(exception.GetType().Name, message)
|
: this(ToErrorType(exception.GetType()), message)
|
||||||
{
|
{
|
||||||
Exception = exception;
|
Exception = exception;
|
||||||
FillExtensionData(exception);
|
ExtensionData = ExtractExtensionData(exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
[JsonConstructor]
|
|
||||||
public ExceptionalError(string type, string message)
|
public ExceptionalError(string type, string message)
|
||||||
{
|
{
|
||||||
Type = type;
|
Type = type;
|
||||||
@@ -218,10 +205,10 @@ public sealed class ExceptionalError : Error
|
|||||||
[Pure] public override string Type { get; }
|
[Pure] public override string Type { get; }
|
||||||
[Pure] public override string Message { get; }
|
[Pure] public override string Message { get; }
|
||||||
|
|
||||||
[Pure, JsonIgnore] public override int Count => 1;
|
[Pure] public override int Count => 1;
|
||||||
[Pure, JsonIgnore] public override bool IsEmpty => false;
|
[Pure] public override bool IsEmpty => false;
|
||||||
[Pure, JsonIgnore] public override bool IsExpected => false;
|
[Pure] public override bool IsExpected => false;
|
||||||
[Pure, JsonIgnore] public override bool IsExeptional => true;
|
[Pure] public override bool IsExeptional => true;
|
||||||
|
|
||||||
[Pure] public override Exception ToException() => Exception ?? base.ToException();
|
[Pure] public override Exception ToException() => Exception ?? base.ToException();
|
||||||
|
|
||||||
@@ -231,69 +218,92 @@ public sealed class ExceptionalError : Error
|
|||||||
yield return this;
|
yield return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void FillExtensionData(Exception exception)
|
private static ImmutableDictionary<string, string> ExtractExtensionData(Exception exception)
|
||||||
{
|
{
|
||||||
|
if (!(exception.Data?.Count > 0))
|
||||||
|
return ImmutableDictionary<string, string>.Empty;
|
||||||
|
|
||||||
|
ImmutableDictionary<string, string>.Builder? values = null;
|
||||||
|
|
||||||
foreach (var key in exception.Data.Keys)
|
foreach (var key in exception.Data.Keys)
|
||||||
{
|
{
|
||||||
|
if (key is null) continue;
|
||||||
|
|
||||||
var value = exception.Data[key];
|
var value = exception.Data[key];
|
||||||
if (key is null || value is null)
|
if (value is null) continue;
|
||||||
continue;
|
|
||||||
this.ExtensionData[key.ToString() ?? string.Empty] = value;
|
var keyString = key.ToString();
|
||||||
|
var valueString = value.ToString();
|
||||||
|
if (string.IsNullOrEmpty(keyString) || string.IsNullOrEmpty(valueString)) continue;
|
||||||
|
|
||||||
|
values ??= ImmutableDictionary.CreateBuilder<string, string>();
|
||||||
|
values.Add(keyString, valueString);
|
||||||
}
|
}
|
||||||
|
return values is not null ? values.ToImmutable() : ImmutableDictionary<string, string>.Empty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[DataContract]
|
[JsonConverter(typeof(ManyErrorsJsonConverter))]
|
||||||
public sealed class ManyErrors : Error, IEnumerable<Error>
|
public sealed class ManyErrors : Error, IEnumerable<Error>, IReadOnlyList<Error>
|
||||||
{
|
{
|
||||||
private readonly List<Error> _errors;
|
private readonly ImmutableArray<Error> _errors;
|
||||||
[Pure, DataMember] public IEnumerable<Error> Errors { get => _errors; }
|
[Pure] public IEnumerable<Error> Errors { get => _errors; }
|
||||||
|
|
||||||
|
internal ManyErrors(ImmutableArray<Error> errors) => _errors = errors;
|
||||||
internal ManyErrors(Error head, Error tail)
|
internal ManyErrors(Error head, Error tail)
|
||||||
{
|
{
|
||||||
_errors = new List<Error>(head.Count + tail.Count);
|
var headCount = head.Count;
|
||||||
|
var tailCount = tail.Count;
|
||||||
|
var errors = ImmutableArray.CreateBuilder<Error>(headCount + tailCount);
|
||||||
|
|
||||||
if (head.Count == 1)
|
if (headCount > 0)
|
||||||
_errors.Add(head);
|
AppendSanitized(errors, head);
|
||||||
else if (head.Count > 1)
|
|
||||||
_errors.AddRange(head.ToEnumerable());
|
|
||||||
|
|
||||||
if (tail.Count == 1)
|
if (tailCount > 0)
|
||||||
_errors.Add(tail);
|
AppendSanitized(errors, tail);
|
||||||
else if (tail.Count > 1)
|
|
||||||
_errors.AddRange(tail.ToEnumerable());
|
_errors = errors.MoveToImmutable();
|
||||||
}
|
}
|
||||||
public ManyErrors(IEnumerable<Error> errors)
|
public ManyErrors(IEnumerable<Error> errors)
|
||||||
{
|
{
|
||||||
_errors = errors.SelectMany(x => x.ToEnumerable())
|
var unpackedErrors = ImmutableArray.CreateBuilder<Error>();
|
||||||
.Where(x => !x.IsEmpty)
|
|
||||||
.ToList();
|
foreach (var err in errors)
|
||||||
|
{
|
||||||
|
if (err.IsEmpty) continue;
|
||||||
|
|
||||||
|
AppendSanitized(unpackedErrors, err);
|
||||||
|
}
|
||||||
|
|
||||||
|
_errors = unpackedErrors.ToImmutable();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Pure] public override string Type => "many_errors";
|
[Pure] public override string Type => "many_errors";
|
||||||
[Pure] public override string Message => ToFullArrayString();
|
|
||||||
[Pure] public override string ToString() => ToFullArrayString();
|
|
||||||
|
|
||||||
[Pure] private string ToFullArrayString()
|
private string? _lazyMessage = null;
|
||||||
|
[Pure] public override string Message => _lazyMessage ??= ToFullArrayString(_errors);
|
||||||
|
|
||||||
|
[Pure] private static string ToFullArrayString(in ImmutableArray<Error> errors)
|
||||||
{
|
{
|
||||||
var separator = Environment.NewLine;
|
var separator = Environment.NewLine;
|
||||||
var lastIndex = _errors.Count - 1;
|
|
||||||
|
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
for (int i = 0; i < _errors.Count; i++)
|
for (int i = 0; i < errors.Length; i++)
|
||||||
{
|
{
|
||||||
sb.Append(_errors[i]);
|
sb.Append(errors[i]);
|
||||||
if (i < lastIndex)
|
sb.Append(separator);
|
||||||
sb.Append(separator);
|
|
||||||
}
|
}
|
||||||
|
sb.Remove(sb.Length - separator.Length, separator.Length);
|
||||||
|
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Pure] public override int Count => _errors.Count;
|
[Pure] public override int Count => _errors.Length;
|
||||||
[Pure, JsonIgnore] public override bool IsEmpty => _errors.Count == 0;
|
[Pure] public override bool IsEmpty => _errors.IsEmpty;
|
||||||
[Pure, JsonIgnore] public override bool IsExpected => _errors.All(static x => x.IsExpected);
|
[Pure] public override bool IsExpected => _errors.All(static x => x.IsExpected);
|
||||||
[Pure, JsonIgnore] public override bool IsExeptional => _errors.Any(static x => x.IsExeptional);
|
[Pure] public override bool IsExeptional => _errors.Any(static x => x.IsExeptional);
|
||||||
|
|
||||||
|
[Pure] public Error this[int index] => _errors[index];
|
||||||
|
|
||||||
[Pure] public override Exception ToException() => new AggregateException(_errors.Select(static x => x.ToException()));
|
[Pure] public override Exception ToException() => new AggregateException(_errors.Select(static x => x.ToException()));
|
||||||
[Pure] public override IEnumerable<Error> ToEnumerable() => _errors;
|
[Pure] public override IEnumerable<Error> ToEnumerable() => _errors;
|
||||||
@@ -302,22 +312,19 @@ public sealed class ManyErrors : Error, IEnumerable<Error>
|
|||||||
{
|
{
|
||||||
if (other is null)
|
if (other is null)
|
||||||
return -1;
|
return -1;
|
||||||
if (other.Count != _errors.Count)
|
if (other.Count != _errors.Length)
|
||||||
return _errors.Count.CompareTo(other.Count);
|
return _errors.Length.CompareTo(other.Count);
|
||||||
|
|
||||||
var compareResult = 0;
|
for (int i = 0; i < _errors.Length; i++)
|
||||||
int i = 0;
|
|
||||||
foreach (var otherErr in other.ToEnumerable())
|
|
||||||
{
|
{
|
||||||
var thisErr = _errors[i++];
|
var compareResult = _errors[i].CompareTo(other.AccessUnsafe(i));
|
||||||
compareResult = thisErr.CompareTo(otherErr);
|
|
||||||
if (compareResult != 0)
|
if (compareResult != 0)
|
||||||
{
|
{
|
||||||
return compareResult;
|
return compareResult;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return compareResult;
|
return 0;
|
||||||
}
|
}
|
||||||
[Pure] public override bool IsSimilarTo([NotNullWhen(true)] Error? other)
|
[Pure] public override bool IsSimilarTo([NotNullWhen(true)] Error? other)
|
||||||
{
|
{
|
||||||
@@ -325,15 +332,13 @@ public sealed class ManyErrors : Error, IEnumerable<Error>
|
|||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (_errors.Count != other.Count)
|
if (_errors.Length != other.Count)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
int i = 0;
|
for (int i = 0; i < _errors.Length; i++)
|
||||||
foreach (var otherErr in other.ToEnumerable())
|
|
||||||
{
|
{
|
||||||
var thisErr = _errors[i++];
|
if (!_errors[i].IsSimilarTo(other.AccessUnsafe(i)))
|
||||||
if (!thisErr.IsSimilarTo(otherErr))
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -346,40 +351,57 @@ public sealed class ManyErrors : Error, IEnumerable<Error>
|
|||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (_errors.Count != other.Count)
|
if (_errors.Length != other.Count)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
int i = 0;
|
for (int i = 0; i < _errors.Length; i++)
|
||||||
foreach (var otherErr in other.ToEnumerable())
|
|
||||||
{
|
{
|
||||||
var thisErr = _errors[i++];
|
if (!_errors[i].Equals(other.AccessUnsafe(i)))
|
||||||
if (!thisErr.Equals(otherErr))
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
[Pure] public override int GetHashCode()
|
|
||||||
|
private int? _lazyHashCode = null;
|
||||||
|
[Pure] public override int GetHashCode() => _lazyHashCode ??= CalcHashCode(_errors);
|
||||||
|
private static int CalcHashCode(in ImmutableArray<Error> errors)
|
||||||
{
|
{
|
||||||
if (_errors.Count == 0)
|
if (errors.IsEmpty)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
var hash = new HashCode();
|
var hash = new HashCode();
|
||||||
foreach (var err in _errors)
|
foreach (var err in errors)
|
||||||
{
|
{
|
||||||
hash.Add(err);
|
hash.Add(err);
|
||||||
}
|
}
|
||||||
return hash.ToHashCode();
|
return hash.ToHashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Pure] public IEnumerator<Error> GetEnumerator() => _errors.GetEnumerator();
|
|
||||||
[Pure] IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
[Pure] public ImmutableArray<Error>.Enumerator GetEnumerator() => _errors.GetEnumerator();
|
||||||
|
[Pure] IEnumerator<Error> IEnumerable<Error>.GetEnumerator() => Errors.GetEnumerator();
|
||||||
|
[Pure] IEnumerator IEnumerable.GetEnumerator() => Errors.GetEnumerator();
|
||||||
|
|
||||||
|
internal static void AppendSanitized(ImmutableArray<Error>.Builder errors, Error error)
|
||||||
|
{
|
||||||
|
if (error is ManyErrors many)
|
||||||
|
errors.AddRange(many._errors);
|
||||||
|
else
|
||||||
|
errors.Add(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)] internal override Error AccessUnsafe(int position) => _errors[position];
|
||||||
}
|
}
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public sealed class ErrorException(string type, string message) : Exception(message)
|
public sealed class ErrorException : Exception
|
||||||
{
|
{
|
||||||
public string Type { get; } = type ?? nameof(ErrorException);
|
public ErrorException(string type, string message) : base(message)
|
||||||
|
{
|
||||||
|
Type = type ?? nameof(ErrorException);
|
||||||
|
}
|
||||||
|
public string Type { get; }
|
||||||
}
|
}
|
||||||
|
|||||||
182
Railway/ErrorJsonConverter.cs
Normal file
182
Railway/ErrorJsonConverter.cs
Normal file
@@ -0,0 +1,182 @@
|
|||||||
|
using System.Collections.Immutable;
|
||||||
|
|
||||||
|
namespace Just.Railway;
|
||||||
|
|
||||||
|
public sealed class ErrorJsonConverter : JsonConverter<Error>
|
||||||
|
{
|
||||||
|
public override Error? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
return reader.TokenType switch
|
||||||
|
{
|
||||||
|
JsonTokenType.StartObject => ToExpectedError(ReadOne(ref reader)),
|
||||||
|
JsonTokenType.StartArray => ReadMany(ref reader),
|
||||||
|
JsonTokenType.None => null,
|
||||||
|
JsonTokenType.Null => null,
|
||||||
|
_ => throw new JsonException("Unexpected JSON token.")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Write(Utf8JsonWriter writer, Error value, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
if (value is ManyErrors manyErrors)
|
||||||
|
{
|
||||||
|
writer.WriteStartArray();
|
||||||
|
foreach (var err in manyErrors)
|
||||||
|
{
|
||||||
|
WriteOne(writer, err);
|
||||||
|
}
|
||||||
|
writer.WriteEndArray();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
WriteOne(writer, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static ManyErrors ReadMany(ref Utf8JsonReader reader)
|
||||||
|
{
|
||||||
|
var errors = ImmutableArray.CreateBuilder<Error>();
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
if (reader.TokenType == JsonTokenType.StartObject)
|
||||||
|
{
|
||||||
|
errors.Add(ToExpectedError(ReadOne(ref reader)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return new ManyErrors(errors.ToImmutable());
|
||||||
|
}
|
||||||
|
internal static ExpectedError ToExpectedError(in (string Type, string Message, ImmutableDictionary<string, string> ExtensionData) errorInfo)
|
||||||
|
=> new(errorInfo.Type, errorInfo.Message) { ExtensionData = errorInfo.ExtensionData };
|
||||||
|
internal static (string Type, string Message, ImmutableDictionary<string, string> ExtensionData) ReadOne(ref Utf8JsonReader reader)
|
||||||
|
{
|
||||||
|
ImmutableDictionary<string, string>.Builder? extensionData = null;
|
||||||
|
string type = "error";
|
||||||
|
string message = "";
|
||||||
|
while (reader.Read())
|
||||||
|
{
|
||||||
|
switch (reader.TokenType)
|
||||||
|
{
|
||||||
|
case JsonTokenType.PropertyName:
|
||||||
|
{
|
||||||
|
var propname = reader.GetString();
|
||||||
|
reader.Read();
|
||||||
|
|
||||||
|
if (reader.TokenType == JsonTokenType.Null)
|
||||||
|
break;
|
||||||
|
|
||||||
|
while (reader.TokenType == JsonTokenType.Comment) reader.Read();
|
||||||
|
|
||||||
|
if (!(reader.TokenType == JsonTokenType.String))
|
||||||
|
throw new JsonException("Unable to deserialize Error type.");
|
||||||
|
|
||||||
|
var propvalue = reader.GetString();
|
||||||
|
if (string.IsNullOrEmpty(propvalue))
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (propname == "type" || string.Equals(propname, "type", StringComparison.InvariantCultureIgnoreCase))
|
||||||
|
{
|
||||||
|
type = propvalue;
|
||||||
|
}
|
||||||
|
else if (propname == "msg" || string.Equals(propname, "msg", StringComparison.InvariantCultureIgnoreCase))
|
||||||
|
{
|
||||||
|
message = propvalue;
|
||||||
|
}
|
||||||
|
else if (!string.IsNullOrEmpty(propname))
|
||||||
|
{
|
||||||
|
extensionData ??= ImmutableDictionary.CreateBuilder<string, string>();
|
||||||
|
extensionData[propname] = propvalue;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case JsonTokenType.Comment: break;
|
||||||
|
case JsonTokenType.EndObject: goto endLoop;
|
||||||
|
default: throw new JsonException("Unable to deserialize Error type.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
endLoop:
|
||||||
|
return (type, message, extensionData?.ToImmutable() ?? ImmutableDictionary<string, string>.Empty);
|
||||||
|
}
|
||||||
|
internal static void WriteOne(Utf8JsonWriter writer, Error value)
|
||||||
|
{
|
||||||
|
writer.WriteStartObject();
|
||||||
|
|
||||||
|
writer.WriteString("type", value.Type);
|
||||||
|
writer.WriteString("msg", value.Message);
|
||||||
|
|
||||||
|
if (value.ExtensionData?.Count > 0)
|
||||||
|
{
|
||||||
|
foreach (var (key, val) in value.ExtensionData)
|
||||||
|
{
|
||||||
|
writer.WriteString(key, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.WriteEndObject();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class ExpectedErrorJsonConverter : JsonConverter<ExpectedError>
|
||||||
|
{
|
||||||
|
public override ExpectedError? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
return reader.TokenType switch
|
||||||
|
{
|
||||||
|
JsonTokenType.StartObject => ErrorJsonConverter.ToExpectedError(ErrorJsonConverter.ReadOne(ref reader)),
|
||||||
|
JsonTokenType.None => null,
|
||||||
|
JsonTokenType.Null => null,
|
||||||
|
_ => throw new JsonException("Unexpected JSON token.")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Write(Utf8JsonWriter writer, ExpectedError value, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
ErrorJsonConverter.WriteOne(writer, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class ExceptionalErrorJsonConverter : JsonConverter<ExceptionalError>
|
||||||
|
{
|
||||||
|
public override ExceptionalError? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
return reader.TokenType switch
|
||||||
|
{
|
||||||
|
JsonTokenType.StartObject => ToExceptionalError(ErrorJsonConverter.ReadOne(ref reader)),
|
||||||
|
JsonTokenType.None => null,
|
||||||
|
JsonTokenType.Null => null,
|
||||||
|
_ => throw new JsonException("Unexpected JSON token.")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Write(Utf8JsonWriter writer, ExceptionalError value, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
ErrorJsonConverter.WriteOne(writer, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ExceptionalError ToExceptionalError(in (string Type, string Message, ImmutableDictionary<string, string> ExtensionData) errorInfo)
|
||||||
|
=> new(errorInfo.Type, errorInfo.Message) { ExtensionData = errorInfo.ExtensionData };
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class ManyErrorsJsonConverter : JsonConverter<ManyErrors>
|
||||||
|
{
|
||||||
|
public override ManyErrors? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
return reader.TokenType switch
|
||||||
|
{
|
||||||
|
JsonTokenType.StartArray => ErrorJsonConverter.ReadMany(ref reader),
|
||||||
|
JsonTokenType.None => null,
|
||||||
|
JsonTokenType.Null => null,
|
||||||
|
_ => throw new JsonException("Unexpected JSON token.")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Write(Utf8JsonWriter writer, ManyErrors value, JsonSerializerOptions options)
|
||||||
|
{
|
||||||
|
writer.WriteStartArray();
|
||||||
|
foreach (var err in value)
|
||||||
|
{
|
||||||
|
ErrorJsonConverter.WriteOne(writer, err);
|
||||||
|
}
|
||||||
|
writer.WriteEndArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,16 +1,20 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
|
||||||
|
<LangVersion>10.0</LangVersion>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<AssemblyName>Just.Railway</AssemblyName>
|
<AssemblyName>Just.Railway</AssemblyName>
|
||||||
<RootNamespace>Just.Railway</RootNamespace>
|
<RootNamespace>Just.Railway</RootNamespace>
|
||||||
|
|
||||||
|
<Description>Base for railway-oriented programming in .NET. Package includes Result object, Error class and most of the common extensions.</Description>
|
||||||
|
<PackageTags>railway-oriented;functional;result-pattern;result-object;error-handling</PackageTags>
|
||||||
<Authors>JustFixMe</Authors>
|
<Authors>JustFixMe</Authors>
|
||||||
<Copyright>Copyright (c) 2023 JustFixMe</Copyright>
|
<Copyright>Copyright (c) 2023-2024 JustFixMe</Copyright>
|
||||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||||
<RepositoryUrl>https://gitea.jstdev.ru/just/Just.Railway/</RepositoryUrl>
|
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||||
|
<RepositoryUrl>https://github.com/JustFixMe/Just.Railway/</RepositoryUrl>
|
||||||
|
|
||||||
<EmitCompilerGeneratedFiles Condition="'$(Configuration)'=='Debug'">true</EmitCompilerGeneratedFiles>
|
<EmitCompilerGeneratedFiles Condition="'$(Configuration)'=='Debug'">true</EmitCompilerGeneratedFiles>
|
||||||
<ReleaseVersion Condition=" '$(ReleaseVersion)' == '' ">1.0.0</ReleaseVersion>
|
<ReleaseVersion Condition=" '$(ReleaseVersion)' == '' ">1.0.0</ReleaseVersion>
|
||||||
@@ -24,6 +28,11 @@
|
|||||||
<InternalsVisibleTo Include="$(AssemblyName).Tests" />
|
<InternalsVisibleTo Include="$(AssemblyName).Tests" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="..\README.md" Pack="true" PackagePath=""/>
|
||||||
|
<None Include="..\LICENSE" Pack="true" Visible="false" PackagePath=""/>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Railway.SourceGenerator\Railway.SourceGenerator.csproj"
|
<ProjectReference Include="..\Railway.SourceGenerator\Railway.SourceGenerator.csproj"
|
||||||
OutputItemType="Analyzer"
|
OutputItemType="Analyzer"
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
|
|
||||||
namespace Just.Railway;
|
namespace Just.Railway;
|
||||||
|
|
||||||
@@ -10,58 +9,58 @@ internal static class ReflectionHelper
|
|||||||
|
|
||||||
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static int Compare<T>(T? left, T? right) => TypeReflectionCache<T>.CompareFunc(left, right);
|
public static int Compare<T>(T? left, T? right) => TypeReflectionCache<T>.CompareFunc(left, right);
|
||||||
}
|
|
||||||
|
|
||||||
file static class TypeReflectionCache<T>
|
private static class TypeReflectionCache<T>
|
||||||
{
|
|
||||||
public static readonly Func<T?, T?, bool> IsEqualFunc;
|
|
||||||
public static readonly Func<T?, T?, int> CompareFunc;
|
|
||||||
|
|
||||||
static TypeReflectionCache()
|
|
||||||
{
|
{
|
||||||
var type = typeof(T);
|
public static readonly Func<T?, T?, bool> IsEqualFunc;
|
||||||
var isNullableStruct = type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
|
public static readonly Func<T?, T?, int> CompareFunc;
|
||||||
var underlyingType = isNullableStruct ? type.GenericTypeArguments.First() : type;
|
|
||||||
var thisType = typeof(TypeReflectionCache<T>);
|
|
||||||
|
|
||||||
var equatableType = typeof(IEquatable<>).MakeGenericType(underlyingType);
|
static TypeReflectionCache()
|
||||||
if (equatableType.IsAssignableFrom(underlyingType))
|
|
||||||
{
|
{
|
||||||
var isEqualFunc = thisType.GetMethod(isNullableStruct ? nameof(IsEqualNullable) : nameof(IsEqual), BindingFlags.Static | BindingFlags.Public)
|
var type = typeof(T);
|
||||||
!.MakeGenericMethod(underlyingType);
|
var isNullableStruct = type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
|
||||||
|
var underlyingType = isNullableStruct ? type.GenericTypeArguments.First() : type;
|
||||||
|
var thisType = typeof(TypeReflectionCache<T>);
|
||||||
|
|
||||||
IsEqualFunc = (Func<T?, T?, bool>)Delegate.CreateDelegate(typeof(Func<T?, T?, bool>), isEqualFunc);
|
var equatableType = typeof(IEquatable<>).MakeGenericType(underlyingType);
|
||||||
}
|
if (equatableType.IsAssignableFrom(underlyingType))
|
||||||
else
|
{
|
||||||
{
|
var isEqualFunc = thisType.GetMethod(isNullableStruct ? nameof(IsEqualNullable) : nameof(IsEqual), BindingFlags.Static | BindingFlags.Public)
|
||||||
IsEqualFunc = static (left, right) => left is null ? right is null : left.Equals(right);
|
!.MakeGenericMethod(underlyingType);
|
||||||
|
|
||||||
|
IsEqualFunc = (Func<T?, T?, bool>)Delegate.CreateDelegate(typeof(Func<T?, T?, bool>), isEqualFunc);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
IsEqualFunc = static (left, right) => left is null ? right is null : left.Equals(right);
|
||||||
|
}
|
||||||
|
|
||||||
|
var comparableType = typeof(IComparable<>).MakeGenericType(underlyingType);
|
||||||
|
if (comparableType.IsAssignableFrom(underlyingType))
|
||||||
|
{
|
||||||
|
var compareFunc = thisType.GetMethod(isNullableStruct ? nameof(CompareNullable) : nameof(Compare), BindingFlags.Static | BindingFlags.Public)
|
||||||
|
!.MakeGenericMethod(underlyingType);
|
||||||
|
|
||||||
|
CompareFunc = (Func<T?, T?, int>)Delegate.CreateDelegate(typeof(Func<T?, T?, int>), compareFunc);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
CompareFunc = static (left, right) => left is null
|
||||||
|
? right is null ? 0 : -1
|
||||||
|
: right is null ? 1 : left.GetHashCode().CompareTo(right.GetHashCode());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var comparableType = typeof(IComparable<>).MakeGenericType(underlyingType);
|
#pragma warning disable CS8604 // Possible null reference argument.
|
||||||
if (comparableType.IsAssignableFrom(underlyingType))
|
[Pure] public static bool IsEqual<R>(R? left, R? right) where R : notnull, IEquatable<R>, T => left is null ? right is null : left.Equals(right);
|
||||||
{
|
[Pure] public static bool IsEqualNullable<R>(R? left, R? right) where R : struct, IEquatable<R> => left is null ? right is null : right is not null && left.Value.Equals(right.Value);
|
||||||
var compareFunc = thisType.GetMethod(isNullableStruct ? nameof(CompareNullable) : nameof(Compare), BindingFlags.Static | BindingFlags.Public)
|
|
||||||
!.MakeGenericMethod(underlyingType);
|
|
||||||
|
|
||||||
CompareFunc = (Func<T?, T?, int>)Delegate.CreateDelegate(typeof(Func<T?, T?, int>), compareFunc);
|
[Pure] public static int Compare<R>(R? left, R? right) where R : notnull, IComparable<R>, T => left is null
|
||||||
}
|
? right is null ? 0 : -1
|
||||||
else
|
: right is null ? 1 : left.CompareTo(right);
|
||||||
{
|
[Pure] public static int CompareNullable<R>(R? left, R? right) where R : struct, IComparable<R> => left is null
|
||||||
CompareFunc = static (left, right) => left is null
|
? right is null ? 0 : -1
|
||||||
? right is null ? 0 : -1
|
: right is null ? 1 : left.Value.CompareTo(right.Value);
|
||||||
: right is null ? 1 : left.GetHashCode().CompareTo(right.GetHashCode());
|
#pragma warning restore CS8604 // Possible null reference argument.
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma warning disable CS8604 // Possible null reference argument.
|
|
||||||
[Pure] public static bool IsEqual<R>(R? left, R? right) where R : notnull, IEquatable<R>, T => left is null ? right is null : left.Equals(right);
|
|
||||||
[Pure] public static bool IsEqualNullable<R>(R? left, R? right) where R : struct, IEquatable<R> => left is null ? right is null : right is not null && left.Value.Equals(right.Value);
|
|
||||||
|
|
||||||
[Pure] public static int Compare<R>(R? left, R? right) where R : notnull, IComparable<R>, T => left is null
|
|
||||||
? right is null ? 0 : -1
|
|
||||||
: right is null ? 1 : left.CompareTo(right);
|
|
||||||
[Pure] public static int CompareNullable<R>(R? left, R? right) where R : struct, IComparable<R> => left is null
|
|
||||||
? right is null ? 0 : -1
|
|
||||||
: right is null ? 1 : left.Value.CompareTo(right.Value);
|
|
||||||
#pragma warning restore CS8604 // Possible null reference argument.
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
namespace Just.Railway;
|
namespace Just.Railway;
|
||||||
|
|
||||||
internal enum ResultState : byte
|
internal enum ResultState : byte
|
||||||
@@ -8,6 +7,13 @@ internal enum ResultState : byte
|
|||||||
|
|
||||||
public readonly partial struct Result : IEquatable<Result>
|
public readonly partial struct Result : IEquatable<Result>
|
||||||
{
|
{
|
||||||
|
[SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Simplified source generation")]
|
||||||
|
internal SuccessUnit Value
|
||||||
|
{
|
||||||
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
get => new();
|
||||||
|
}
|
||||||
|
|
||||||
internal readonly Error? Error;
|
internal readonly Error? Error;
|
||||||
internal readonly ResultState State;
|
internal readonly ResultState State;
|
||||||
|
|
||||||
@@ -36,12 +42,18 @@ public readonly partial struct Result : IEquatable<Result>
|
|||||||
public static Result<(T1, T2, T3, T4, T5)> Success<T1, T2, T3, T4, T5>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5) => new((value1, value2, value3, value4, value5));
|
public static Result<(T1, T2, T3, T4, T5)> Success<T1, T2, T3, T4, T5>(T1 value1, T2 value2, T3 value3, T4 value4, T5 value5) => new((value1, value2, value3, value4, value5));
|
||||||
|
|
||||||
|
|
||||||
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static Result Failure(string error) => Error.New(error ?? throw new ArgumentNullException(nameof(error)));
|
||||||
|
|
||||||
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static Result Failure(Error error) => new(error ?? throw new ArgumentNullException(nameof(error)));
|
public static Result Failure(Error error) => new(error ?? throw new ArgumentNullException(nameof(error)));
|
||||||
|
|
||||||
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static Result Failure(Exception exception) => new(Error.New(exception) ?? throw new ArgumentNullException(nameof(exception)));
|
public static Result Failure(Exception exception) => new(Error.New(exception) ?? throw new ArgumentNullException(nameof(exception)));
|
||||||
|
|
||||||
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static Result<T> Failure<T>(string error) => Error.New(error ?? throw new ArgumentNullException(nameof(error)));
|
||||||
|
|
||||||
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static Result<T> Failure<T>(Error error) => new(error ?? throw new ArgumentNullException(nameof(error)));
|
public static Result<T> Failure<T>(Error error) => new(error ?? throw new ArgumentNullException(nameof(error)));
|
||||||
|
|
||||||
@@ -51,23 +63,28 @@ public readonly partial struct Result : IEquatable<Result>
|
|||||||
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static implicit operator Result(Error error) => new(error ?? throw new ArgumentNullException(nameof(error)));
|
public static implicit operator Result(Error error) => new(error ?? throw new ArgumentNullException(nameof(error)));
|
||||||
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static implicit operator Result(Exception exception) => new(
|
||||||
|
new ExceptionalError(exception ?? throw new ArgumentNullException(nameof(exception))));
|
||||||
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
public static implicit operator Result<SuccessUnit>(Result result) => result.State switch
|
public static implicit operator Result<SuccessUnit>(Result result) => result.State switch
|
||||||
{
|
{
|
||||||
ResultState.Success => new(new SuccessUnit()),
|
ResultState.Success => new(new SuccessUnit()),
|
||||||
ResultState.Error => new(result.Error!),
|
ResultState.Error => new(result.Error!),
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
_ => throw new ResultNotInitializedException(nameof(result))
|
||||||
};
|
};
|
||||||
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static explicit operator Result(SuccessUnit _) => new(null);
|
||||||
|
|
||||||
[Pure] public bool IsSuccess => Error is null;
|
[Pure] public bool IsSuccess => Error is null;
|
||||||
[Pure] public bool IsFailure => Error is not null;
|
[Pure] public bool IsFailure => Error is not null;
|
||||||
|
|
||||||
[Pure] public bool Success([MaybeNullWhen(false)]out SuccessUnit? u, [MaybeNullWhen(true), NotNullWhen(false)]out Error? error)
|
[Pure] public bool TryGetValue([MaybeNullWhen(false)]out SuccessUnit? u, [MaybeNullWhen(true), NotNullWhen(false)]out Error? error)
|
||||||
{
|
{
|
||||||
switch (State)
|
switch (State)
|
||||||
{
|
{
|
||||||
case ResultState.Success:
|
case ResultState.Success:
|
||||||
u = new SuccessUnit();
|
u = new SuccessUnit();
|
||||||
error = default;
|
error = null;
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
case ResultState.Error:
|
case ResultState.Error:
|
||||||
@@ -136,26 +153,33 @@ public readonly struct Result<T> : IEquatable<Result<T>>
|
|||||||
{
|
{
|
||||||
Value = value;
|
Value = value;
|
||||||
State = ResultState.Success;
|
State = ResultState.Success;
|
||||||
|
Error = default;
|
||||||
}
|
}
|
||||||
|
|
||||||
[Pure] public static explicit operator Result(Result<T> result) => result.State switch
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static explicit operator Result(Result<T> result) => result.State switch
|
||||||
{
|
{
|
||||||
ResultState.Success => new(null),
|
ResultState.Success => new(null),
|
||||||
ResultState.Error => new(result.Error!),
|
ResultState.Error => new(result.Error!),
|
||||||
_ => throw new ResultNotInitializedException(nameof(result))
|
_ => throw new ResultNotInitializedException(nameof(result))
|
||||||
};
|
};
|
||||||
[Pure] public static implicit operator Result<T>(Error error) => new(error);
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
[Pure] public static implicit operator Result<T>(T value) => new(value);
|
public static implicit operator Result<T>(Error error) => new(error);
|
||||||
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static implicit operator Result<T>(Exception exception) => new(
|
||||||
|
new ExceptionalError(exception ?? throw new ArgumentNullException(nameof(exception))));
|
||||||
|
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public static implicit operator Result<T>(T value) => new(value);
|
||||||
[Pure] public bool IsSuccess => State == ResultState.Success;
|
[Pure] public bool IsSuccess => State == ResultState.Success;
|
||||||
[Pure] public bool IsFailure => State == ResultState.Error;
|
[Pure] public bool IsFailure => State == ResultState.Error;
|
||||||
|
|
||||||
[Pure] public bool Success([MaybeNullWhen(false)]out T value, [MaybeNullWhen(true), NotNullWhen(false)]out Error? error)
|
[Pure] public bool TryGetValue([MaybeNullWhen(false)]out T value, [MaybeNullWhen(true), NotNullWhen(false)]out Error? error)
|
||||||
{
|
{
|
||||||
switch (State)
|
switch (State)
|
||||||
{
|
{
|
||||||
case ResultState.Success:
|
case ResultState.Success:
|
||||||
value = Value;
|
value = Value;
|
||||||
error = default;
|
error = null;
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
case ResultState.Error:
|
case ResultState.Error:
|
||||||
@@ -263,7 +287,12 @@ public readonly struct SuccessUnit : IEquatable<SuccessUnit>
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Serializable]
|
[Serializable]
|
||||||
public class ResultNotInitializedException(string variableName = "this") : InvalidOperationException("Result was not properly initialized.")
|
public class ResultNotInitializedException : InvalidOperationException
|
||||||
{
|
{
|
||||||
public string VariableName { get; } = variableName;
|
public ResultNotInitializedException(string variableName = "this")
|
||||||
|
: base("Result was not properly initialized.")
|
||||||
|
{
|
||||||
|
VariableName = variableName;
|
||||||
|
}
|
||||||
|
public string VariableName { get; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,88 @@
|
|||||||
|
using System.Collections.Immutable;
|
||||||
|
|
||||||
namespace Just.Railway;
|
namespace Just.Railway;
|
||||||
|
|
||||||
public static partial class ResultExtensions
|
public static partial class ResultExtensions
|
||||||
{
|
{
|
||||||
|
#region Match (with fallback)
|
||||||
|
|
||||||
|
public static T Match<T>(this in Result<T> result, Func<Error, T> fallback)
|
||||||
|
{
|
||||||
|
return result.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => result.Value,
|
||||||
|
ResultState.Error => fallback(result.Error!),
|
||||||
|
_ => throw new ResultNotInitializedException(nameof(result))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<T> Match<T>(this Result<T> result, Func<Error, Task<T>> fallback)
|
||||||
|
{
|
||||||
|
return result.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => result.Value,
|
||||||
|
ResultState.Error => await fallback(result.Error!).ConfigureAwait(false),
|
||||||
|
_ => throw new ResultNotInitializedException(nameof(result))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
public static async Task<T> Match<T>(this Task<Result<T>> resultTask, Func<Error, T> fallback)
|
||||||
|
{
|
||||||
|
var result = await resultTask.ConfigureAwait(false);
|
||||||
|
return result.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => result.Value,
|
||||||
|
ResultState.Error => fallback(result.Error!),
|
||||||
|
_ => throw new ResultNotInitializedException(nameof(resultTask))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
public static async Task<T> Match<T>(this Task<Result<T>> resultTask, Func<Error, Task<T>> fallback)
|
||||||
|
{
|
||||||
|
var result = await resultTask.ConfigureAwait(false);
|
||||||
|
return result.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => result.Value,
|
||||||
|
ResultState.Error => await fallback(result.Error!).ConfigureAwait(false),
|
||||||
|
_ => throw new ResultNotInitializedException(nameof(resultTask))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async ValueTask<T> Match<T>(this Result<T> result, Func<Error, ValueTask<T>> fallback)
|
||||||
|
{
|
||||||
|
return result.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => result.Value,
|
||||||
|
ResultState.Error => await fallback(result.Error!).ConfigureAwait(false),
|
||||||
|
_ => throw new ResultNotInitializedException(nameof(result))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
public static async ValueTask<T> Match<T>(this ValueTask<Result<T>> resultTask, Func<Error, T> fallback)
|
||||||
|
{
|
||||||
|
var result = await resultTask.ConfigureAwait(false);
|
||||||
|
return result.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => result.Value,
|
||||||
|
ResultState.Error => fallback(result.Error!),
|
||||||
|
_ => throw new ResultNotInitializedException(nameof(resultTask))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
public static async ValueTask<T> Match<T>(this ValueTask<Result<T>> resultTask, Func<Error, ValueTask<T>> fallback)
|
||||||
|
{
|
||||||
|
var result = await resultTask.ConfigureAwait(false);
|
||||||
|
return result.State switch
|
||||||
|
{
|
||||||
|
ResultState.Success => result.Value,
|
||||||
|
ResultState.Error => await fallback(result.Error!).ConfigureAwait(false),
|
||||||
|
_ => throw new ResultNotInitializedException(nameof(resultTask))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
#region Merge
|
#region Merge
|
||||||
|
|
||||||
public static Result Merge(this IEnumerable<Result> results)
|
public static Result Merge(this IEnumerable<Result> results)
|
||||||
{
|
{
|
||||||
List<Error>? errors = null;
|
ImmutableArray<Error>.Builder? errors = null;
|
||||||
bool hasErrors = false;
|
bool hasErrors = false;
|
||||||
|
|
||||||
foreach (var result in results.OrderBy(x => x.State))
|
foreach (var result in results.OrderBy(x => x.State))
|
||||||
@@ -15,8 +91,8 @@ public static partial class ResultExtensions
|
|||||||
{
|
{
|
||||||
case ResultState.Error:
|
case ResultState.Error:
|
||||||
hasErrors = true;
|
hasErrors = true;
|
||||||
errors ??= [];
|
errors ??= ImmutableArray.CreateBuilder<Error>();
|
||||||
errors.Add(result.Error!);
|
ManyErrors.AppendSanitized(errors, result.Error!);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ResultState.Success:
|
case ResultState.Success:
|
||||||
@@ -28,7 +104,7 @@ public static partial class ResultExtensions
|
|||||||
}
|
}
|
||||||
afterLoop:
|
afterLoop:
|
||||||
return hasErrors
|
return hasErrors
|
||||||
? new(new ManyErrors(errors!))
|
? new(new ManyErrors(errors!.ToImmutable()))
|
||||||
: new(null);
|
: new(null);
|
||||||
}
|
}
|
||||||
public static async Task<Result> Merge(this IEnumerable<Task<Result>> tasks)
|
public static async Task<Result> Merge(this IEnumerable<Task<Result>> tasks)
|
||||||
@@ -39,8 +115,8 @@ public static partial class ResultExtensions
|
|||||||
|
|
||||||
public static Result<IEnumerable<T>> Merge<T>(this IEnumerable<Result<T>> results)
|
public static Result<IEnumerable<T>> Merge<T>(this IEnumerable<Result<T>> results)
|
||||||
{
|
{
|
||||||
List<T>? values = null;
|
ImmutableList<T>.Builder? values = null;
|
||||||
List<Error>? errors = null;
|
ImmutableArray<Error>.Builder? errors = null;
|
||||||
bool hasErrors = false;
|
bool hasErrors = false;
|
||||||
|
|
||||||
foreach (var result in results.OrderBy(x => x.State))
|
foreach (var result in results.OrderBy(x => x.State))
|
||||||
@@ -49,13 +125,13 @@ public static partial class ResultExtensions
|
|||||||
{
|
{
|
||||||
case ResultState.Error:
|
case ResultState.Error:
|
||||||
hasErrors = true;
|
hasErrors = true;
|
||||||
errors ??= [];
|
errors ??= ImmutableArray.CreateBuilder<Error>();
|
||||||
errors.Add(result.Error!);
|
ManyErrors.AppendSanitized(errors, result.Error!);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ResultState.Success:
|
case ResultState.Success:
|
||||||
if (hasErrors) goto afterLoop;
|
if (hasErrors) goto afterLoop;
|
||||||
values ??= [];
|
values ??= ImmutableList.CreateBuilder<T>();
|
||||||
values.Add(result.Value);
|
values.Add(result.Value);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -64,8 +140,8 @@ public static partial class ResultExtensions
|
|||||||
}
|
}
|
||||||
afterLoop:
|
afterLoop:
|
||||||
return hasErrors
|
return hasErrors
|
||||||
? new(new ManyErrors(errors!))
|
? new(new ManyErrors(errors!.ToImmutable()))
|
||||||
: new((IEnumerable<T>?)values ?? Array.Empty<T>());
|
: new(values is not null ? values.ToImmutable() : ImmutableList<T>.Empty);
|
||||||
}
|
}
|
||||||
public static async Task<Result<IEnumerable<T>>> Merge<T>(this IEnumerable<Task<Result<T>>> tasks)
|
public static async Task<Result<IEnumerable<T>>> Merge<T>(this IEnumerable<Task<Result<T>>> tasks)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ public class Satisfy
|
|||||||
{
|
{
|
||||||
var result = Ensure.That(69)
|
var result = Ensure.That(69)
|
||||||
.Satisfies(i => i < 100)
|
.Satisfies(i => i < 100)
|
||||||
|
.LessThan(100)
|
||||||
.Result();
|
.Result();
|
||||||
|
|
||||||
Assert.True(result.IsSuccess);
|
Assert.True(result.IsSuccess);
|
||||||
@@ -18,6 +19,7 @@ public class Satisfy
|
|||||||
var error = Error.New(Ensure.DefaultErrorType, "Value {69} does not satisfy the requirement.");
|
var error = Error.New(Ensure.DefaultErrorType, "Value {69} does not satisfy the requirement.");
|
||||||
var result = Ensure.That(69)
|
var result = Ensure.That(69)
|
||||||
.Satisfies(i => i > 100)
|
.Satisfies(i => i > 100)
|
||||||
|
.GreaterThan(100)
|
||||||
.Result();
|
.Result();
|
||||||
|
|
||||||
Assert.True(result.IsFailure);
|
Assert.True(result.IsFailure);
|
||||||
@@ -32,6 +34,7 @@ public class Satisfy
|
|||||||
.NotEmpty()
|
.NotEmpty()
|
||||||
.NotWhitespace()
|
.NotWhitespace()
|
||||||
.Satisfies(s => s == "69")
|
.Satisfies(s => s == "69")
|
||||||
|
.EqualTo("69")
|
||||||
.Result();
|
.Result();
|
||||||
|
|
||||||
Assert.True(result.IsSuccess);
|
Assert.True(result.IsSuccess);
|
||||||
@@ -47,6 +50,7 @@ public class Satisfy
|
|||||||
.NotEmpty()
|
.NotEmpty()
|
||||||
.NotWhitespace()
|
.NotWhitespace()
|
||||||
.Satisfies(s => s == "69")
|
.Satisfies(s => s == "69")
|
||||||
|
.EqualTo("69")
|
||||||
.Result();
|
.Result();
|
||||||
|
|
||||||
Assert.True(result.IsFailure);
|
Assert.True(result.IsFailure);
|
||||||
|
|||||||
@@ -6,40 +6,71 @@ public class Serialization
|
|||||||
public void WhenSerializingManyErrors()
|
public void WhenSerializingManyErrors()
|
||||||
{
|
{
|
||||||
// Given
|
// Given
|
||||||
Error many_errors = new ManyErrors(new Error[]{
|
Error many_errors = new ManyErrors(
|
||||||
new ExpectedError("err1", "msg1"){
|
[
|
||||||
ExtensionData = {
|
Error.New("err1", "msg1", new KeyValuePair<string, string>[]
|
||||||
["ext"] = "ext_value"
|
{
|
||||||
}
|
new("ext", "ext_value"),
|
||||||
},
|
}),
|
||||||
new ExceptionalError(new Exception("msg2")),
|
Error.New(new Exception("msg2")),
|
||||||
});
|
]);
|
||||||
// When
|
// When
|
||||||
var result = JsonSerializer.Serialize(many_errors);
|
var result = JsonSerializer.Serialize(many_errors);
|
||||||
// Then
|
// Then
|
||||||
Assert.Equal(
|
Assert.Equal(
|
||||||
expected: "[{\"$$err\":0,\"Type\":\"err1\",\"Message\":\"msg1\",\"ext\":\"ext_value\"},{\"$$err\":1,\"Type\":\"Exception\",\"Message\":\"msg2\"}]",
|
expected: "[{\"type\":\"err1\",\"msg\":\"msg1\",\"ext\":\"ext_value\"},{\"type\":\"System.Exception\",\"msg\":\"msg2\"}]",
|
||||||
result);
|
result);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void WhenDeserializingManyErrors()
|
public void WhenDeserializingManyErrorsAsError()
|
||||||
{
|
{
|
||||||
// Given
|
// Given
|
||||||
var json = "[{\"$$err\":0,\"Type\":\"err1\",\"Message\":\"msg1\",\"ext\":\"ext_value\"},{\"$$err\":1,\"Type\":\"Exception\",\"Message\":\"msg2\"}]";
|
var json = "[{\"type\":\"err1\",\"msg\":\"msg1\",\"ext1\":\"ext_value1\",\"ext2\":\"ext_value2\"},{\"type\":\"System.Exception\",\"msg\":\"msg2\"}]";
|
||||||
// When
|
// When
|
||||||
var result = JsonSerializer.Deserialize<Error[]>(json);
|
var result = JsonSerializer.Deserialize<Error>(json);
|
||||||
// Then
|
// Then
|
||||||
Assert.True(result?.Length == 2);
|
Assert.IsType<ManyErrors>(result);
|
||||||
|
ManyErrors manyErrors = (ManyErrors)result;
|
||||||
|
|
||||||
|
Assert.True(manyErrors.Count == 2);
|
||||||
Assert.Equal(
|
Assert.Equal(
|
||||||
expected: new ManyErrors(new Error[]{
|
expected: Error.Many(
|
||||||
new ExpectedError("err1", "msg1"),
|
Error.New("err1", "msg1"),
|
||||||
new ExceptionalError(new Exception("msg2")),
|
Error.New(new Exception("msg2"))
|
||||||
}),
|
).ToEnumerable(),
|
||||||
|
manyErrors
|
||||||
|
);
|
||||||
|
Assert.Equal(
|
||||||
|
expected: "ext_value1",
|
||||||
|
manyErrors[0]["ext1"]);
|
||||||
|
Assert.Equal(
|
||||||
|
expected: "ext_value2",
|
||||||
|
manyErrors[0]["ext2"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void WhenDeserializingManyErrorsAsManyErrors()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var json = "[{\"type\":\"err1\",\"msg\":\"msg1\",\"ext1\":\"ext_value1\",\"ext2\":\"ext_value2\"},{\"type\":\"System.Exception\",\"msg\":\"msg2\"}]";
|
||||||
|
// When
|
||||||
|
var result = JsonSerializer.Deserialize<ManyErrors>(json);
|
||||||
|
// Then
|
||||||
|
Assert.NotNull(result);
|
||||||
|
Assert.True(result.Count == 2);
|
||||||
|
Assert.Equal(
|
||||||
|
expected: Error.Many(
|
||||||
|
Error.New("err1", "msg1"),
|
||||||
|
Error.New(new Exception("msg2"))
|
||||||
|
).ToEnumerable(),
|
||||||
result
|
result
|
||||||
);
|
);
|
||||||
Assert.Equal(
|
Assert.Equal(
|
||||||
expected: "ext_value",
|
expected: "ext_value1",
|
||||||
result[0].ExtensionData["ext"].ToString());
|
result[0]["ext1"]);
|
||||||
|
Assert.Equal(
|
||||||
|
expected: "ext_value2",
|
||||||
|
result[0]["ext2"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
78
Raliway.Tests/Results/Combine.cs
Normal file
78
Raliway.Tests/Results/Combine.cs
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
namespace Raliway.Tests.Results;
|
||||||
|
|
||||||
|
public class Combine
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void TwoResultCombination_WhenThereIsAnError()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var result1 = Result.Success(1);
|
||||||
|
var result2 = Result.Failure(Error.New("some error"));
|
||||||
|
// When
|
||||||
|
var result = Result.Combine(result1, result2);
|
||||||
|
// Then
|
||||||
|
Assert.True(result.IsFailure);
|
||||||
|
Assert.Equal(result2.Error, result.Error);
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void TwoResultCombination_WhenThereAreTwoErrors()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var result1 = Result.Failure<byte>(Error.New("1"));
|
||||||
|
var result2 = Result.Failure(Error.New("2"));
|
||||||
|
// When
|
||||||
|
var result = Result.Combine(result1, result2);
|
||||||
|
// Then
|
||||||
|
Assert.True(result.IsFailure);
|
||||||
|
Assert.Equal(result1.Error + result2.Error, result.Error);
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void TwoResultCombination_WhenThereIsNoError()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var result1 = Result.Success(1);
|
||||||
|
var result2 = Result.Success(3.14);
|
||||||
|
// When
|
||||||
|
var result = Result.Combine(result1, result2);
|
||||||
|
// Then
|
||||||
|
Assert.True(result.IsSuccess);
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void ThreeResultCombination_WhenThereIsAnError()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var result1 = Result.Success(1);
|
||||||
|
var result2 = Result.Success(3.14);
|
||||||
|
var result3 = Result.Failure(Error.New("some error"));
|
||||||
|
// When
|
||||||
|
Result<(int, double)> result = Result.Combine(result1, result2, result3);
|
||||||
|
// Then
|
||||||
|
Assert.True(result.IsFailure);
|
||||||
|
Assert.Equal(result3.Error, result.Error);
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void ThreeResultCombination_WhenThereAreTwoErrors()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var result1 = Result.Failure<int?>(Error.New("1"));
|
||||||
|
var result2 = Result.Success(3.14);
|
||||||
|
var result3 = Result.Failure(Error.New("3"));
|
||||||
|
// When
|
||||||
|
Result<(int?, double)> result = Result.Combine(result1, result2, result3);
|
||||||
|
// Then
|
||||||
|
Assert.True(result.IsFailure);
|
||||||
|
Assert.Equal(result1.Error + result3.Error, result.Error);
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void ThreeResultCombination_WhenThereIsNoError()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var result1 = Result.Success(1);
|
||||||
|
var result2 = Result.Success(3.14);
|
||||||
|
var result3 = Result.Success();
|
||||||
|
// When
|
||||||
|
var result = Result.Combine(result1, result2, result3);
|
||||||
|
// Then
|
||||||
|
Assert.True(result.IsSuccess);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,80 +2,6 @@ namespace Raliway.Tests.Results;
|
|||||||
|
|
||||||
public class GeneralUsage
|
public class GeneralUsage
|
||||||
{
|
{
|
||||||
[Fact]
|
|
||||||
public void TwoResultCombination_WhenThereIsAnError()
|
|
||||||
{
|
|
||||||
// Given
|
|
||||||
var result1 = Result.Success(1);
|
|
||||||
var result2 = Result.Failure(Error.New("some error"));
|
|
||||||
// When
|
|
||||||
var result = Result.Combine(result1, result2);
|
|
||||||
// Then
|
|
||||||
Assert.True(result.IsFailure);
|
|
||||||
Assert.Equal(result2.Error, result.Error);
|
|
||||||
}
|
|
||||||
[Fact]
|
|
||||||
public void TwoResultCombination_WhenThereAreTwoErrors()
|
|
||||||
{
|
|
||||||
// Given
|
|
||||||
var result1 = Result.Failure<byte>(Error.New("1"));
|
|
||||||
var result2 = Result.Failure(Error.New("2"));
|
|
||||||
// When
|
|
||||||
var result = Result.Combine(result1, result2);
|
|
||||||
// Then
|
|
||||||
Assert.True(result.IsFailure);
|
|
||||||
Assert.Equal(result1.Error + result2.Error, result.Error);
|
|
||||||
}
|
|
||||||
[Fact]
|
|
||||||
public void TwoResultCombination_WhenThereIsNoError()
|
|
||||||
{
|
|
||||||
// Given
|
|
||||||
var result1 = Result.Success(1);
|
|
||||||
var result2 = Result.Success(3.14);
|
|
||||||
// When
|
|
||||||
var result = Result.Combine(result1, result2);
|
|
||||||
// Then
|
|
||||||
Assert.True(result.IsSuccess);
|
|
||||||
}
|
|
||||||
[Fact]
|
|
||||||
public void ThreeResultCombination_WhenThereIsAnError()
|
|
||||||
{
|
|
||||||
// Given
|
|
||||||
var result1 = Result.Success(1);
|
|
||||||
var result2 = Result.Success(3.14);
|
|
||||||
var result3 = Result.Failure(Error.New("some error"));
|
|
||||||
// When
|
|
||||||
Result<(int, double)> result = Result.Combine(result1, result2, result3);
|
|
||||||
// Then
|
|
||||||
Assert.True(result.IsFailure);
|
|
||||||
Assert.Equal(result3.Error, result.Error);
|
|
||||||
}
|
|
||||||
[Fact]
|
|
||||||
public void ThreeResultCombination_WhenThereAreTwoErrors()
|
|
||||||
{
|
|
||||||
// Given
|
|
||||||
var result1 = Result.Failure<int?>(Error.New("1"));
|
|
||||||
var result2 = Result.Success(3.14);
|
|
||||||
var result3 = Result.Failure(Error.New("3"));
|
|
||||||
// When
|
|
||||||
Result<(int?, double)> result = Result.Combine(result1, result2, result3);
|
|
||||||
// Then
|
|
||||||
Assert.True(result.IsFailure);
|
|
||||||
Assert.Equal(result1.Error + result3.Error, result.Error);
|
|
||||||
}
|
|
||||||
[Fact]
|
|
||||||
public void ThreeResultCombination_WhenThereIsNoError()
|
|
||||||
{
|
|
||||||
// Given
|
|
||||||
var result1 = Result.Success(1);
|
|
||||||
var result2 = Result.Success(3.14);
|
|
||||||
var result3 = Result.Success();
|
|
||||||
// When
|
|
||||||
var result = Result.Combine(result1, result2, result3);
|
|
||||||
// Then
|
|
||||||
Assert.True(result.IsSuccess);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void ChainedResultExtensions_WhenThereIsNoError()
|
public void ChainedResultExtensions_WhenThereIsNoError()
|
||||||
{
|
{
|
||||||
@@ -104,10 +30,9 @@ public class GeneralUsage
|
|||||||
public void ChainedResultExtensions_WhenThereIsAnError()
|
public void ChainedResultExtensions_WhenThereIsAnError()
|
||||||
{
|
{
|
||||||
// Given
|
// Given
|
||||||
|
|
||||||
// When
|
|
||||||
var error = Error.New("test");
|
var error = Error.New("test");
|
||||||
|
|
||||||
|
// When
|
||||||
|
|
||||||
var result = Result.Success()
|
var result = Result.Success()
|
||||||
.Append(() => Result.Failure<int>(error))
|
.Append(() => Result.Failure<int>(error))
|
||||||
@@ -115,7 +40,7 @@ public class GeneralUsage
|
|||||||
.Map((i, s) =>
|
.Map((i, s) =>
|
||||||
{
|
{
|
||||||
Assert.Fail();
|
Assert.Fail();
|
||||||
return Result.Success("");
|
return "";
|
||||||
})
|
})
|
||||||
.Append("some")
|
.Append("some")
|
||||||
.Bind((s1, s2) =>
|
.Bind((s1, s2) =>
|
||||||
@@ -139,4 +64,152 @@ public class GeneralUsage
|
|||||||
// Then
|
// Then
|
||||||
Assert.Equal("satisfied", result);
|
Assert.Equal("satisfied", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ChainedResultAsyncExtensions_WhenThereIsNoError()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
|
||||||
|
// When
|
||||||
|
var result = await Result.Success()
|
||||||
|
.Append(() => ValueTask.FromResult(Result.Success(1)))
|
||||||
|
.Append("test")
|
||||||
|
.Map((i, s) => $"{s}_{i}")
|
||||||
|
.Append("some")
|
||||||
|
.Bind(async (s1, s2) => await ValueTask.FromResult(Result.Success(string.Join(';', s1, s2))))
|
||||||
|
.Match(
|
||||||
|
onSuccess: s => s.ToUpper(),
|
||||||
|
onFailure: _ =>
|
||||||
|
{
|
||||||
|
Assert.Fail();
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
Assert.Equal("TEST_1;SOME", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ChainedResultAsyncExtensions_WhenThereIsAnError()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var error = Error.New("test");
|
||||||
|
|
||||||
|
// When
|
||||||
|
|
||||||
|
var result = await Result.Success()
|
||||||
|
.Append(() => Task.FromResult(Result.Failure<int>(error)))
|
||||||
|
.Append("test")
|
||||||
|
.Map((i, s) =>
|
||||||
|
{
|
||||||
|
Assert.Fail();
|
||||||
|
return "";
|
||||||
|
})
|
||||||
|
.Append("some")
|
||||||
|
.Bind(async (s1, s2) =>
|
||||||
|
{
|
||||||
|
Assert.Fail();
|
||||||
|
await Task.CompletedTask;
|
||||||
|
return Result.Success("");
|
||||||
|
})
|
||||||
|
.Match(
|
||||||
|
onSuccess: _ =>
|
||||||
|
{
|
||||||
|
Assert.Fail();
|
||||||
|
return "";
|
||||||
|
},
|
||||||
|
onFailure: err =>
|
||||||
|
{
|
||||||
|
Assert.Equal(error, err);
|
||||||
|
return "satisfied";
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
Assert.Equal("satisfied", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void RecoverResultFromFailureState()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
Result<string> failed = new NotImplementedException();
|
||||||
|
// When
|
||||||
|
var result = failed.TryRecover(err =>
|
||||||
|
{
|
||||||
|
Assert.IsType<NotImplementedException>(err.ToException());
|
||||||
|
|
||||||
|
if (err.Type == "System.NotImplementedException")
|
||||||
|
return "recovered";
|
||||||
|
|
||||||
|
Assert.Fail();
|
||||||
|
return "";
|
||||||
|
});
|
||||||
|
// Then
|
||||||
|
Assert.True(result.IsSuccess);
|
||||||
|
Assert.Equal("recovered", result.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void WhenCanNotRecoverResultFromFailureState()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var error = Error.New("test");
|
||||||
|
Result<string> failed = new NotImplementedException();
|
||||||
|
// When
|
||||||
|
var result = failed.TryRecover(err =>
|
||||||
|
{
|
||||||
|
if (err.Type == "System.NotImplementedException")
|
||||||
|
return error;
|
||||||
|
|
||||||
|
Assert.Fail();
|
||||||
|
return "";
|
||||||
|
});
|
||||||
|
// Then
|
||||||
|
Assert.True(result.IsFailure);
|
||||||
|
Assert.Equal(error, result.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void WhenExtendingSuccessWithSuccess_ShouldReturnSuccess()
|
||||||
|
{
|
||||||
|
var success = Result.Success(1)
|
||||||
|
.Append("2");
|
||||||
|
|
||||||
|
var result = success
|
||||||
|
.Extend((i, s) => Result.Success($"{i} + {s}"));
|
||||||
|
|
||||||
|
Assert.True(result.IsSuccess);
|
||||||
|
Assert.Equal((1, "2", "1 + 2"), result.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void WhenExtendingFailureWithSuccess_ShouldNotEvaluateExtension()
|
||||||
|
{
|
||||||
|
var failure = Result.Success(1)
|
||||||
|
.Append(Result.Failure<string>("failure"));
|
||||||
|
|
||||||
|
var result = failure
|
||||||
|
.Extend((i, s) =>
|
||||||
|
{
|
||||||
|
Assert.Fail();
|
||||||
|
return Result.Success("");
|
||||||
|
});
|
||||||
|
|
||||||
|
Assert.True(result.IsFailure);
|
||||||
|
Assert.Equal(Error.New("failure"), result.Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void WhenExtendingSuccessWithFailure_ShouldReturnFailure()
|
||||||
|
{
|
||||||
|
var success = Result.Success(1)
|
||||||
|
.Append("2");
|
||||||
|
|
||||||
|
var result = success
|
||||||
|
.Extend((i, s) => Result.Failure<string>("failure"));
|
||||||
|
|
||||||
|
Assert.True(result.IsFailure);
|
||||||
|
Assert.Equal(Error.New("failure"), result.Error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user