public static void Serialize <T>( [NotNull] this FileInfo fileInfo, [CanBeNull] T obj, DuplicateFileResolution duplicateFileResolution = DuplicateFileResolution.Error, JsonSerializerSettings?settings = default ) { switch (duplicateFileResolution) { case DuplicateFileResolution.Error: fileInfo.SerializeCautiously(obj, settings); break; case DuplicateFileResolution.Overwrite: fileInfo.SerializeForcefully(obj, settings); break; case DuplicateFileResolution.Backup: fileInfo.SerializeSafely(obj, settings); break; default: throw BEnum.InvalidEnumArgumentException(nameof(duplicateFileResolution), duplicateFileResolution); } Console.WriteLine($"💾 → {fileInfo.ToUri()}"); }
public static bool Boolean(this Should should) { return(should switch { Should.Pass => true, Should.Fail => false, _ => throw BEnum.InvalidEnumArgumentException(nameof(should), should) });
public static char ToChar(this DirectorySeparator separator) { return(separator switch { DirectorySeparator.Universal => '/', DirectorySeparator.Windows => '\\', DirectorySeparator.PlatformDependent => Path.DirectorySeparatorChar, _ => throw BEnum.InvalidEnumArgumentException(nameof(separator), separator) });
private static string StylizeGenericTypeArguments([NotNull, ItemCanBeNull] IEnumerable <Type> genericTypeArguments, [CanBeNull] PrettificationSettings settings) { settings ??= Prettification.DefaultPrettificationSettings; return(settings.TypeLabelStyle.Value switch { TypeNameStyle.None => "", TypeNameStyle.Full => genericTypeArguments.Select(it => it.PrettifyType(settings)).JoinString(", "), TypeNameStyle.Short => genericTypeArguments.Select(_ => "").JoinString(","), _ => throw BEnum.InvalidEnumArgumentException(nameof(settings.TypeLabelStyle.Value), settings.TypeLabelStyle.Value) });
private static object GetHeader( [NotNull, ItemCanBeNull] IEnumerable values, [CanBeNull] Type valueType, [NotNull] string fallback, [NotNull] PrettificationSettings settings ) { return(settings.HeaderStyle.Value switch { HeaderStyle.None => fallback, HeaderStyle.TypeNames => valueType ?? InferType(values), _ => throw BEnum.InvalidEnumArgumentException(nameof(settings.HeaderStyle), settings.HeaderStyle.Value) });
public static string PrettifyEnumerable( [NotNull][ItemCanBeNull] IEnumerable enumerable, [NotNull] PrettificationSettings settings ) { var asObjects = enumerable.Cast <object>(); var enumerableType = enumerable.GetType(); var lineStyle = settings?.PreferredLineStyle.Value ?? default; var innerSettings = settings.JsonClone( it => { it.EnumLabelStyle.Set(TypeNameStyle.None); it.TypeLabelStyle.Set(it.TypeLabelStyle.Value.Reduce()); } ); return(lineStyle switch { LineStyle.Dynamic => PrettifyEnumerable_DynamicLine(asObjects, enumerableType, settings, innerSettings), LineStyle.Multi => PrettifyEnumerable_MultiLine(asObjects, enumerableType, settings, innerSettings), LineStyle.Single => PrettifyEnumerable_SingleLine(asObjects, enumerableType, settings, innerSettings), _ => throw BEnum.InvalidEnumArgumentException(nameof(PrettificationSettings.PreferredLineStyle), lineStyle) });
/// <summary> /// The number of <see cref="TimeUnit.Ticks"/> for a <b>single</b> instance of <paramref name="timeUnit"/>. /// </summary> /// <param name="timeUnit">the original <see cref="TimeUnit"/></param> /// <returns>the number of <see cref="TimeUnit.Ticks"/> per <paramref name="timeUnit"/></returns> /// <exception cref="System.ComponentModel.InvalidEnumArgumentException">if an unsupported <see cref="TimeUnit"/> is provided</exception> public static long TicksPer(this TimeUnit timeUnit) { return(TicksPerUnit.ContainsKey(timeUnit) ? TicksPerUnit[timeUnit] : throw BEnum.InvalidEnumArgumentException(nameof(timeUnit), timeUnit)); }
public static bool IsBetween(this int value, int min, int max, Clusivity clusivity = Clusivity.Inclusive) => clusivity == Clusivity.Inclusive ? value >= min && value <= max : clusivity == Clusivity.Exclusive ? value > min && value < max : throw BEnum.InvalidEnumArgumentException(nameof(clusivity), clusivity);