using System.Collections.Generic; using SabreTools.Core.Tools; namespace SabreTools.DatItems { public static class Extensions { #region Private Maps /// /// Set of enum to string mappings for ChipType /// private static readonly Dictionary _chipTypeMap = Converters.GenerateToEnum(); /// /// Set of enum to string mappings for ControlType /// private static readonly Dictionary _controlTypeMap = Converters.GenerateToEnum(); /// /// Set of enum to string mappings for DeviceType /// private static readonly Dictionary _deviceTypeMap = Converters.GenerateToEnum(); /// /// Set of enum to string mappings for DisplayType /// private static readonly Dictionary _displayTypeMap = Converters.GenerateToEnum(); /// /// Set of enum to string mappings for Endianness /// private static readonly Dictionary _endiannessMap = Converters.GenerateToEnum(); /// /// Set of enum to string mappings for FeatureStatus /// private static readonly Dictionary _featureStatusMap = Converters.GenerateToEnum(); /// /// Set of enum to string mappings for FeatureType /// private static readonly Dictionary _featureTypeMap = Converters.GenerateToEnum(); /// /// Set of enum to string mappings for ItemStatus /// private static readonly Dictionary _itemStatusMap = Converters.GenerateToEnum(); /// /// Set of enum to string mappings for ItemType /// private static readonly Dictionary _itemTypeMap = Converters.GenerateToEnum(); /// /// Set of enum to string mappings for LoadFlag /// private static readonly Dictionary _loadFlagMap = Converters.GenerateToEnum(); /// /// Set of enum to string mappings for MachineType /// private static readonly Dictionary _machineTypeMap = Converters.GenerateToEnum(); /// /// Set of enum to string mappings for OpenMSXSubType /// private static readonly Dictionary _openMSXSubTypeMap = Converters.GenerateToEnum(); /// /// Set of enum to string mappings for Relation /// private static readonly Dictionary _relationMap = Converters.GenerateToEnum(); /// /// Set of enum to string mappings for Runnable /// private static readonly Dictionary _runnableMap = Converters.GenerateToEnum(); /// /// Set of enum to string mappings for SoftwareListStatus /// private static readonly Dictionary _softwareListStatusMap = Converters.GenerateToEnum(); /// /// Set of enum to string mappings for Supported /// private static readonly Dictionary _supportedMap = Converters.GenerateToEnum(); /// /// Set of enum to string mappings for SupportStatus /// private static readonly Dictionary _supportStatusMap = Converters.GenerateToEnum(); #endregion #region String to Enum /// /// Get the enum value for an input string, if possible /// /// String value to parse/param> /// Enum type that is expected /// Enum value representing the input, default on error public static ChipType AsChipType(this string? value) { // Normalize the input value value = value?.ToLowerInvariant(); if (value == null) return default; // Try to get the value from the mappings if (_chipTypeMap.ContainsKey(value)) return _chipTypeMap[value]; // Otherwise, return the default value for the enum return default; } /// /// Get the enum value for an input string, if possible /// /// String value to parse/param> /// Enum type that is expected /// Enum value representing the input, default on error public static ControlType AsControlType(this string? value) { // Normalize the input value value = value?.ToLowerInvariant(); if (value == null) return default; // Try to get the value from the mappings if (_controlTypeMap.ContainsKey(value)) return _controlTypeMap[value]; // Otherwise, return the default value for the enum return default; } /// /// Get the enum value for an input string, if possible /// /// String value to parse/param> /// Enum type that is expected /// Enum value representing the input, default on error public static DeviceType AsDeviceType(this string? value) { // Normalize the input value value = value?.ToLowerInvariant(); if (value == null) return default; // Try to get the value from the mappings if (_deviceTypeMap.ContainsKey(value)) return _deviceTypeMap[value]; // Otherwise, return the default value for the enum return default; } /// /// Get the enum value for an input string, if possible /// /// String value to parse/param> /// Enum type that is expected /// Enum value representing the input, default on error public static DisplayType AsDisplayType(this string? value) { // Normalize the input value value = value?.ToLowerInvariant(); if (value == null) return default; // Try to get the value from the mappings if (_displayTypeMap.ContainsKey(value)) return _displayTypeMap[value]; // Otherwise, return the default value for the enum return default; } /// /// Get the enum value for an input string, if possible /// /// String value to parse/param> /// Enum type that is expected /// Enum value representing the input, default on error public static Endianness AsEndianness(this string? value) { // Normalize the input value value = value?.ToLowerInvariant(); if (value == null) return default; // Try to get the value from the mappings if (_endiannessMap.ContainsKey(value)) return _endiannessMap[value]; // Otherwise, return the default value for the enum return default; } /// /// Get the enum value for an input string, if possible /// /// String value to parse/param> /// Enum type that is expected /// Enum value representing the input, default on error public static FeatureStatus AsFeatureStatus(this string? value) { // Normalize the input value value = value?.ToLowerInvariant(); if (value == null) return default; // Try to get the value from the mappings if (_featureStatusMap.ContainsKey(value)) return _featureStatusMap[value]; // Otherwise, return the default value for the enum return default; } /// /// Get the enum value for an input string, if possible /// /// String value to parse/param> /// Enum type that is expected /// Enum value representing the input, default on error public static FeatureType AsFeatureType(this string? value) { // Normalize the input value value = value?.ToLowerInvariant(); if (value == null) return default; // Try to get the value from the mappings if (_featureTypeMap.ContainsKey(value)) return _featureTypeMap[value]; // Otherwise, return the default value for the enum return default; } /// /// Get the enum value for an input string, if possible /// /// String value to parse/param> /// Enum type that is expected /// Enum value representing the input, default on error public static ItemStatus AsItemStatus(this string? value) { // Normalize the input value value = value?.ToLowerInvariant(); if (value == null) return default; // Try to get the value from the mappings if (_itemStatusMap.ContainsKey(value)) return _itemStatusMap[value]; // Otherwise, return the default value for the enum return default; } /// /// Get the enum value for an input string, if possible /// /// String value to parse/param> /// Enum type that is expected /// Enum value representing the input, default on error public static ItemType AsItemType(this string? value) { // Normalize the input value value = value?.ToLowerInvariant(); if (value == null) return default; // Try to get the value from the mappings if (_itemTypeMap.ContainsKey(value)) return _itemTypeMap[value]; // Otherwise, return the default value for the enum return default; } /// /// Get the enum value for an input string, if possible /// /// String value to parse/param> /// Enum type that is expected /// Enum value representing the input, default on error public static LoadFlag AsLoadFlag(this string? value) { // Normalize the input value value = value?.ToLowerInvariant(); if (value == null) return default; // Try to get the value from the mappings if (_loadFlagMap.ContainsKey(value)) return _loadFlagMap[value]; // Otherwise, return the default value for the enum return default; } /// /// Get the enum value for an input string, if possible /// /// String value to parse/param> /// Enum type that is expected /// Enum value representing the input, default on error public static MachineType AsMachineType(this string? value) { // Normalize the input value value = value?.ToLowerInvariant(); if (value == null) return default; // Try to get the value from the mappings if (_machineTypeMap.ContainsKey(value)) return _machineTypeMap[value]; // Otherwise, return the default value for the enum return default; } /// /// Get the enum value for an input string, if possible /// /// String value to parse/param> /// Enum type that is expected /// Enum value representing the input, default on error public static OpenMSXSubType AsOpenMSXSubType(this string? value) { // Normalize the input value value = value?.ToLowerInvariant(); if (value == null) return default; // Try to get the value from the mappings if (_openMSXSubTypeMap.ContainsKey(value)) return _openMSXSubTypeMap[value]; // Otherwise, return the default value for the enum return default; } /// /// Get the enum value for an input string, if possible /// /// String value to parse/param> /// Enum type that is expected /// Enum value representing the input, default on error public static Relation AsRelation(this string? value) { // Normalize the input value value = value?.ToLowerInvariant(); if (value == null) return default; // Try to get the value from the mappings if (_relationMap.ContainsKey(value)) return _relationMap[value]; // Otherwise, return the default value for the enum return default; } /// /// Get the enum value for an input string, if possible /// /// String value to parse/param> /// Enum type that is expected /// Enum value representing the input, default on error public static Runnable AsRunnable(this string? value) { // Normalize the input value value = value?.ToLowerInvariant(); if (value == null) return default; // Try to get the value from the mappings if (_runnableMap.ContainsKey(value)) return _runnableMap[value]; // Otherwise, return the default value for the enum return default; } /// /// Get the enum value for an input string, if possible /// /// String value to parse/param> /// Enum type that is expected /// Enum value representing the input, default on error public static SoftwareListStatus AsSoftwareListStatus(this string? value) { // Normalize the input value value = value?.ToLowerInvariant(); if (value == null) return default; // Try to get the value from the mappings if (_softwareListStatusMap.ContainsKey(value)) return _softwareListStatusMap[value]; // Otherwise, return the default value for the enum return default; } /// /// Get the enum value for an input string, if possible /// /// String value to parse/param> /// Enum type that is expected /// Enum value representing the input, default on error public static Supported AsSupported(this string? value) { // Normalize the input value value = value?.ToLowerInvariant(); if (value == null) return default; // Try to get the value from the mappings if (_supportedMap.ContainsKey(value)) return _supportedMap[value]; // Otherwise, return the default value for the enum return default; } /// /// Get the enum value for an input string, if possible /// /// String value to parse/param> /// Enum type that is expected /// Enum value representing the input, default on error public static SupportStatus AsSupportStatus(this string? value) { // Normalize the input value value = value?.ToLowerInvariant(); if (value == null) return default; // Try to get the value from the mappings if (_supportStatusMap.ContainsKey(value)) return _supportStatusMap[value]; // Otherwise, return the default value for the enum return default; } #endregion } }