From fef475c8ad9904e4c02a220f1b60ccec992fcc57 Mon Sep 17 00:00:00 2001 From: Kienan Stewart Date: Sun, 25 Sep 2022 17:16:50 -0400 Subject: [PATCH] Set default output_file for jobs when loaded from file --- src/job.rs | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/job.rs b/src/job.rs index 1c33588..504c5e2 100644 --- a/src/job.rs +++ b/src/job.rs @@ -31,15 +31,19 @@ impl Job { }; } + fn set_default_output_file(&mut self, conf: &Conf) { + let mut output_file = conf.output_dir.clone(); + let mut file_name = self.url.clone().replace("/", "-"); + file_name.push_str(".rss"); + output_file = output_file.join(Path::new(&file_name)); + self.output_file = Some(output_file); + } + pub fn new(url: &str, selector: &str, conf: &Conf) -> Job { let mut job = Job::default(conf); job.url = url.to_string(); + job.set_default_output_file(conf); job.selector = selector.to_string(); - let mut output_file = conf.output_dir.clone(); - let mut file_name = job.url.clone().replace("/", "-"); - file_name.push_str(".rss"); - output_file = output_file.join(Path::new(&file_name)); - job.output_file = Some(output_file); match std::fs::File::open(job.output_file.as_ref().unwrap()) { Err(why) => { @@ -99,6 +103,9 @@ impl Job { } } } + if job.output_file.is_none() { + job.set_default_output_file(conf); + } return Ok(job); } @@ -184,4 +191,23 @@ selector = section.listing:nth-child(2) > ul:nth-child(1) > li:nth-child(3) > he assert_eq!(job.selector, "section.listing:nth-child(2) > ul:nth-child(1) > li:nth-child(3) > header:nth-child(3) > h2:nth-child(1) > a:nth-child(1)"); } + #[test] + fn create_from_file_default_output_file() { + let conf = Conf::get_default_conf(); + let mut tf = NamedTempFile::new().unwrap(); + let job_conf = r#" +url = http://example.com/test +every=7200 + +selector = section.listing:nth-child(2) > ul:nth-child(1) > li:nth-child(3) > header:nth-child(3) > h2:nth-child(1) > a:nth-child(1) +"#; + tf.write_all(job_conf.as_bytes()).expect("Failed to write configuration to file"); + + let job = Job::from_file(tf.path(), &conf).expect("Failed to read configuration file"); + assert_eq!(job.url, "http://example.com/test"); + assert_eq!(job.output_file.unwrap().to_str().unwrap(), "results.d/http:--example.com-test.rss"); + assert_eq!(job.every.as_secs(), 7200); + assert_eq!(job.selector, "section.listing:nth-child(2) > ul:nth-child(1) > li:nth-child(3) > header:nth-child(3) > h2:nth-child(1) > a:nth-child(1)"); + } + }