1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
use crate::{
config::{utils::get_optional_string, ConfigError, ConfigErrorCause},
git::{Config, ErrorCode},
};
pub(crate) fn get_bool(config: Option<&Config>, name: &str, default: bool) -> Result<bool, ConfigError> {
if let Some(cfg) = config {
match cfg.get_bool(name) {
Ok(v) => Ok(v),
Err(e) if e.code() == ErrorCode::NotFound => Ok(default),
Err(e) if e.message().contains("failed to parse") => {
Err(ConfigError::new_with_optional_input(
name,
get_optional_string(config, name).ok().flatten(),
ConfigErrorCause::InvalidBoolean,
))
},
Err(e) => {
Err(ConfigError::new_with_optional_input(
name,
get_optional_string(config, name).ok().flatten(),
ConfigErrorCause::UnknownError(String::from(e.message())),
))
},
}
}
else {
Ok(default)
}
}
#[cfg(test)]
mod tests {
use claims::assert_ok_eq;
use testutils::assert_err_eq;
use super::*;
use crate::{config::testutils::with_git_config, test_helpers::invalid_utf};
#[test]
fn read_true() {
with_git_config(&["[test]", "bool = true"], |git_config| {
assert_ok_eq!(get_bool(Some(&git_config), "test.bool", false), true);
});
}
#[test]
fn read_false() {
with_git_config(&["[test]", "bool = false"], |git_config| {
assert_ok_eq!(get_bool(Some(&git_config), "test.bool", true), false);
});
}
#[test]
fn read_default() {
with_git_config(&[], |git_config| {
assert_ok_eq!(get_bool(Some(&git_config), "test.bool", true), true);
});
}
#[test]
fn read_invalid_value() {
with_git_config(&["[test]", "bool = invalid"], |git_config| {
assert_err_eq!(
get_bool(Some(&git_config), "test.bool", true),
ConfigError::new("test.bool", "invalid", ConfigErrorCause::InvalidBoolean)
);
});
}
#[test]
fn read_unexpected_error() {
with_git_config(&["[test]", "bool = invalid"], |git_config| {
assert_err_eq!(
get_bool(Some(&git_config), "test", true),
ConfigError::new_read_error(
"test",
ConfigErrorCause::UnknownError(String::from("invalid config item name 'test'"))
)
);
});
}
#[test]
fn read_invalid_non_utf() {
with_git_config(
&["[test]", format!("bool = {}", invalid_utf()).as_str()],
|git_config| {
assert_err_eq!(
get_bool(Some(&git_config), "test.bool", true),
ConfigError::new_read_error("test.bool", ConfigErrorCause::InvalidBoolean)
);
},
);
}
}
|