Skip to content

Commit aa002a4

Browse files
authored
toml: return an error from toml.parse_file(), when the passed file path does not exist (#20912)
1 parent d8c4a84 commit aa002a4

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

vlib/toml/input/input.v

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,23 @@ pub:
1313
file_path string // '/path/to/file.toml'
1414
}
1515

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-
2816
// read_input returns either Config.text or the read file contents of Config.file_path
2917
// depending on which one is not empty.
3018
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()}"')
3833
}
3934
return text
4035
}

0 commit comments

Comments
 (0)