Flatten implementation of net::file_loader::factory.

This commit is contained in:
Corey Farwell 2016-04-15 09:11:43 -04:00
parent 6c5cfb738a
commit 50c6d937a5

View file

@ -72,54 +72,54 @@ pub fn factory(load_data: LoadData,
cancel_listener: CancellationListener) { cancel_listener: CancellationListener) {
assert!(&*load_data.url.scheme == "file"); assert!(&*load_data.url.scheme == "file");
spawn_named("file_loader".to_owned(), move || { spawn_named("file_loader".to_owned(), move || {
let file_path: Result<PathBuf, ()> = load_data.url.to_file_path(); let file_path = match load_data.url.to_file_path() {
match file_path { Ok(file_path) => file_path,
Ok(file_path) => { Err(_) => {
match File::open(&file_path) { send_error(load_data.url, "Could not parse path".to_owned(), senders);
Ok(ref mut reader) => { return;
if cancel_listener.is_cancelled() { },
if let Ok(progress_chan) = get_progress_chan(load_data, file_path, };
senders, classifier, &[]) { let mut file = File::open(&file_path);
let _ = progress_chan.send(Done(Err("load cancelled".to_owned()))); let reader = match file {
} Ok(ref mut reader) => reader,
return; Err(_) => {
} // this should be one of the three errors listed in
match read_block(reader) { // http://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.open
Ok(ReadStatus::Partial(buf)) => { // but, we'll go for a "file not found!"
let progress_chan = get_progress_chan(load_data, file_path, let url = Url::parse("about:not-found").unwrap();
senders, classifier, &buf).ok().unwrap(); let load_data_404 = LoadData::new(load_data.context, url, None);
progress_chan.send(Payload(buf)).unwrap(); about_loader::factory(load_data_404, senders, classifier, cancel_listener);
let read_result = read_all(reader, &progress_chan, &cancel_listener); return;
if let Ok(load_result) = read_result { }
match load_result { };
LoadResult::Cancelled => return, if cancel_listener.is_cancelled() {
LoadResult::Finished => progress_chan.send(Done(Ok(()))).unwrap(), if let Ok(progress_chan) = get_progress_chan(load_data, file_path,
} senders, classifier, &[]) {
} let _ = progress_chan.send(Done(Err("load cancelled".to_owned())));
} }
Ok(ReadStatus::EOF) => { return;
if let Ok(chan) = get_progress_chan(load_data, file_path, }
senders, classifier, &[]) { match read_block(reader) {
let _ = chan.send(Done(Ok(()))); Ok(ReadStatus::Partial(buf)) => {
} let progress_chan = get_progress_chan(load_data, file_path,
} senders, classifier, &buf).ok().unwrap();
Err(e) => { progress_chan.send(Payload(buf)).unwrap();
send_error(load_data.url, e, senders); let read_result = read_all(reader, &progress_chan, &cancel_listener);
} if let Ok(load_result) = read_result {
}; match load_result {
} LoadResult::Cancelled => return,
Err(_) => { LoadResult::Finished => progress_chan.send(Done(Ok(()))).unwrap(),
// this should be one of the three errors listed in
// http://doc.rust-lang.org/std/fs/struct.OpenOptions.html#method.open
// but, we'll go for a "file not found!"
let url = Url::parse("about:not-found").unwrap();
let load_data_404 = LoadData::new(load_data.context, url, None);
about_loader::factory(load_data_404, senders, classifier, cancel_listener)
} }
} }
} }
Err(_) => { Ok(ReadStatus::EOF) => {
send_error(load_data.url, "Could not parse path".to_owned(), senders); if let Ok(chan) = get_progress_chan(load_data, file_path,
senders, classifier, &[]) {
let _ = chan.send(Done(Ok(())));
}
}
Err(e) => {
send_error(load_data.url, e, senders);
} }
} }
}); });