using Connected.Components; namespace Connected; public class RangeConverter : ToStringConverter> { readonly DefaultConverter _converter; public RangeConverter() { _converter = new DefaultConverter(); } protected override string? ConvertValue(Range? value) { return OnSet(value); } protected override Range? ConvertValueBack(string? value) { return OnGet(value); } private Range OnGet(string value) { if (!Split(value, out var valueStart, out var valueEnd)) return null; return new Range(_converter.ConvertBack(valueStart), _converter.ConvertBack(valueEnd)); } private string OnSet(Range arg) { if (arg == null) return string.Empty; return Join(_converter.Convert(arg.Start), _converter.Convert(arg.End)); } public static string Join(string valueStart, string valueEnd) { if (string.IsNullOrEmpty(valueStart) && string.IsNullOrEmpty(valueEnd)) return string.Empty; return $"[{valueStart};{valueEnd}]"; } public static bool Split(string value, out string valueStart, out string valueEnd) { valueStart = valueEnd = string.Empty; if (string.IsNullOrEmpty(value) || value[0] != '[' || value[^1] != ']') { return false; } var idx = value.IndexOf(';'); if (idx < 1) { return false; } valueStart = value[1..idx]; valueEnd = value[(idx + 1)..^1]; return true; } }