diff --git a/src/job.rs b/src/job.rs index 1f99e49..142e23b 100644 --- a/src/job.rs +++ b/src/job.rs @@ -41,31 +41,34 @@ impl Job { self.output_file = Some(output_file); } + fn set_default_channel(&mut self) { + match std::fs::File::open(self.output_file.as_ref().unwrap()) { + Err(why) => { + println!("Failed to open '{}': {}", self.output_file.as_ref().unwrap().display(), why); + println!("Creating empty RSS channel for job '{}'", self.url); + self.channel = Some( + rss::ChannelBuilder::default() + .title(self.url.as_str()) + .link(self.url.as_str()) + .description("haunting") + .build() + ); + self.channel.as_mut().unwrap().set_generator("Haunter".to_string()); + }, + Ok(file) => { + self.channel = Some( + rss::Channel::read_from(std::io::BufReader::new(file)).unwrap() + ); + }, + }; + } + 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(); - - match std::fs::File::open(job.output_file.as_ref().unwrap()) { - Err(why) => { - println!("Failed to open '{}': {}", job.output_file.as_ref().unwrap().display(), why); - println!("Creating empty RSS channel for job '{}'", job.url); - job.channel = Some( - rss::ChannelBuilder::default() - .title(url) - .link(url) - .description("haunting") - .build() - ); - job.channel.as_mut().unwrap().set_generator("Haunter".to_string()); - }, - Ok(file) => { - job.channel = Some( - rss::Channel::read_from(std::io::BufReader::new(file)).unwrap() - ); - }, - }; + job.set_default_channel(); return job; } @@ -108,6 +111,7 @@ impl Job { if job.output_file.is_none() { job.set_default_output_file(conf); } + job.set_default_channel(); job.source_file = Some(PathBuf::from(path)); return Ok(job); } @@ -172,6 +176,8 @@ mod tests { assert_eq!(job.url, "my/url"); assert_eq!(job.output_file.unwrap().to_str().unwrap(), "results.d/my-url.rss"); assert_eq!(job.selector, "myselector"); + assert!(job.source_file.is_none()); + assert!(job.channel.is_some()); } #[test] @@ -192,6 +198,8 @@ selector = section.listing:nth-child(2) > ul:nth-child(1) > li:nth-child(3) > he assert_eq!(job.output_file.unwrap().to_str().unwrap(), "results.d/example_output.atom"); 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)"); + assert_eq!(job.source_file.unwrap().to_str(), tf.path().to_str()); + assert!(job.channel.is_some()); } #[test] @@ -212,6 +220,7 @@ selector = section.listing:nth-child(2) > ul:nth-child(1) > li:nth-child(3) > he 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)"); assert_eq!(job.source_file.unwrap().to_str(), tf.path().to_str()); + assert!(job.channel.is_some()); } }