Skip to content

Commit

Permalink
Allow partial rustfmt.toml
Browse files Browse the repository at this point in the history
With this change one can use a config file
that only specifies a subset of config keys
to overwrite. E.g. a config file that looks like this

struct_trailing_comma = "Never"
struct_lit_trailing_comma = "Never"

Fixes #255
  • Loading branch information
cburgdorf committed Sep 4, 2015
1 parent 17302f8 commit 14a94f0
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/config.rs
Expand Up @@ -52,18 +52,39 @@ macro_rules! create_config {
$(pub $i: $ty),+
}

// Just like the Config struct but with each property wrapped
// as Option<T>. This is used to parse a rustfmt.toml that doesn't
// specity all properties of `Config`.
// We first parse into `ParsedConfig`, then create a default `Config`
// and overwrite the properties with corresponding values from `ParsedConfig`
#[derive(RustcDecodable, Clone)]
pub struct ParsedConfig {
$(pub $i: Option<$ty>),+
}

impl Config {

fn fill_from_parsed_config(mut self, parsed: &ParsedConfig) -> Config {
$(
if let Some(val) = parsed.$i {
self.$i = val;
}
)+
self
}

pub fn from_toml(toml: &str) -> Config {
let parsed = toml.parse().unwrap();
match toml::decode(parsed) {
let parsed_config:ParsedConfig = match toml::decode(parsed) {
Some(decoded) => decoded,
None => {
println!("Decoding config file failed. Config:\n{}", toml);
let parsed: toml::Value = toml.parse().unwrap();
println!("\n\nParsed:\n{:?}", parsed);
panic!();
}
}
};
Config::default().fill_from_parsed_config(&parsed_config)
}

pub fn override_value(&mut self, key: &str, val: &str) {
Expand Down

0 comments on commit 14a94f0

Please sign in to comment.