Skip to content

Commit

Permalink
fix: #3117 For backwards compatibility only capitalise first letter o… (
Browse files Browse the repository at this point in the history
#3124)

* fix: #3117 For backwards compatibility only capitalise first letter of log level in config file

* fix: For forwards compatibility old config needs Warning log level changed to standard log::Level WARN

* refactor: renamed some variables
  • Loading branch information
JosephGoulden authored and antiochp committed Nov 18, 2019
1 parent 6d864a8 commit 8fde3b3
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ impl GlobalConfig {
let mut file = File::open(self.config_file_path.as_mut().unwrap())?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let decoded: Result<ConfigMembers, toml::de::Error> = toml::from_str(&contents);
let fixed = GlobalConfig::fix_warning_level(contents);
let decoded: Result<ConfigMembers, toml::de::Error> = toml::from_str(&fixed);
match decoded {
Ok(gc) => {
self.members = Some(gc);
Expand Down Expand Up @@ -296,9 +297,38 @@ impl GlobalConfig {
/// Write configuration to a file
pub fn write_to_file(&mut self, name: &str) -> Result<(), ConfigError> {
let conf_out = self.ser_config()?;
let conf_out = insert_comments(conf_out);
let fixed_config = GlobalConfig::fix_log_level(conf_out);
let commented_config = insert_comments(fixed_config);
let mut file = File::create(name)?;
file.write_all(conf_out.as_bytes())?;
file.write_all(commented_config.as_bytes())?;
Ok(())
}

// For forwards compatibility old config needs `Warning` log level changed to standard log::Level `WARN`
fn fix_warning_level(conf: String) -> String {
conf.replace("Warning", "WARN")
}

// For backwards compatibility only first letter of log level should be capitalised.
fn fix_log_level(conf: String) -> String {
conf.replace("TRACE", "Trace")
.replace("DEBUG", "Debug")
.replace("INFO", "Info")
.replace("WARN", "Warning")
.replace("ERROR", "Error")
}
}

#[test]
fn test_fix_log_level() {
let config = "TRACE DEBUG INFO WARN ERROR".to_string();
let fixed_config = GlobalConfig::fix_log_level(config);
assert_eq!(fixed_config, "Trace Debug Info Warning Error");
}

#[test]
fn test_fix_warning_level() {
let config = "Warning".to_string();
let fixed_config = GlobalConfig::fix_warning_level(config);
assert_eq!(fixed_config, "WARN");
}

0 comments on commit 8fde3b3

Please sign in to comment.