rabidus-test/Assets/Plugins/QFSW/Quantum Console/Source/Scripts/Parsing/Parsers/PrimitiveParser.cs

47 lines
1.2 KiB
C#
Raw Permalink Normal View History

2023-08-22 15:41:12 +03:00
using QFSW.QC.Utilities;
using System;
using System.Collections.Generic;
using System.Globalization;
namespace QFSW.QC.Parsers
{
public class PrimitiveParser : IQcParser
{
private readonly HashSet<Type> _primitiveTypes = new HashSet<Type>
{
typeof(int),
typeof(float),
typeof(decimal),
typeof(double),
typeof(bool),
typeof(byte),
typeof(sbyte),
typeof(uint),
typeof(short),
typeof(ushort),
typeof(long),
typeof(ulong),
typeof(char)
};
public int Priority => -1000;
public bool CanParse(Type type)
{
return _primitiveTypes.Contains(type);
}
public object Parse(string value, Type type, Func<string, Type, object> recursiveParser)
{
try
{
return Convert.ChangeType(value, type, CultureInfo.InvariantCulture);
}
catch (FormatException e)
{
throw new ParserInputException($"Cannot parse '{value}' to the type '{type.GetDisplayName()}'.", e);
}
}
}
}