Browse Source

make SearchResult public (#365)

pull/1966/head
Cenk Ergen GitHub 3 years ago
parent
commit
f4539e2d3f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 57 additions and 1 deletions
  1. +57
    -1
      src/Discord.Net.Interactions/Results/SearchResult.cs

+ 57
- 1
src/Discord.Net.Interactions/Results/SearchResult.cs View File

@@ -2,15 +2,34 @@ using System;


namespace Discord.Interactions namespace Discord.Interactions
{ {
internal struct SearchResult<T> : IResult where T : class, ICommandInfo
/// <summary>
/// Contains information of a command search.
/// </summary>
/// <typeparam name="T">Type of the target command type.</typeparam>
public struct SearchResult<T> : IResult where T : class, ICommandInfo
{ {
/// <summary>
/// Gets the input text of the command search.
/// </summary>
public string Text { get; } public string Text { get; }

/// <summary>
/// Gets the found command, if the search was successful.
/// </summary>
public T Command { get; } public T Command { get; }

/// <summary>
/// Gets the Regex groups captured by the wild card pattern.
/// </summary>
public string[] RegexCaptureGroups { get; } public string[] RegexCaptureGroups { get; }

/// <inheritdoc/>
public InteractionCommandError? Error { get; } public InteractionCommandError? Error { get; }


/// <inheritdoc/>
public string ErrorReason { get; } public string ErrorReason { get; }


/// <inheritdoc/>
public bool IsSuccess => !Error.HasValue; public bool IsSuccess => !Error.HasValue;


private SearchResult (string text, T commandInfo, string[] captureGroups, InteractionCommandError? error, string reason) private SearchResult (string text, T commandInfo, string[] captureGroups, InteractionCommandError? error, string reason)
@@ -22,16 +41,53 @@ namespace Discord.Interactions
ErrorReason = reason; ErrorReason = reason;
} }


/// <summary>
/// Initializes a new <see cref="SearchResult{T}" /> with no error, indicating a successful execution.
/// </summary>
/// <returns>
/// A <see cref="SearchResult{T}" /> that does not contain any errors.
/// </returns>
public static SearchResult<T> FromSuccess (string text, T commandInfo, string[] wildCardMatch = null) => public static SearchResult<T> FromSuccess (string text, T commandInfo, string[] wildCardMatch = null) =>
new SearchResult<T>(text, commandInfo, wildCardMatch, null, null); new SearchResult<T>(text, commandInfo, wildCardMatch, null, null);


/// <summary>
/// Initializes a new <see cref="SearchResult{T}" /> with a specified <see cref="InteractionCommandError" /> and its
/// reason, indicating an unsuccessful execution.
/// </summary>
/// <param name="error">The type of error.</param>
/// <param name="reason">The reason behind the error.</param>
/// <returns>
/// A <see cref="SearchResult{T}" /> that contains a <see cref="InteractionCommandError" /> and reason.
/// </returns>
public static SearchResult<T> FromError (string text, InteractionCommandError error, string reason) => public static SearchResult<T> FromError (string text, InteractionCommandError error, string reason) =>
new SearchResult<T>(text, null, null, error, reason); new SearchResult<T>(text, null, null, error, reason);

/// <summary>
/// Initializes a new <see cref="SearchResult{T}" /> with a specified exception, indicating an unsuccessful
/// execution.
/// </summary>
/// <param name="ex">The exception that caused the command execution to fail.</param>
/// <returns>
/// A <see cref="SearchResult{T}" /> that contains the exception that caused the unsuccessful execution, along
/// with a <see cref="InteractionCommandError" /> of type <c>Exception</c> as well as the exception message as the
/// reason.
/// </returns>
public static SearchResult<T> FromError (Exception ex) => public static SearchResult<T> FromError (Exception ex) =>
new SearchResult<T>(null, null, null, InteractionCommandError.Exception, ex.Message); new SearchResult<T>(null, null, null, InteractionCommandError.Exception, ex.Message);

/// <summary>
/// Initializes a new <see cref="SearchResult{T}" /> with a specified result; this may or may not be an
/// successful depending on the <see cref="IResult.Error" /> and
/// <see cref="IResult.ErrorReason" /> specified.
/// </summary>
/// <param name="result">The result to inherit from.</param>
/// <returns>
/// A <see cref="SearchResult{T}"/> that inherits the <see cref="IResult"/> error type and reason.
/// </returns>
public static SearchResult<T> FromError (IResult result) => public static SearchResult<T> FromError (IResult result) =>
new SearchResult<T>(null, null, null, result.Error, result.ErrorReason); new SearchResult<T>(null, null, null, result.Error, result.ErrorReason);


/// <inheritdoc/>
public override string ToString ( ) => IsSuccess ? "Success" : $"{Error}: {ErrorReason}"; public override string ToString ( ) => IsSuccess ? "Success" : $"{Error}: {ErrorReason}";
} }
} }

Loading…
Cancel
Save