diff --git a/vlib/toml/tests/encode_and_decode_test.v b/vlib/toml/tests/encode_and_decode_test.v index 6f57bf7473829c..66e7192bf97cfa 100644 --- a/vlib/toml/tests/encode_and_decode_test.v +++ b/vlib/toml/tests/encode_and_decode_test.v @@ -177,3 +177,13 @@ times = [ assert toml.encode[Arrs](a) == s assert toml.decode[Arrs](s)! == a } + +fn test_unsupported_type() { + s := 'name = "Peter"' + err_msg := 'toml.decode: expected struct, found ' + toml.decode[string](s) or { assert err.msg() == err_msg + 'string' } + toml.decode[[]string](s) or { assert err.msg() == err_msg + '[]string' } + toml.decode[int](s) or { assert err.msg() == err_msg + 'int' } + toml.decode[[]f32](s) or { assert err.msg() == err_msg + '[]f32' } + // ... +} diff --git a/vlib/toml/toml.v b/vlib/toml/toml.v index ad94095c38c0fc..fea73da731f908 100644 --- a/vlib/toml/toml.v +++ b/vlib/toml/toml.v @@ -23,6 +23,9 @@ pub fn decode[T](toml_txt string) !T { return typ } } + $if T !is $struct { + return error('toml.decode: expected struct, found ${T.name}') + } decode_struct[T](doc.to_any(), mut typ) return typ }