diff --git a/Tomlet.Tests/EnumTests.cs b/Tomlet.Tests/EnumTests.cs index 8cca0cc..f023651 100644 --- a/Tomlet.Tests/EnumTests.cs +++ b/Tomlet.Tests/EnumTests.cs @@ -31,6 +31,23 @@ public void SerializingAnUndefinedEnumValueThrows() Assert.Throws(() => TomletMain.TomlStringFrom(testObj)); } + [Fact] + public void CanSerializeAndDeserializeCombinedFlagsEnum() + { + var toml = TomletMain.TomlStringFrom(new {EnumValue = TestFlagsEnum.Flag1 | TestFlagsEnum.Flag3}).Trim(); + Assert.Equal("EnumValue = \"Flag1, Flag3\"", toml); + + var result = TomletMain.To>(toml); + Assert.Equal(TestFlagsEnum.Flag1 | TestFlagsEnum.Flag3, result["EnumValue"]); + } + + [Fact] + public void SerializingAFlagsEnumWithAnUndeclaredBitThrows() + { + var testObj = new {EnumValue = TestFlagsEnum.Flag1 | (TestFlagsEnum)8}; + Assert.Throws(() => TomletMain.TomlStringFrom(testObj)); + } + [Fact] public void CanSerializeDictWithEnumValues() { diff --git a/Tomlet.Tests/TestModelClasses/TestFlagsEnum.cs b/Tomlet.Tests/TestModelClasses/TestFlagsEnum.cs new file mode 100644 index 0000000..30862b2 --- /dev/null +++ b/Tomlet.Tests/TestModelClasses/TestFlagsEnum.cs @@ -0,0 +1,12 @@ +using System; + +namespace Tomlet.Tests.TestModelClasses; + +[Flags] +public enum TestFlagsEnum +{ + None = 0, + Flag1 = 1, + Flag2 = 2, + Flag3 = 4 +} \ No newline at end of file diff --git a/Tomlet/TomlCompositeSerializer.cs b/Tomlet/TomlCompositeSerializer.cs index f91b3c9..99cbe6b 100644 --- a/Tomlet/TomlCompositeSerializer.cs +++ b/Tomlet/TomlCompositeSerializer.cs @@ -11,6 +11,24 @@ namespace Tomlet; internal static class TomlCompositeSerializer { + private static string GetEnumName(Type type, object value) + { + var name = Enum.GetName(type, value); + if (name != null) + return name; + + //A [Flags] enum value can be a combination of declared values, which ToString writes as "A, B" and Enum.Parse reads back. + //If any bit has no declared name, ToString falls back to a number, which we still reject. + if (type.IsDefined(typeof(FlagsAttribute), false)) + { + var combined = value.ToString(); + if (!char.IsDigit(combined[0]) && combined[0] != '-') + return combined; + } + + throw new ArgumentException($"Tomlet: Cannot serialize {value} as an enum of type {type} because the enum type does not declare a name for that value"); + } + #if MODERN_DOTNET [UnconditionalSuppressMessage("AOT", "IL2072", Justification = "Any field that is being serialized must have been used as a field in the consuming code in order for the code path that queries it to run, so the dynamic code requirement is already satisfied.")] public static TomlSerializationMethods.Serialize For([DynamicallyAccessedMembers(TomlSerializationMethods.MainDeserializerAccessedMemberTypes)] Type type, TomlSerializerOptions options) @@ -23,7 +41,7 @@ public static TomlSerializationMethods.Serialize For(Type type, TomlSeri if (type.IsEnum) { var stringSerializer = TomlSerializationMethods.GetSerializer(typeof(string), options); - serializer = o => stringSerializer.Invoke(Enum.GetName(type, o!) ?? throw new ArgumentException($"Tomlet: Cannot serialize {o} as an enum of type {type} because the enum type does not declare a name for that value")); + serializer = o => stringSerializer.Invoke(GetEnumName(type, o!)); } else {