1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 09:09:00 +03:00
Octobot/TeamOctolings.Octobot/Data/Options/TimeSpanOption.cs

37 lines
1.1 KiB
C#

using System.Text.Json.Nodes;
using Remora.Results;
using TeamOctolings.Octobot.Parsers;
namespace TeamOctolings.Octobot.Data.Options;
public sealed class TimeSpanOption : GuildOption<TimeSpan>
{
public TimeSpanOption(string name, TimeSpan defaultValue) : base(name, defaultValue) { }
public override Result<bool> ValueEquals(JsonNode settings, string value)
{
if (!TimeSpanParser.TryParse(value).IsDefined(out var span))
{
return new ArgumentInvalidError(nameof(value), Messages.InvalidSettingValue);
}
return Value(settings).Equals(span.ToString());
}
public override TimeSpan Get(JsonNode settings)
{
var property = settings[Name];
return property != null ? TimeSpanParser.TryParse(property.GetValue<string>()).Entity : DefaultValue;
}
public override Result Set(JsonNode settings, string from)
{
if (!TimeSpanParser.TryParse(from).IsDefined(out var span))
{
return new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue);
}
settings[Name] = span.ToString();
return Result.Success;
}
}