1
0

feat: use lightweight OneOf instead of LanguageExt

This commit is contained in:
2026-04-10 21:05:56 +08:00
parent 1f4d70c766
commit a800b53188
3 changed files with 10 additions and 14 deletions

View File

@@ -17,7 +17,7 @@
<PackageReference Include="CommunityToolkit.HighPerformance" Version="8.4.0" /> <PackageReference Include="CommunityToolkit.HighPerformance" Version="8.4.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" /> <PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" />
<PackageReference Include="DotNetZip" Version="1.9.1.8" /> <PackageReference Include="DotNetZip" Version="1.9.1.8" />
<PackageReference Include="LanguageExt.Core" Version="4.4.9" /> <PackageReference Include="OneOf" Version="3.0.271" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="10.0.5" /> <PackageReference Include="System.Configuration.ConfigurationManager" Version="10.0.5" />
</ItemGroup> </ItemGroup>

View File

@@ -17,20 +17,16 @@ namespace BallanceTasEditor.Frontend.Validator {
private readonly IValidator<TIn, TOut> m_Validator; private readonly IValidator<TIn, TOut> m_Validator;
public ValidationResult? Validate(TIn value, ValidationContext validationContext) { public ValidationResult? Validate(TIn value, ValidationContext validationContext) {
// YYC MARK: return m_Validator.Validate(value).Match(
// Due to the shitty behavior of LanguageExt v => ValidationResult.Success,
// which do not allow I return nullable class from Match, err => new ValidationResult(err)
// I was forcely use MatchUnsafe.
return m_Validator.Validate(value).MatchUnsafe(
Left: v => ValidationResult.Success,
Right: err => new ValidationResult(err)
); );
} }
public TOut Conclude(TIn value) { public TOut Conclude(TIn value) {
return m_Validator.Validate(value).Match( return m_Validator.Validate(value).Match(
Left: v => v, v => v,
Right: _ => throw new InvalidOperationException("Can not unwrap an error casting.") _ => throw new InvalidOperationException("Can not unwrap an error casting.")
); );
} }

View File

@@ -1,4 +1,4 @@
using LanguageExt; using OneOf;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@@ -8,11 +8,11 @@ using System.Threading.Tasks;
namespace BallanceTasEditor.Frontend.Validator { namespace BallanceTasEditor.Frontend.Validator {
public interface IValidator<TIn, TOut> { public interface IValidator<TIn, TOut> {
Either<TOut, string> Validate(TIn value); OneOf<TOut, string> Validate(TIn value);
} }
public sealed class FpsValidator : IValidator<string, uint> { public sealed class FpsValidator : IValidator<string, uint> {
public Either<uint, string> Validate(string value) { public OneOf<uint, string> Validate(string value) {
if (uint.TryParse(value, System.Globalization.CultureInfo.InvariantCulture, out uint fps)) { if (uint.TryParse(value, System.Globalization.CultureInfo.InvariantCulture, out uint fps)) {
if (Backend.FpsConverter.IsValidFps(fps)) { if (Backend.FpsConverter.IsValidFps(fps)) {
return fps; return fps;
@@ -26,7 +26,7 @@ namespace BallanceTasEditor.Frontend.Validator {
} }
public sealed class CountValidator : IValidator<string, int> { public sealed class CountValidator : IValidator<string, int> {
public Either<int, string> Validate(string value) { public OneOf<int, string> Validate(string value) {
if (int.TryParse(value, System.Globalization.CultureInfo.InvariantCulture, out int count)) { if (int.TryParse(value, System.Globalization.CultureInfo.InvariantCulture, out int count)) {
if (count > 0) { if (count > 0) {
return count; return count;