Skip to content

Commit 085cc90

Browse files
committed
Load and compile template in proper function
Get rid of the unncessary closure.
1 parent 1db84a3 commit 085cc90

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

src/config/config_type.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -392,22 +392,14 @@ macro_rules! create_config {
392392
}
393393

394394
fn set_license_template(&mut self) {
395-
if !self.was_set().license_template_path() {
396-
return;
395+
if self.was_set().license_template_path() {
396+
let lt_path = self.license_template_path();
397+
match license::load_and_compile_template(&lt_path) {
398+
Ok(re) => self.license_template = Some(re),
399+
Err(msg) => eprintln!("Warning for license template file {:?}: {}",
400+
lt_path, msg),
401+
}
397402
}
398-
let lt_path = self.license_template_path();
399-
let try = || -> Result<Regex, LicenseError> {
400-
let mut lt_file = File::open(&lt_path)?;
401-
let mut lt_str = String::new();
402-
lt_file.read_to_string(&mut lt_str)?;
403-
let lt_parsed = TemplateParser::parse(&lt_str)?;
404-
Ok(Regex::new(&lt_parsed)?)
405-
};
406-
match try() {
407-
Ok(re) => self.license_template = Some(re),
408-
Err(msg) => eprintln!("Warning for license template file {:?}: {}",
409-
lt_path, msg),
410-
};
411403
}
412404
}
413405

src/config/license.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use std::io;
22
use std::fmt;
3+
use std::fs::File;
4+
use std::io::Read;
35

46
use regex;
7+
use regex::Regex;
58

69
#[derive(Debug)]
710
pub enum LicenseError {
@@ -210,6 +213,14 @@ impl TemplateParser {
210213
}
211214
}
212215

216+
pub fn load_and_compile_template(path: &str) -> Result<Regex, LicenseError> {
217+
let mut lt_file = File::open(&path)?;
218+
let mut lt_str = String::new();
219+
lt_file.read_to_string(&mut lt_str)?;
220+
let lt_parsed = TemplateParser::parse(&lt_str)?;
221+
Ok(Regex::new(&lt_parsed)?)
222+
}
223+
213224
#[cfg(test)]
214225
mod test {
215226
use super::TemplateParser;

src/config/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ pub mod license;
2929

3030
use config::config_type::ConfigType;
3131
use config::file_lines::FileLines;
32-
use config::license::{LicenseError, TemplateParser};
3332
pub use config::lists::*;
3433
pub use config::options::*;
3534
use config::summary::Summary;

0 commit comments

Comments
 (0)