2023-07-18 15:25:02 +03:00
|
|
|
using System.Text.Json.Nodes;
|
|
|
|
using Remora.Results;
|
|
|
|
|
2024-05-16 18:34:26 +03:00
|
|
|
namespace TeamOctolings.Octobot.Data.Options;
|
2023-07-18 15:25:02 +03:00
|
|
|
|
2024-05-16 18:34:26 +03:00
|
|
|
public sealed class BoolOption : GuildOption<bool>
|
2023-08-02 23:51:16 +03:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2024-07-31 12:13:29 +03:00
|
|
|
public override Result<bool> ValueEquals(JsonNode settings, string value)
|
|
|
|
{
|
|
|
|
if (!TryParseBool(value, out var boolean))
|
|
|
|
{
|
|
|
|
return new ArgumentInvalidError(nameof(value), Messages.InvalidSettingValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Value(settings).Equals(boolean.ToString());
|
|
|
|
}
|
|
|
|
|
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;
|
2024-03-21 18:55:34 +03:00
|
|
|
return Result.Success;
|
2023-07-18 15:25:02 +03:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|