Skip to content

Commit 9d3bebb

Browse files
committed
Code review feedback
1 parent 02adf44 commit 9d3bebb

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

src/rust/engine/options/src/args.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ impl OptionsSource for Args {
145145

146146
fn get_dict(&self, id: &OptionId) -> Result<Option<DictEdit>, String> {
147147
match self.find_flag(Self::arg_names(id, Negate::False))? {
148-
Some((name, ref value, _)) => parse_dict(value)
149-
.map(Some)
150-
.map_err(|e| e.render(name)),
148+
Some((name, ref value, _)) => parse_dict(value).map(Some).map_err(|e| e.render(name)),
151149
None => Ok(None),
152150
}
153151
}

src/rust/engine/options/src/lib.rs

+12-15
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use std::collections::{BTreeMap, HashMap, HashSet};
3434
use std::fmt::Debug;
3535
use std::hash::Hash;
3636
use std::os::unix::ffi::OsStrExt;
37-
use std::path;
3837
use std::path::Path;
3938
use std::rc::Rc;
4039

@@ -259,22 +258,20 @@ impl OptionParser {
259258
};
260259

261260
fn path_join(prefix: &str, suffix: &str) -> String {
262-
if prefix.ends_with(path::MAIN_SEPARATOR) {
263-
format!("{}{}", prefix, suffix)
264-
} else {
265-
format!("{}{}{}", prefix, path::MAIN_SEPARATOR, suffix)
266-
}
261+
// TODO: The calling code should traffic in Path, or OsString, not String.
262+
// For now we assume the paths are valid UTF8 strings, via unwrap().
263+
Path::new(prefix).join(suffix).to_str().unwrap().to_string()
267264
}
268265

269266
fn path_strip(prefix: &str, path: &str) -> String {
270-
match path.strip_prefix(prefix) {
271-
Some(suffix) => match suffix.strip_prefix(path::MAIN_SEPARATOR) {
272-
Some(suffix_suffix) => suffix_suffix,
273-
_ => suffix,
274-
},
275-
_ => path,
276-
}
277-
.to_string()
267+
// TODO: The calling code should traffic in Path, or OsString, not String.
268+
// For now we assume the paths are valid UTF8 strings, via unwrap().
269+
Path::new(path)
270+
.strip_prefix(prefix)
271+
.unwrap()
272+
.to_str()
273+
.unwrap()
274+
.to_string()
278275
}
279276

280277
let repo_config_files = match config_paths {
@@ -316,7 +313,7 @@ impl OptionParser {
316313
ordinal,
317314
path: path_strip(&buildroot_string, path),
318315
},
319-
Rc::new(config.clone()),
316+
Rc::new(config),
320317
);
321318
ordinal += 1;
322319
}

src/rust/engine/options/src/tests.rs

+27
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,33 @@ fn with_setup(
7373
do_check(option_parser);
7474
}
7575

76+
#[test]
77+
fn test_source_ordering() {
78+
assert!(
79+
Source::Default
80+
< Source::Config {
81+
ordinal: 0,
82+
path: "pants.toml".to_string()
83+
}
84+
);
85+
assert!(
86+
Source::Config {
87+
ordinal: 0,
88+
path: "pants.toml".to_string()
89+
} < Source::Config {
90+
ordinal: 1,
91+
path: "extra_pants.toml".to_string()
92+
}
93+
);
94+
assert!(
95+
Source::Config {
96+
ordinal: 1,
97+
path: "extra_pants.toml".to_string()
98+
} < Source::Env
99+
);
100+
assert!(Source::Env < Source::Flag);
101+
}
102+
76103
#[test]
77104
fn test_parse_single_valued_options() {
78105
fn check(

0 commit comments

Comments
 (0)