diff --git a/vlib/toml/input/input.v b/vlib/toml/input/input.v index a7b9064429..cc54396367 100644 --- a/vlib/toml/input/input.v +++ b/vlib/toml/input/input.v @@ -3,6 +3,8 @@ // that can be found in the LICENSE file. module input +import os + // Config is used to configure input to the toml module. // Only one of the fields `text` and `file_path` is allowed to be set at time of configuration. pub struct Config { @@ -11,6 +13,23 @@ pub: file_path string // '/path/to/file.toml' } +// auto_config returns an, automatic determined, input Config based on heuristics +// found in `toml` +pub fn auto_config(toml string) ?Config { + mut config := Config{} + if !toml.contains('\n') && os.is_file(toml) { + config = Config{ + file_path: toml + } + } else { + config = Config{ + text: toml + } + } + config.validate() ? + return config +} + // validate returns an optional error if more than one of the fields // in `Config` has a non-default value (empty string). pub fn (c Config) validate() ? { @@ -22,3 +41,14 @@ pub fn (c Config) validate() ? { ' ${typeof(c).name} must either contain a valid `file_path` OR a non-empty `text` field') } } + +pub fn (c Config) read_input() ?string { + mut text := c.text + if os.is_file(c.file_path) { + text = os.read_file(c.file_path) or { + return error(@MOD + '.' + @STRUCT + '.' + @FN + + ' Could not read "$c.file_path": "$err.msg"') + } + } + return text +} diff --git a/vlib/toml/scanner/scanner.v b/vlib/toml/scanner/scanner.v index 8a380d8ce3..7df0f3cf29 100644 --- a/vlib/toml/scanner/scanner.v +++ b/vlib/toml/scanner/scanner.v @@ -38,7 +38,7 @@ pub: tokenize_formating bool // if true, generate tokens for `\n`, ` `, `\t`, `\r` etc. } -// new_scanner returns a new heap allocated `Scanner` instance. +// new_scanner returns a new *heap* allocated `Scanner` instance. pub fn new_scanner(config Config) ?&Scanner { config.input.validate() ? mut text := config.input.text @@ -56,6 +56,17 @@ pub fn new_scanner(config Config) ?&Scanner { return s } +// returns a new *stack* allocated `Scanner` instance. +pub fn new_simple(toml_input string) ?Scanner { + config := Config{ + input: input.auto_config(toml_input) ? + } + return Scanner{ + config: config + text: config.input.read_input() ? + } +} + // scan returns the next token from the input. [direct_array_access] pub fn (mut s Scanner) scan() ?token.Token { diff --git a/vlib/toml/toml.v b/vlib/toml/toml.v index 0ae18d187b..c644a79508 100644 --- a/vlib/toml/toml.v +++ b/vlib/toml/toml.v @@ -3,7 +3,6 @@ // that can be found in the LICENSE file. module toml -import os import toml.ast import toml.util import toml.input @@ -71,17 +70,7 @@ pub fn parse_text(text string) ?Doc { // 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`. pub fn parse(toml string) ?Doc { - mut input_config := input.Config{} - if !toml.contains('\n') && os.is_file(toml) { - input_config = input.Config{ - file_path: toml - } - } else { - input_config = input.Config{ - text: toml - } - } - + mut input_config := input.auto_config(toml) ? scanner_config := scanner.Config{ input: input_config }