using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
namespace LibationFileManager.Templates;
public class SeriesOrder : IFormattable
{
///
/// A numeric span from the original order string. Keep the original digits for unformatted
/// output so large values (e.g. 2147483647) are not rounded through into
/// scientific notation (issue #2024). Apply the numeric format only when the template asks.
///
private readonly record struct NumberPart(string Raw, decimal Value) : IFormattable
{
public override string ToString() => Raw;
public string ToString(string? format, IFormatProvider? formatProvider)
=> string.IsNullOrEmpty(format)
? Raw
: Value.ToString(format, formatProvider ?? CultureInfo.InvariantCulture);
}
private object[] OrderParts { get; }
private SeriesOrder(object[] orderParts)
{
OrderParts = orderParts;
}
public override string ToString() => ToString(null, null);
///
/// Use numeric formatters to format the number parts of the order.
///
public string ToString(string? format, IFormatProvider? formatProvider)
=> string.Concat(OrderParts.Select(p => p switch
{
IFormattable f => f.ToString(format, formatProvider ?? CultureInfo.InvariantCulture),
_ => p.ToString(),
})).Trim();
public static SeriesOrder Parse(string? order)
{
List