1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 17:19:00 +03:00
Octobot/src/Data/Options/TimeSpanOption.cs
Macintxsh e01fde83c6
Use custom TimeSpanParser (#223)
Closes #154

---------

Signed-off-by: mctaylors <cantsendmails@mctaylors.ru>
2023-12-31 12:27:00 +00:00

32 lines
909 B
C#

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