using System.Diagnostics.CodeAnalysis; namespace Iceshrimp.Backend.Core.Helpers; public abstract record Result where TResult : class where TError : class { private Result() { } public sealed record Success(TResult Result) : Result; public sealed record Failure(TError Error) : Result; public static implicit operator Result(TResult result) => new Success(result); public static implicit operator Result(TError error) => new Failure(error); public bool TryGetResult([NotNullWhen(true)] out TResult? result) { if (this is Success s) { result = s.Result; return true; } result = null; return false; } public bool TryGetError([NotNullWhen(true)] out TError? error) { if (this is Failure f) { error = f.Error; return true; } error = null; return false; } public bool IsSuccess => this is Success; public bool IsFailure => this is Failure; } public abstract record Result where TResult : class { private Result() { } public sealed record Success(TResult Result) : Result; public sealed record Failure(Exception Error) : Result; public static implicit operator Result(TResult result) => new Success(result); public static implicit operator Result(Exception error) => new Failure(error); public bool TryGetResult([NotNullWhen(true)] out TResult? result) { if (this is Success s) { result = s.Result; return true; } result = null; return false; } public bool TryGetError([NotNullWhen(true)] out Exception? error) { if (this is Failure f) { error = f.Error; return true; } error = null; return false; } public bool IsSuccess => this is Success; public bool IsFailure => this is Failure; }