|
| 1 | +using Ardalis.GuardClauses; |
| 2 | +using System; |
| 3 | + |
| 4 | +namespace Ardalis.SmartEnum.GuardClauses |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Provides guard clauses to ensure input values are valid instances of a specified SmartEnum. |
| 8 | + /// </summary> |
| 9 | + public static class GuardAgainstSmartEnumOutOfRange |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// Throws a<see cref="SmartEnumNotFoundException" /> or a custom<see cref="Exception" /> |
| 13 | + /// if <paramref name="input"/> is not a valid <see cref="SmartEnum{TEnum}"/> value. |
| 14 | + /// </summary> |
| 15 | + /// <typeparam name="TEnum">The type of the smart enum.</typeparam> |
| 16 | + /// <param name="guardClause">The guard clause interface.</param> |
| 17 | + /// <param name="input">The value to check against the smart enum values.</param> |
| 18 | + /// <param name="message">Optional. Custom error message to pass to <see cref="SmartEnumNotFoundException"/>.</param> |
| 19 | + /// <param name="exceptionCreator">Optional. A function that creates a custom exception.</param> |
| 20 | + /// <returns>The valid <see cref="SmartEnum{TEnum}"/> value <paramref name="input" />.</returns> |
| 21 | + /// <exception cref="SmartEnumNotFoundException">Thrown when <paramref name="input" /> |
| 22 | + /// is not a valid enum value, and no custom exception is provided.</exception> |
| 23 | + /// <exception cref="Exception">Thrown when a custom exception is provided by <paramref name="exceptionCreator" />.</exception> |
| 24 | + public static TEnum SmartEnumOutOfRange<TEnum>( |
| 25 | + this IGuardClause guardClause, |
| 26 | + int input, |
| 27 | + string message = null, |
| 28 | + Func<Exception> exceptionCreator = null) |
| 29 | + where TEnum : SmartEnum<TEnum> |
| 30 | + { |
| 31 | + return guardClause.SmartEnumOutOfRange<TEnum, int>(input, message, exceptionCreator); |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Throws a <see cref="SmartEnumNotFoundException"/> or a custom <see cref="Exception"/> |
| 36 | + /// if <paramref name="input"/> is not a valid <see cref="SmartEnum{TEnum, TValue}"/> value. |
| 37 | + /// </summary> |
| 38 | + /// <typeparam name="TEnum">The type of the smart enum.</typeparam> |
| 39 | + /// <typeparam name="TValue">The type of the value that the smart enum uses.</typeparam> |
| 40 | + /// <param name="guardClause">The guard clause interface.</param> |
| 41 | + /// <param name="input">The value to check against the smart enum values.</param> |
| 42 | + /// <param name="message">Optional. Custom error message to pass to <see cref="SmartEnumNotFoundException"/>.</param> |
| 43 | + /// <param name="exceptionCreator">Optional. A function that creates a custom exception.</param> |
| 44 | + /// <returns>The valid enum value <typeparamref name="TEnum"/> corresponding to <paramref name="input"/>.</returns> |
| 45 | + /// <exception cref="SmartEnumNotFoundException">Thrown when <paramref name="input"/> |
| 46 | + /// is not a valid enum value and no custom exception is provided.</exception> |
| 47 | + /// <exception cref="Exception">Thrown when a custom exception |
| 48 | + /// is provided by <paramref name="exceptionCreator"/>.</exception> |
| 49 | + public static TEnum SmartEnumOutOfRange<TEnum, TValue>( |
| 50 | + this IGuardClause guardClause, |
| 51 | + TValue input, |
| 52 | + string message = null, |
| 53 | + Func<Exception> exceptionCreator = null) |
| 54 | + where TEnum : SmartEnum<TEnum, TValue> |
| 55 | + where TValue : IEquatable<TValue>, IComparable<TValue> |
| 56 | + { |
| 57 | + if (SmartEnum<TEnum, TValue>.TryFromValue(input, out TEnum result)) |
| 58 | + { |
| 59 | + return result; |
| 60 | + } |
| 61 | + |
| 62 | + var exceptionMessage = message ?? $"The value '{input}' is not a valid {typeof(TEnum).Name}."; |
| 63 | + throw exceptionCreator?.Invoke() ?? new SmartEnumNotFoundException(exceptionMessage); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments