clippy fixes
This commit is contained in:
parent
12d44bd3c4
commit
3b8c13e6e7
3 changed files with 21 additions and 23 deletions
|
@ -14,8 +14,10 @@ pub fn find(site: &str, channel_name: &str, e: &NetworkEnv) -> Result<String> {
|
||||||
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)
|
||||||
.context(format!("Fetching channel to find RSS: {}", channel_url))?;
|
.context(format!("Fetching channel to find RSS: {}", channel_url))?;
|
||||||
|
let rss_selector = scraper::Selector::parse("link[title='RSS']")
|
||||||
|
.map_err(|e| anyhow!("Invalid selector: {}", e))?;
|
||||||
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(&rss_selector)
|
||||||
.next()
|
.next()
|
||||||
.context("No RSS link found")?
|
.context("No RSS link found")?
|
||||||
.value()
|
.value()
|
||||||
|
|
11
src/lib.rs
11
src/lib.rs
|
@ -29,11 +29,11 @@ pub fn run(site: &str, a: &Args, e: Env) -> Result<()> {
|
||||||
.context("Fetching channel")?
|
.context("Fetching channel")?
|
||||||
.entries()
|
.entries()
|
||||||
{
|
{
|
||||||
if let Some(link) = entry.links().get(0).cloned() {
|
if let Some(link) = entry.links().first() {
|
||||||
if !history::find(&link, &a.history, &e.file).context("Finding history")? {
|
if !history::find(link, &a.history, &e.file).context("Finding history")? {
|
||||||
println!("Downloading {}: {}", &channel_name, entry.title().as_str());
|
println!("Downloading {}: {}", &channel_name, entry.title().as_str());
|
||||||
(e.network.download_as_mp3)(&link.href).context("Downloading as MP3")?;
|
(e.network.download_as_mp3)(&link.href).context("Downloading as MP3")?;
|
||||||
history::add(&link, &a.history, &e.file).context("Adding to history")?;
|
history::add(link, &a.history, &e.file).context("Adding to history")?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,13 +64,12 @@ mod tests {
|
||||||
|
|
||||||
// two channels in subscriptions.txt
|
// two channels in subscriptions.txt
|
||||||
let subs_file_name = "subs";
|
let subs_file_name = "subs";
|
||||||
let subs_dir =
|
let subs_dir = create_text_file(subs_file_name, b"@channel1\nignore me\n@channel2")?;
|
||||||
create_text_file(subs_file_name, "@channel1\nignore me\n@channel2".as_bytes())?;
|
|
||||||
let subs_file_name = format!("{}/{}", subs_dir.path().to_string_lossy(), subs_file_name);
|
let subs_file_name = format!("{}/{}", subs_dir.path().to_string_lossy(), subs_file_name);
|
||||||
|
|
||||||
// one item from each channel is already listed in the downloads.txt file
|
// one item from each channel is already listed in the downloads.txt file
|
||||||
let history_file_name = "history";
|
let history_file_name = "history";
|
||||||
let history_dir = create_text_file(history_file_name, "c1-f2\nc2-f3".as_bytes())?;
|
let history_dir = create_text_file(history_file_name, b"c1-f2\nc2-f3")?;
|
||||||
|
|
||||||
let history_file_name = format!(
|
let history_file_name = format!(
|
||||||
"{}/{}",
|
"{}/{}",
|
||||||
|
|
|
@ -26,7 +26,10 @@ pub fn create_text_file(name: &str, data: &[u8]) -> Result<TempDir> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_text_file(path: &Path, file_name: &str) -> Result<Vec<String>> {
|
pub fn read_text_file(path: &Path, file_name: &str) -> Result<Vec<String>> {
|
||||||
let file_name = format!("{}/{}", path.to_str().unwrap(), file_name);
|
let path = path
|
||||||
|
.to_str()
|
||||||
|
.ok_or_else(|| anyhow!("Path has non-utf8 character(s)"))?;
|
||||||
|
let file_name = format!("{}/{}", path, file_name);
|
||||||
Ok(read_to_string(file_name)?
|
Ok(read_to_string(file_name)?
|
||||||
.lines()
|
.lines()
|
||||||
.map(String::from)
|
.map(String::from)
|
||||||
|
@ -35,27 +38,21 @@ pub fn read_text_file(path: &Path, file_name: &str) -> Result<Vec<String>> {
|
||||||
pub fn mock_fetch_as_text_with_rss_url(
|
pub fn mock_fetch_as_text_with_rss_url(
|
||||||
map: HashMap<&'static str, &'static str>,
|
map: HashMap<&'static str, &'static str>,
|
||||||
) -> NetworkFetchAsTextFn {
|
) -> NetworkFetchAsTextFn {
|
||||||
Box::new(move |url: &str| match map.get(url) {
|
Box::new(move |url: &str| {
|
||||||
Some(url) => Ok(format!(
|
map.get(url).map_or_else(
|
||||||
r#"
|
|| Err(anyhow!("Unexpected request for {}", url)),
|
||||||
<html>
|
|url| Ok(format!(r#"<html><link title="RSS" href="{}"></html>"#, url)),
|
||||||
<link title="RSS" href="{}">
|
)
|
||||||
</html>
|
|
||||||
"#,
|
|
||||||
url
|
|
||||||
)),
|
|
||||||
None => Err(anyhow!("Unexpected request for {}", url)),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub fn mock_network_fetch_as_bytes_with_rss_entries(
|
pub fn mock_network_fetch_as_bytes_with_rss_entries(
|
||||||
feeds: HashMap<String, String>,
|
feeds: HashMap<String, String>,
|
||||||
) -> NetworkFetchAsBytesFn {
|
) -> NetworkFetchAsBytesFn {
|
||||||
Box::new(move |url| {
|
Box::new(move |url| {
|
||||||
if let Some(feed) = feeds.get(url).cloned() {
|
feeds.get(url).cloned().map_or_else(
|
||||||
Ok(bytes::Bytes::from(feed))
|
|| Err(anyhow!("No mock feed: {}", url)),
|
||||||
} else {
|
|feed| Ok(bytes::Bytes::from(feed)),
|
||||||
Err(anyhow!("No mock feed: {}", url))
|
)
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub fn mock_file_open(real_paths: HashMap<String, String>) -> FileOpenFn {
|
pub fn mock_file_open(real_paths: HashMap<String, String>) -> FileOpenFn {
|
||||||
|
|
Loading…
Add table
Reference in a new issue