toml: add comptime check if a supported type (struct) was passed to toml.decode, when the type has no custom .from_toml method defined (#19317)

This commit is contained in:
Turiiya 2023-09-10 16:32:02 +02:00 committed by GitHub
parent 1218d88cfd
commit 12dd6e8b39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 0 deletions

View File

@ -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' }
// ...
}

View File

@ -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
}