diff options
| author | Jokler <jokler.contact@gmail.com> | 2018-02-25 02:30:53 +0100 |
|---|---|---|
| committer | Jokler <jokler.contact@gmail.com> | 2018-02-25 02:30:53 +0100 |
| commit | 76461addd2fb7ec383123b1efc4368b7662eea04 (patch) | |
| tree | c4784dc665df1f955bfc8c1859e366a4ca9d6640 /src/plugins/url.rs | |
| parent | ac1afe4c7b0f62160f62b848d7f747cb7be74bbd (diff) | |
| download | frippy-76461addd2fb7ec383123b1efc4368b7662eea04.tar.gz frippy-76461addd2fb7ec383123b1efc4368b7662eea04.zip | |
Remove unwraps from the Url plugin
Diffstat (limited to 'src/plugins/url.rs')
| -rw-r--r-- | src/plugins/url.rs | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/src/plugins/url.rs b/src/plugins/url.rs index 52f92d8..b19d4e5 100644 --- a/src/plugins/url.rs +++ b/src/plugins/url.rs @@ -32,12 +32,23 @@ impl Url { Some(captures) => { debug!("Url captures: {:?}", captures); - Some(captures.get(2).unwrap().as_str().to_string()) + Some(captures.get(2)?.as_str().to_string()) } None => None, } } + fn get_title(&self, body: &str) -> Option<String> { + let doc = Document::from(body.as_ref()); + let title = doc.find(Name("title")).next()?; + let title = title.children().next()?; + let title_text = title.as_text()?.trim().replace("\n", "|"); + debug!("Title: {:?}", title); + debug!("Text: {:?}", title_text); + + Some(title_text) + } + fn url(&self, text: &str) -> Result<String, &str> { let url = match self.grep_url(text) { Some(url) => url, @@ -46,16 +57,9 @@ impl Url { match utils::download(self.max_kib, &url) { Some(body) => { - let doc = Document::from(body.as_ref()); - if let Some(title) = doc.find(Name("title")).next() { - let title = title.children().next().unwrap(); - let title_text = title.as_text().unwrap().trim().replace("\n", "|"); - debug!("Title: {:?}", title); - debug!("Text: {:?}", title_text); - - Ok(title_text) - } else { - Err("No title was found.") + match self.get_title(&body) { + Some(title) => Ok(title), + None => Err("No title was found.") } } None => Err("Failed to download document."), @@ -79,7 +83,10 @@ impl Plugin for Url { match message.command { Command::PRIVMSG(_, ref content) => match self.url(content) { Ok(title) => client.send_privmsg(message.response_target().unwrap(), &title), - Err(_) => Ok(()), + Err(e) => { + error!("Url plugin error: {}", e); + Ok(()) + }, }, _ => Ok(()), } |
