Skip to content

Commit

Permalink
toml: add comptime check if a supported type (struct) was passed to `…
Browse files Browse the repository at this point in the history
…toml.decode`, when the type has no custom `.from_toml` method defined (#19317)
  • Loading branch information
ttytm committed Sep 10, 2023
1 parent 1218d88 commit 12dd6e8
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
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
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 12dd6e8

Please sign in to comment.