This commit is contained in:
@@ -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 Result<T> Result<T>(this 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 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 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);
|
||||
[Pure] public static async ValueTask<Result<T>> Result<T>(this ValueTask<Ensure<T>> ensureTask) => await ensureTask.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public readonly struct Ensure<T>
|
||||
@@ -58,6 +35,22 @@ public readonly struct Ensure<T>
|
||||
Value = default!;
|
||||
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]
|
||||
|
||||
@@ -49,7 +49,7 @@ public sealed class ErrorJsonConverter : JsonConverter<Error>
|
||||
=> new(errorInfo.Type, errorInfo.Message) { ExtensionData = errorInfo.ExtensionData };
|
||||
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 message = "";
|
||||
while (reader.Read())
|
||||
@@ -83,8 +83,8 @@ public sealed class ErrorJsonConverter : JsonConverter<Error>
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(propname))
|
||||
{
|
||||
extensionData ??= new(4);
|
||||
extensionData.Add(new(propname, propvalue));
|
||||
extensionData ??= ImmutableDictionary.CreateBuilder<string, string>();
|
||||
extensionData.Add(propname, propvalue);
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -95,7 +95,7 @@ public sealed class ErrorJsonConverter : JsonConverter<Error>
|
||||
}
|
||||
}
|
||||
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)
|
||||
{
|
||||
|
||||
@@ -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>
|
||||
<PackageTags>railway-oriented;functional;result-pattern;result-object;error-handling</PackageTags>
|
||||
<Authors>JustFixMe</Authors>
|
||||
<Copyright>Copyright (c) 2023 JustFixMe</Copyright>
|
||||
<Copyright>Copyright (c) 2023-2024 JustFixMe</Copyright>
|
||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
<RepositoryUrl>https://github.com/JustFixMe/Just.Railway/</RepositoryUrl>
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Just.Railway;
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
namespace Just.Railway;
|
||||
|
||||
internal enum ResultState : byte
|
||||
|
||||
Reference in New Issue
Block a user