diff --git a/src/errors.rs b/src/errors.rs deleted file mode 100644 index 149c8e4..0000000 --- a/src/errors.rs +++ /dev/null @@ -1,79 +0,0 @@ -use std::{str::Utf8Error, string::FromUtf8Error}; - -#[derive(Debug)] -pub struct Error { - pub details: String, - pub source: String, -} -impl Error { - pub fn message(details: &str) -> Self { - Self { - details: details.to_string(), - source: "(not provided)".to_string(), - } - } -} -impl From for Error { - fn from(value: anyhow::Error) -> Self { - Self { - details: value.to_string(), - source: value.source().unwrap().to_string(), - } - } -} -impl From for Error { - fn from(value: Utf8Error) -> Self { - Self { - details: value.to_string(), - source: "Utf8Error".to_string(), - } - } -} -impl From for Error { - fn from(value: FromUtf8Error) -> Self { - Self { - details: value.to_string(), - source: "FromUtf8Error".to_string(), - } - } -} -impl From for Error { - fn from(details: String) -> Self { - Self { - details, - source: "String".to_string(), - } - } -} -impl From for Error { - fn from(value: std::io::Error) -> Self { - Self { - details: value.to_string(), - source: "std::io::Error".to_string(), - } - } -} -impl From> for Error { - fn from(value: std::sync::mpsc::SendError) -> Self { - Self { - details: value.to_string(), - source: "std::sync::mpsc::SendError".to_string(), - } - } -} -impl From for Error { - fn from(value: atom_syndication::Error) -> Self { - Self { - details: value.to_string(), - source: "atom_syndication::Error".to_string(), - } - } -} -impl From for Error { - fn from(value: reqwest::Error) -> Self { - Self { - details: value.to_string(), - source: "reqwest::Error".to_string(), - } - } -} diff --git a/src/feed/find.rs b/src/feed/find.rs index 154c596..dbb1772 100644 --- a/src/feed/find.rs +++ b/src/feed/find.rs @@ -1,14 +1,19 @@ -use crate::network::NetworkEnv; use crate::prelude::*; +use crate::network::NetworkEnv; + pub fn find(site: &str, channel_name: &str, e: &NetworkEnv) -> Result { if let Some(channel_prefix) = channel_name.chars().next() { if channel_prefix != '@' { - return Err(format!("Channel Name must begin with an '@': {}", channel_name).into()); + return Err(anyhow!( + "Channel Name must begin with an '@': {}", + channel_name + )); } } let channel_url = format!("{}{}", site, channel_name); - let response = (e.fetch_as_text)(&channel_url)?; + let response = + (e.fetch_as_text)(&channel_url).with_context(|| format!("Fetching {}", channel_url))?; let rss_url = scraper::Html::parse_document(&response) .select(&scraper::Selector::parse("link[title='RSS']").unwrap()) .next() diff --git a/src/lib.rs b/src/lib.rs index c28d6c0..6398a93 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,6 @@ use params::Args; use prelude::*; -mod errors; pub mod feed; pub mod file; pub mod history; diff --git a/src/prelude.rs b/src/prelude.rs index 476ec26..9ead7ae 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -1,3 +1 @@ -use crate::errors::Error; - -pub type Result = std::result::Result; +pub use anyhow::{anyhow, Context, Error, Result}; diff --git a/src/test_utils.rs b/src/test_utils.rs index 8f58ea3..31f6e7d 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -11,7 +11,6 @@ use anyhow::Context; use tempfile::{tempdir, TempDir}; use crate::{ - errors::Error, file::{FileAppendLineFn, FileOpenFn}, network::{NetworkDownloadAsMp3Fn, NetworkFetchAsBytesFn, NetworkFetchAsTextFn}, prelude::*, @@ -45,9 +44,7 @@ pub fn mock_fetch_as_text_with_rss_url( "#, url )), - None => Err(Error::message( - format!("Unexpected request for {}", url).as_str(), - )), + None => Err(anyhow!("Unexpected request for {}", url)), }) } pub fn mock_network_fetch_as_bytes_with_rss_entries( @@ -57,7 +54,7 @@ pub fn mock_network_fetch_as_bytes_with_rss_entries( if let Some(feed) = feeds.get(url).cloned() { Ok(bytes::Bytes::from(feed)) } else { - Err(Error::message(format!("No mock feed: {}", url).as_str())) + Err(anyhow!("No mock feed: {}", url)) } }) } @@ -68,8 +65,9 @@ pub fn mock_file_open(real_paths: HashMap) -> FileOpenFn { format!("test_utils/mock_file_open: path={path}, real_path={real_path}, path_map=[{:?}]", real_paths) })?) } else { - Err(Error::message( - format!("Not implemented: test_utils/mock_file_open: {}", path).as_str(), + Err(anyhow!( + "Not implemented: test_utils/mock_file_open: {}", + path )) } }) @@ -87,30 +85,8 @@ pub fn mock_file_append_line() -> FileAppendLineFn { } pub fn stub_network_fetch_as_bytes() -> NetworkFetchAsBytesFn { - Box::new(|url: &str| { - Err(Error::message( - format!("Not implemented: network_fetch_as_bytes: {}", url).as_str(), - )) - }) + Box::new(|url: &str| Err(anyhow!("Not implemented: network_fetch_as_bytes: {}", url))) } pub fn stub_network_download_as_mp3() -> NetworkDownloadAsMp3Fn { - Box::new(|url: &str| { - Err(Error::message( - format!("Not implemented: network_download_as_mp3: {}", url).as_str(), - )) - }) + Box::new(|url: &str| Err(anyhow!("Not implemented: network_download_as_mp3: {}", url))) } -// pub fn stub_file_open() -> FileOpenFn { -// Box::new(|path: &str| { -// Err(Error::message( -// format!("Not implemented: file_open: {}", path).as_str(), -// )) -// }) -// } -// pub fn stub_file_append_line() -> FileAppendLineFn { -// Box::new(|path: &str, line: &str| { -// Err(Error::message( -// format!("Not implemented: file_append_line: {} to {}", line, path).as_str(), -// )) -// }) -// }