// #nullable disable using System; using System.Collections; using System.Collections.Generic; namespace OpenAI { internal static partial class Argument { public static void AssertNotNull(T value, string name) { if (value is null) { throw new ArgumentNullException(name); } } public static void AssertNotNull(T? value, string name) where T : struct { if (!value.HasValue) { throw new ArgumentNullException(name); } } public static void AssertNotNullOrEmpty(IEnumerable value, string name) { if (value is null) { throw new ArgumentNullException(name); } if (value is ICollection collectionOfT && collectionOfT.Count == 0) { throw new ArgumentException("Value cannot be an empty collection.", name); } if (value is ICollection collection && collection.Count == 0) { throw new ArgumentException("Value cannot be an empty collection.", name); } using IEnumerator e = value.GetEnumerator(); if (!e.MoveNext()) { throw new ArgumentException("Value cannot be an empty collection.", name); } } public static void AssertNotNullOrEmpty(string value, string name) { if (value is null) { throw new ArgumentNullException(name); } if (value.Length == 0) { throw new ArgumentException("Value cannot be an empty string.", name); } } public static void AssertNotNullOrWhiteSpace(string value, string name) { if (value is null) { throw new ArgumentNullException(name); } if (string.IsNullOrWhiteSpace(value)) { throw new ArgumentException("Value cannot be empty or contain only white-space characters.", name); } } public static void AssertInRange(T value, T minimum, T maximum, string name) where T : notnull, IComparable { if (minimum.CompareTo(value) > 0) { throw new ArgumentOutOfRangeException(name, "Value is less than the minimum allowed."); } if (maximum.CompareTo(value) < 0) { throw new ArgumentOutOfRangeException(name, "Value is greater than the maximum allowed."); } } public static string CheckNotNullOrEmpty(string value, string name) { AssertNotNullOrEmpty(value, name); return value; } } }