From 085cc90599bea1c31f565f1495c4ce79d177ce49 Mon Sep 17 00:00:00 2001 From: David Lukes Date: Tue, 27 Feb 2018 15:00:29 +0100 Subject: [PATCH] Load and compile template in proper function Get rid of the unncessary closure. --- src/config/config_type.rs | 22 +++++++--------------- src/config/license.rs | 11 +++++++++++ src/config/mod.rs | 1 - 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/config/config_type.rs b/src/config/config_type.rs index 314a1a26b4e..dc768490fbb 100644 --- a/src/config/config_type.rs +++ b/src/config/config_type.rs @@ -392,22 +392,14 @@ macro_rules! create_config { } fn set_license_template(&mut self) { - if !self.was_set().license_template_path() { - return; + if self.was_set().license_template_path() { + let lt_path = self.license_template_path(); + match license::load_and_compile_template(<_path) { + Ok(re) => self.license_template = Some(re), + Err(msg) => eprintln!("Warning for license template file {:?}: {}", + lt_path, msg), + } } - let lt_path = self.license_template_path(); - let try = || -> Result { - let mut lt_file = File::open(<_path)?; - let mut lt_str = String::new(); - lt_file.read_to_string(&mut lt_str)?; - let lt_parsed = TemplateParser::parse(<_str)?; - Ok(Regex::new(<_parsed)?) - }; - match try() { - Ok(re) => self.license_template = Some(re), - Err(msg) => eprintln!("Warning for license template file {:?}: {}", - lt_path, msg), - }; } } diff --git a/src/config/license.rs b/src/config/license.rs index ce05634b876..1830fcb3cf2 100644 --- a/src/config/license.rs +++ b/src/config/license.rs @@ -1,7 +1,10 @@ use std::io; use std::fmt; +use std::fs::File; +use std::io::Read; use regex; +use regex::Regex; #[derive(Debug)] pub enum LicenseError { @@ -210,6 +213,14 @@ impl TemplateParser { } } +pub fn load_and_compile_template(path: &str) -> Result { + let mut lt_file = File::open(&path)?; + let mut lt_str = String::new(); + lt_file.read_to_string(&mut lt_str)?; + let lt_parsed = TemplateParser::parse(<_str)?; + Ok(Regex::new(<_parsed)?) +} + #[cfg(test)] mod test { use super::TemplateParser; diff --git a/src/config/mod.rs b/src/config/mod.rs index 8142b5034fb..0d4ec8557d3 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -29,7 +29,6 @@ pub mod license; use config::config_type::ConfigType; use config::file_lines::FileLines; -use config::license::{LicenseError, TemplateParser}; pub use config::lists::*; pub use config::options::*; use config::summary::Summary;