i3-cli-download-dir (#12)
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Closes kemitix/podal#3 Reviewed-on: #12 Co-authored-by: Paul Campbell <pcampbell@kemitix.net> Co-committed-by: Paul Campbell <pcampbell@kemitix.net>
This commit is contained in:
parent
eadf921589
commit
9d578c2d86
11 changed files with 148 additions and 53 deletions
20
Cargo.lock
generated
20
Cargo.lock
generated
|
@ -183,6 +183,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5fd304a20bff958a57f04c4e96a2e7594cc4490a0e809cbd48bb6437edaa452d"
|
checksum = "5fd304a20bff958a57f04c4e96a2e7594cc4490a0e809cbd48bb6437edaa452d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap_builder",
|
"clap_builder",
|
||||||
|
"clap_derive",
|
||||||
|
"once_cell",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -197,6 +199,18 @@ dependencies = [
|
||||||
"strsim",
|
"strsim",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clap_derive"
|
||||||
|
version = "4.3.12"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "54a9bb5758fc5dfe728d1019941681eccaf0cf8a4189b692a0ee2f2ecf90a050"
|
||||||
|
dependencies = [
|
||||||
|
"heck",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 2.0.27",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clap_lex"
|
name = "clap_lex"
|
||||||
version = "0.5.0"
|
version = "0.5.0"
|
||||||
|
@ -545,6 +559,12 @@ version = "0.12.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
|
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "heck"
|
||||||
|
version = "0.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "hermit-abi"
|
name = "hermit-abi"
|
||||||
version = "0.3.2"
|
version = "0.3.2"
|
||||||
|
|
|
@ -9,7 +9,7 @@ edition = "2021"
|
||||||
atom_syndication = "0.12.1"
|
atom_syndication = "0.12.1"
|
||||||
reqwest = { version = "0.11.18", features = ["json", "blocking"] }
|
reqwest = { version = "0.11.18", features = ["json", "blocking"] }
|
||||||
scraper = "0.17.1"
|
scraper = "0.17.1"
|
||||||
clap = "4.3.19"
|
clap = {version = "4.3.19", features = ["derive"]}
|
||||||
bytes = "1.4.0"
|
bytes = "1.4.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|
|
@ -3,11 +3,13 @@ use std::{str::Utf8Error, string::FromUtf8Error};
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Error {
|
pub struct Error {
|
||||||
pub details: String,
|
pub details: String,
|
||||||
|
pub source: String,
|
||||||
}
|
}
|
||||||
impl Error {
|
impl Error {
|
||||||
pub fn message(details: &str) -> Self {
|
pub fn message(details: &str) -> Self {
|
||||||
Self {
|
Self {
|
||||||
details: details.to_string(),
|
details: details.to_string(),
|
||||||
|
source: "(not provided)".to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,6 +17,7 @@ impl From<Utf8Error> for Error {
|
||||||
fn from(value: Utf8Error) -> Self {
|
fn from(value: Utf8Error) -> Self {
|
||||||
Self {
|
Self {
|
||||||
details: value.to_string(),
|
details: value.to_string(),
|
||||||
|
source: "Utf8Error".to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,18 +25,23 @@ impl From<FromUtf8Error> for Error {
|
||||||
fn from(value: FromUtf8Error) -> Self {
|
fn from(value: FromUtf8Error) -> Self {
|
||||||
Self {
|
Self {
|
||||||
details: value.to_string(),
|
details: value.to_string(),
|
||||||
|
source: "FromUtf8Error".to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl From<String> for Error {
|
impl From<String> for Error {
|
||||||
fn from(details: String) -> Self {
|
fn from(details: String) -> Self {
|
||||||
Self { details }
|
Self {
|
||||||
|
details,
|
||||||
|
source: "String".to_string(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl From<std::io::Error> for Error {
|
impl From<std::io::Error> for Error {
|
||||||
fn from(value: std::io::Error) -> Self {
|
fn from(value: std::io::Error) -> Self {
|
||||||
Self {
|
Self {
|
||||||
details: value.to_string(),
|
details: value.to_string(),
|
||||||
|
source: "std::io::Error".to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,6 +49,7 @@ impl From<std::sync::mpsc::SendError<String>> for Error {
|
||||||
fn from(value: std::sync::mpsc::SendError<String>) -> Self {
|
fn from(value: std::sync::mpsc::SendError<String>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
details: value.to_string(),
|
details: value.to_string(),
|
||||||
|
source: "std::sync::mpsc::SendError".to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,6 +57,7 @@ impl From<atom_syndication::Error> for Error {
|
||||||
fn from(value: atom_syndication::Error) -> Self {
|
fn from(value: atom_syndication::Error) -> Self {
|
||||||
Self {
|
Self {
|
||||||
details: value.to_string(),
|
details: value.to_string(),
|
||||||
|
source: "atom_syndication::Error".to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -55,6 +65,7 @@ impl From<reqwest::Error> for Error {
|
||||||
fn from(value: reqwest::Error) -> Self {
|
fn from(value: reqwest::Error) -> Self {
|
||||||
Self {
|
Self {
|
||||||
details: value.to_string(),
|
details: value.to_string(),
|
||||||
|
source: "reqwest::Error".to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,16 +11,23 @@ pub struct FileEnv {
|
||||||
pub open: FileOpenFn,
|
pub open: FileOpenFn,
|
||||||
pub append_line: FileAppendLineFn,
|
pub append_line: FileAppendLineFn,
|
||||||
}
|
}
|
||||||
impl Default for FileEnv {
|
impl FileEnv {
|
||||||
fn default() -> Self {
|
pub fn create(directory: String) -> Self {
|
||||||
|
let open_dir = directory.clone();
|
||||||
|
let append_dir = directory.clone();
|
||||||
Self {
|
Self {
|
||||||
open: Box::new(|path| Ok(File::open(path)?)),
|
open: Box::new(move |file_name| {
|
||||||
append_line: Box::new(|file_name, line| {
|
let path = format!("{}/{}", &open_dir, file_name);
|
||||||
|
let file = File::open(&path)?;
|
||||||
|
Ok(file)
|
||||||
|
}),
|
||||||
|
append_line: Box::new(move |file_name, line| {
|
||||||
|
let path = format!("{}/{}", &append_dir, file_name);
|
||||||
let mut file = OpenOptions::new()
|
let mut file = OpenOptions::new()
|
||||||
.write(true)
|
.write(true)
|
||||||
.append(true)
|
.append(true)
|
||||||
.create(true)
|
.create(true)
|
||||||
.open(file_name)
|
.open(&path)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
writeln!(file, "{}", line)?;
|
writeln!(file, "{}", line)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -25,14 +25,15 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn can_load_file() -> Result<()> {
|
fn can_load_file() -> Result<()> {
|
||||||
//given
|
//given
|
||||||
let (dir, file_name) = create_text_file(
|
let file_name = "subscriptions.txt";
|
||||||
"subscriptions.txt",
|
let dir = create_text_file(
|
||||||
|
file_name,
|
||||||
include_bytes!("../../test/data/subscriptions.txt"),
|
include_bytes!("../../test/data/subscriptions.txt"),
|
||||||
)?;
|
)?;
|
||||||
let file_env = FileEnv::default();
|
let file_env = FileEnv::create(dir.path().to_string_lossy().to_string());
|
||||||
|
|
||||||
//when
|
//when
|
||||||
let result = lines_from(&file_name, &file_env)?;
|
let result = lines_from(file_name, &file_env)?;
|
||||||
|
|
||||||
//then
|
//then
|
||||||
drop(dir);
|
drop(dir);
|
||||||
|
@ -43,14 +44,15 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn ignores_blank_lines() -> Result<()> {
|
fn ignores_blank_lines() -> Result<()> {
|
||||||
//given
|
//given
|
||||||
let (dir, file_name) = create_text_file(
|
let file_name = "subscriptions.txt";
|
||||||
"subscriptions.txt",
|
let dir = create_text_file(
|
||||||
|
file_name,
|
||||||
include_bytes!("../../test/data/subscriptions-blank-line.txt"),
|
include_bytes!("../../test/data/subscriptions-blank-line.txt"),
|
||||||
)?;
|
)?;
|
||||||
let file_env = FileEnv::default();
|
let file_env = FileEnv::create(dir.path().to_string_lossy().to_string());
|
||||||
|
|
||||||
//when
|
//when
|
||||||
let result = lines_from(&file_name, &file_env)?;
|
let result = lines_from(file_name, &file_env)?;
|
||||||
|
|
||||||
//then
|
//then
|
||||||
drop(dir);
|
drop(dir);
|
||||||
|
@ -61,14 +63,15 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn ignores_comments() -> Result<()> {
|
fn ignores_comments() -> Result<()> {
|
||||||
//given
|
//given
|
||||||
let (dir, file_name) = create_text_file(
|
let file_name = "subscriptions.txt";
|
||||||
"subscriptions.txt",
|
let dir = create_text_file(
|
||||||
|
file_name,
|
||||||
include_bytes!("../../test/data/subscriptions-comment.txt"),
|
include_bytes!("../../test/data/subscriptions-comment.txt"),
|
||||||
)?;
|
)?;
|
||||||
let file_env = FileEnv::default();
|
let file_env = FileEnv::create(dir.path().to_string_lossy().to_string());
|
||||||
|
|
||||||
//when
|
//when
|
||||||
let result = lines_from(&file_name, &file_env)?;
|
let result = lines_from(file_name, &file_env)?;
|
||||||
|
|
||||||
//then
|
//then
|
||||||
drop(dir);
|
drop(dir);
|
||||||
|
|
|
@ -21,9 +21,10 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn creates_file_if_missing() -> Result<()> {
|
fn creates_file_if_missing() -> Result<()> {
|
||||||
//given
|
//given
|
||||||
let (dir, file_name) =
|
let file_name = "download.txt";
|
||||||
create_text_file("download.txt", include_bytes!("../../test/data/empty.txt"))?;
|
let dir = create_text_file(file_name, include_bytes!("../../test/data/empty.txt"))?;
|
||||||
std::fs::remove_file(&file_name)?;
|
let path = format!("{}/{}", dir.path().to_string_lossy(), file_name);
|
||||||
|
std::fs::remove_file(path)?;
|
||||||
|
|
||||||
let link = Link {
|
let link = Link {
|
||||||
href: "foo".to_string(),
|
href: "foo".to_string(),
|
||||||
|
@ -34,10 +35,14 @@ mod tests {
|
||||||
length: None,
|
length: None,
|
||||||
};
|
};
|
||||||
//when
|
//when
|
||||||
add(&link, &file_name, &FileEnv::default())?;
|
add(
|
||||||
|
&link,
|
||||||
|
file_name,
|
||||||
|
&FileEnv::create(dir.path().to_string_lossy().to_string()),
|
||||||
|
)?;
|
||||||
|
|
||||||
//then
|
//then
|
||||||
let content: Vec<String> = read_text_file(&file_name)?;
|
let content: Vec<String> = read_text_file(dir.path(), file_name)?;
|
||||||
drop(dir);
|
drop(dir);
|
||||||
|
|
||||||
let expected = vec!["foo".to_string()];
|
let expected = vec!["foo".to_string()];
|
||||||
|
@ -49,10 +54,8 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn appends_to_exising_file() -> Result<()> {
|
fn appends_to_exising_file() -> Result<()> {
|
||||||
// given
|
// given
|
||||||
let (dir, file_name) = create_text_file(
|
let file_name = "download.txt";
|
||||||
"download.txt",
|
let dir = create_text_file(file_name, include_bytes!("../../test/data/downloads.txt"))?;
|
||||||
include_bytes!("../../test/data/downloads.txt"),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let link = Link {
|
let link = Link {
|
||||||
href: "foo".to_string(),
|
href: "foo".to_string(),
|
||||||
|
@ -63,10 +66,14 @@ mod tests {
|
||||||
length: None,
|
length: None,
|
||||||
};
|
};
|
||||||
//when
|
//when
|
||||||
add(&link, &file_name, &FileEnv::default())?;
|
add(
|
||||||
|
&link,
|
||||||
|
file_name,
|
||||||
|
&FileEnv::create(dir.path().to_string_lossy().to_string()),
|
||||||
|
)?;
|
||||||
|
|
||||||
//then
|
//then
|
||||||
let content: Vec<String> = read_text_file(&file_name)?;
|
let content: Vec<String> = read_text_file(dir.path(), file_name)?;
|
||||||
drop(dir);
|
drop(dir);
|
||||||
|
|
||||||
let expected = vec![
|
let expected = vec![
|
||||||
|
|
|
@ -24,8 +24,8 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn true_if_line_exists() -> Result<()> {
|
fn true_if_line_exists() -> Result<()> {
|
||||||
//given
|
//given
|
||||||
let (dir, file_name) =
|
let file_name = "file";
|
||||||
create_text_file("file", include_bytes!("../../test/data/with-llamma.txt"))?;
|
let dir = create_text_file(file_name, include_bytes!("../../test/data/with-llamma.txt"))?;
|
||||||
let link = Link {
|
let link = Link {
|
||||||
href: "llamma".to_string(),
|
href: "llamma".to_string(),
|
||||||
rel: "".to_string(),
|
rel: "".to_string(),
|
||||||
|
@ -35,7 +35,11 @@ mod test {
|
||||||
length: None,
|
length: None,
|
||||||
};
|
};
|
||||||
//when
|
//when
|
||||||
let result = find(&link, &file_name, &FileEnv::default())?;
|
let result = find(
|
||||||
|
&link,
|
||||||
|
file_name,
|
||||||
|
&FileEnv::create(dir.path().to_string_lossy().to_string()),
|
||||||
|
)?;
|
||||||
|
|
||||||
//then
|
//then
|
||||||
drop(dir);
|
drop(dir);
|
||||||
|
@ -48,8 +52,11 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn false_if_line_absent() -> Result<()> {
|
fn false_if_line_absent() -> Result<()> {
|
||||||
//given
|
//given
|
||||||
let (dir, file_name) =
|
let file_name = "file";
|
||||||
create_text_file("file", include_bytes!("../../test/data/without-llamma.txt"))?;
|
let dir = create_text_file(
|
||||||
|
file_name,
|
||||||
|
include_bytes!("../../test/data/without-llamma.txt"),
|
||||||
|
)?;
|
||||||
let link = Link {
|
let link = Link {
|
||||||
href: "llamma".to_string(),
|
href: "llamma".to_string(),
|
||||||
rel: "".to_string(),
|
rel: "".to_string(),
|
||||||
|
@ -60,7 +67,11 @@ mod test {
|
||||||
};
|
};
|
||||||
|
|
||||||
//when
|
//when
|
||||||
let result = find(&link, &file_name, &FileEnv::default())?;
|
let result = find(
|
||||||
|
&link,
|
||||||
|
file_name,
|
||||||
|
&FileEnv::create(dir.path().to_string_lossy().to_string()),
|
||||||
|
)?;
|
||||||
|
|
||||||
//then
|
//then
|
||||||
drop(dir);
|
drop(dir);
|
||||||
|
@ -73,8 +84,9 @@ mod test {
|
||||||
#[test]
|
#[test]
|
||||||
fn false_if_embedded_within_line() -> Result<()> {
|
fn false_if_embedded_within_line() -> Result<()> {
|
||||||
//given
|
//given
|
||||||
let (dir, file_name) = create_text_file(
|
let file_name = "file";
|
||||||
"file",
|
let dir = create_text_file(
|
||||||
|
file_name,
|
||||||
include_bytes!("../../test/data/with-embedded-llamma.txt"),
|
include_bytes!("../../test/data/with-embedded-llamma.txt"),
|
||||||
)?;
|
)?;
|
||||||
let link = Link {
|
let link = Link {
|
||||||
|
@ -87,7 +99,11 @@ mod test {
|
||||||
};
|
};
|
||||||
|
|
||||||
//when
|
//when
|
||||||
let result = find(&link, &file_name, &FileEnv::default())?;
|
let result = find(
|
||||||
|
&link,
|
||||||
|
file_name,
|
||||||
|
&FileEnv::create(dir.path().to_string_lossy().to_string()),
|
||||||
|
)?;
|
||||||
|
|
||||||
//then
|
//then
|
||||||
drop(dir);
|
drop(dir);
|
||||||
|
|
27
src/lib.rs
27
src/lib.rs
|
@ -5,6 +5,7 @@ pub mod feed;
|
||||||
pub mod file;
|
pub mod file;
|
||||||
pub mod history;
|
pub mod history;
|
||||||
pub mod network;
|
pub mod network;
|
||||||
|
pub mod params;
|
||||||
pub mod prelude;
|
pub mod prelude;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -57,11 +58,12 @@ mod tests {
|
||||||
let (tx, rx) = mpsc::channel::<String>(); // channel to recieve notice of downloaded urls
|
let (tx, rx) = mpsc::channel::<String>(); // channel to recieve notice of downloaded urls
|
||||||
|
|
||||||
// two channels in subscriptions.txt
|
// two channels in subscriptions.txt
|
||||||
let (subs_dir, subs_file_name) =
|
let subs_file_name = "subs";
|
||||||
create_text_file("subs", "@channel1\nignore me\n@channel2".as_bytes())?;
|
let subs_dir =
|
||||||
|
create_text_file(subs_file_name, "@channel1\nignore me\n@channel2".as_bytes())?;
|
||||||
// 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_dir, history_file_name) =
|
let history_file_name = "history";
|
||||||
create_text_file("history", "c1-f2\nc2-f3".as_bytes())?;
|
let history_dir = create_text_file(history_file_name, "c1-f2\nc2-f3".as_bytes())?;
|
||||||
|
|
||||||
let env = Env {
|
let env = Env {
|
||||||
network: NetworkEnv {
|
network: NetworkEnv {
|
||||||
|
@ -82,13 +84,26 @@ mod tests {
|
||||||
download_as_mp3: mock_network_download_as_mp3(tx),
|
download_as_mp3: mock_network_download_as_mp3(tx),
|
||||||
},
|
},
|
||||||
file: FileEnv {
|
file: FileEnv {
|
||||||
open: mock_file_open(vec![subs_file_name.clone(), history_file_name.clone()]),
|
open: mock_file_open(HashMap::from([
|
||||||
|
(
|
||||||
|
subs_file_name.to_string(),
|
||||||
|
format!("{}/{}", subs_dir.path().to_string_lossy(), subs_file_name),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
history_file_name.to_string(),
|
||||||
|
format!(
|
||||||
|
"{}/{}",
|
||||||
|
history_dir.path().to_string_lossy(),
|
||||||
|
history_file_name
|
||||||
|
),
|
||||||
|
),
|
||||||
|
])),
|
||||||
append_line: mock_file_append_line(),
|
append_line: mock_file_append_line(),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
//when
|
//when
|
||||||
run(&subs_file_name, &history_file_name, site, env)?;
|
run(subs_file_name, history_file_name, site, env)?;
|
||||||
//then
|
//then
|
||||||
drop(subs_dir);
|
drop(subs_dir);
|
||||||
drop(history_dir);
|
drop(history_dir);
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
|
use clap::Parser;
|
||||||
use podal::file::FileEnv;
|
use podal::file::FileEnv;
|
||||||
use podal::network::NetworkEnv;
|
use podal::network::NetworkEnv;
|
||||||
|
use podal::params::Args;
|
||||||
use podal::prelude::*;
|
use podal::prelude::*;
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
|
@ -8,13 +10,15 @@ fn main() -> Result<()> {
|
||||||
let history = "downloaded.txt";
|
let history = "downloaded.txt";
|
||||||
let site = "https://www.youtube.com/";
|
let site = "https://www.youtube.com/";
|
||||||
|
|
||||||
|
let args = Args::parse();
|
||||||
|
|
||||||
podal::run(
|
podal::run(
|
||||||
subscriptions,
|
subscriptions,
|
||||||
history,
|
history,
|
||||||
site,
|
site,
|
||||||
podal::Env {
|
podal::Env {
|
||||||
network: NetworkEnv::default(),
|
network: NetworkEnv::default(),
|
||||||
file: FileEnv::default(),
|
file: FileEnv::create(args.directory),
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
|
|
10
src/params/mod.rs
Normal file
10
src/params/mod.rs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
use clap::Parser;
|
||||||
|
|
||||||
|
#[derive(Parser, Debug)]
|
||||||
|
pub struct Args {
|
||||||
|
/// The directory to download mp3 files into.
|
||||||
|
/// This is also the directory where the subscription and history files are stored.
|
||||||
|
/// Defaults to the current directory
|
||||||
|
#[arg(short, long, default_value = ".")]
|
||||||
|
pub directory: String,
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
fs::{read_to_string, File},
|
fs::{read_to_string, File},
|
||||||
io::Write,
|
io::Write,
|
||||||
|
path::Path,
|
||||||
str::from_utf8,
|
str::from_utf8,
|
||||||
sync::mpsc::Sender,
|
sync::mpsc::Sender,
|
||||||
};
|
};
|
||||||
|
@ -15,16 +16,17 @@ use crate::{
|
||||||
prelude::*,
|
prelude::*,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn create_text_file(name: &str, data: &[u8]) -> Result<(TempDir, String)> {
|
pub fn create_text_file(name: &str, data: &[u8]) -> Result<TempDir> {
|
||||||
let data = from_utf8(data)?;
|
let data = from_utf8(data)?;
|
||||||
let dir = tempdir()?;
|
let dir = tempdir()?;
|
||||||
let filename = format!("{}", &dir.path().join(name).display());
|
let filename = format!("{}", &dir.path().join(name).display());
|
||||||
let file = File::create(&filename)?;
|
let file = File::create(filename)?;
|
||||||
write!(&file, "{data}")?;
|
write!(&file, "{data}")?;
|
||||||
Ok((dir, filename))
|
Ok(dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_text_file(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);
|
||||||
Ok(read_to_string(file_name)?
|
Ok(read_to_string(file_name)?
|
||||||
.lines()
|
.lines()
|
||||||
.map(String::from)
|
.map(String::from)
|
||||||
|
@ -58,10 +60,10 @@ pub fn mock_network_fetch_as_bytes_with_rss_entries(
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub fn mock_file_open(real_paths: Vec<String>) -> FileOpenFn {
|
pub fn mock_file_open(real_paths: HashMap<String, String>) -> FileOpenFn {
|
||||||
Box::new(move |path: &str| {
|
Box::new(move |path: &str| {
|
||||||
if real_paths.contains(&path.to_string()) {
|
if let Some(real_path) = real_paths.get(&path.to_string()) {
|
||||||
Ok(File::open(path)?)
|
Ok(File::open(real_path)?)
|
||||||
} else {
|
} else {
|
||||||
Err(Error::message(
|
Err(Error::message(
|
||||||
format!("Not implemented: file_open: {}", path).as_str(),
|
format!("Not implemented: file_open: {}", path).as_str(),
|
||||||
|
|
Loading…
Add table
Reference in a new issue