Skip to content

Commit 3f5e718

Browse files
committed
Simplify config parsing by creating parse_string_array function
1 parent 3ce65d6 commit 3f5e718

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

src/config.rs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -76,34 +76,13 @@ fn read_config_inner(manifest_path: &Path) -> Result<Config> {
7676
config.test_success_exit_code = Some(exit_code as i32);
7777
}
7878
("run-command", Value::Array(array)) => {
79-
let mut command = Vec::new();
80-
for value in array {
81-
match value {
82-
Value::String(s) => command.push(s),
83-
_ => return Err(anyhow!("run-command must be a list of strings")),
84-
}
85-
}
86-
config.run_command = Some(command);
79+
config.run_command = Some(parse_string_array(array, "run-command")?);
8780
}
8881
("run-args", Value::Array(array)) => {
89-
let mut args = Vec::new();
90-
for value in array {
91-
match value {
92-
Value::String(s) => args.push(s),
93-
_ => return Err(anyhow!("run-args must be a list of strings")),
94-
}
95-
}
96-
config.run_args = Some(args);
82+
config.run_args = Some(parse_string_array(array, "run-args")?);
9783
}
9884
("test-args", Value::Array(array)) => {
99-
let mut args = Vec::new();
100-
for value in array {
101-
match value {
102-
Value::String(s) => args.push(s),
103-
_ => return Err(anyhow!("test-args must be a list of strings")),
104-
}
105-
}
106-
config.test_args = Some(args);
85+
config.test_args = Some(parse_string_array(array, "test-args")?);
10786
}
10887
(key, value) => {
10988
return Err(anyhow!(
@@ -118,6 +97,17 @@ fn read_config_inner(manifest_path: &Path) -> Result<Config> {
11897
Ok(config.into())
11998
}
12099

100+
fn parse_string_array(array: Vec<Value>, prop_name: &str) -> Result<Vec<String>> {
101+
let mut parsed = Vec::new();
102+
for value in array {
103+
match value {
104+
Value::String(s) => parsed.push(s),
105+
_ => return Err(anyhow!("{} must be a list of strings", prop_name)),
106+
}
107+
}
108+
Ok(parsed)
109+
}
110+
121111
#[derive(Default)]
122112
struct ConfigBuilder {
123113
run_command: Option<Vec<String>>,

0 commit comments

Comments
 (0)