using System; using System.Collections.Generic; using System.Collections.Immutable; namespace Discord.Commands { internal delegate bool TryParseDelegate(string str, out T value); internal static class PrimitiveParsers { private static readonly Lazy> Parsers = new Lazy>(CreateParsers); public static IEnumerable SupportedTypes = Parsers.Value.Keys; static IReadOnlyDictionary CreateParsers() { var parserBuilder = ImmutableDictionary.CreateBuilder(); parserBuilder[typeof(bool)] = (TryParseDelegate)bool.TryParse; parserBuilder[typeof(sbyte)] = (TryParseDelegate)sbyte.TryParse; parserBuilder[typeof(byte)] = (TryParseDelegate)byte.TryParse; parserBuilder[typeof(short)] = (TryParseDelegate)short.TryParse; parserBuilder[typeof(ushort)] = (TryParseDelegate)ushort.TryParse; parserBuilder[typeof(int)] = (TryParseDelegate)int.TryParse; parserBuilder[typeof(uint)] = (TryParseDelegate)uint.TryParse; parserBuilder[typeof(long)] = (TryParseDelegate)long.TryParse; parserBuilder[typeof(ulong)] = (TryParseDelegate)ulong.TryParse; parserBuilder[typeof(float)] = (TryParseDelegate)float.TryParse; parserBuilder[typeof(double)] = (TryParseDelegate)double.TryParse; parserBuilder[typeof(decimal)] = (TryParseDelegate)decimal.TryParse; parserBuilder[typeof(DateTime)] = (TryParseDelegate)DateTime.TryParse; parserBuilder[typeof(DateTimeOffset)] = (TryParseDelegate)DateTimeOffset.TryParse; //parserBuilder[typeof(TimeSpan)] = (TryParseDelegate)TimeSpan.TryParse; parserBuilder[typeof(char)] = (TryParseDelegate)char.TryParse; return parserBuilder.ToImmutable(); } public static TryParseDelegate Get() => (TryParseDelegate)Parsers.Value[typeof(T)]; public static Delegate Get(Type type) => Parsers.Value[type]; } }