From c9d50071edbdc3a9d178f4a2bdec0ea3e1c45dac Mon Sep 17 00:00:00 2001 From: Ionite Date: Sun, 22 Oct 2023 21:46:46 -0400 Subject: [PATCH] Change from DiscriminatedUnion to OneOf --- .../Models/Inference/FileNameFormatPart.cs | 15 ++----- .../Inference/FileNameFormatProvider.cs | 42 ++++++++++--------- .../StabilityMatrix.Avalonia.csproj | 1 - 3 files changed, 25 insertions(+), 33 deletions(-) diff --git a/StabilityMatrix.Avalonia/Models/Inference/FileNameFormatPart.cs b/StabilityMatrix.Avalonia/Models/Inference/FileNameFormatPart.cs index 9210adc0..3b17284b 100644 --- a/StabilityMatrix.Avalonia/Models/Inference/FileNameFormatPart.cs +++ b/StabilityMatrix.Avalonia/Models/Inference/FileNameFormatPart.cs @@ -1,16 +1,7 @@ using System; -using System.Runtime.InteropServices; -using CSharpDiscriminatedUnion.Attributes; +using OneOf; namespace StabilityMatrix.Avalonia.Models.Inference; -[GenerateDiscriminatedUnion(CaseFactoryPrefix = "From")] -[StructLayout(LayoutKind.Auto)] -public readonly partial struct FileNameFormatPart -{ - [StructCase("Constant", isDefaultValue: true)] - private readonly string constant; - - [StructCase("Substitution")] - private readonly Func substitution; -} +[GenerateOneOf] +public partial class FileNameFormatPart : OneOfBase> { } diff --git a/StabilityMatrix.Avalonia/Models/Inference/FileNameFormatProvider.cs b/StabilityMatrix.Avalonia/Models/Inference/FileNameFormatProvider.cs index e8c13f79..1c5790d9 100644 --- a/StabilityMatrix.Avalonia/Models/Inference/FileNameFormatProvider.cs +++ b/StabilityMatrix.Avalonia/Models/Inference/FileNameFormatProvider.cs @@ -84,7 +84,7 @@ public partial class FileNameFormatProvider if (result.Index != currentIndex) { var constant = template[currentIndex..result.Index]; - parts.Add(FileNameFormatPart.FromConstant(constant)); + parts.Add(constant); currentIndex += constant.Length; } @@ -97,30 +97,32 @@ public partial class FileNameFormatProvider if (slice is not null) { parts.Add( - FileNameFormatPart.FromSubstitution(() => - { - var value = substitution(); - if (value is null) - return null; - - if (slice.End is null) - { - value = value[(slice.Start ?? 0)..]; - } - else + (FileNameFormatPart)( + () => { - var length = - Math.Min(value.Length, slice.End.Value) - (slice.Start ?? 0); - value = value.Substring(slice.Start ?? 0, length); + var value = substitution(); + if (value is null) + return null; + + if (slice.End is null) + { + value = value[(slice.Start ?? 0)..]; + } + else + { + var length = + Math.Min(value.Length, slice.End.Value) - (slice.Start ?? 0); + value = value.Substring(slice.Start ?? 0, length); + } + + return value; } - - return value; - }) + ) ); } else { - parts.Add(FileNameFormatPart.FromSubstitution(substitution)); + parts.Add(substitution); } currentIndex += result.Length; @@ -130,7 +132,7 @@ public partial class FileNameFormatProvider if (currentIndex != template.Length) { var constant = template[currentIndex..]; - parts.Add(FileNameFormatPart.FromConstant(constant)); + parts.Add(constant); } return parts; diff --git a/StabilityMatrix.Avalonia/StabilityMatrix.Avalonia.csproj b/StabilityMatrix.Avalonia/StabilityMatrix.Avalonia.csproj index dcc6b0ae..68eb7e14 100644 --- a/StabilityMatrix.Avalonia/StabilityMatrix.Avalonia.csproj +++ b/StabilityMatrix.Avalonia/StabilityMatrix.Avalonia.csproj @@ -32,7 +32,6 @@ -