Implemented command type readers, parser and service.
This commit is contained in:
35
src/Discord.Net.Commands/Results/ExecuteResult.cs
Normal file
35
src/Discord.Net.Commands/Results/ExecuteResult.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Discord.Commands
|
||||
{
|
||||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
|
||||
public struct ExecuteResult : IResult
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
internal static ExecuteResult FromSuccess()
|
||||
=> new ExecuteResult(null, null, null);
|
||||
internal static ExecuteResult FromError(CommandError error, string reason)
|
||||
=> new ExecuteResult(null, error, reason);
|
||||
internal static ExecuteResult FromError(Exception ex)
|
||||
=> new ExecuteResult(ex, CommandError.Exception, ex.Message);
|
||||
internal static ExecuteResult FromError(ParseResult result)
|
||||
=> new ExecuteResult(null, result.Error, result.ErrorReason);
|
||||
|
||||
public override string ToString() => IsSuccess ? "Success" : $"{Error}: {ErrorReason}";
|
||||
private string DebuggerDisplay => IsSuccess ? "Success" : $"{Error}: {ErrorReason}";
|
||||
}
|
||||
}
|
||||
9
src/Discord.Net.Commands/Results/IResult.cs
Normal file
9
src/Discord.Net.Commands/Results/IResult.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Discord.Commands
|
||||
{
|
||||
public interface IResult
|
||||
{
|
||||
CommandError? Error { get; }
|
||||
string ErrorReason { get; }
|
||||
bool IsSuccess { get; }
|
||||
}
|
||||
}
|
||||
35
src/Discord.Net.Commands/Results/ParseResult.cs
Normal file
35
src/Discord.Net.Commands/Results/ParseResult.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Discord.Commands
|
||||
{
|
||||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
|
||||
public struct ParseResult : IResult
|
||||
{
|
||||
public IReadOnlyList<object> Values { get; }
|
||||
|
||||
public CommandError? Error { get; }
|
||||
public string ErrorReason { get; }
|
||||
|
||||
public bool IsSuccess => !Error.HasValue;
|
||||
|
||||
private ParseResult(IReadOnlyList<object> values, CommandError? error, string errorReason)
|
||||
{
|
||||
Values = values;
|
||||
Error = error;
|
||||
ErrorReason = errorReason;
|
||||
}
|
||||
|
||||
internal static ParseResult FromSuccess(IReadOnlyList<object> values)
|
||||
=> new ParseResult(values, null, null);
|
||||
internal static ParseResult FromError(CommandError error, string reason)
|
||||
=> new ParseResult(null, error, reason);
|
||||
internal static ParseResult FromError(SearchResult result)
|
||||
=> new ParseResult(null, result.Error, result.ErrorReason);
|
||||
internal static ParseResult FromError(TypeReaderResult result)
|
||||
=> new ParseResult(null, result.Error, result.ErrorReason);
|
||||
|
||||
public override string ToString() => IsSuccess ? "Success" : $"{Error}: {ErrorReason}";
|
||||
private string DebuggerDisplay => IsSuccess ? $"Success ({Values.Count} Values)" : $"{Error}: {ErrorReason}";
|
||||
}
|
||||
}
|
||||
33
src/Discord.Net.Commands/Results/SearchResult.cs
Normal file
33
src/Discord.Net.Commands/Results/SearchResult.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Discord.Commands
|
||||
{
|
||||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
|
||||
public struct SearchResult : IResult
|
||||
{
|
||||
public IReadOnlyList<Command> Commands { get; }
|
||||
public string ArgText { get; }
|
||||
|
||||
public CommandError? Error { get; }
|
||||
public string ErrorReason { get; }
|
||||
|
||||
public bool IsSuccess => !Error.HasValue;
|
||||
|
||||
private SearchResult(IReadOnlyList<Command> commands, string argText, CommandError? error, string errorReason)
|
||||
{
|
||||
Commands = commands;
|
||||
ArgText = argText;
|
||||
Error = error;
|
||||
ErrorReason = errorReason;
|
||||
}
|
||||
|
||||
internal static SearchResult FromSuccess(IReadOnlyList<Command> commands, string argText)
|
||||
=> new SearchResult(commands, argText, null, null);
|
||||
internal static SearchResult FromError(CommandError error, string reason)
|
||||
=> new SearchResult(null, null, error, reason);
|
||||
|
||||
public override string ToString() => IsSuccess ? "Success" : $"{Error}: {ErrorReason}";
|
||||
private string DebuggerDisplay => IsSuccess ? $"Success ({Commands.Count} Results)" : $"{Error}: {ErrorReason}";
|
||||
}
|
||||
}
|
||||
30
src/Discord.Net.Commands/Results/TypeReaderResult.cs
Normal file
30
src/Discord.Net.Commands/Results/TypeReaderResult.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Discord.Commands
|
||||
{
|
||||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
|
||||
public struct TypeReaderResult : IResult
|
||||
{
|
||||
public object Value { get; }
|
||||
|
||||
public CommandError? Error { get; }
|
||||
public string ErrorReason { get; }
|
||||
|
||||
public bool IsSuccess => !Error.HasValue;
|
||||
|
||||
private TypeReaderResult(object value, CommandError? error, string errorReason)
|
||||
{
|
||||
Value = value;
|
||||
Error = error;
|
||||
ErrorReason = errorReason;
|
||||
}
|
||||
|
||||
public static TypeReaderResult FromSuccess(object value)
|
||||
=> new TypeReaderResult(value, null, null);
|
||||
public static TypeReaderResult FromError(CommandError error, string reason)
|
||||
=> new TypeReaderResult(null, error, reason);
|
||||
|
||||
public override string ToString() => IsSuccess ? "Success" : $"{Error}: {ErrorReason}";
|
||||
private string DebuggerDisplay => IsSuccess ? $"Success ({Value})" : $"{Error}: {ErrorReason}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user