swap in anyhow replacing local Error and Result
This commit is contained in:
parent
d305a2bb1b
commit
750cc830c8
5 changed files with 16 additions and 117 deletions
|
@ -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<anyhow::Error> for Error {
|
|
||||||
fn from(value: anyhow::Error) -> Self {
|
|
||||||
Self {
|
|
||||||
details: value.to_string(),
|
|
||||||
source: value.source().unwrap().to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl From<Utf8Error> for Error {
|
|
||||||
fn from(value: Utf8Error) -> Self {
|
|
||||||
Self {
|
|
||||||
details: value.to_string(),
|
|
||||||
source: "Utf8Error".to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl From<FromUtf8Error> for Error {
|
|
||||||
fn from(value: FromUtf8Error) -> Self {
|
|
||||||
Self {
|
|
||||||
details: value.to_string(),
|
|
||||||
source: "FromUtf8Error".to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl From<String> for Error {
|
|
||||||
fn from(details: String) -> Self {
|
|
||||||
Self {
|
|
||||||
details,
|
|
||||||
source: "String".to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl From<std::io::Error> for Error {
|
|
||||||
fn from(value: std::io::Error) -> Self {
|
|
||||||
Self {
|
|
||||||
details: value.to_string(),
|
|
||||||
source: "std::io::Error".to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl From<std::sync::mpsc::SendError<String>> for Error {
|
|
||||||
fn from(value: std::sync::mpsc::SendError<String>) -> Self {
|
|
||||||
Self {
|
|
||||||
details: value.to_string(),
|
|
||||||
source: "std::sync::mpsc::SendError".to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl From<atom_syndication::Error> for Error {
|
|
||||||
fn from(value: atom_syndication::Error) -> Self {
|
|
||||||
Self {
|
|
||||||
details: value.to_string(),
|
|
||||||
source: "atom_syndication::Error".to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl From<reqwest::Error> for Error {
|
|
||||||
fn from(value: reqwest::Error) -> Self {
|
|
||||||
Self {
|
|
||||||
details: value.to_string(),
|
|
||||||
source: "reqwest::Error".to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,14 +1,19 @@
|
||||||
use crate::network::NetworkEnv;
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
use crate::network::NetworkEnv;
|
||||||
|
|
||||||
pub fn find(site: &str, channel_name: &str, e: &NetworkEnv) -> Result<String> {
|
pub fn find(site: &str, channel_name: &str, e: &NetworkEnv) -> Result<String> {
|
||||||
if let Some(channel_prefix) = channel_name.chars().next() {
|
if let Some(channel_prefix) = channel_name.chars().next() {
|
||||||
if channel_prefix != '@' {
|
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 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)
|
let rss_url = scraper::Html::parse_document(&response)
|
||||||
.select(&scraper::Selector::parse("link[title='RSS']").unwrap())
|
.select(&scraper::Selector::parse("link[title='RSS']").unwrap())
|
||||||
.next()
|
.next()
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use params::Args;
|
use params::Args;
|
||||||
use prelude::*;
|
use prelude::*;
|
||||||
|
|
||||||
mod errors;
|
|
||||||
pub mod feed;
|
pub mod feed;
|
||||||
pub mod file;
|
pub mod file;
|
||||||
pub mod history;
|
pub mod history;
|
||||||
|
|
|
@ -1,3 +1 @@
|
||||||
use crate::errors::Error;
|
pub use anyhow::{anyhow, Context, Error, Result};
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, Error>;
|
|
||||||
|
|
|
@ -11,7 +11,6 @@ use anyhow::Context;
|
||||||
use tempfile::{tempdir, TempDir};
|
use tempfile::{tempdir, TempDir};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
errors::Error,
|
|
||||||
file::{FileAppendLineFn, FileOpenFn},
|
file::{FileAppendLineFn, FileOpenFn},
|
||||||
network::{NetworkDownloadAsMp3Fn, NetworkFetchAsBytesFn, NetworkFetchAsTextFn},
|
network::{NetworkDownloadAsMp3Fn, NetworkFetchAsBytesFn, NetworkFetchAsTextFn},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
|
@ -45,9 +44,7 @@ pub fn mock_fetch_as_text_with_rss_url(
|
||||||
"#,
|
"#,
|
||||||
url
|
url
|
||||||
)),
|
)),
|
||||||
None => Err(Error::message(
|
None => Err(anyhow!("Unexpected request for {}", url)),
|
||||||
format!("Unexpected request for {}", url).as_str(),
|
|
||||||
)),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub fn mock_network_fetch_as_bytes_with_rss_entries(
|
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() {
|
if let Some(feed) = feeds.get(url).cloned() {
|
||||||
Ok(bytes::Bytes::from(feed))
|
Ok(bytes::Bytes::from(feed))
|
||||||
} else {
|
} 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<String, String>) -> FileOpenFn {
|
||||||
format!("test_utils/mock_file_open: path={path}, real_path={real_path}, path_map=[{:?}]", real_paths)
|
format!("test_utils/mock_file_open: path={path}, real_path={real_path}, path_map=[{:?}]", real_paths)
|
||||||
})?)
|
})?)
|
||||||
} else {
|
} else {
|
||||||
Err(Error::message(
|
Err(anyhow!(
|
||||||
format!("Not implemented: test_utils/mock_file_open: {}", path).as_str(),
|
"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 {
|
pub fn stub_network_fetch_as_bytes() -> NetworkFetchAsBytesFn {
|
||||||
Box::new(|url: &str| {
|
Box::new(|url: &str| Err(anyhow!("Not implemented: network_fetch_as_bytes: {}", url)))
|
||||||
Err(Error::message(
|
|
||||||
format!("Not implemented: network_fetch_as_bytes: {}", url).as_str(),
|
|
||||||
))
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
pub fn stub_network_download_as_mp3() -> NetworkDownloadAsMp3Fn {
|
pub fn stub_network_download_as_mp3() -> NetworkDownloadAsMp3Fn {
|
||||||
Box::new(|url: &str| {
|
Box::new(|url: &str| Err(anyhow!("Not implemented: network_download_as_mp3: {}", url)))
|
||||||
Err(Error::message(
|
|
||||||
format!("Not implemented: network_download_as_mp3: {}", url).as_str(),
|
|
||||||
))
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
// 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(),
|
|
||||||
// ))
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue