Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f2f0221f76 | |||
| e909eeae10 | |||
| 719b4e85f5 | |||
| 3d34a3021d |
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
|
||||||
|
|||||||
23
README.md
23
README.md
@@ -69,9 +69,9 @@ Result<T> Bar()
|
|||||||
```csharp
|
```csharp
|
||||||
Result<int> result = GetResult();
|
Result<int> result = GetResult();
|
||||||
|
|
||||||
var value = result
|
string value = result
|
||||||
.Append("new")
|
.Append("new") // -> Result<(int, string)>
|
||||||
.Map((i, s) => $"{s} result {i}")
|
.Map((i, s) => $"{s} result {i}") // -> Result<string>
|
||||||
.Match(
|
.Match(
|
||||||
onSuccess: x => x,
|
onSuccess: x => x,
|
||||||
onFailure: err => err.ToString()
|
onFailure: err => err.ToString()
|
||||||
@@ -81,6 +81,17 @@ var value = result
|
|||||||
Result<int> GetResult() => Result.Success(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
|
### Try
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
@@ -99,9 +110,9 @@ int SomeFunction() => 1;
|
|||||||
### Ensure
|
### Ensure
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
var value = GetValue();
|
int? value = GetValue();
|
||||||
Result<int> result = Ensure.That(value)
|
Result<int> result = Ensure.That(value) // -> Ensure<int?>
|
||||||
.NotNull()
|
.NotNull() // -> Ensure<int>
|
||||||
.Satisfies(i => i < 100)
|
.Satisfies(i => i < 100)
|
||||||
.Result();
|
.Result();
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,4 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Immutable;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
@@ -41,22 +38,82 @@ public sealed class EnsureExtensionsExecutor : IGeneratorExecutor
|
|||||||
|
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
|
|
||||||
sb.AppendLine($"#region Satisfies");
|
sb.AppendLine("#region Satisfies");
|
||||||
errorGenerationDefinitions.ForEach(def => GenerateSatisfiesExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
errorGenerationDefinitions.ForEach(def => GenerateSatisfiesExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
||||||
sb.AppendLine("#endregion");
|
sb.AppendLine("#endregion");
|
||||||
|
|
||||||
sb.AppendLine($"#region NotNull");
|
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));
|
errorGenerationDefinitions.ForEach(def => GenerateNotNullExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
||||||
sb.AppendLine("#endregion");
|
sb.AppendLine("#endregion");
|
||||||
|
|
||||||
sb.AppendLine($"#region NotEmpty");
|
sb.AppendLine("#region NotEmpty");
|
||||||
errorGenerationDefinitions.ForEach(def => GenerateNotEmptyExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
errorGenerationDefinitions.ForEach(def => GenerateNotEmptyExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
||||||
sb.AppendLine("#endregion");
|
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();
|
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)
|
private void GenerateNotEmptyExtensions(StringBuilder sb, string errorParameterDecl, string errorValueExpr)
|
||||||
{
|
{
|
||||||
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} is empty.\")";
|
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} is empty.\")";
|
||||||
@@ -95,6 +152,44 @@ public sealed class EnsureExtensionsExecutor : IGeneratorExecutor
|
|||||||
"""));
|
"""));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
private void GenerateNotNullExtensions(StringBuilder sb, string errorParameterDecl, string errorValueExpr)
|
||||||
{
|
{
|
||||||
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} is null.\")";
|
string defaultErrorExpr = "?? Error.New(DefaultErrorType, $\"Value {{{ensure.ValueExpression}}} is null.\")";
|
||||||
@@ -206,4 +301,204 @@ public sealed class EnsureExtensionsExecutor : IGeneratorExecutor
|
|||||||
}
|
}
|
||||||
""");
|
""");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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,6 +13,7 @@ public class ExtensionsMethodGenerator : IIncrementalGenerator
|
|||||||
new ResultMapExecutor(),
|
new ResultMapExecutor(),
|
||||||
new ResultBindExecutor(),
|
new ResultBindExecutor(),
|
||||||
new ResultTapExecutor(),
|
new ResultTapExecutor(),
|
||||||
|
new ResultTryRecoverExecutor(),
|
||||||
new ResultAppendExecutor(),
|
new ResultAppendExecutor(),
|
||||||
new TryExtensionsExecutor(),
|
new TryExtensionsExecutor(),
|
||||||
new EnsureExtensionsExecutor(),
|
new EnsureExtensionsExecutor(),
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
|
|||||||
@@ -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>
|
||||||
@@ -70,6 +35,22 @@ 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]
|
||||||
|
|||||||
@@ -178,14 +178,17 @@ 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().FullName ?? exception.GetType().Name, exception.Message)
|
: this(ToErrorType(exception.GetType()), exception.Message)
|
||||||
{
|
{
|
||||||
Exception = exception;
|
Exception = exception;
|
||||||
ExtensionData = ExtractExtensionData(exception);
|
ExtensionData = ExtractExtensionData(exception);
|
||||||
}
|
}
|
||||||
internal ExceptionalError(string message, Exception exception)
|
internal ExceptionalError(string message, Exception exception)
|
||||||
: this(exception.GetType().FullName ?? exception.GetType().Name, message)
|
: this(ToErrorType(exception.GetType()), message)
|
||||||
{
|
{
|
||||||
Exception = exception;
|
Exception = exception;
|
||||||
ExtensionData = ExtractExtensionData(exception);
|
ExtensionData = ExtractExtensionData(exception);
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ public sealed class ErrorJsonConverter : JsonConverter<Error>
|
|||||||
=> new(errorInfo.Type, errorInfo.Message) { ExtensionData = errorInfo.ExtensionData };
|
=> new(errorInfo.Type, errorInfo.Message) { ExtensionData = errorInfo.ExtensionData };
|
||||||
internal static (string Type, string Message, ImmutableDictionary<string, string> ExtensionData) ReadOne(ref Utf8JsonReader reader)
|
internal static (string Type, string Message, ImmutableDictionary<string, string> ExtensionData) ReadOne(ref Utf8JsonReader reader)
|
||||||
{
|
{
|
||||||
List<KeyValuePair<string, string>>? extensionData = null;
|
ImmutableDictionary<string, string>.Builder? extensionData = null;
|
||||||
string type = "error";
|
string type = "error";
|
||||||
string message = "";
|
string message = "";
|
||||||
while (reader.Read())
|
while (reader.Read())
|
||||||
@@ -83,8 +83,8 @@ public sealed class ErrorJsonConverter : JsonConverter<Error>
|
|||||||
}
|
}
|
||||||
else if (!string.IsNullOrEmpty(propname))
|
else if (!string.IsNullOrEmpty(propname))
|
||||||
{
|
{
|
||||||
extensionData ??= new(4);
|
extensionData ??= ImmutableDictionary.CreateBuilder<string, string>();
|
||||||
extensionData.Add(new(propname, propvalue));
|
extensionData.Add(propname, propvalue);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@@ -95,7 +95,7 @@ public sealed class ErrorJsonConverter : JsonConverter<Error>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
endLoop:
|
endLoop:
|
||||||
return (type, message, extensionData?.ToImmutableDictionary() ?? ImmutableDictionary<string, string>.Empty);
|
return (type, message, extensionData?.ToImmutable() ?? ImmutableDictionary<string, string>.Empty);
|
||||||
}
|
}
|
||||||
internal static void WriteOne(Utf8JsonWriter writer, Error value)
|
internal static void WriteOne(Utf8JsonWriter writer, Error value)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
<Description>Base for railway-oriented programming in .NET. Package includes Result object, Error class and most of the common extensions.</Description>
|
<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>
|
<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>
|
||||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||||
<RepositoryUrl>https://github.com/JustFixMe/Just.Railway/</RepositoryUrl>
|
<RepositoryUrl>https://github.com/JustFixMe/Just.Railway/</RepositoryUrl>
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
|
|
||||||
namespace Just.Railway;
|
namespace Just.Railway;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
namespace Just.Railway;
|
namespace Just.Railway;
|
||||||
|
|
||||||
internal enum ResultState : byte
|
internal enum ResultState : byte
|
||||||
@@ -8,6 +7,7 @@ internal enum ResultState : byte
|
|||||||
|
|
||||||
public readonly partial struct Result : IEquatable<Result>
|
public readonly partial struct Result : IEquatable<Result>
|
||||||
{
|
{
|
||||||
|
internal SuccessUnit Value => new();
|
||||||
internal readonly Error? Error;
|
internal readonly Error? Error;
|
||||||
internal readonly ResultState State;
|
internal readonly ResultState State;
|
||||||
|
|
||||||
@@ -51,12 +51,17 @@ 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;
|
||||||
@@ -139,14 +144,20 @@ public readonly struct Result<T> : IEquatable<Result<T>>
|
|||||||
Error = default;
|
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;
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -128,4 +128,45 @@ public class GeneralUsage
|
|||||||
// Then
|
// Then
|
||||||
Assert.Equal("satisfied", result);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user