// Copyright (c) Six Labors. // Licensed under the Six Labors Split License. using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; namespace SixLabors { /// /// Helper methods to throw exceptions. /// #pragma warning disable RCS1043 // Remove 'partial' modifier from type with a single part. internal static partial class ThrowHelper #pragma warning restore RCS1043 // Remove 'partial' modifier from type with a single part. { /// /// Throws an when fails. /// /// The value. /// The argument name. [DoesNotReturn] public static void ThrowArgumentExceptionForNotNullOrWhitespace(string? value, string name) { if (value is null) { ThrowArgumentNullException(name, $"Parameter \"{name}\" must be not null."); } else { ThrowArgumentException(name, $"Parameter \"{name}\" must not be empty or whitespace."); } } /// /// Throws an when fails. /// /// The type of value. /// The value. /// The maximum allowable value. /// The argument name. [DoesNotReturn] public static void ThrowArgumentOutOfRangeExceptionForMustBeLessThan(T value, T max, string name) => ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T)}) must be less than {max}, was {value}"); /// /// Throws an when fails. /// /// The type of value. /// The value. /// The maximum allowable value. /// The argument name. [DoesNotReturn] public static void ThrowArgumentOutOfRangeExceptionForMustBeLessThanOrEqualTo(T value, T maximum, string name) => ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T)}) must be less than or equal to {maximum}, was {value}"); /// /// Throws an when fails. /// /// The type of value. /// The value. /// The minimum allowable value. /// The argument name. [DoesNotReturn] public static void ThrowArgumentOutOfRangeExceptionForMustBeGreaterThan(T value, T minimum, string name) => ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T)}) must be greater than {minimum}, was {value}"); /// /// Throws an when fails. /// /// The type of value. /// The value. /// The minimum allowable value. /// The argument name. [DoesNotReturn] public static void ThrowArgumentOutOfRangeExceptionForMustBeGreaterThanOrEqualTo(T value, T minimum, string name) => ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T)}) must be greater than or equal to {minimum}, was {value}"); /// /// Throws an when fails. /// /// The type of value. /// The value. /// The minimum allowable value. /// The maximum allowable value. /// The argument name. [DoesNotReturn] public static void ThrowArgumentOutOfRangeExceptionForMustBeBetweenOrEqualTo(T value, T minimum, T maximum, string name) => ThrowArgumentOutOfRangeException(name, $"Parameter \"{name}\" ({typeof(T)}) must be between or equal to {minimum} and {maximum}, was {value}"); /// /// Throws an when fails. /// /// The minimum allowable length. /// The paramere name. [DoesNotReturn] public static void ThrowArgumentOutOfRangeExceptionForMustBeSizedAtLeast(int minLength, string parameterName) => ThrowArgumentException($"Spans must be at least of length {minLength}!", parameterName); /// /// Throws a new . /// /// The message to include in the exception. /// The argument name. /// Thrown with and . [DoesNotReturn] public static void ThrowArgumentException(string message, string name) => throw new ArgumentException(message, name); /// /// Throws a new . /// /// The argument name. /// The message to include in the exception. /// Thrown with and . [DoesNotReturn] public static void ThrowArgumentNullException(string name, string message) => throw new ArgumentNullException(name, message); /// /// Throws a new . /// /// The argument name. /// The message to include in the exception. /// Thrown with and . [DoesNotReturn] public static void ThrowArgumentOutOfRangeException(string name, string message) => throw new ArgumentOutOfRangeException(name, message); } }