for line in String::from_utf8_lossy(&output.stdout).lines() {
if let Some((key,val)) = line.split_once('=') {
key.strip_prefix("alias ").and_then(|alias| {
let mut vals = val.chars();
if val.len() > 2 && vals.next().unwrap() == '\'' && vals.last().unwrap() == '\'' {
aliases.insert(alias.to_string(), val[1..val.len() - 1].split_whitespace().map(str::to_string).collect());
}
None::<()>
}).or_else(|| unsafe { env::set_var(key,val); None::<_>} );
}
}
Specifically, I do not like returning None, it looks weird. However using a simple if makes the code even worse. Another thing I do not like - it's the way of trimming quotes. Any other code improvement suggestions are always welcome.
for line in String::from_utf8_lossy(&output.stdout).lines() {
if let Some((key, val)) = line.split_once('=') {
if let Some(key) = key.strip_prefix("alias ") {
set_alias(&mut aliases, key, val);
} else {
set_env(key, val);
}
}
}
unlike C, rust does support function definitions within functions, so I don't see why one wound seek to avoid small utility functions, if they help improve readability.