diff --git a/src/config.rs b/src/config.rs index 572d4db..d6284f6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,3 +1,5 @@ +use std::io::Read; + pub trait ConfigSource { /// Skip hidden files and directories. fn ignore_hidden(&self) -> Option { @@ -43,6 +45,18 @@ pub struct Config { } impl Config { + pub fn from_file(path: &std::path::Path) -> Result { + let mut file = std::fs::File::open(path)?; + let mut s = String::new(); + file.read_to_string(&mut s)?; + Self::from_toml(&s) + } + + pub fn from_toml(data: &str) -> Result { + let content = toml::from_str(data)?; + Ok(content) + } + pub fn update(&mut self, source: &dyn ConfigSource) { if let Some(source) = source.ignore_hidden() { self.ignore_hidden = Some(source); diff --git a/src/main.rs b/src/main.rs index 48b7d87..ffef65b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,6 @@ #[macro_use] extern crate clap; -use std::io::Read; use std::io::Write; use structopt::StructOpt; @@ -45,7 +44,7 @@ struct Options { #[structopt(short = "c", long = "config")] /// Custom config file - custom_config: Option, + custom_config: Option, #[structopt(long, raw(overrides_with = r#""check-filenames""#))] /// Skip verifying spelling in file names. @@ -258,10 +257,7 @@ fn run() -> Result { let mut config = config::Config::default(); if let Some(path) = options.custom_config.as_ref() { - let mut file = std::fs::File::open(path)?; - let mut s = String::new(); - file.read_to_string(&mut s)?; - let custom: config::Config = toml::from_str(&s)?; + let custom = config::Config::from_file(path)?; config.update(&custom); } config.update(&options);