Skip to content

Commit

Permalink
Load and compile template in proper function
Browse files Browse the repository at this point in the history
Get rid of the unncessary closure.
  • Loading branch information
dlukes committed Feb 27, 2018
1 parent cce264b commit 2d7dbc1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
22 changes: 7 additions & 15 deletions rustfmt-config/src/config_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
}
}

Expand Down
1 change: 0 additions & 1 deletion rustfmt-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ pub mod license;

use config_type::ConfigType;
use file_lines::FileLines;
use license::{LicenseError, TemplateParser};
pub use lists::*;
pub use options::*;
use summary::Summary;
Expand Down
11 changes: 11 additions & 0 deletions rustfmt-config/src/license.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 2d7dbc1

Please sign in to comment.