This commit is contained in:
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
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
@@ -45,6 +42,10 @@ public sealed class EnsureExtensionsExecutor : IGeneratorExecutor
|
|||||||
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 Null");
|
||||||
|
errorGenerationDefinitions.ForEach(def => GenerateNullExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
||||||
|
sb.AppendLine("#endregion");
|
||||||
|
|
||||||
sb.AppendLine("#region NotNull");
|
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");
|
||||||
@@ -57,6 +58,34 @@ public sealed class EnsureExtensionsExecutor : IGeneratorExecutor
|
|||||||
errorGenerationDefinitions.ForEach(def => GenerateNotWhitespaceExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
errorGenerationDefinitions.ForEach(def => GenerateNotWhitespaceExtensions(sb, def.ErrorParameterDecl, def.ErrorValueExpr));
|
||||||
sb.AppendLine("#endregion");
|
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 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,6 +148,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.\")";
|
||||||
@@ -230,4 +297,183 @@ 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 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;
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.CodeAnalysis;
|
|
||||||
|
|
||||||
namespace Just.Railway.SourceGen;
|
namespace Just.Railway.SourceGen;
|
||||||
|
|
||||||
|
|||||||
@@ -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,32 +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))
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly struct Ensure<T>
|
public readonly struct Ensure<T>
|
||||||
@@ -58,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]
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -7,8 +7,9 @@ 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);
|
||||||
Assert.Equal(69, result.Value);
|
Assert.Equal(69, result.Value);
|
||||||
}
|
}
|
||||||
@@ -18,8 +19,9 @@ 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);
|
||||||
Assert.Equal(error, result.Error);
|
Assert.Equal(error, result.Error);
|
||||||
}
|
}
|
||||||
@@ -32,8 +34,9 @@ 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);
|
||||||
Assert.Equal("69", result.Value);
|
Assert.Equal("69", result.Value);
|
||||||
}
|
}
|
||||||
@@ -47,8 +50,9 @@ 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);
|
||||||
Assert.Equal(error, result.Error);
|
Assert.Equal(error, result.Error);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user