Minor cleanup of nested match/if let statements into using and_then().

This commit is contained in:
Jordan Petridis 2017-12-04 14:32:16 +02:00
parent 8bd48a09a6
commit 276169e43d
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
2 changed files with 15 additions and 20 deletions

View File

@ -38,24 +38,7 @@ fn download_into(dir: &str, file_title: &str, url: &str) -> Result<String> {
ct_len.map(|x| info!("File Lenght: {}", x));
ct_type.map(|x| info!("Content Type: {}", x));
// This could be prettier.
// Determine the file extension from the http content-type header.
let ext = if let Some(t) = ct_type {
let mime = mime_guess::get_extensions(t.type_().as_ref(), t.subtype().as_ref());
if let Some(m) = mime {
if m.contains(&t.subtype().as_ref()) {
t.subtype().as_ref().to_string()
} else {
m.first().unwrap().to_string()
}
} else {
error!("Unkown mime type. {}", t);
"unkown".to_string()
}
} else {
error!("Unkown mime type.");
"unkown".to_string()
};
let ext = get_ext(ct_type.cloned()).unwrap_or(String::from("unkown"));
info!("Extension: {}", ext);
// Construct a temp file to save desired content.
@ -74,6 +57,19 @@ fn download_into(dir: &str, file_title: &str, url: &str) -> Result<String> {
Ok(target)
}
// Determine the file extension from the http content-type header.
fn get_ext(content: Option<ContentType>) -> Option<String> {
let cont = content.clone()?;
content.and_then(|c| mime_guess::get_extensions(c.type_().as_ref(), c.subtype().as_ref()))
.and_then(|c| {
if c.contains(&cont.subtype().as_ref()) {
Some(cont.subtype().as_ref().to_string())
} else {
Some(c.first().unwrap().to_string())
}
})
}
// TODO: Write unit-tests.
/// Handles the I/O of fetching a remote file and saving into a Buffer and A File.
fn save_io(file: &str, resp: &mut reqwest::Response, content_lenght: Option<u64>) -> Result<()> {

View File

@ -1,8 +1,6 @@
use gtk;
use gtk::prelude::*;
// use gdk_pixbuf::Pixbuf;
// use diesel::associations::Identifiable;
use hammond_data::Podcast;
use widgets::podcast::PodcastWidget;
@ -89,6 +87,7 @@ fn replace_podcasts(stack: &gtk::Stack, pop: &PopulatedView) {
// old.destroy();
// }
#[allow(dead_code)]
pub fn show_widget(stack: &gtk::Stack) {
stack.set_visible_child_name("widget")
}