using System;
using System.Diagnostics;
namespace Discord.Commands
{
///
/// Contains information of the command's overall execution result.
///
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public struct ExecuteResult : IResult
{
///
/// Gets the exception that may have occurred during the command execution.
///
public Exception Exception { get; }
///
public CommandError? Error { get; }
///
public string ErrorReason { get; }
///
public bool IsSuccess => !Error.HasValue;
private ExecuteResult(Exception exception, CommandError? error, string errorReason)
{
Exception = exception;
Error = error;
ErrorReason = errorReason;
}
///
/// Initializes a new with no error, indicating a successful execution.
///
///
/// A that does not contain any errors.
///
public static ExecuteResult FromSuccess()
=> new ExecuteResult(null, null, null);
///
/// Initializes a new with a specified and its
/// reason, indicating an unsuccessful execution.
///
/// The type of error.
/// The reason behind the error.
///
/// A that contains a and reason.
///
public static ExecuteResult FromError(CommandError error, string reason)
=> new ExecuteResult(null, error, reason);
///
/// Initializes a new with a specified exception, indicating an unsuccessful
/// execution.
///
/// The exception that caused the command execution to fail.
///
/// A that contains the exception that caused the unsuccessful execution, along
/// with a of type Exception as well as the exception message as the
/// reason.
///
public static ExecuteResult FromError(Exception ex)
=> new ExecuteResult(ex, CommandError.Exception, ex.Message);
///
/// Initializes a new with a specified result; this may or may not be an
/// successful execution depending on the and
/// specified.
///
/// The result to inherit from.
///
/// A that inherits the error type and reason.
///
public static ExecuteResult FromError(IResult result)
=> new ExecuteResult(null, result.Error, result.ErrorReason);
///
/// Gets a string that indicates the execution result.
///
///
/// Success if is true; otherwise ":
/// ".
///
public override string ToString() => IsSuccess ? "Success" : $"{Error}: {ErrorReason}";
private string DebuggerDisplay => IsSuccess ? "Success" : $"{Error}: {ErrorReason}";
}
}