Repository: orchestr7/ktoml (kotlin)

Issue:
Cannot deserialize a list of Enums
```kotlin
import com.akuleshov7.ktoml.Toml
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString

enum class Enum {
    A,
    B,
    C
}

@Serializable
data class Enums(
    val enums: List<Enum>
)

fun main() {
    val enums = Enums(listOf(Enum.A, Enum.B, Enum.C))

    val toml = Toml()
    val encoded = toml.encodeToString(enums)
    println(encoded)
    println(toml.decodeFromString<Enums>(encoded))
}
```

Gives:

```
enums = [ "A", "B", "C" ]
Exception in thread "main" java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Integer (java.lang.String and java.lang.Integer are in module java.base of loader 'bootstrap')
	at kotlinx.serialization.encoding.AbstractDecoder.decodeEnum(AbstractDecoder.kt:35)
	at com.akuleshov7.ktoml.decoders.TomlArrayDecoder.decodeEnum(TomlArrayDecoder.kt:91)
	at kotlinx.serialization.internal.EnumSerializer.deserialize(Enums.kt:136)
	at kotlinx.serialization.internal.EnumSerializer.deserialize(Enums.kt:102)
	at kotlinx.serialization.encoding.Decoder.decodeSerializableValue(Decoding.kt:258)
	at com.akuleshov7.ktoml.decoders.TomlAbstractDecoder.decodeSerializableValue(TomlAbstractDecoder.kt:116)
	at com.akuleshov7.ktoml.decoders.TomlArrayDecoder.decodeSerializableValue(TomlArrayDecoder.kt:97)
	at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:43)
	at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableElement(AbstractDecoder.kt:70)
	at kotlinx.serialization.encoding.CompositeDecoder.decodeSerializableElement$default(Decoding.kt:539)
	at kotlinx.serialization.internal.CollectionLikeSerializer.readElement(CollectionSerializers.kt:80)
	at kotlinx.serialization.internal.AbstractCollectionSerializer.readElement$default(CollectionSerializers.kt:51)
	at kotlinx.serialization.internal.AbstractCollectionSerializer.merge(CollectionSerializers.kt:36)
	at kotlinx.serialization.internal.AbstractCollectionSerializer.deserialize(CollectionSerializers.kt:43)
	at kotlinx.serialization.encoding.Decoder.decodeSerializableValue(Decoding.kt:258)
	at com.akuleshov7.ktoml.decoders.TomlAbstractDecoder.decodeSerializableValue(TomlAbstractDecoder.kt:116)
	at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableValue(AbstractDecoder.kt:43)
	at kotlinx.serialization.encoding.AbstractDecoder.decodeSerializableElement(AbstractDecoder.kt:70)
	at Enums$$serializer.deserialize(Agent.kt:12)
	at Enums$$serializer.deserialize(Agent.kt:12)
	at kotlinx.serialization.encoding.Decoder.decodeSerializableValue(Decoding.kt:258)
	at com.akuleshov7.ktoml.decoders.TomlAbstractDecoder.decodeSerializableValue(TomlAbstractDecoder.kt:116)
	at com.akuleshov7.ktoml.decoders.TomlMainDecoder$Companion.decode(TomlMainDecoder.kt:298)
	at com.akuleshov7.ktoml.Toml.decode(Toml.kt:158)
	at com.akuleshov7.ktoml.Toml.decodeFromString(Toml.kt:50)
	at AgentKt.main(Agent.kt:26)
	at AgentKt.main(Agent.kt)
```

Pull request:
Fixes #340

Patch:
diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/decoders/TomlArrayDecoder.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/decoders/TomlArrayDecoder.kt
index 4ba3920d..f5cefc4d 100644
--- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/decoders/TomlArrayDecoder.kt
+++ b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/decoders/TomlArrayDecoder.kt
@@ -2,6 +2,7 @@ package com.akuleshov7.ktoml.decoders
 
 import com.akuleshov7.ktoml.Toml.Default.serializersModule
 import com.akuleshov7.ktoml.TomlInputConfig
+import com.akuleshov7.ktoml.tree.nodes.TomlFile
 import com.akuleshov7.ktoml.tree.nodes.TomlKeyValue
 import com.akuleshov7.ktoml.tree.nodes.TomlKeyValueArray
 import com.akuleshov7.ktoml.tree.nodes.TomlKeyValuePrimitive
