|
13 | 13 | file_path string // '/path/to/file.toml' |
14 | 14 | } |
15 | 15 |
|
16 | | -// validate returns an optional error if more than one of the fields |
17 | | -// in `Config` has a non-default value (empty string). |
18 | | -fn (c Config) validate() ! { |
19 | | - if c.file_path != '' && c.text != '' { |
20 | | - error(@MOD + '.' + @FN + |
21 | | - ' ${typeof(c).name} should contain only one of the fields `file_path` OR `text` filled out') |
22 | | - } else if c.file_path == '' && c.text == '' { |
23 | | - error(@MOD + '.' + @FN + |
24 | | - ' ${typeof(c).name} must either contain a valid `file_path` OR a non-empty `text` field') |
25 | | - } |
26 | | -} |
27 | | - |
28 | 16 | // read_input returns either Config.text or the read file contents of Config.file_path |
29 | 17 | // depending on which one is not empty. |
30 | 18 | pub fn (c Config) read_input() !string { |
31 | | - c.validate()! |
32 | | - mut text := c.text |
33 | | - if text == '' && os.is_file(c.file_path) { |
34 | | - text = os.read_file(c.file_path) or { |
35 | | - return error(@MOD + '.' + @STRUCT + '.' + @FN + |
36 | | - ' Could not read "${c.file_path}": "${err.msg()}"') |
37 | | - } |
| 19 | + if c.file_path != '' && c.text != '' { |
| 20 | + return error(@MOD + '.' + @FN + |
| 21 | + ' ${typeof(c).name} should contain only one of the fields `file_path` OR `text` filled out') |
| 22 | + } |
| 23 | + if c.file_path == '' && c.text == '' { |
| 24 | + // TODO: passing both empty is used *a lot* by `./v vlib/toml/tests/burntsushi_toml_test.v`; investigate why. |
| 25 | + return '' |
| 26 | + } |
| 27 | + if c.text != '' { |
| 28 | + return c.text |
| 29 | + } |
| 30 | + text := os.read_file(c.file_path) or { |
| 31 | + return error(@MOD + '.' + @STRUCT + '.' + @FN + |
| 32 | + ' Could not read "${c.file_path}": "${err.msg()}"') |
38 | 33 | } |
39 | 34 | return text |
40 | 35 | } |
0 commit comments