Skip to content

Commit

Permalink
Refactored ugly rustflags lookup code into a separate function.
Browse files Browse the repository at this point in the history
  • Loading branch information
ibabushkin committed Dec 2, 2016
1 parent 4eda23f commit f440704
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
28 changes: 4 additions & 24 deletions src/cargo/ops/cargo_rustc/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -856,37 +856,17 @@ fn env_args(config: &Config,
// Then the target.*.rustflags value
let target = build_config.requested_target.as_ref().unwrap_or(&build_config.host_triple);
let key = format!("target.{}.{}", target, name);
let list = config.get_list(&key);
if let Ok(Some(args)) = list {
let args = args.val.into_iter().map(|a| a.0);
if let Some(args) = config.get_list_or_split_string(&key)? {
let args = args.val.into_iter();
return Ok(args.collect());
}
let string = config.get_string(&key);
if let Ok(Some(arg)) = string {
return Ok(arg.val.split(' ').map(str::to_string).collect());
}
if list.is_err() && string.is_err() {
if let Some(value) = config.values()?.get(&key) {
return config.expected("list or string", &key, value.clone());
}
}

// Then the build.rustflags value
let key = format!("build.{}", name);
let list = config.get_list(&key);
if let Ok(Some(args)) = list {
let args = args.val.into_iter().map(|a| a.0);
if let Some(args) = config.get_list_or_split_string(&key)? {
let args = args.val.into_iter();
return Ok(args.collect());
}
let string = config.get_string(&key);
if let Ok(Some(arg)) = string {
return Ok(arg.val.split(' ').map(str::to_string).collect());
}
if list.is_err() && string.is_err() {
if let Some(value) = config.values()?.get(&key) {
return config.expected("list or string", &key, value.clone());
}
}

Ok(Vec::new())
}
30 changes: 30 additions & 0 deletions src/cargo/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,36 @@ impl Config {
}
}

pub fn get_list_or_split_string(&self, key: &str)
-> CargoResult<Option<Value<Vec<String>>>> {
match self.get_env::<String>(key) {
Ok(Some(value)) =>
return Ok(Some(Value {
val: value.val.split(' ').map(str::to_string).collect(),
definition: value.definition
})),
Err(err) => return Err(err),
Ok(None) => (),
}

match self.get(key)? {
Some(CV::List(i, path)) => {
Ok(Some(Value {
val: i.into_iter().map(|(s, _)| s).collect(),
definition: Definition::Path(path),
}))
}
Some(CV::String(i, path)) => {
Ok(Some(Value {
val: i.split(' ').map(str::to_string).collect(),
definition: Definition::Path(path),
}))
}
Some(val) => self.expected("list or string", key, val),
None => Ok(None),
}
}

pub fn get_table(&self, key: &str)
-> CargoResult<Option<Value<HashMap<String, CV>>>> {
match self.get(key)? {
Expand Down

0 comments on commit f440704

Please sign in to comment.