@@ -42,32 +43,45 @@ public class TomlArrayDecoder(
 
         currentPrimitiveElementOfArray = list[nextElementIndex]
 
-        currentElementDecoder = if (currentPrimitiveElementOfArray is TomlArray) {
-            TomlArrayDecoder(
-                TomlKeyValueArray(
-                    rootNode.key,
-                    currentPrimitiveElementOfArray,
-                    rootNode.lineNo,
-                    comments = emptyList(),
-                    inlineComment = "",
-                ),
-                config,
-                serializersModule,
-            )
+        if (currentPrimitiveElementOfArray is TomlArray) {
+            setArrayDecoder()
         } else {
-            TomlPrimitiveDecoder(
-                // a small hack that creates a PrimitiveKeyValue node that is used in the decoder
+            setPrimitiveDecoder()
+        }
+        return nextElementIndex++
+    }
+
+    private fun setArrayDecoder() {
+        currentElementDecoder = TomlArrayDecoder(
+            TomlKeyValueArray(
+                rootNode.key,
+                currentPrimitiveElementOfArray,
+                rootNode.lineNo,
+                comments = emptyList(),
+                inlineComment = "",
+            ),
+            config,
+            serializersModule,
+        )
+    }
+
+    private fun setPrimitiveDecoder() {
+        val primitiveRoot = TomlFile().also { root ->
+            root.appendChild(
                 TomlKeyValuePrimitive(
                     rootNode.key,
                     currentPrimitiveElementOfArray,
                     rootNode.lineNo,
                     comments = emptyList(),
                     inlineComment = "",
-                ),
-                serializersModule,
+                )
             )
         }
-        return nextElementIndex++
+        currentElementDecoder = TomlMainDecoder(
+            rootNode = primitiveRoot,
+            config = config,
+            serializersModule = serializersModule,
+        )
     }
 
     override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder {
diff --git a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/decoders/TomlPrimitiveDecoder.kt b/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/decoders/TomlPrimitiveDecoder.kt
deleted file mode 100644
index bb343513..00000000
--- a/ktoml-core/src/commonMain/kotlin/com/akuleshov7/ktoml/decoders/TomlPrimitiveDecoder.kt
+++ /dev/null
@@ -1,21 +0,0 @@
-package com.akuleshov7.ktoml.decoders
-
-import com.akuleshov7.ktoml.tree.nodes.TomlKeyValue
-import com.akuleshov7.ktoml.tree.nodes.TomlKeyValuePrimitive
-import kotlinx.serialization.ExperimentalSerializationApi
-import kotlinx.serialization.descriptors.SerialDescriptor
-import kotlinx.serialization.modules.SerializersModule
-
-/**
- * @param rootNode
- * @property serializersModule
- */
-@ExperimentalSerializationApi
-public class TomlPrimitiveDecoder(
-    private val rootNode: TomlKeyValuePrimitive,
-    override val serializersModule: SerializersModule,
-) : TomlAbstractDecoder() {
-    override fun decodeValue(): Any = rootNode.value.content
-    override fun decodeElementIndex(descriptor: SerialDescriptor): Int = 0
-    override fun decodeKeyValue(): TomlKeyValue = rootNode
-}


Test patch:
diff --git a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/structures/ArrayDecoderTest.kt b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/structures/ArrayDecoderTest.kt
index 89ed2771..d5ca2a35 100644
--- a/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/structures/ArrayDecoderTest.kt
+++ b/ktoml-core/src/commonTest/kotlin/com/akuleshov7/ktoml/decoders/structures/ArrayDecoderTest.kt
@@ -200,4 +200,25 @@ class SimpleArrayDecoderTest {
             Toml.decodeFromString<ArrayWrapper>(toml)
         }
     }
+
+    enum class Enum {
+        A,
+        B,
+        C
+    }
+
+    @Serializable
+    data class Enums(
+        val enums: List<Enum>
+    )
+
+    @Test
+    fun decodeEnumList() {
+        val test = """
+            enums = ["A", "B", "C"]
+        """.trimIndent()
+
+        val decoded = Toml.decodeFromString<Enums>(test)
+        assertEquals(Enums(listOf(Enum.A, Enum.B, Enum.C)), decoded)
+    }
 }