summaryrefslogtreecommitdiffstats
path: root/examples/download_progress/src/download.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-03-23 20:43:55 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-03-23 20:43:55 +0100
commit0d719bbdf336a022c073986e1e5a91cf632a270c (patch)
tree912103234bf134221e44fc6405c76beabfc19e50 /examples/download_progress/src/download.rs
parentb92e1f957408e3254e5fe0da389808474de6c4a9 (diff)
downloadiced-0d719bbdf336a022c073986e1e5a91cf632a270c.tar.gz
iced-0d719bbdf336a022c073986e1e5a91cf632a270c.tar.bz2
iced-0d719bbdf336a022c073986e1e5a91cf632a270c.zip
Handle errors in `download_progress` example
Diffstat (limited to 'examples/download_progress/src/download.rs')
-rw-r--r--examples/download_progress/src/download.rs38
1 files changed, 27 insertions, 11 deletions
diff --git a/examples/download_progress/src/download.rs b/examples/download_progress/src/download.rs
index 0562f54d..96e1dc28 100644
--- a/examples/download_progress/src/download.rs
+++ b/examples/download_progress/src/download.rs
@@ -35,15 +35,23 @@ where
let response = reqwest::get(&url).await;
match response {
- Ok(response) => Some((
- Progress::Started,
- State::Downloading {
- total: response.content_length().unwrap(),
- downloaded: 0,
- response,
- },
- )),
- Err(_) => None,
+ Ok(response) => {
+ if let Some(total) = response.content_length() {
+ Some((
+ Progress::Started,
+ State::Downloading {
+ response,
+ total,
+ downloaded: 0,
+ },
+ ))
+ } else {
+ Some((Progress::Errored, State::Finished))
+ }
+ }
+ Err(_) => {
+ Some((Progress::Errored, State::Finished))
+ }
}
}
State::Downloading {
@@ -67,9 +75,16 @@ where
))
}
Ok(None) => Some((Progress::Finished, State::Finished)),
- Err(_) => None,
+ Err(_) => Some((Progress::Errored, State::Finished)),
},
- State::Finished => None,
+ State::Finished => {
+ // We do not let the stream die, as it would start a
+ // new download repeatedly if the user is not careful
+ // in case of errors.
+ let _: () = iced::futures::future::pending().await;
+
+ None
+ }
}
},
))
@@ -81,6 +96,7 @@ pub enum Progress {
Started,
Advanced(f32),
Finished,
+ Errored,
}
pub enum State {