2021-09-24 21:13:52 +03:00
|
|
|
// Copyright (c) 2021 Lars Pontoppidan. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module toml
|
|
|
|
|
|
|
|
import toml.ast
|
|
|
|
import toml.input
|
|
|
|
import toml.scanner
|
|
|
|
import toml.parser
|
|
|
|
|
|
|
|
// Null is used in sumtype checks as a "default" value when nothing else is possible.
|
|
|
|
pub struct Null {
|
|
|
|
}
|
|
|
|
|
2022-01-21 22:21:31 +03:00
|
|
|
// decode decodes a TOML `string` into the target type `T`.
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn decode[T](toml_txt string) !T {
|
2022-10-16 09:28:57 +03:00
|
|
|
doc := parse_text(toml_txt)!
|
2022-01-21 22:21:31 +03:00
|
|
|
mut typ := T{}
|
|
|
|
typ.from_toml(doc.to_any())
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
2022-01-22 11:27:16 +03:00
|
|
|
// encode encodes the type `T` into a TOML string.
|
2022-01-21 22:21:31 +03:00
|
|
|
// Currently encode expects the method `.to_toml()` exists on `T`.
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn encode[T](typ T) string {
|
2022-01-21 22:21:31 +03:00
|
|
|
return typ.to_toml()
|
|
|
|
}
|
|
|
|
|
2021-11-18 14:27:59 +03:00
|
|
|
// DateTime is the representation of an RFC 3339 datetime string.
|
2021-11-18 08:44:24 +03:00
|
|
|
pub struct DateTime {
|
|
|
|
datetime string
|
|
|
|
}
|
|
|
|
|
2022-11-22 20:45:12 +03:00
|
|
|
// str returns the RFC 3339 string representation of the datetime.
|
2021-11-18 08:44:24 +03:00
|
|
|
pub fn (dt DateTime) str() string {
|
|
|
|
return dt.datetime
|
|
|
|
}
|
|
|
|
|
2021-11-18 14:27:59 +03:00
|
|
|
// Date is the representation of an RFC 3339 date-only string.
|
2021-11-18 08:44:24 +03:00
|
|
|
pub struct Date {
|
|
|
|
date string
|
|
|
|
}
|
|
|
|
|
2022-11-22 20:45:12 +03:00
|
|
|
// str returns the RFC 3339 date-only string representation.
|
2021-11-18 08:44:24 +03:00
|
|
|
pub fn (d Date) str() string {
|
|
|
|
return d.date
|
|
|
|
}
|
|
|
|
|
|
|
|
// Time is the representation of an RFC 3339 time-only string.
|
|
|
|
pub struct Time {
|
|
|
|
time string
|
|
|
|
}
|
|
|
|
|
2022-11-22 20:45:12 +03:00
|
|
|
// str returns the RFC 3339 time-only string representation.
|
2021-11-18 08:44:24 +03:00
|
|
|
pub fn (t Time) str() string {
|
|
|
|
return t.time
|
|
|
|
}
|
|
|
|
|
2021-09-24 21:13:52 +03:00
|
|
|
// Config is used to configure the toml parser.
|
|
|
|
// Only one of the fields `text` or `file_path`, is allowed to be set at time of configuration.
|
|
|
|
pub struct Config {
|
|
|
|
pub:
|
|
|
|
text string // TOML text
|
|
|
|
file_path string // '/path/to/file.toml'
|
|
|
|
parse_comments bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Doc is a representation of a TOML document.
|
|
|
|
// A document can be constructed from a `string` buffer or from a file path
|
|
|
|
pub struct Doc {
|
|
|
|
pub:
|
2022-09-15 07:59:31 +03:00
|
|
|
ast &ast.Root = unsafe { nil }
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// parse_file parses the TOML file in `path`.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn parse_file(path string) !Doc {
|
2021-09-24 21:13:52 +03:00
|
|
|
input_config := input.Config{
|
|
|
|
file_path: path
|
|
|
|
}
|
|
|
|
scanner_config := scanner.Config{
|
|
|
|
input: input_config
|
|
|
|
}
|
|
|
|
parser_config := parser.Config{
|
2022-10-16 09:28:57 +03:00
|
|
|
scanner: scanner.new_scanner(scanner_config)!
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
|
|
|
mut p := parser.new_parser(parser_config)
|
2023-02-08 21:37:04 +03:00
|
|
|
ast_ := p.parse()!
|
2021-09-24 21:13:52 +03:00
|
|
|
return Doc{
|
2023-02-08 21:37:04 +03:00
|
|
|
ast: ast_
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse_text parses the TOML document provided in `text`.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn parse_text(text string) !Doc {
|
2021-09-24 21:13:52 +03:00
|
|
|
input_config := input.Config{
|
|
|
|
text: text
|
|
|
|
}
|
|
|
|
scanner_config := scanner.Config{
|
|
|
|
input: input_config
|
|
|
|
}
|
|
|
|
parser_config := parser.Config{
|
2022-10-16 09:28:57 +03:00
|
|
|
scanner: scanner.new_scanner(scanner_config)!
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
|
|
|
mut p := parser.new_parser(parser_config)
|
2023-02-08 21:37:04 +03:00
|
|
|
ast_ := p.parse()!
|
2021-09-24 21:13:52 +03:00
|
|
|
return Doc{
|
2023-02-08 21:37:04 +03:00
|
|
|
ast: ast_
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-26 07:34:47 +03:00
|
|
|
// parse parses the TOML document provided in `toml`.
|
|
|
|
// parse automatically try to determine if the type of `toml` is a file or text.
|
|
|
|
// For explicit parsing of input types see `parse_file` or `parse_text`.
|
2022-03-18 23:33:51 +03:00
|
|
|
[deprecated: 'use parse_file or parse_text instead']
|
|
|
|
[deprecated_after: '2022-06-18']
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn parse(toml string) !Doc {
|
|
|
|
mut input_config := input.auto_config(toml)!
|
2021-09-24 21:13:52 +03:00
|
|
|
scanner_config := scanner.Config{
|
|
|
|
input: input_config
|
|
|
|
}
|
|
|
|
parser_config := parser.Config{
|
2022-10-16 09:28:57 +03:00
|
|
|
scanner: scanner.new_scanner(scanner_config)!
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
|
|
|
mut p := parser.new_parser(parser_config)
|
2023-02-08 21:37:04 +03:00
|
|
|
ast_ := p.parse()!
|
2021-09-24 21:13:52 +03:00
|
|
|
return Doc{
|
2023-02-08 21:37:04 +03:00
|
|
|
ast: ast_
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-18 14:27:59 +03:00
|
|
|
// parse_dotted_key converts `key` string to an array of strings.
|
|
|
|
// parse_dotted_key preserves strings delimited by both `"` and `'`.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn parse_dotted_key(key string) ![]string {
|
2021-11-18 14:27:59 +03:00
|
|
|
mut out := []string{}
|
|
|
|
mut buf := ''
|
|
|
|
mut in_string := false
|
2022-04-15 14:58:56 +03:00
|
|
|
mut delim := u8(` `)
|
2021-11-18 14:27:59 +03:00
|
|
|
for ch in key {
|
|
|
|
if ch in [`"`, `'`] {
|
|
|
|
if !in_string {
|
|
|
|
delim = ch
|
|
|
|
}
|
|
|
|
in_string = !in_string && ch == delim
|
|
|
|
if !in_string {
|
|
|
|
if buf != '' && buf != ' ' {
|
|
|
|
out << buf
|
|
|
|
}
|
|
|
|
buf = ''
|
|
|
|
delim = ` `
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
buf += ch.ascii_str()
|
|
|
|
if !in_string && ch == `.` {
|
|
|
|
if buf != '' && buf != ' ' {
|
2021-11-25 17:51:54 +03:00
|
|
|
buf = buf[..buf.len - 1]
|
|
|
|
if buf != '' && buf != ' ' {
|
|
|
|
out << buf
|
|
|
|
}
|
2021-11-18 14:27:59 +03:00
|
|
|
}
|
|
|
|
buf = ''
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if buf != '' && buf != ' ' {
|
|
|
|
out << buf
|
|
|
|
}
|
|
|
|
if in_string {
|
|
|
|
return error(@FN +
|
2022-11-15 16:53:13 +03:00
|
|
|
': could not parse key, missing closing string delimiter `${delim.ascii_str()}`')
|
2021-11-18 14:27:59 +03:00
|
|
|
}
|
|
|
|
return out
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
|
|
|
|
2021-11-24 21:39:22 +03:00
|
|
|
// parse_array_key converts `key` string to a key and index part.
|
|
|
|
fn parse_array_key(key string) (string, int) {
|
|
|
|
mut index := -1
|
|
|
|
mut k := key
|
|
|
|
if k.contains('[') {
|
|
|
|
index = k.all_after('[').all_before(']').int()
|
|
|
|
if k.starts_with('[') {
|
|
|
|
k = '' // k.all_after(']')
|
|
|
|
} else {
|
|
|
|
k = k.all_before('[')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return k, index
|
|
|
|
}
|
|
|
|
|
2021-11-15 20:00:09 +03:00
|
|
|
// to_any converts the `Doc` to toml.Any type.
|
|
|
|
pub fn (d Doc) to_any() Any {
|
2021-11-25 13:33:54 +03:00
|
|
|
return ast_to_any(d.ast.table)
|
2021-11-15 20:00:09 +03:00
|
|
|
}
|
|
|
|
|
2021-12-04 00:07:44 +03:00
|
|
|
// reflect returns `T` with `T.<field>`'s value set to the
|
|
|
|
// value of any 1st level TOML key by the same name.
|
2022-11-26 19:23:26 +03:00
|
|
|
pub fn (d Doc) reflect[T]() T {
|
|
|
|
return d.to_any().reflect[T]()
|
2021-12-03 13:40:46 +03:00
|
|
|
}
|
|
|
|
|
2021-09-24 21:13:52 +03:00
|
|
|
// value queries a value from the TOML document.
|
2021-11-20 20:45:17 +03:00
|
|
|
// `key` supports a small query syntax scheme:
|
|
|
|
// Maps can be queried in "dotted" form e.g. `a.b.c`.
|
|
|
|
// quoted keys are supported as `a."b.c"` or `a.'b.c'`.
|
2021-11-24 21:39:22 +03:00
|
|
|
// Arrays can be queried with `a[0].b[1].[2]`.
|
2021-09-24 21:13:52 +03:00
|
|
|
pub fn (d Doc) value(key string) Any {
|
2022-05-28 09:17:28 +03:00
|
|
|
key_split := parse_dotted_key(key) or { return toml.null }
|
2021-11-20 20:45:17 +03:00
|
|
|
return d.value_(d.ast.table, key_split)
|
2021-11-13 12:17:35 +03:00
|
|
|
}
|
|
|
|
|
2022-05-28 09:17:28 +03:00
|
|
|
pub const null = Any(Null{})
|
|
|
|
|
2022-11-22 20:45:12 +03:00
|
|
|
// value_opt queries a value from the TOML document. Returns an error if the
|
|
|
|
// key is not valid or there is no value for the key.
|
2022-10-16 09:28:57 +03:00
|
|
|
pub fn (d Doc) value_opt(key string) !Any {
|
2022-05-28 09:17:28 +03:00
|
|
|
key_split := parse_dotted_key(key) or { return error('invalid dotted key') }
|
|
|
|
x := d.value_(d.ast.table, key_split)
|
|
|
|
if x is Null {
|
|
|
|
return error('no value for key')
|
|
|
|
}
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2021-11-13 12:17:35 +03:00
|
|
|
// value_ returns the value found at `key` in the map `values` as `Any` type.
|
2021-11-20 20:45:17 +03:00
|
|
|
fn (d Doc) value_(value ast.Value, key []string) Any {
|
2022-05-28 09:17:28 +03:00
|
|
|
if key.len == 0 {
|
|
|
|
return toml.null
|
|
|
|
}
|
2021-11-20 20:45:17 +03:00
|
|
|
mut ast_value := ast.Value(ast.Null{})
|
2021-11-24 21:39:22 +03:00
|
|
|
k, index := parse_array_key(key[0])
|
|
|
|
|
2021-11-20 20:45:17 +03:00
|
|
|
if k == '' {
|
|
|
|
a := value as []ast.Value
|
2022-05-28 09:17:28 +03:00
|
|
|
ast_value = a[index] or { return toml.null }
|
2021-11-20 20:45:17 +03:00
|
|
|
}
|
|
|
|
|
2021-11-13 12:17:35 +03:00
|
|
|
if value is map[string]ast.Value {
|
2022-05-28 09:17:28 +03:00
|
|
|
ast_value = value[k] or { return toml.null }
|
2021-11-20 20:45:17 +03:00
|
|
|
if index > -1 {
|
|
|
|
a := ast_value as []ast.Value
|
2022-05-28 09:17:28 +03:00
|
|
|
ast_value = a[index] or { return toml.null }
|
2021-11-13 12:17:35 +03:00
|
|
|
}
|
2021-11-20 20:45:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if key.len <= 1 {
|
2021-11-25 13:33:54 +03:00
|
|
|
return ast_to_any(ast_value)
|
2021-11-20 20:45:17 +03:00
|
|
|
}
|
2021-11-24 21:39:22 +03:00
|
|
|
match ast_value {
|
|
|
|
map[string]ast.Value, []ast.Value {
|
|
|
|
return d.value_(ast_value, key[1..])
|
|
|
|
}
|
|
|
|
else {
|
2021-11-25 13:33:54 +03:00
|
|
|
return ast_to_any(value)
|
2021-11-24 21:39:22 +03:00
|
|
|
}
|
2021-11-13 12:17:35 +03:00
|
|
|
}
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
|
|
|
|
2021-11-18 08:44:24 +03:00
|
|
|
// ast_to_any converts `from` ast.Value to toml.Any value.
|
2021-11-25 13:33:54 +03:00
|
|
|
pub fn ast_to_any(value ast.Value) Any {
|
2021-09-24 21:13:52 +03:00
|
|
|
match value {
|
2021-11-18 08:44:24 +03:00
|
|
|
ast.Date {
|
|
|
|
return Any(Date{value.text})
|
|
|
|
}
|
|
|
|
ast.Time {
|
|
|
|
return Any(Time{value.text})
|
|
|
|
}
|
|
|
|
ast.DateTime {
|
|
|
|
return Any(DateTime{value.text})
|
|
|
|
}
|
2021-09-24 21:13:52 +03:00
|
|
|
ast.Quoted {
|
2021-11-18 08:44:24 +03:00
|
|
|
return Any(value.text)
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
|
|
|
ast.Number {
|
2021-11-25 13:33:54 +03:00
|
|
|
val_text := value.text
|
|
|
|
if val_text == 'inf' || val_text == '+inf' || val_text == '-inf' {
|
|
|
|
// NOTE values taken from strconv
|
|
|
|
if !val_text.starts_with('-') {
|
|
|
|
// strconv.double_plus_infinity
|
|
|
|
return Any(u64(0x7FF0000000000000))
|
|
|
|
} else {
|
|
|
|
// strconv.double_minus_infinity
|
|
|
|
return Any(u64(0xFFF0000000000000))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if val_text == 'nan' || val_text == '+nan' || val_text == '-nan' {
|
|
|
|
return Any('nan')
|
|
|
|
}
|
|
|
|
if !val_text.starts_with('0x')
|
|
|
|
&& (val_text.contains('.') || val_text.to_lower().contains('e')) {
|
2021-11-18 20:46:19 +03:00
|
|
|
return Any(value.f64())
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
2021-11-18 20:46:19 +03:00
|
|
|
return Any(value.i64())
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
|
|
|
ast.Bool {
|
|
|
|
str := (value as ast.Bool).text
|
|
|
|
if str == 'true' {
|
|
|
|
return Any(true)
|
|
|
|
}
|
|
|
|
return Any(false)
|
|
|
|
}
|
2021-09-25 20:31:02 +03:00
|
|
|
map[string]ast.Value {
|
|
|
|
m := (value as map[string]ast.Value)
|
2021-09-24 21:13:52 +03:00
|
|
|
mut am := map[string]Any{}
|
|
|
|
for k, v in m {
|
2021-11-25 13:33:54 +03:00
|
|
|
am[k] = ast_to_any(v)
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
|
|
|
return am
|
|
|
|
// return d.get_map_value(m, key_split[1..].join('.'))
|
|
|
|
}
|
2021-09-25 20:31:02 +03:00
|
|
|
[]ast.Value {
|
|
|
|
a := (value as []ast.Value)
|
2021-09-24 21:13:52 +03:00
|
|
|
mut aa := []Any{}
|
|
|
|
for val in a {
|
2021-11-25 13:33:54 +03:00
|
|
|
aa << ast_to_any(val)
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
|
|
|
return aa
|
|
|
|
}
|
|
|
|
else {
|
2022-05-28 09:17:28 +03:00
|
|
|
return toml.null
|
2021-09-24 21:13:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 09:17:28 +03:00
|
|
|
return toml.null
|
2021-09-24 21:13:52 +03:00
|
|
|
// TODO decide this
|
|
|
|
// panic(@MOD + '.' + @STRUCT + '.' + @FN + ' can\'t convert "$value"')
|
|
|
|
// return Any('')
|
|
|
|
}
|