extracted File::open use into FileEnv
This commit is contained in:
parent
92e369beab
commit
5931014636
6 changed files with 37 additions and 18 deletions
13
src/file.rs
Normal file
13
src/file.rs
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
use std::fs::File;
|
||||||
|
|
||||||
|
pub struct FileEnv {
|
||||||
|
pub open: FileOpen,
|
||||||
|
}
|
||||||
|
impl Default for FileEnv {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
open: |path| File::open(path),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub type FileOpen = fn(path: &str) -> std::io::Result<File>;
|
|
@ -1,11 +1,10 @@
|
||||||
use crate::prelude::*;
|
use crate::{file::FileEnv, prelude::*};
|
||||||
|
|
||||||
use atom_syndication::Link;
|
use atom_syndication::Link;
|
||||||
use std::fs::File;
|
|
||||||
use std::io::{BufRead, BufReader};
|
use std::io::{BufRead, BufReader};
|
||||||
|
|
||||||
pub fn find(link: &Link, file_name: &str) -> Result<bool> {
|
pub fn find(link: &Link, file_name: &str, e: &FileEnv) -> Result<bool> {
|
||||||
if let Ok(file) = File::open(file_name) {
|
if let Ok(file) = (e.open)(file_name) {
|
||||||
let reader = BufReader::new(file);
|
let reader = BufReader::new(file);
|
||||||
for line in reader.lines() {
|
for line in reader.lines() {
|
||||||
if line? == link.href {
|
if line? == link.href {
|
||||||
|
@ -34,9 +33,8 @@ mod test {
|
||||||
title: None,
|
title: None,
|
||||||
length: None,
|
length: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
//when
|
//when
|
||||||
let result = find(&link, &file_name)?;
|
let result = find(&link, &file_name, &FileEnv::default())?;
|
||||||
|
|
||||||
//then
|
//then
|
||||||
drop(dir);
|
drop(dir);
|
||||||
|
@ -61,7 +59,7 @@ mod test {
|
||||||
};
|
};
|
||||||
|
|
||||||
//when
|
//when
|
||||||
let result = find(&link, &file_name)?;
|
let result = find(&link, &file_name, &FileEnv::default())?;
|
||||||
|
|
||||||
//then
|
//then
|
||||||
drop(dir);
|
drop(dir);
|
||||||
|
@ -88,7 +86,7 @@ mod test {
|
||||||
};
|
};
|
||||||
|
|
||||||
//when
|
//when
|
||||||
let result = find(&link, &file_name)?;
|
let result = find(&link, &file_name, &FileEnv::default())?;
|
||||||
|
|
||||||
//then
|
//then
|
||||||
drop(dir);
|
drop(dir);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::prelude::*;
|
use crate::{file::FileEnv, prelude::*};
|
||||||
|
|
||||||
mod add;
|
mod add;
|
||||||
mod find;
|
mod find;
|
||||||
|
@ -13,5 +13,5 @@ pub struct HistoryEnv {
|
||||||
pub add: HistoryAdd,
|
pub add: HistoryAdd,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type HistoryFind = fn(&Link, &str) -> Result<bool>;
|
pub type HistoryFind = fn(&Link, &str, &FileEnv) -> Result<bool>;
|
||||||
pub type HistoryAdd = fn(&Link, &str) -> Result<()>;
|
pub type HistoryAdd = fn(&Link, &str) -> Result<()>;
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
mod errors;
|
mod errors;
|
||||||
pub mod feed;
|
pub mod feed;
|
||||||
pub mod fetch;
|
pub mod fetch;
|
||||||
|
pub mod file;
|
||||||
pub mod history;
|
pub mod history;
|
||||||
pub mod prelude;
|
pub mod prelude;
|
||||||
mod subscriptions;
|
mod subscriptions;
|
||||||
|
@ -10,6 +11,7 @@ mod test_utils;
|
||||||
|
|
||||||
use feed::FeedEnv;
|
use feed::FeedEnv;
|
||||||
use fetch::FetchEnv;
|
use fetch::FetchEnv;
|
||||||
|
use file::FileEnv;
|
||||||
use history::HistoryEnv;
|
use history::HistoryEnv;
|
||||||
use prelude::*;
|
use prelude::*;
|
||||||
|
|
||||||
|
@ -17,15 +19,16 @@ pub struct Env {
|
||||||
pub feed: FeedEnv,
|
pub feed: FeedEnv,
|
||||||
pub history: HistoryEnv,
|
pub history: HistoryEnv,
|
||||||
pub fetch: FetchEnv,
|
pub fetch: FetchEnv,
|
||||||
|
pub file: FileEnv,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(subscriptions: &str, history: &str, site: &str, e: Env) -> Result<()> {
|
pub fn run(subscriptions: &str, history: &str, site: &str, e: Env) -> Result<()> {
|
||||||
for channel_name in subscriptions::lines_from(subscriptions)? {
|
for channel_name in subscriptions::lines_from(subscriptions, &e.file)? {
|
||||||
println!("Channel: {}", channel_name);
|
println!("Channel: {}", channel_name);
|
||||||
let feed_url = (e.feed.find)(site, &channel_name, &e.fetch.get)?;
|
let feed_url = (e.feed.find)(site, &channel_name, &e.fetch.get)?;
|
||||||
for entry in (e.feed.get)(&feed_url)?.entries() {
|
for entry in (e.feed.get)(&feed_url)?.entries() {
|
||||||
if let Some(link) = entry.links().get(0).cloned() {
|
if let Some(link) = entry.links().get(0).cloned() {
|
||||||
if !(e.history.find)(&link, history)? {
|
if !(e.history.find)(&link, history, &e.file)? {
|
||||||
println!("Downloading {}: {}", &channel_name, entry.title().as_str());
|
println!("Downloading {}: {}", &channel_name, entry.title().as_str());
|
||||||
(e.fetch.download)(&link)?;
|
(e.fetch.download)(&link)?;
|
||||||
(e.history.add)(&link, history)?;
|
(e.history.add)(&link, history)?;
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use podal::file::FileEnv;
|
||||||
use podal::prelude::*;
|
use podal::prelude::*;
|
||||||
|
|
||||||
use podal::{feed::FeedEnv, fetch::FetchEnv, history::HistoryEnv};
|
use podal::{feed::FeedEnv, fetch::FetchEnv, history::HistoryEnv};
|
||||||
|
@ -26,6 +27,7 @@ fn main() -> Result<()> {
|
||||||
download: podal::fetch::download,
|
download: podal::fetch::download,
|
||||||
get: podal::fetch::get,
|
get: podal::fetch::get,
|
||||||
},
|
},
|
||||||
|
file: FileEnv::default(),
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
use std::fs::File;
|
use crate::file::FileEnv;
|
||||||
use std::io::{BufRead, BufReader};
|
use std::io::{BufRead, BufReader};
|
||||||
|
|
||||||
pub fn lines_from(file_name: &str) -> Result<Vec<String>> {
|
pub fn lines_from(file_name: &str, e: &FileEnv) -> Result<Vec<String>> {
|
||||||
let file = File::open(file_name)?;
|
let file = (e.open)(file_name)?;
|
||||||
let reader = BufReader::new(file);
|
let reader = BufReader::new(file);
|
||||||
let mut lines = vec![];
|
let mut lines = vec![];
|
||||||
for line in reader.lines().flatten() {
|
for line in reader.lines().flatten() {
|
||||||
|
@ -29,9 +29,10 @@ mod tests {
|
||||||
"subscriptions.txt",
|
"subscriptions.txt",
|
||||||
include_bytes!("../test/data/subscriptions.txt"),
|
include_bytes!("../test/data/subscriptions.txt"),
|
||||||
)?;
|
)?;
|
||||||
|
let file_env = FileEnv::default();
|
||||||
|
|
||||||
//when
|
//when
|
||||||
let result = lines_from(&file_name)?;
|
let result = lines_from(&file_name, &file_env)?;
|
||||||
|
|
||||||
//then
|
//then
|
||||||
drop(dir);
|
drop(dir);
|
||||||
|
@ -46,9 +47,10 @@ mod tests {
|
||||||
"subscriptions.txt",
|
"subscriptions.txt",
|
||||||
include_bytes!("../test/data/subscriptions-blank-line.txt"),
|
include_bytes!("../test/data/subscriptions-blank-line.txt"),
|
||||||
)?;
|
)?;
|
||||||
|
let file_env = FileEnv::default();
|
||||||
|
|
||||||
//when
|
//when
|
||||||
let result = lines_from(&file_name)?;
|
let result = lines_from(&file_name, &file_env)?;
|
||||||
|
|
||||||
//then
|
//then
|
||||||
drop(dir);
|
drop(dir);
|
||||||
|
@ -63,9 +65,10 @@ mod tests {
|
||||||
"subscriptions.txt",
|
"subscriptions.txt",
|
||||||
include_bytes!("../test/data/subscriptions-comment.txt"),
|
include_bytes!("../test/data/subscriptions-comment.txt"),
|
||||||
)?;
|
)?;
|
||||||
|
let file_env = FileEnv::default();
|
||||||
|
|
||||||
//when
|
//when
|
||||||
let result = lines_from(&file_name)?;
|
let result = lines_from(&file_name, &file_env)?;
|
||||||
|
|
||||||
//then
|
//then
|
||||||
drop(dir);
|
drop(dir);
|
||||||
|
|
Loading…
Add table
Reference in a new issue