/
SearchResult.cs
41 lines (35 loc) · 1.53 KB
/
SearchResult.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Discord.Commands
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public struct SearchResult : IResult
{
public string Text { get; }
public IReadOnlyList<CommandMatch> Commands { get; }
/// <inheritdoc/>
public CommandError? Error { get; }
/// <inheritdoc/>
public string ErrorReason { get; }
/// <inheritdoc/>
public bool IsSuccess => !Error.HasValue;
private SearchResult(string text, IReadOnlyList<CommandMatch> commands, CommandError? error, string errorReason)
{
Text = text;
Commands = commands;
Error = error;
ErrorReason = errorReason;
}
public static SearchResult FromSuccess(string text, IReadOnlyList<CommandMatch> commands)
=> new SearchResult(text, commands, null, null);
public static SearchResult FromError(CommandError error, string reason)
=> new SearchResult(null, null, error, reason);
public static SearchResult FromError(Exception ex)
=> FromError(CommandError.Exception, ex.Message);
public static SearchResult FromError(IResult result)
=> new SearchResult(null, null, result.Error, result.ErrorReason);
public override string ToString() => IsSuccess ? "Success" : $"{Error}: {ErrorReason}";
private string DebuggerDisplay => IsSuccess ? $"Success ({Commands.Count} Results)" : $"{Error}: {ErrorReason}";
}
}