Skip to content

Commit

Permalink
toml: check if supported type was passed when it has no method
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Sep 9, 2023
1 parent b0fb9fb commit 5931238
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
10 changes: 10 additions & 0 deletions vlib/toml/tests/encode_and_decode_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
// ...
}
3 changes: 3 additions & 0 deletions vlib/toml/toml.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 5931238

Please sign in to comment.