use a function generators for NetworkEnv
This commit is contained in:
parent
b5a3f5ece2
commit
e54c136f5a
5 changed files with 60 additions and 30 deletions
|
@ -23,16 +23,18 @@ pub fn find(site: &str, channel_name: &str, e: &NetworkEnv) -> Result<String> {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use crate::errors::Error;
|
||||
use crate::test_utils::{
|
||||
mock_fetch_as_text_with_rss_url, stub_download_as_mp3, stub_fetch_as_bytes,
|
||||
};
|
||||
|
||||
use super::*;
|
||||
#[test]
|
||||
fn finds_rss_url() -> Result<()> {
|
||||
//given
|
||||
let network_env = NetworkEnv {
|
||||
fetch_as_text: dummy_fetch_as_text,
|
||||
fetch_as_bytes: dummy_fetch_as_bytes,
|
||||
download_as_mp3: dummy_download_as_mp3,
|
||||
fetch_as_text: mock_fetch_as_text_with_rss_url("the-rss-url"),
|
||||
fetch_as_bytes: stub_fetch_as_bytes(),
|
||||
download_as_mp3: stub_download_as_mp3(),
|
||||
};
|
||||
//when
|
||||
let result = find("site", "@channel", &network_env)?;
|
||||
|
@ -45,9 +47,9 @@ mod tests {
|
|||
fn error_if_channel_name_is_invalid() -> Result<()> {
|
||||
//given
|
||||
let network_env = NetworkEnv {
|
||||
fetch_as_text: dummy_fetch_as_text,
|
||||
fetch_as_bytes: dummy_fetch_as_bytes,
|
||||
download_as_mp3: dummy_download_as_mp3,
|
||||
fetch_as_text: mock_fetch_as_text_with_rss_url("the-rss-url"),
|
||||
fetch_as_bytes: stub_fetch_as_bytes(),
|
||||
download_as_mp3: stub_download_as_mp3(),
|
||||
};
|
||||
//when
|
||||
let result = find("site", "invalid-channel-name", &network_env);
|
||||
|
@ -55,19 +57,4 @@ mod tests {
|
|||
assert!(result.is_err());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn dummy_fetch_as_text(_url: &str) -> Result<String> {
|
||||
Ok(r#"
|
||||
<html>
|
||||
<link title="RSS" href="the-rss-url">
|
||||
</html>
|
||||
"#
|
||||
.to_string())
|
||||
}
|
||||
fn dummy_fetch_as_bytes(_url: &str) -> Result<bytes::Bytes> {
|
||||
Err(Error::message("Not implemented"))
|
||||
}
|
||||
fn dummy_download_as_mp3(_url: &str) -> Result<()> {
|
||||
Err(Error::message("Not implemented"))
|
||||
}
|
||||
}
|
||||
|
|
12
src/lib.rs
12
src/lib.rs
|
@ -34,3 +34,15 @@ pub fn run(subscriptions: &str, history: &str, site: &str, e: Env) -> Result<()>
|
|||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn downloads_two_items_from_two_feeds_ignoring_existing_items_in_history() -> Result<()> {
|
||||
//given
|
||||
//when
|
||||
//then
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,17 +2,23 @@ use std::process::Command;
|
|||
|
||||
use crate::prelude::*;
|
||||
|
||||
pub type NetworkFetchAsTextFn = Box<dyn Fn(&str) -> Result<String>>;
|
||||
|
||||
pub type NetworkFetchAsBytesFn = Box<dyn Fn(&str) -> Result<bytes::Bytes>>;
|
||||
|
||||
pub type NetworkDownloadAsMp3Fn = Box<dyn Fn(&str) -> Result<()>>;
|
||||
|
||||
pub struct NetworkEnv {
|
||||
pub fetch_as_text: fn(url: &str) -> Result<String>,
|
||||
pub fetch_as_bytes: fn(url: &str) -> Result<bytes::Bytes>,
|
||||
pub download_as_mp3: fn(url: &str) -> Result<()>,
|
||||
pub fetch_as_text: NetworkFetchAsTextFn,
|
||||
pub fetch_as_bytes: NetworkFetchAsBytesFn,
|
||||
pub download_as_mp3: NetworkDownloadAsMp3Fn,
|
||||
}
|
||||
impl Default for NetworkEnv {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
fetch_as_text: |url| Ok(reqwest::blocking::get(url)?.text()?),
|
||||
fetch_as_bytes: |url| Ok(reqwest::blocking::get(url)?.bytes()?),
|
||||
download_as_mp3: |url| {
|
||||
fetch_as_text: Box::new(|url| Ok(reqwest::blocking::get(url)?.text()?)),
|
||||
fetch_as_bytes: Box::new(|url| Ok(reqwest::blocking::get(url)?.bytes()?)),
|
||||
download_as_mp3: Box::new(|url| {
|
||||
let cmd = "yt-dlp";
|
||||
// println!("{} --extract-audio --audio-format mp3 {}", cmd, &link.href);
|
||||
let output = Command::new(cmd)
|
||||
|
@ -26,7 +32,7 @@ impl Default for NetworkEnv {
|
|||
println!("{}", String::from_utf8(output.stdout)?);
|
||||
}
|
||||
Ok(())
|
||||
},
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
mod env;
|
||||
|
||||
pub use env::NetworkDownloadAsMp3Fn;
|
||||
pub use env::NetworkEnv;
|
||||
pub use env::NetworkFetchAsBytesFn;
|
||||
pub use env::NetworkFetchAsTextFn;
|
||||
|
|
|
@ -6,7 +6,11 @@ use std::{
|
|||
|
||||
use tempfile::{tempdir, TempDir};
|
||||
|
||||
use crate::prelude::*;
|
||||
use crate::{
|
||||
errors::Error,
|
||||
network::{NetworkDownloadAsMp3Fn, NetworkFetchAsBytesFn, NetworkFetchAsTextFn},
|
||||
prelude::*,
|
||||
};
|
||||
|
||||
pub fn create_text_file(name: &str, data: &[u8]) -> Result<(TempDir, String)> {
|
||||
let data = from_utf8(data)?;
|
||||
|
@ -23,3 +27,21 @@ pub fn read_text_file(file_name: &str) -> Result<Vec<String>> {
|
|||
.map(String::from)
|
||||
.collect())
|
||||
}
|
||||
pub fn mock_fetch_as_text_with_rss_url(url: &'static str) -> NetworkFetchAsTextFn {
|
||||
Box::new(move |_url: &str| {
|
||||
Ok(format!(
|
||||
r#"
|
||||
<html>
|
||||
<link title="RSS" href="{}">
|
||||
</html>
|
||||
"#,
|
||||
url
|
||||
))
|
||||
})
|
||||
}
|
||||
pub fn stub_fetch_as_bytes() -> NetworkFetchAsBytesFn {
|
||||
Box::new(|_url: &str| Err(Error::message("Not implemented")))
|
||||
}
|
||||
pub fn stub_download_as_mp3() -> NetworkDownloadAsMp3Fn {
|
||||
Box::new(|_url: &str| Err(Error::message("Not implemented")))
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue