Merge pull request #959 from jeremystucki/refactoring

Minor Refactoring
This commit is contained in:
Eric Huss 2019-06-20 20:05:02 -07:00 committed by GitHub
commit a2029f0a78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 23 deletions

View File

@ -343,18 +343,16 @@ impl MDBook {
/// Look at the `Config` and try to figure out what renderers to use. /// Look at the `Config` and try to figure out what renderers to use.
fn determine_renderers(config: &Config) -> Vec<Box<dyn Renderer>> { fn determine_renderers(config: &Config) -> Vec<Box<dyn Renderer>> {
let mut renderers: Vec<Box<dyn Renderer>> = Vec::new(); let mut renderers = Vec::new();
if let Some(output_table) = config.get("output").and_then(Value::as_table) { if let Some(output_table) = config.get("output").and_then(Value::as_table) {
for (key, table) in output_table.iter() { renderers.extend(output_table.iter().map(|(key, table)| {
// the "html" backend has its own Renderer
if key == "html" { if key == "html" {
renderers.push(Box::new(HtmlHandlebars::new())); Box::new(HtmlHandlebars::new()) as Box<dyn Renderer>
} else { } else {
let renderer = interpret_custom_renderer(key, table); interpret_custom_renderer(key, table)
renderers.push(renderer);
} }
} }));
} }
// if we couldn't find anything, add the HTML renderer as a default // if we couldn't find anything, add the HTML renderer as a default

View File

@ -131,10 +131,8 @@ impl Config {
pub fn update_from_env(&mut self) { pub fn update_from_env(&mut self) {
debug!("Updating the config from environment variables"); debug!("Updating the config from environment variables");
let overrides = env::vars().filter_map(|(key, value)| match parse_env(&key) { let overrides =
Some(index) => Some((index, value)), env::vars().filter_map(|(key, value)| parse_env(&key).map(|index| (index, value)));
None => None,
});
for (key, value) in overrides { for (key, value) in overrides {
trace!("{} => {}", key, value); trace!("{} => {}", key, value);
@ -151,14 +149,11 @@ impl Config {
/// `output.html.playpen` will fetch the "playpen" out of the html output /// `output.html.playpen` will fetch the "playpen" out of the html output
/// table). /// table).
pub fn get(&self, key: &str) -> Option<&Value> { pub fn get(&self, key: &str) -> Option<&Value> {
match self.rest.read(key) { self.rest.read(key).unwrap_or(None)
Ok(inner) => inner,
Err(_) => None,
}
} }
/// Fetch a value from the `Config` so you can mutate it. /// Fetch a value from the `Config` so you can mutate it.
pub fn get_mut<'a>(&'a mut self, key: &str) -> Option<&'a mut Value> { pub fn get_mut(&mut self, key: &str) -> Option<&mut Value> {
match self.rest.read_mut(key) { match self.rest.read_mut(key) {
Ok(inner) => inner, Ok(inner) => inner,
Err(_) => None, Err(_) => None,
@ -208,7 +203,7 @@ impl Config {
} else { } else {
self.rest self.rest
.insert(index, value) .insert(index, value)
.map_err(|e| ErrorKind::TomlQueryError(e))?; .map_err(ErrorKind::TomlQueryError)?;
} }
Ok(()) Ok(())
@ -545,12 +540,10 @@ trait Updateable<'de>: Serialize + Deserialize<'de> {
fn update_value<S: Serialize>(&mut self, key: &str, value: S) { fn update_value<S: Serialize>(&mut self, key: &str, value: S) {
let mut raw = Value::try_from(&self).expect("unreachable"); let mut raw = Value::try_from(&self).expect("unreachable");
{ if let Ok(value) = Value::try_from(value) {
if let Ok(value) = Value::try_from(value) { let _ = raw.insert(key, value);
let _ = raw.insert(key, value); } else {
} else { return;
return;
}
} }
if let Ok(updated) = raw.try_into() { if let Ok(updated) = raw.try_into() {