Implemented command type readers, parser and service.

This commit is contained in:
RogueException
2016-06-26 03:35:40 -03:00
parent d934a5a1eb
commit f59b6b9004
19 changed files with 775 additions and 42 deletions

View 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}";
}
}

View File

@@ -0,0 +1,9 @@
namespace Discord.Commands
{
public interface IResult
{
CommandError? Error { get; }
string ErrorReason { get; }
bool IsSuccess { get; }
}
}

View 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}";
}
}

View 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}";
}
}

View 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}";
}
}