using System; using System.Threading.Tasks; namespace Discord.Commands { internal static class PrimitiveTypeReader { public static TypeReader Create(Type type) { type = typeof(PrimitiveTypeReader<>).MakeGenericType(type); return Activator.CreateInstance(type) as TypeReader; } } internal class PrimitiveTypeReader : TypeReader { private readonly TryParseDelegate _tryParse; private readonly float _score; /// must be within the range [0, 1]. public PrimitiveTypeReader() : this(PrimitiveParsers.Get(), 1) { } /// must be within the range [0, 1]. public PrimitiveTypeReader(TryParseDelegate tryParse, float score) { if (score < 0 || score > 1) throw new ArgumentOutOfRangeException(nameof(score), score, "Scores must be within the range [0, 1]."); _tryParse = tryParse; _score = score; } public override Task ReadAsync(ICommandContext context, string input, IServiceProvider services) { if (_tryParse(input, out T value)) return Task.FromResult(TypeReaderResult.FromSuccess(new TypeReaderValue(value, _score))); return Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, $"Failed to parse {typeof(T).Name}.")); } } }