diff --git a/Sources/TOMLDecoder/_TOMLDecoder.swift b/Sources/TOMLDecoder/_TOMLDecoder.swift index d46600d..43b3722 100644 --- a/Sources/TOMLDecoder/_TOMLDecoder.swift +++ b/Sources/TOMLDecoder/_TOMLDecoder.swift @@ -51,7 +51,16 @@ final class _TOMLDecoder: Decoder { } func singleValueContainer() throws -> any SingleValueDecodingContainer { - self + guard token != .empty else { + throw DecodingError.typeMismatch( + (any SingleValueDecodingContainer).self, + DecodingError.Context( + codingPath: codingPath, + debugDescription: "Cannot decode a single value from a TOML table." + ) + ) + } + return self } init(referencing container: Container, at codingPath: [any CodingKey] = [], strategy: TOMLDecoder.Strategy, isLenient: Bool) { diff --git a/Tests/TOMLDecoderTests/TOMLDecoderTests.swift b/Tests/TOMLDecoderTests/TOMLDecoderTests.swift index 329d80e..96f83ba 100644 --- a/Tests/TOMLDecoderTests/TOMLDecoderTests.swift +++ b/Tests/TOMLDecoderTests/TOMLDecoderTests.swift @@ -501,6 +501,12 @@ struct TOMLDecoderTests { #expect(result == Team(languages: [.swift, .mojo, .zig], favorite: .swift)) } + @Test func topLevelTableCannotDecodeAsSingleValue() { + #expect(throws: DecodingError.self) { + try TOMLDecoder().decode(Int.self, from: "-1 = true") + } + } + @Test func twitter() throws { _ = try TOMLDecoder().decode(TwitterArchive.self, from: Resources.twitterTOMLString) }