1 Commits

Author SHA1 Message Date
57e83fbafa removed some new lang features to support net6.0 and net7.0
All checks were successful
.NET Test / test (push) Successful in 1m5s
.NET Publish / publish (push) Successful in 51s
2023-12-15 12:37:39 +04:00
7 changed files with 76 additions and 60 deletions

View File

@@ -60,6 +60,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)
@@ -72,7 +73,12 @@ public readonly struct Ensure<T>
} }
[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; }
} }

View File

@@ -52,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([]), (null, null) => new ManyErrors(new List<Error>()),
(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,
@@ -231,7 +231,7 @@ public sealed class ExceptionalError : Error
var valueString = value.ToString(); var valueString = value.ToString();
if (string.IsNullOrEmpty(keyString) || string.IsNullOrEmpty(valueString)) continue; if (string.IsNullOrEmpty(keyString) || string.IsNullOrEmpty(valueString)) continue;
values ??= []; values ??= new List<KeyValuePair<string, string>>(4);
values.Add(new(keyString, valueString)); values.Add(new(keyString, valueString));
} }
return values?.ToImmutableDictionary() ?? ImmutableDictionary<string, string>.Empty; return values?.ToImmutableDictionary() ?? ImmutableDictionary<string, string>.Empty;
@@ -377,7 +377,11 @@ public sealed class ManyErrors : Error, IEnumerable<Error>, IReadOnlyList<Error>
} }
[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; }
} }

View File

@@ -35,7 +35,7 @@ public sealed class ErrorJsonConverter : JsonConverter<Error>
internal static ManyErrors ReadMany(ref Utf8JsonReader reader) internal static ManyErrors ReadMany(ref Utf8JsonReader reader)
{ {
List<Error> errors = []; List<Error> errors = new(4);
while (reader.Read()) while (reader.Read())
{ {
if (reader.TokenType == JsonTokenType.StartObject) if (reader.TokenType == JsonTokenType.StartObject)
@@ -83,7 +83,7 @@ public sealed class ErrorJsonConverter : JsonConverter<Error>
} }
else if (!string.IsNullOrEmpty(propname)) else if (!string.IsNullOrEmpty(propname))
{ {
extensionData ??= []; extensionData ??= new(4);
extensionData.Add(new(propname, propvalue)); extensionData.Add(new(propname, propvalue));
} }

View File

@@ -1,7 +1,8 @@
<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>
@@ -13,7 +14,6 @@
<Copyright>Copyright (c) 2023 JustFixMe</Copyright> <Copyright>Copyright (c) 2023 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>
<EmitCompilerGeneratedFiles Condition="'$(Configuration)'=='Debug'">true</EmitCompilerGeneratedFiles> <EmitCompilerGeneratedFiles Condition="'$(Configuration)'=='Debug'">true</EmitCompilerGeneratedFiles>

View File

@@ -10,10 +10,9 @@ 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?, bool> IsEqualFunc;
public static readonly Func<T?, T?, int> CompareFunc; public static readonly Func<T?, T?, int> CompareFunc;
@@ -53,7 +52,7 @@ file static class TypeReflectionCache<T>
} }
} }
#pragma warning disable 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 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 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);
@@ -63,5 +62,6 @@ file static class TypeReflectionCache<T>
[Pure] public static int CompareNullable<R>(R? left, R? right) where R : struct, IComparable<R> => left is null [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 ? 0 : -1
: right is null ? 1 : left.Value.CompareTo(right.Value); : right is null ? 1 : left.Value.CompareTo(right.Value);
#pragma warning restore CS8604 // Possible null reference argument. #pragma warning restore CS8604 // Possible null reference argument.
}
} }

View File

@@ -136,6 +136,7 @@ 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] public static explicit operator Result(Result<T> result) => result.State switch
@@ -263,7 +264,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; }
} }

View File

@@ -89,7 +89,7 @@ public static partial class ResultExtensions
{ {
case ResultState.Error: case ResultState.Error:
hasErrors = true; hasErrors = true;
errors ??= []; errors ??= new(4);
errors.Add(result.Error!); errors.Add(result.Error!);
break; break;
@@ -123,13 +123,13 @@ public static partial class ResultExtensions
{ {
case ResultState.Error: case ResultState.Error:
hasErrors = true; hasErrors = true;
errors ??= []; errors ??= new(4);
errors.Add(result.Error!); errors.Add(result.Error!);
break; break;
case ResultState.Success: case ResultState.Success:
if (hasErrors) goto afterLoop; if (hasErrors) goto afterLoop;
values ??= []; values ??= new(4);
values.Add(result.Value); values.Add(result.Value);
break; break;