2019-11-03 02:49:39 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack
|
|
|
|
|
{
|
|
|
|
|
public struct GetResponse<T> : IEquatable<GetResponse<T>>, IErrorResponse
|
|
|
|
|
{
|
|
|
|
|
public static readonly GetResponse<T> Failure = new GetResponse<T>();
|
|
|
|
|
|
|
|
|
|
public readonly T Value;
|
|
|
|
|
public readonly bool Succeeded;
|
2020-04-03 23:23:13 +00:00
|
|
|
|
public readonly Exception? Exception;
|
2019-11-03 02:49:39 +00:00
|
|
|
|
private readonly string _reason;
|
|
|
|
|
|
|
|
|
|
public bool Failed => !Succeeded;
|
|
|
|
|
public string Reason
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2019-11-21 15:04:48 +00:00
|
|
|
|
if (Exception != null)
|
2019-11-03 02:49:39 +00:00
|
|
|
|
{
|
2019-11-21 15:04:48 +00:00
|
|
|
|
return Exception.ToString();
|
2019-11-03 02:49:39 +00:00
|
|
|
|
}
|
|
|
|
|
return _reason;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-21 15:04:48 +00:00
|
|
|
|
bool IErrorResponse.Succeeded => Succeeded;
|
2020-04-03 23:23:13 +00:00
|
|
|
|
Exception? IErrorResponse.Exception => Exception;
|
2019-11-03 02:49:39 +00:00
|
|
|
|
|
|
|
|
|
private GetResponse(
|
|
|
|
|
bool succeeded,
|
2020-04-03 23:23:13 +00:00
|
|
|
|
T val = default,
|
|
|
|
|
string? reason = null,
|
|
|
|
|
Exception? ex = null)
|
2019-11-03 02:49:39 +00:00
|
|
|
|
{
|
2019-11-21 15:04:48 +00:00
|
|
|
|
Value = val;
|
|
|
|
|
Succeeded = succeeded;
|
2020-04-03 23:23:13 +00:00
|
|
|
|
_reason = reason ?? string.Empty;
|
2019-11-21 15:04:48 +00:00
|
|
|
|
Exception = ex;
|
2019-11-03 02:49:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Equals(GetResponse<T> other)
|
|
|
|
|
{
|
2019-11-21 15:04:48 +00:00
|
|
|
|
return Succeeded == other.Succeeded
|
|
|
|
|
&& Equals(Value, other.Value);
|
2019-11-03 02:49:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 23:23:13 +00:00
|
|
|
|
public override bool Equals(object? obj)
|
2019-11-03 02:49:39 +00:00
|
|
|
|
{
|
|
|
|
|
if (!(obj is GetResponse<T> rhs)) return false;
|
|
|
|
|
return Equals(rhs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
2020-04-02 16:09:05 +00:00
|
|
|
|
System.HashCode hash = new HashCode();
|
|
|
|
|
hash.Add(Value);
|
|
|
|
|
hash.Add(Succeeded);
|
|
|
|
|
return hash.ToHashCode();
|
2019-11-03 02:49:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return $"({(Succeeded ? "Success" : "Fail")}, {Value}, {Reason})";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public GetResponse<R> BubbleFailure<R>()
|
|
|
|
|
{
|
|
|
|
|
return new GetResponse<R>(
|
|
|
|
|
succeeded: false,
|
2019-11-21 15:04:48 +00:00
|
|
|
|
reason: _reason,
|
|
|
|
|
ex: Exception);
|
2019-11-03 02:49:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public GetResponse<R> Bubble<R>(Func<T, R> conv)
|
|
|
|
|
{
|
|
|
|
|
return new GetResponse<R>(
|
2019-11-21 15:04:48 +00:00
|
|
|
|
succeeded: Succeeded,
|
|
|
|
|
val: conv(Value),
|
|
|
|
|
reason: _reason,
|
|
|
|
|
ex: Exception);
|
2019-11-03 02:49:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T EvaluateOrThrow()
|
|
|
|
|
{
|
2019-11-21 15:04:48 +00:00
|
|
|
|
if (Succeeded)
|
2019-11-03 02:49:39 +00:00
|
|
|
|
{
|
2019-11-21 15:04:48 +00:00
|
|
|
|
return Value;
|
2019-11-03 02:49:39 +00:00
|
|
|
|
}
|
2019-11-21 15:04:48 +00:00
|
|
|
|
throw new ArgumentException(Reason);
|
2019-11-03 02:49:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region Factories
|
|
|
|
|
public static GetResponse<T> Succeed(T value)
|
|
|
|
|
{
|
|
|
|
|
return new GetResponse<T>(true, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static GetResponse<T> Succeed(T value, string reason)
|
|
|
|
|
{
|
|
|
|
|
return new GetResponse<T>(true, value, reason);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static GetResponse<T> Fail(string reason)
|
|
|
|
|
{
|
|
|
|
|
return new GetResponse<T>(false, reason: reason);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static GetResponse<T> Fail(T val, string reason)
|
|
|
|
|
{
|
|
|
|
|
return new GetResponse<T>(false, val, reason);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static GetResponse<T> Fail(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return new GetResponse<T>(false, ex: ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static GetResponse<T> Fail(T val, Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return new GetResponse<T>(false, val, ex: ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static GetResponse<T> Fail(T val)
|
|
|
|
|
{
|
|
|
|
|
return new GetResponse<T>(false, val);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 23:23:13 +00:00
|
|
|
|
public static GetResponse<T> Create(bool successful, T val = default(T), string? reason = null)
|
2019-11-03 02:49:39 +00:00
|
|
|
|
{
|
|
|
|
|
return new GetResponse<T>(successful, val, reason);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|