1
0
mirror of https://github.com/chylex/Query.git synced 2025-05-23 01:34:06 +02:00
Query/AppConv/App.cs
2024-08-01 21:35:14 +02:00

50 lines
1.2 KiB
C#

using System;
using System.Linq;
using AppConv.General;
using AppConv.Units;
using Base;
namespace AppConv {
public sealed class App : IApp {
private static readonly IUnitType[] Processors = {
new Temperature(),
new Weight(),
new Length(),
new Area(),
new Volume(),
new Angle(),
new Storage(),
new Radix()
};
public string[] RecognizedNames => new string[] {
"conv",
"convert"
};
public MatchConfidence GetConfidence(Command cmd) {
return cmd.Text.IndexOf(" to ", StringComparison.InvariantCultureIgnoreCase) != -1 ? MatchConfidence.Possible : MatchConfidence.None;
}
public string ProcessCommand(Command cmd) {
string[] data = cmd.Text.Split(new string[] { " to " }, 2, StringSplitOptions.None);
string src = data[0].Trim();
string dst = data[1].Trim();
if (src.Length == 0 || dst.Length == 0) {
throw new CommandException("Unrecognized conversion app syntax.");
}
string result = string.Empty;
IUnitType used = Processors.FirstOrDefault(processor => processor.TryProcess(src, dst, out result));
if (used == null) {
throw new CommandException("Could not recognize conversion units.");
}
return result;
}
}
}