1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

toml: add encode<T> and decode<T> (#13244)

This commit is contained in:
Larpon
2022-01-21 20:21:31 +01:00
committed by GitHub
parent 7ae96f0e38
commit 2b4f7e7685
3 changed files with 147 additions and 0 deletions

View File

@@ -12,6 +12,20 @@ import toml.parser
pub struct Null {
}
// decode decodes a TOML `string` into the target type `T`.
pub fn decode<T>(toml_txt string) ?T {
doc := parse_text(toml_txt) ?
mut typ := T{}
typ.from_toml(doc.to_any())
return typ
}
// encode encodes the type `T` into a JSON string.
// Currently encode expects the method `.to_toml()` exists on `T`.
pub fn encode<T>(typ T) string {
return typ.to_toml()
}
// DateTime is the representation of an RFC 3339 datetime string.
pub struct DateTime {
datetime string