Skip to content

Commit

Permalink
Merge pull request #57 from Kiryuumaru/feat/add-convenient-members
Browse files Browse the repository at this point in the history
Added non-append Success variants
  • Loading branch information
Kiryuumaru committed May 29, 2024
2 parents faa34a7 + 8f6add0 commit 39d66cb
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
39 changes: 39 additions & 0 deletions TransactionHelpers/Interface/IResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ public interface IResult
/// </summary>
void ThrowIfError();

/// <summary>
/// Checks if the result has an error.
/// </summary>
/// <returns>True if the result has an error, otherwise false.</returns>
[MemberNotNullWhen(false, nameof(Error))]
bool Success();

/// <summary>
/// Checks if the result has an error or no value.
/// </summary>
/// <returns>True if the result has an error, otherwise false.</returns>
[MemberNotNullWhen(false, nameof(Error))]
bool SuccessAndHasValue();

/// <summary>
/// Appends the specified result and checks if the appended result has an error.
/// </summary>
Expand Down Expand Up @@ -174,4 +188,29 @@ public interface IResult<TValue> : IResult
/// <exception cref="EmptyResultException">the <see cref="IResult{TValue}.Value"/> has no value.</exception>
[MemberNotNull(nameof(Value))]
TValue GetValueOrThrow();

/// <summary>
/// Checks if the result has an error.
/// </summary>
/// <param name="value">The out result value.</param>
/// <returns>false if the result has an error or no value, otherwise true.</returns>
[MemberNotNullWhen(false, nameof(Error))]
bool Success(out TValue? value);

/// <summary>
/// Checks if the result has an error or no value.
/// </summary>
/// <returns>True if the result has an error, otherwise false.</returns>
[MemberNotNullWhen(false, nameof(Error))]
[MemberNotNullWhen(true, nameof(Value))]
new bool SuccessAndHasValue();

/// <summary>
/// Checks if the result has an error.
/// </summary>
/// <param name="value">The out result value.</param>
/// <returns>false if the result has an error or no value, otherwise true.</returns>
[MemberNotNullWhen(false, nameof(Error))]
[MemberNotNullWhen(true, nameof(Value))]
bool SuccessAndHasValue([NotNullWhen(true)] out TValue? value);
}
43 changes: 43 additions & 0 deletions TransactionHelpers/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ public virtual void ThrowIfError()
}
}

/// <inheritdoc/>
[MemberNotNullWhen(false, nameof(Error))]
public bool Success()
{
return !IsError;
}

/// <inheritdoc/>
[MemberNotNullWhen(false, nameof(Error))]
public bool SuccessAndHasValue()
{
return !IsError;
}

/// <inheritdoc/>
[MemberNotNullWhen(false, nameof(Error))]
public bool Success<TAppend>(TAppend resultAppend, bool appendResultValues)
Expand Down Expand Up @@ -188,6 +202,17 @@ public class Result<TValue> : Result, IResult<TValue>
{
internal TValue? InternalValue;

/// <inheritdoc/>
[JsonIgnore]
public override Error? Error => base.Error;

/// <inheritdoc/>
public override IReadOnlyList<Error> Errors
{
get => base.Errors;
init => base.Errors = value.ToList();
}

/// <inheritdoc/>
public virtual TValue? Value
{
Expand Down Expand Up @@ -226,6 +251,24 @@ public virtual TValue GetValueOrThrow()
return Value;
}

/// <inheritdoc/>
[MemberNotNullWhen(false, nameof(Error))]
[MemberNotNullWhen(true, nameof(Value))]
public bool Success(out TValue? value)
{
value = Value;
return !IsError;
}

/// <inheritdoc/>
[MemberNotNullWhen(false, nameof(Error))]
[MemberNotNullWhen(true, nameof(Value))]
public bool SuccessAndHasValue([NotNullWhen(true)] out TValue? value)
{
value = Value;
return !IsError && !HasNoValue;
}

/// <summary>
/// Implicit operator for <see cref="Error"/> conversion.
/// </summary>
Expand Down

0 comments on commit 39d66cb

Please sign in to comment.