i5-add-tests (part 3) #9
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 std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
|
||||
pub fn find(link: &Link, file_name: &str) -> Result<bool> {
|
||||
if let Ok(file) = File::open(file_name) {
|
||||
pub fn find(link: &Link, file_name: &str, e: &FileEnv) -> Result<bool> {
|
||||
if let Ok(file) = (e.open)(file_name) {
|
||||
let reader = BufReader::new(file);
|
||||
for line in reader.lines() {
|
||||
if line? == link.href {
|
||||
|
@ -34,9 +33,8 @@ mod test {
|
|||
title: None,
|
||||
length: None,
|
||||
};
|
||||
|
||||
//when
|
||||
let result = find(&link, &file_name)?;
|
||||
let result = find(&link, &file_name, &FileEnv::default())?;
|
||||
|
||||
//then
|
||||
drop(dir);
|
||||
|
@ -61,7 +59,7 @@ mod test {
|
|||
};
|
||||
|
||||
//when
|
||||
let result = find(&link, &file_name)?;
|
||||
let result = find(&link, &file_name, &FileEnv::default())?;
|
||||
|
||||
//then
|
||||
drop(dir);
|
||||
|
@ -88,7 +86,7 @@ mod test {
|
|||
};
|
||||
|
||||
//when
|
||||
let result = find(&link, &file_name)?;
|
||||
let result = find(&link, &file_name, &FileEnv::default())?;
|
||||
|
||||
//then
|
||||
drop(dir);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::prelude::*;
|
||||
use crate::{file::FileEnv, prelude::*};
|
||||
|
||||
mod add;
|
||||
mod find;
|
||||
|
@ -13,5 +13,5 @@ pub struct HistoryEnv {
|
|||
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<()>;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
mod errors;
|
||||
pub mod feed;
|
||||
pub mod fetch;
|
||||
pub mod file;
|
||||
pub mod history;
|
||||
pub mod prelude;
|
||||
mod subscriptions;
|
||||
|
@ -10,6 +11,7 @@ mod test_utils;
|
|||
|
||||
use feed::FeedEnv;
|
||||
use fetch::FetchEnv;
|
||||
use file::FileEnv;
|
||||
use history::HistoryEnv;
|
||||
use prelude::*;
|
||||
|
||||
|
@ -17,15 +19,16 @@ pub struct Env {
|
|||
pub feed: FeedEnv,
|
||||
pub history: HistoryEnv,
|
||||
pub fetch: FetchEnv,
|
||||
pub file: FileEnv,
|
||||
}
|
||||
|
||||
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);
|
||||
let feed_url = (e.feed.find)(site, &channel_name, &e.fetch.get)?;
|
||||
for entry in (e.feed.get)(&feed_url)?.entries() {
|
||||
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());
|
||||
(e.fetch.download)(&link)?;
|
||||
(e.history.add)(&link, history)?;
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use podal::file::FileEnv;
|
||||
use podal::prelude::*;
|
||||
|
||||
use podal::{feed::FeedEnv, fetch::FetchEnv, history::HistoryEnv};
|
||||
|
@ -26,6 +27,7 @@ fn main() -> Result<()> {
|
|||
download: podal::fetch::download,
|
||||
get: podal::fetch::get,
|
||||
},
|
||||
file: FileEnv::default(),
|
||||
},
|
||||
)?;
|
||||
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
use crate::prelude::*;
|
||||
|
||||
use std::fs::File;
|
||||
use crate::file::FileEnv;
|
||||
use std::io::{BufRead, BufReader};
|
||||
|
||||
pub fn lines_from(file_name: &str) -> Result<Vec<String>> {
|
||||
let file = File::open(file_name)?;
|
||||
pub fn lines_from(file_name: &str, e: &FileEnv) -> Result<Vec<String>> {
|
||||
let file = (e.open)(file_name)?;
|
||||
let reader = BufReader::new(file);
|
||||
let mut lines = vec![];
|
||||
for line in reader.lines().flatten() {
|
||||
|
@ -29,9 +29,10 @@ mod tests {
|
|||
"subscriptions.txt",
|
||||
include_bytes!("../test/data/subscriptions.txt"),
|
||||
)?;
|
||||
let file_env = FileEnv::default();
|
||||
|
||||
//when
|
||||
let result = lines_from(&file_name)?;
|
||||
let result = lines_from(&file_name, &file_env)?;
|
||||
|
||||
//then
|
||||
drop(dir);
|
||||
|
@ -46,9 +47,10 @@ mod tests {
|
|||
"subscriptions.txt",
|
||||
include_bytes!("../test/data/subscriptions-blank-line.txt"),
|
||||
)?;
|
||||
let file_env = FileEnv::default();
|
||||
|
||||
//when
|
||||
let result = lines_from(&file_name)?;
|
||||
let result = lines_from(&file_name, &file_env)?;
|
||||
|
||||
//then
|
||||
drop(dir);
|
||||
|
@ -63,9 +65,10 @@ mod tests {
|
|||
"subscriptions.txt",
|
||||
include_bytes!("../test/data/subscriptions-comment.txt"),
|
||||
)?;
|
||||
let file_env = FileEnv::default();
|
||||
|
||||
//when
|
||||
let result = lines_from(&file_name)?;
|
||||
let result = lines_from(&file_name, &file_env)?;
|
||||
|
||||
//then
|
||||
drop(dir);
|
||||
|
|
Loading…
Add table
Reference in a new issue