2023-07-18 15:25:02 +03:00
|
|
|
using System.Text.Json.Nodes;
|
|
|
|
using Remora.Results;
|
|
|
|
|
2023-09-30 16:58:32 +03:00
|
|
|
namespace Octobot.Data.Options;
|
2023-07-18 15:25:02 +03:00
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public sealed class BoolOption : Option<bool>
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
public BoolOption(string name, bool defaultValue) : base(name, defaultValue) { }
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public override string Display(JsonNode settings)
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
return Get(settings) ? Messages.Yes : Messages.No;
|
|
|
|
}
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
public override Result Set(JsonNode settings, string from)
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
if (!TryParseBool(from, out var value))
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
2023-07-28 19:58:55 +03:00
|
|
|
return new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue);
|
2023-08-02 23:51:16 +03:00
|
|
|
}
|
2023-07-18 15:25:02 +03:00
|
|
|
|
|
|
|
settings[Name] = value;
|
|
|
|
return Result.FromSuccess();
|
|
|
|
}
|
|
|
|
|
2023-08-02 23:51:16 +03:00
|
|
|
private static bool TryParseBool(string from, out bool value)
|
|
|
|
{
|
2023-07-18 15:25:02 +03:00
|
|
|
value = false;
|
2023-08-02 23:51:16 +03:00
|
|
|
switch (from.ToLowerInvariant())
|
|
|
|
{
|
2023-07-25 17:53:11 +03:00
|
|
|
case "true" or "1" or "y" or "yes" or "д" or "да":
|
2023-07-18 15:25:02 +03:00
|
|
|
value = true;
|
|
|
|
return true;
|
2023-07-25 17:53:11 +03:00
|
|
|
case "false" or "0" or "n" or "no" or "н" or "не" or "нет" or "нъет":
|
2023-07-18 15:25:02 +03:00
|
|
|
value = false;
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|