i15-anyhow-context #17

Merged
kemitix merged 2 commits from i15-anyhow-context into main 2023-08-13 16:11:16 +01:00
5 changed files with 16 additions and 117 deletions
Showing only changes of commit 750cc830c8 - Show all commits

View file

@ -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(),
}
}
}

View file

@ -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<String> {
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()

View file

@ -1,7 +1,6 @@
use params::Args;
use prelude::*;
mod errors;
pub mod feed;
pub mod file;
pub mod history;

View file

@ -1,3 +1 @@
use crate::errors::Error;
pub type Result<T> = std::result::Result<T, Error>;
pub use anyhow::{anyhow, Context, Error, Result};

View file

@ -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<String, String>) -> 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(),
// ))
// })
// }