crate::ix!();
pub fn interpret_bool(str_value: &str) -> bool {
if str_value.is_empty() {
return true;
}
locale_independent_atoi::<i32>(str_value) != 0
}
pub fn interpret_option(
section: &mut String,
key: &mut String,
value: &String) -> SettingsValue {
if let Some(option_index) = key.find('.') {
*section = key[0..option_index].to_string();
key.replace_range(0..option_index + 1, "");
}
if &key[0..2] == "no" {
key.replace_range(0..2, "");
if !interpret_bool(value) {
log_printf!(
"Warning: parsed potentially confusing double-negative -{}={}\n",
key,
value
);
return SettingsValue(UniValue::from(true));
}
return SettingsValue(UniValue::from(false));
}
SettingsValue(UniValue::from(value.as_str()))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn interpret_bool_rules() {
assert!(interpret_bool("")); assert!(interpret_bool("1"));
assert!(!interpret_bool("0"));
assert!(!interpret_bool("foo")); }
#[test]
fn interpret_option_handles_no_and_double_negative() {
let mut section = "".to_string();
let mut key = "nofoo".to_string();
let v = interpret_option(&mut section, &mut key, &"1".to_string());
assert_eq!(key, "foo");
assert!(v.0.is_false());
let mut section = "".to_string();
let mut key = "nobar".to_string();
let v = interpret_option(&mut section, &mut key, &"0".to_string()); assert_eq!(key, "bar");
assert!(v.0.is_true());
}
#[test]
fn interpret_option_splits_section_dot_key() {
let mut section = "".to_string();
let mut key = "testnet.rpcport".to_string();
let v = interpret_option(&mut section, &mut key, &"18332".to_string());
assert_eq!(section, "testnet");
assert_eq!(key, "rpcport");
assert_eq!(v.0.get_str(), "18332");
}
}