Skip to content

Commit 2d7dbc1

Browse files
committed
Load and compile template in proper function
Get rid of the unncessary closure.
1 parent cce264b commit 2d7dbc1

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

rustfmt-config/src/config_type.rs

+7-15
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

rustfmt-config/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ pub mod license;
3737

3838
use config_type::ConfigType;
3939
use file_lines::FileLines;
40-
use license::{LicenseError, TemplateParser};
4140
pub use lists::*;
4241
pub use options::*;
4342
use summary::Summary;

rustfmt-config/src/license.rs

+11
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;

0 commit comments

Comments
 (0)