Simplify match nesting in theme with if let

This commit is contained in:
mdinger 2015-09-12 22:51:28 -04:00
parent 37b68c41f7
commit c113c2eb31
1 changed files with 16 additions and 41 deletions

View File

@ -34,7 +34,7 @@ pub struct Theme {
} }
impl Theme { impl Theme {
pub fn new(src: &Path) -> Self{ pub fn new(src: &Path) -> Self {
// Default theme // Default theme
let mut theme = Theme { let mut theme = Theme {
@ -60,58 +60,33 @@ impl Theme {
// Check for individual files if they exist // Check for individual files if they exist
// index.hbs // index.hbs
match File::open(&src.join("index.hbs")) { if let Ok(mut f) = File::open(&src.join("index.hbs")) {
Ok(mut f) => {
theme.index.clear(); // Reset the value, because read_to_string appends... theme.index.clear(); // Reset the value, because read_to_string appends...
match f.read_to_end(&mut theme.index) { let _ = f.read_to_end(&mut theme.index);
_ => {}
};
},
_ => {},
} }
// book.js // book.js
match File::open(&src.join("book.js")) { if let Ok(mut f) = File::open(&src.join("book.js")) {
Ok(mut f) => {
theme.js.clear(); theme.js.clear();
match f.read_to_end(&mut theme.js){ let _ = f.read_to_end(&mut theme.js);
_ => {}
}
},
_ => {},
} }
// book.css // book.css
match File::open(&src.join("book.css")) { if let Ok(mut f) = File::open(&src.join("book.css")) {
Ok(mut f) => {
theme.css.clear(); theme.css.clear();
match f.read_to_end(&mut theme.css) { let _ = f.read_to_end(&mut theme.css);
_ => {}
}
},
_ => {},
} }
// highlight.js // highlight.js
match File::open(&src.join("highlight.js")) { if let Ok(mut f) = File::open(&src.join("highlight.js")) {
Ok(mut f) => {
theme.highlight_js.clear(); theme.highlight_js.clear();
match f.read_to_end(&mut theme.highlight_js) { let _ = f.read_to_end(&mut theme.highlight_js);
_ => {}
}
},
_ => {},
} }
// highlight.css // highlight.css
match File::open(&src.join("highlight.css")) { if let Ok(mut f) = File::open(&src.join("highlight.css")) {
Ok(mut f) => {
theme.highlight_css.clear(); theme.highlight_css.clear();
match f.read_to_end(&mut theme.highlight_css) { let _ = f.read_to_end(&mut theme.highlight_css);
_ => {}
}
},
_ => {},
} }
theme theme