// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
namespace CommunityToolkit.Diagnostics;
///
partial class Guard
{
///
/// Helper methods to efficiently throw exceptions.
///
[StackTraceHidden]
private static partial class ThrowHelper
{
///
/// Returns a formatted representation of the input value.
///
/// The input to format.
/// A formatted representation of to display in error messages.
private static string AssertString(object? obj)
{
return obj switch
{
string _ => $"\"{obj}\"",
null => "null",
_ => $"<{obj}>"
};
}
///
/// Throws an when (where is ) fails.
///
/// The type of the input value.
[DoesNotReturn]
public static void ThrowArgumentExceptionForIsNull(T value, string name)
{
throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must be null, was {AssertString(value)} ({value!.GetType().ToTypeString()}).", name);
}
///
/// Throws an when (where is ) fails.
///
/// The type of the input value.
[DoesNotReturn]
public static void ThrowArgumentExceptionForIsNull(T? value, string name)
where T : struct
{
throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T?).ToTypeString()}) must be null, was {AssertString(value)} ({typeof(T).ToTypeString()}).", name);
}
///
/// Throws an when fails.
///
/// The type of the input value.
[DoesNotReturn]
public static void ThrowArgumentNullExceptionForIsNotNull(string name)
{
throw new ArgumentNullException(name, $"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must be not null).");
}
///
/// Throws an when fails.
///
/// The type of the input value.
[DoesNotReturn]
public static void ThrowArgumentExceptionForIsOfType(object value, string name)
{
throw new ArgumentException($"Parameter {AssertString(name)} must be of type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}.", name);
}
///
/// Throws an when fails.
///
/// The type of the input value.
[DoesNotReturn]
public static void ThrowArgumentExceptionForIsNotOfType(object value, string name)
{
throw new ArgumentException($"Parameter {AssertString(name)} must not be of type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}.", name);
}
///
/// Throws an when fails.
///
[DoesNotReturn]
public static void ThrowArgumentExceptionForIsOfType(object value, Type type, string name)
{
throw new ArgumentException($"Parameter {AssertString(name)} must be of type {type.ToTypeString()}, was {value.GetType().ToTypeString()}.", name);
}
///
/// Throws an when fails.
///
[DoesNotReturn]
public static void ThrowArgumentExceptionForIsNotOfType(object value, Type type, string name)
{
throw new ArgumentException($"Parameter {AssertString(name)} must not be of type {type.ToTypeString()}, was {value.GetType().ToTypeString()}.", name);
}
///
/// Throws an when fails.
///
/// The type being checked against.
[DoesNotReturn]
public static void ThrowArgumentExceptionForIsAssignableToType(object value, string name)
{
throw new ArgumentException($"Parameter {AssertString(name)} must be assignable to type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}.", name);
}
///
/// Throws an when fails.
///
/// The type being checked against.
[DoesNotReturn]
public static void ThrowArgumentExceptionForIsNotAssignableToType(object value, string name)
{
throw new ArgumentException($"Parameter {AssertString(name)} must not be assignable to type {typeof(T).ToTypeString()}, was {value.GetType().ToTypeString()}.", name);
}
///
/// Throws an when fails.
///
[DoesNotReturn]
public static void ThrowArgumentExceptionForIsAssignableToType(object value, Type type, string name)
{
throw new ArgumentException($"Parameter {AssertString(name)} must be assignable to type {type.ToTypeString()}, was {value.GetType().ToTypeString()}.", name);
}
///
/// Throws an when fails.
///
[DoesNotReturn]
public static void ThrowArgumentExceptionForIsNotAssignableToType(object value, Type type, string name)
{
throw new ArgumentException($"Parameter {AssertString(name)} must not be assignable to type {type.ToTypeString()}, was {value.GetType().ToTypeString()}.", name);
}
///
/// Throws an when fails.
///
/// The type of input value being compared.
[DoesNotReturn]
public static void ThrowArgumentExceptionForIsReferenceEqualTo(string name)
where T : class
{
throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must be the same instance as the target object.", name);
}
///
/// Throws an when fails.
///
/// The type of input value being compared.
[DoesNotReturn]
public static void ThrowArgumentExceptionForIsReferenceNotEqualTo(string name)
where T : class
{
throw new ArgumentException($"Parameter {AssertString(name)} ({typeof(T).ToTypeString()}) must not be the same instance as the target object.", name);
}
///
/// Throws an when fails.
///
[DoesNotReturn]
public static void ThrowArgumentExceptionForIsTrue(string name)
{
throw new ArgumentException($"Parameter {AssertString(name)} must be true, was false.", name);
}
///
/// Throws an when fails.
///
[DoesNotReturn]
public static void ThrowArgumentExceptionForIsTrue(string name, string message)
{
throw new ArgumentException($"Parameter {AssertString(name)} must be true, was false: {AssertString(message)}.", name);
}
///
/// Throws an when fails.
///
[DoesNotReturn]
public static void ThrowArgumentExceptionForIsFalse(string name)
{
throw new ArgumentException($"Parameter {AssertString(name)} must be false, was true.", name);
}
///
/// Throws an when fails.
///
[DoesNotReturn]
public static void ThrowArgumentExceptionForIsFalse(string name, string message)
{
throw new ArgumentException($"Parameter {AssertString(name)} must be false, was true: {AssertString(message)}.", name);
}
}
}