mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2025-04-28 10:15:47 +02:00
Make Result type serializable
This commit is contained in:
parent
c99f5bc6bf
commit
d03f532996
Agent/Phantom.Agent.Services/Instances
Common/Phantom.Common.Data
Controller
Phantom.Controller.Database/Repositories
Phantom.Controller.Services/Users
Utils/Phantom.Utils/Tasks
Web/Phantom.Web/Pages
@ -12,7 +12,6 @@ using Phantom.Common.Data.Replies;
|
|||||||
using Phantom.Utils.Actor;
|
using Phantom.Utils.Actor;
|
||||||
using Phantom.Utils.IO;
|
using Phantom.Utils.IO;
|
||||||
using Phantom.Utils.Logging;
|
using Phantom.Utils.Logging;
|
||||||
using Phantom.Utils.Tasks;
|
|
||||||
using Serilog;
|
using Serilog;
|
||||||
|
|
||||||
namespace Phantom.Agent.Services.Instances;
|
namespace Phantom.Agent.Services.Instances;
|
||||||
@ -140,8 +139,8 @@ sealed class InstanceManagerActor : ReceiveActor<InstanceManagerActor.ICommand>
|
|||||||
}
|
}
|
||||||
|
|
||||||
var ticket = instanceTicketManager.Reserve(instanceInfo.Configuration);
|
var ticket = instanceTicketManager.Reserve(instanceInfo.Configuration);
|
||||||
if (ticket is Result<InstanceTicketManager.Ticket, LaunchInstanceResult>.Fail fail) {
|
if (!ticket) {
|
||||||
return InstanceActionResult.Concrete(fail.Error);
|
return InstanceActionResult.Concrete(ticket.Error);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (agentState.InstancesByGuid.TryGetValue(instanceGuid, out var instance)) {
|
if (agentState.InstancesByGuid.TryGetValue(instanceGuid, out var instance)) {
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
using Phantom.Agent.Minecraft.Instance;
|
using Phantom.Agent.Minecraft.Instance;
|
||||||
using Phantom.Agent.Minecraft.Launcher;
|
using Phantom.Agent.Minecraft.Launcher;
|
||||||
using Phantom.Agent.Minecraft.Server;
|
using Phantom.Agent.Minecraft.Server;
|
||||||
|
using Phantom.Common.Data;
|
||||||
using Phantom.Common.Data.Instance;
|
using Phantom.Common.Data.Instance;
|
||||||
using Phantom.Utils.Tasks;
|
|
||||||
|
|
||||||
namespace Phantom.Agent.Services.Instances.State;
|
namespace Phantom.Agent.Services.Instances.State;
|
||||||
|
|
||||||
|
91
Common/Phantom.Common.Data/Result.cs
Normal file
91
Common/Phantom.Common.Data/Result.cs
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using MemoryPack;
|
||||||
|
|
||||||
|
namespace Phantom.Common.Data;
|
||||||
|
|
||||||
|
[MemoryPackable(GenerateType.VersionTolerant)]
|
||||||
|
public sealed partial class Result<TValue, TError> {
|
||||||
|
[MemoryPackOrder(0)]
|
||||||
|
[MemoryPackInclude]
|
||||||
|
private readonly bool hasValue;
|
||||||
|
|
||||||
|
[MemoryPackOrder(1)]
|
||||||
|
[MemoryPackInclude]
|
||||||
|
private readonly TValue? value;
|
||||||
|
|
||||||
|
[MemoryPackOrder(2)]
|
||||||
|
[MemoryPackInclude]
|
||||||
|
private readonly TError? error;
|
||||||
|
|
||||||
|
[MemoryPackIgnore]
|
||||||
|
public TValue Value => hasValue ? value! : throw new InvalidOperationException("Attempted to get value from an error result.");
|
||||||
|
|
||||||
|
[MemoryPackIgnore]
|
||||||
|
public TError Error => !hasValue ? error! : throw new InvalidOperationException("Attempted to get error from a success result.");
|
||||||
|
|
||||||
|
private Result(bool hasValue, TValue? value, TError? error) {
|
||||||
|
this.hasValue = hasValue;
|
||||||
|
this.value = value;
|
||||||
|
this.error = error;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static implicit operator Result<TValue, TError>(TValue value) {
|
||||||
|
return new Result<TValue, TError>(hasValue: true, value, default);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static implicit operator Result<TValue, TError>(TError error) {
|
||||||
|
return new Result<TValue, TError>(hasValue: false, default, error);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static implicit operator bool(Result<TValue, TError> result) {
|
||||||
|
return result.hasValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[MemoryPackable(GenerateType.VersionTolerant)]
|
||||||
|
public sealed partial class Result<TError> {
|
||||||
|
[MemoryPackOrder(0)]
|
||||||
|
[MemoryPackInclude]
|
||||||
|
private readonly bool hasValue;
|
||||||
|
|
||||||
|
[MemoryPackOrder(1)]
|
||||||
|
[MemoryPackInclude]
|
||||||
|
private readonly TError? error;
|
||||||
|
|
||||||
|
[MemoryPackIgnore]
|
||||||
|
public TError Error => !hasValue ? error! : throw new InvalidOperationException("Attempted to get error from a success result.");
|
||||||
|
|
||||||
|
private Result(bool hasValue, TError? error) {
|
||||||
|
this.hasValue = hasValue;
|
||||||
|
this.error = error;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool TryGetError([MaybeNullWhen(false)] out TError error) {
|
||||||
|
if (hasValue) {
|
||||||
|
error = default;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
error = this.error!;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static implicit operator Result<TError>([SuppressMessage("ReSharper", "UnusedParameter.Global")] Result.OkType _) {
|
||||||
|
return new Result<TError>(hasValue: true, default);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static implicit operator Result<TError>(TError error) {
|
||||||
|
return new Result<TError>(hasValue: false, error);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static implicit operator bool(Result<TError> result) {
|
||||||
|
return result.hasValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Result {
|
||||||
|
public static OkType Ok { get; } = new ();
|
||||||
|
|
||||||
|
public readonly record struct OkType;
|
||||||
|
}
|
@ -1,9 +1,9 @@
|
|||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Phantom.Common.Data;
|
||||||
using Phantom.Common.Data.Web.Users;
|
using Phantom.Common.Data.Web.Users;
|
||||||
using Phantom.Controller.Database.Entities;
|
using Phantom.Controller.Database.Entities;
|
||||||
using Phantom.Utils.Collections;
|
using Phantom.Utils.Collections;
|
||||||
using Phantom.Utils.Tasks;
|
|
||||||
|
|
||||||
namespace Phantom.Controller.Database.Repositories;
|
namespace Phantom.Controller.Database.Repositories;
|
||||||
|
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Phantom.Common.Data;
|
||||||
using Phantom.Common.Data.Web.Users;
|
using Phantom.Common.Data.Web.Users;
|
||||||
using Phantom.Common.Data.Web.Users.AddUserErrors;
|
using Phantom.Common.Data.Web.Users.AddUserErrors;
|
||||||
using Phantom.Common.Data.Web.Users.PasswordRequirementViolations;
|
using Phantom.Common.Data.Web.Users.PasswordRequirementViolations;
|
||||||
using Phantom.Common.Data.Web.Users.UsernameRequirementViolations;
|
using Phantom.Common.Data.Web.Users.UsernameRequirementViolations;
|
||||||
using Phantom.Controller.Database.Entities;
|
using Phantom.Controller.Database.Entities;
|
||||||
using Phantom.Utils.Collections;
|
using Phantom.Utils.Collections;
|
||||||
using Phantom.Utils.Tasks;
|
|
||||||
|
|
||||||
namespace Phantom.Controller.Database.Repositories;
|
namespace Phantom.Controller.Database.Repositories;
|
||||||
|
|
||||||
|
@ -54,9 +54,8 @@ sealed class UserManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var result = userRepository.SetUserPassword(user, password);
|
if (userRepository.SetUserPassword(user, password).TryGetError(out var error)) {
|
||||||
if (!result) {
|
return new Common.Data.Web.Users.CreateOrUpdateAdministratorUserResults.UpdatingFailed(error);
|
||||||
return new Common.Data.Web.Users.CreateOrUpdateAdministratorUserResults.UpdatingFailed(result.Error);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auditLogWriter.AdministratorUserModified(user);
|
auditLogWriter.AdministratorUserModified(user);
|
||||||
|
@ -1,69 +0,0 @@
|
|||||||
using System.Diagnostics.CodeAnalysis;
|
|
||||||
|
|
||||||
namespace Phantom.Utils.Tasks;
|
|
||||||
|
|
||||||
public abstract record Result<TValue, TError> {
|
|
||||||
private Result() {}
|
|
||||||
|
|
||||||
public abstract TValue Value { get; init; }
|
|
||||||
public abstract TError Error { get; init; }
|
|
||||||
|
|
||||||
public static implicit operator Result<TValue, TError>(TValue value) {
|
|
||||||
return new Ok(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static implicit operator Result<TValue, TError>(TError error) {
|
|
||||||
return new Fail(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static implicit operator bool(Result<TValue, TError> result) {
|
|
||||||
return result is Ok;
|
|
||||||
}
|
|
||||||
|
|
||||||
public sealed record Ok(TValue Value) : Result<TValue, TError> {
|
|
||||||
public override TError Error {
|
|
||||||
get => throw new InvalidOperationException("Attempted to get error from Ok result.");
|
|
||||||
init {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public sealed record Fail(TError Error) : Result<TValue, TError> {
|
|
||||||
public override TValue Value {
|
|
||||||
get => throw new InvalidOperationException("Attempted to get value from Fail result.");
|
|
||||||
init {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public abstract record Result<TError> {
|
|
||||||
private Result() {}
|
|
||||||
|
|
||||||
public abstract TError Error { get; init; }
|
|
||||||
|
|
||||||
public static implicit operator Result<TError>(TError error) {
|
|
||||||
return new Fail(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static implicit operator Result<TError>([SuppressMessage("ReSharper", "UnusedParameter.Global")] Result.OkType _) {
|
|
||||||
return new Ok();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static implicit operator bool(Result<TError> result) {
|
|
||||||
return result is Ok;
|
|
||||||
}
|
|
||||||
|
|
||||||
public sealed record Ok : Result<TError> {
|
|
||||||
public override TError Error {
|
|
||||||
get => throw new InvalidOperationException("Attempted to get error from Ok result.");
|
|
||||||
init {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public sealed record Fail(TError Error) : Result<TError>;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Result {
|
|
||||||
public static OkType Ok { get; } = new ();
|
|
||||||
|
|
||||||
public readonly record struct OkType;
|
|
||||||
}
|
|
@ -1,14 +1,14 @@
|
|||||||
@page "/setup"
|
@page "/setup"
|
||||||
@using Phantom.Utils.Tasks
|
@using Phantom.Common.Data
|
||||||
|
@using Phantom.Common.Data.Web.Users
|
||||||
|
@using Phantom.Common.Data.Web.Users.CreateOrUpdateAdministratorUserResults
|
||||||
|
@using Phantom.Common.Messages.Web.ToController
|
||||||
|
@using Phantom.Utils.Cryptography
|
||||||
@using Phantom.Web.Services
|
@using Phantom.Web.Services
|
||||||
@using Phantom.Web.Services.Authentication
|
@using Phantom.Web.Services.Authentication
|
||||||
@using Phantom.Web.Services.Rpc
|
@using Phantom.Web.Services.Rpc
|
||||||
@using System.ComponentModel.DataAnnotations
|
@using System.ComponentModel.DataAnnotations
|
||||||
@using Phantom.Utils.Cryptography
|
|
||||||
@using System.Security.Cryptography
|
@using System.Security.Cryptography
|
||||||
@using Phantom.Common.Messages.Web.ToController
|
|
||||||
@using Phantom.Common.Data.Web.Users
|
|
||||||
@using Phantom.Common.Data.Web.Users.CreateOrUpdateAdministratorUserResults
|
|
||||||
@attribute [AllowAnonymous]
|
@attribute [AllowAnonymous]
|
||||||
@inject ApplicationProperties ApplicationProperties
|
@inject ApplicationProperties ApplicationProperties
|
||||||
@inject UserLoginManager LoginManager
|
@inject UserLoginManager LoginManager
|
||||||
@ -65,8 +65,9 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (await CreateOrUpdateAdministrator() is Result<string>.Fail fail) {
|
var createOrUpdateAdministratorResult = await CreateOrUpdateAdministrator();
|
||||||
form.SubmitModel.StopSubmitting(fail.Error);
|
if (createOrUpdateAdministratorResult.TryGetError(out var error)) {
|
||||||
|
form.SubmitModel.StopSubmitting(error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user