using System.Diagnostics; using System.Diagnostics.Contracts; using OneOf; namespace StabilityMatrix.Core.Processes; /// /// Builder for . /// public record ProcessArgsBuilder { protected ProcessArgsBuilder() { } public ProcessArgsBuilder(params Argument[] arguments) { Arguments = arguments.ToList(); } public List Arguments { get; init; } = new(); private IEnumerable ToStringArgs() { foreach (var argument in Arguments) { if (argument.IsT0) { yield return argument.AsT0; } else { yield return argument.AsT1.Item1; yield return argument.AsT1.Item2; } } } /// public override string ToString() { return ToProcessArgs().ToString(); } public ProcessArgs ToProcessArgs() { return ToStringArgs().ToArray(); } public static implicit operator ProcessArgs(ProcessArgsBuilder builder) => builder.ToProcessArgs(); } public static class ProcessArgBuilderExtensions { [Pure] public static T AddArg(this T builder, Argument argument) where T : ProcessArgsBuilder { return builder with { Arguments = builder.Arguments.Append(argument).ToList() }; } [Pure] public static T RemoveArgKey(this T builder, string argumentKey) where T : ProcessArgsBuilder { return builder with { Arguments = builder.Arguments .Where( x => x.Match( stringArg => stringArg != argumentKey, tupleArg => tupleArg.Item1 != argumentKey ) ) .ToList() }; } }