diff options
Diffstat (limited to 'src/manager.rs')
-rw-r--r-- | src/manager.rs | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/src/manager.rs b/src/manager.rs index 5330d80..83e8c1b 100644 --- a/src/manager.rs +++ b/src/manager.rs | |||
@@ -13,8 +13,8 @@ pub struct Manager { | |||
13 | } | 13 | } |
14 | 14 | ||
15 | impl Manager { | 15 | impl Manager { |
16 | fn add_feed(&mut self, url: &str) -> Result<&mut Feed, Error> { | 16 | fn add_feed<P: AsRef<str>>(&mut self, url: P) -> Result<(), Error> { |
17 | let link = Url::parse(&url).map_err(|e| AddError::InvalidUrl(e.to_string()))?; | 17 | let link = Url::parse(url.as_ref()).map_err(|e| AddError::InvalidUrl(e.to_string()))?; |
18 | 18 | ||
19 | // check if this feed is already present | 19 | // check if this feed is already present |
20 | if self.feeds.iter().any(|f| f.link == link) { | 20 | if self.feeds.iter().any(|f| f.link == link) { |
@@ -27,19 +27,31 @@ impl Manager { | |||
27 | // add new feed | 27 | // add new feed |
28 | self.feeds.push(feed); | 28 | self.feeds.push(feed); |
29 | 29 | ||
30 | Ok(self.feeds.last_mut().unwrap()) | 30 | Ok(()) |
31 | } | 31 | } |
32 | 32 | ||
33 | pub async fn add_feed_and_pull(&mut self, url: &str) -> Result<PullStatus, Error> { | 33 | pub async fn add_feed_and_pull(&mut self, url: &str) -> Result<PullStatus, Error> { |
34 | let feed = self.add_feed(url)?; | 34 | self.add_feed(url)?; |
35 | let feed = self.feeds.last_mut().unwrap(); | ||
35 | feed.pull() | 36 | feed.pull() |
36 | .await | 37 | .await |
37 | .map_err(|pull_err| Error::Pull(feed.link.clone(), pull_err)) | 38 | .map_err(|pull_err| Error::Pull(feed.link.clone(), pull_err)) |
38 | } | 39 | } |
39 | 40 | ||
40 | pub async fn add_feeds_and_pull(&mut self, urls: &[&str]) -> Vec<Result<PullStatus, Error>> { | 41 | pub async fn add_feeds_and_pull<P: AsRef<str>>( |
41 | let add_results = urls.into_iter().map(|url| self.add_feed(&url)); | 42 | &mut self, |
42 | self.pull() | 43 | urls: &[P], |
44 | ) -> Vec<Result<PullStatus, Error>> { | ||
45 | for url in urls { | ||
46 | // TODO: handle this error | ||
47 | self.add_feed(&url); | ||
48 | } | ||
49 | futures::future::join_all(self.feeds.iter_mut().map(|feed| async { | ||
50 | feed.pull() | ||
51 | .await | ||
52 | .map_err(|pull_err| Error::Pull(feed.link.clone(), pull_err)) | ||
53 | })) | ||
54 | .await | ||
43 | } | 55 | } |
44 | 56 | ||
45 | pub async fn pull(&mut self) -> Vec<Result<PullStatus, PullError>> { | 57 | pub async fn pull(&mut self) -> Vec<Result<PullStatus, PullError>> { |