Load and compile template in proper function

Get rid of the unncessary closure.
This commit is contained in:
David Lukes 2018-02-27 15:00:29 +01:00
parent 1db84a3ec5
commit 085cc90599
3 changed files with 18 additions and 16 deletions

View File

@ -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(&lt_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<Regex, LicenseError> {
let mut lt_file = File::open(&lt_path)?;
let mut lt_str = String::new();
lt_file.read_to_string(&mut lt_str)?;
let lt_parsed = TemplateParser::parse(&lt_str)?;
Ok(Regex::new(&lt_parsed)?)
};
match try() {
Ok(re) => self.license_template = Some(re),
Err(msg) => eprintln!("Warning for license template file {:?}: {}",
lt_path, msg),
};
}
}

View File

@ -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<Regex, LicenseError> {
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(&lt_str)?;
Ok(Regex::new(&lt_parsed)?)
}
#[cfg(test)]
mod test {
use super::TemplateParser;

View File

@ -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;