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 _toChipTypeMap = Converters.GenerateToEnum(); /// /// Set of string to enum mappings for ChipType /// private static readonly Dictionary _fromChipTypeMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for ControlType /// private static readonly Dictionary _toControlTypeMap = Converters.GenerateToEnum(); /// /// Set of string to enum mappings for ControlType /// private static readonly Dictionary _fromControlTypeMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for DeviceType /// private static readonly Dictionary _toDeviceTypeMap = Converters.GenerateToEnum(); /// /// Set of string to enum mappings for DeviceType /// private static readonly Dictionary _fromDeviceTypeMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for DisplayType /// private static readonly Dictionary _toDisplayTypeMap = Converters.GenerateToEnum(); /// /// Set of string to enum mappings for DisplayType /// private static readonly Dictionary _fromDisplayTypeMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for Endianness /// private static readonly Dictionary _toEndiannessMap = Converters.GenerateToEnum(); /// /// Set of string to enum mappings for Endianness /// private static readonly Dictionary _fromEndiannessMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for FeatureStatus /// private static readonly Dictionary _toFeatureStatusMap = Converters.GenerateToEnum(); /// /// Set of string to enum mappings for FeatureStatus /// private static readonly Dictionary _fromFeatureStatusMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for FeatureType /// private static readonly Dictionary _toFeatureTypeMap = Converters.GenerateToEnum(); /// /// Set of string to enum mappings for FeatureType /// private static readonly Dictionary _fromFeatureTypeMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for ItemStatus /// private static readonly Dictionary _toItemStatusMap = Converters.GenerateToEnum(); /// /// Set of string to enum mappings for ItemStatus /// private static readonly Dictionary _fromItemStatusMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for ItemType /// private static readonly Dictionary _toItemTypeMap = Converters.GenerateToEnum(); /// /// Set of string to enum mappings for ItemType /// private static readonly Dictionary _fromItemTypeMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for LoadFlag /// private static readonly Dictionary _toLoadFlagMap = Converters.GenerateToEnum(); /// /// Set of string to enum mappings for LoadFlag /// private static readonly Dictionary _fromLoadFlagMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for MachineType /// private static readonly Dictionary _toMachineTypeMap = Converters.GenerateToEnum(); /// /// Set of enum to string mappings for OpenMSXSubType /// private static readonly Dictionary _toOpenMSXSubTypeMap = Converters.GenerateToEnum(); /// /// Set of string to enum mappings for OpenMSXSubType /// private static readonly Dictionary _fromOpenMSXSubTypeMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for Relation /// private static readonly Dictionary _toRelationMap = Converters.GenerateToEnum(); /// /// Set of string to enum mappings for Relation /// private static readonly Dictionary _fromRelationMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for Runnable /// private static readonly Dictionary _toRunnableMap = Converters.GenerateToEnum(); /// /// Set of string to enum mappings for Runnable /// private static readonly Dictionary _fromRunnableMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for SoftwareListStatus /// private static readonly Dictionary _toSoftwareListStatusMap = Converters.GenerateToEnum(); /// /// Set of string to enum mappings for SoftwareListStatus /// private static readonly Dictionary _fromSoftwareListStatusMap = Converters.GenerateToString(useSecond: false); /// /// Set of enum to string mappings for Supported /// private static readonly Dictionary _toSupportedMap = Converters.GenerateToEnum(); /// /// Set of string to enum mappings for Supported /// private static readonly Dictionary _fromSupportedMap = Converters.GenerateToString(useSecond: false); /// /// Set of string to enum mappings for Supported (secondary) /// private static readonly Dictionary _fromSupportedSecondaryMap = Converters.GenerateToString(useSecond: true); /// /// Set of enum to string mappings for SupportStatus /// private static readonly Dictionary _toSupportStatusMap = Converters.GenerateToEnum(); /// /// Set of string to enum mappings for SupportStatus /// private static readonly Dictionary _fromSupportStatusMap = Converters.GenerateToString(useSecond: false); #endregion #region String to Enum /// /// Get the enum value for an input string, if possible /// /// String value to parse/param> /// 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 (_toChipTypeMap.ContainsKey(value)) return _toChipTypeMap[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 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 (_toControlTypeMap.ContainsKey(value)) return _toControlTypeMap[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 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 (_toDeviceTypeMap.ContainsKey(value)) return _toDeviceTypeMap[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 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 (_toDisplayTypeMap.ContainsKey(value)) return _toDisplayTypeMap[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 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 (_toEndiannessMap.ContainsKey(value)) return _toEndiannessMap[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 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 (_toFeatureStatusMap.ContainsKey(value)) return _toFeatureStatusMap[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 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 (_toFeatureTypeMap.ContainsKey(value)) return _toFeatureTypeMap[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 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 (_toItemStatusMap.ContainsKey(value)) return _toItemStatusMap[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 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 (_toItemTypeMap.ContainsKey(value)) return _toItemTypeMap[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 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 (_toLoadFlagMap.ContainsKey(value)) return _toLoadFlagMap[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 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 (_toMachineTypeMap.ContainsKey(value)) return _toMachineTypeMap[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 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 (_toOpenMSXSubTypeMap.ContainsKey(value)) return _toOpenMSXSubTypeMap[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 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 (_toRelationMap.ContainsKey(value)) return _toRelationMap[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 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 (_toRunnableMap.ContainsKey(value)) return _toRunnableMap[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 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 (_toSoftwareListStatusMap.ContainsKey(value)) return _toSoftwareListStatusMap[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 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 (_toSupportedMap.ContainsKey(value)) return _toSupportedMap[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 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 (_toSupportStatusMap.ContainsKey(value)) return _toSupportStatusMap[value]; // Otherwise, return the default value for the enum return default; } #endregion #region Enum to String /// /// Get the string value for an input enum, if possible /// /// Enum value to parse/param> /// True to use the second mapping option, if it exists /// String value representing the input, default on error public static string? AsStringValue(this ChipType value) { // Try to get the value from the mappings if (_fromChipTypeMap.ContainsKey(value)) return _fromChipTypeMap[value]; // Otherwise, return null return null; } /// /// Get the string value for an input enum, if possible /// /// Enum value to parse/param> /// True to use the second mapping option, if it exists /// String value representing the input, default on error public static string? AsStringValue(this ControlType value) { // Try to get the value from the mappings if (_fromControlTypeMap.ContainsKey(value)) return _fromControlTypeMap[value]; // Otherwise, return null return null; } /// /// Get the string value for an input enum, if possible /// /// Enum value to parse/param> /// True to use the second mapping option, if it exists /// String value representing the input, default on error public static string? AsStringValue(this DeviceType value) { // Try to get the value from the mappings if (_fromDeviceTypeMap.ContainsKey(value)) return _fromDeviceTypeMap[value]; // Otherwise, return null return null; } /// /// Get the string value for an input enum, if possible /// /// Enum value to parse/param> /// True to use the second mapping option, if it exists /// String value representing the input, default on error public static string? AsStringValue(this DisplayType value) { // Try to get the value from the mappings if (_fromDisplayTypeMap.ContainsKey(value)) return _fromDisplayTypeMap[value]; // Otherwise, return null return null; } /// /// Get the string value for an input enum, if possible /// /// Enum value to parse/param> /// True to use the second mapping option, if it exists /// String value representing the input, default on error public static string? AsStringValue(this Endianness value) { // Try to get the value from the mappings if (_fromEndiannessMap.ContainsKey(value)) return _fromEndiannessMap[value]; // Otherwise, return null return null; } /// /// Get the string value for an input enum, if possible /// /// Enum value to parse/param> /// True to use the second mapping option, if it exists /// String value representing the input, default on error public static string? AsStringValue(this FeatureStatus value) { // Try to get the value from the mappings if (_fromFeatureStatusMap.ContainsKey(value)) return _fromFeatureStatusMap[value]; // Otherwise, return null return null; } /// /// Get the string value for an input enum, if possible /// /// Enum value to parse/param> /// True to use the second mapping option, if it exists /// String value representing the input, default on error public static string? AsStringValue(this FeatureType value) { // Try to get the value from the mappings if (_fromFeatureTypeMap.ContainsKey(value)) return _fromFeatureTypeMap[value]; // Otherwise, return null return null; } /// /// Get the string value for an input enum, if possible /// /// Enum value to parse/param> /// True to use the second mapping option, if it exists /// String value representing the input, default on error public static string? AsStringValue(this ItemStatus value) { // Try to get the value from the mappings if (_fromItemStatusMap.ContainsKey(value)) return _fromItemStatusMap[value]; // Otherwise, return null return null; } /// /// Get the string value for an input enum, if possible /// /// Enum value to parse/param> /// True to use the second mapping option, if it exists /// String value representing the input, default on error public static string? AsStringValue(this ItemType value) { // Try to get the value from the mappings if (_fromItemTypeMap.ContainsKey(value)) return _fromItemTypeMap[value]; // Otherwise, return null return null; } /// /// Get the string value for an input enum, if possible /// /// Enum value to parse/param> /// True to use the second mapping option, if it exists /// String value representing the input, default on error public static string? AsStringValue(this LoadFlag value) { // Try to get the value from the mappings if (_fromLoadFlagMap.ContainsKey(value)) return _fromLoadFlagMap[value]; // Otherwise, return null return null; } /// /// Get the string value for an input enum, if possible /// /// Enum value to parse/param> /// True to use the second mapping option, if it exists /// String value representing the input, default on error public static string? AsStringValue(this OpenMSXSubType value) { // Try to get the value from the mappings if (_fromOpenMSXSubTypeMap.ContainsKey(value)) return _fromOpenMSXSubTypeMap[value]; // Otherwise, return null return null; } /// /// Get the string value for an input enum, if possible /// /// Enum value to parse/param> /// True to use the second mapping option, if it exists /// String value representing the input, default on error public static string? AsStringValue(this Relation value) { // Try to get the value from the mappings if (_fromRelationMap.ContainsKey(value)) return _fromRelationMap[value]; // Otherwise, return null return null; } /// /// Get the string value for an input enum, if possible /// /// Enum value to parse/param> /// True to use the second mapping option, if it exists /// String value representing the input, default on error public static string? AsStringValue(this Runnable value) { // Try to get the value from the mappings if (_fromRunnableMap.ContainsKey(value)) return _fromRunnableMap[value]; // Otherwise, return null return null; } /// /// Get the string value for an input enum, if possible /// /// Enum value to parse/param> /// True to use the second mapping option, if it exists /// String value representing the input, default on error public static string? AsStringValue(this SoftwareListStatus value) { // Try to get the value from the mappings if (_fromSoftwareListStatusMap.ContainsKey(value)) return _fromSoftwareListStatusMap[value]; // Otherwise, return null return null; } /// /// Get the string value for an input enum, if possible /// /// Enum value to parse/param> /// True to use the second mapping option, if it exists /// String value representing the input, default on error public static string? AsStringValue(this Supported value, bool useSecond = false) { // Try to get the value from the mappings if (!useSecond && _fromSupportedMap.ContainsKey(value)) return _fromSupportedMap[value]; else if (useSecond && _fromSupportedSecondaryMap.ContainsKey(value)) return _fromSupportedSecondaryMap[value]; // Otherwise, return null return null; } /// /// Get the string value for an input enum, if possible /// /// Enum value to parse/param> /// True to use the second mapping option, if it exists /// String value representing the input, default on error public static string? AsStringValue(this SupportStatus value) { // Try to get the value from the mappings if (_fromSupportStatusMap.ContainsKey(value)) return _fromSupportStatusMap[value]; // Otherwise, return null return null; } #endregion } }