Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attach key to type error generated from Config::get_<type>() #413

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl Config {
Ok(())
}

pub fn get<'de, T: Deserialize<'de>>(&self, key: &str) -> Result<T> {
fn get_value(&self, key: &str) -> Result<Value> {
let k = key.to_lowercase();
let key = k.as_str();
// Parse the key into a path expression
Expand All @@ -159,38 +159,44 @@ impl Config {
// Traverse the cache using the path to (possibly) retrieve a value
let value = expr.get(&self.cache).cloned();

match value {
Some(value) => {
// Deserialize the received value into the requested type
T::deserialize(value).map_err(|e| e.extend_with_key(key))
}
value.ok_or_else(|| ConfigError::NotFound(key.into()))
}

None => Err(ConfigError::NotFound(key.into())),
}
pub fn get<'de, T: Deserialize<'de>>(&self, key: &str) -> Result<T> {
self.get_value(key).and_then(|value| {
// Deserialize the received value into the requested type
T::deserialize(value).map_err(|e| e.extend_with_key(key))
})
}

pub fn get_string(&self, key: &str) -> Result<String> {
self.get(key).and_then(Value::into_string)
self.get_value(key)
.and_then(|value| value.into_string().map_err(|e| e.extend_with_key(key)))
}

pub fn get_int(&self, key: &str) -> Result<i64> {
self.get(key).and_then(Value::into_int)
self.get_value(key)
.and_then(|value| value.into_int().map_err(|e| e.extend_with_key(key)))
}

pub fn get_float(&self, key: &str) -> Result<f64> {
self.get(key).and_then(Value::into_float)
self.get_value(key)
.and_then(|value| value.into_float().map_err(|e| e.extend_with_key(key)))
}

pub fn get_bool(&self, key: &str) -> Result<bool> {
self.get(key).and_then(Value::into_bool)
self.get_value(key)
.and_then(|value| value.into_bool().map_err(|e| e.extend_with_key(key)))
}

pub fn get_table(&self, key: &str) -> Result<Map<String, Value>> {
self.get(key).and_then(Value::into_table)
self.get_value(key)
.and_then(|value| value.into_table().map_err(|e| e.extend_with_key(key)))
}

pub fn get_array(&self, key: &str) -> Result<Vec<Value>> {
self.get(key).and_then(Value::into_array)
self.get_value(key)
.and_then(|value| value.into_array().map_err(|e| e.extend_with_key(key)))
}

/// Attempt to deserialize the entire configuration into the requested type.
Expand Down
54 changes: 54 additions & 0 deletions tests/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,60 @@ fn test_error_type_detached() {
);
}

#[test]
fn test_error_type_get_bool() {
let c = make();

let res = c.get_bool("boolean_s_parse");

let path: PathBuf = ["tests", "Settings.toml"].iter().collect();

assert!(res.is_err());
assert_eq!(
res.unwrap_err().to_string(),
format!(
"invalid type: string \"fals\", expected a boolean for key `boolean_s_parse` in {}",
path.display()
)
);
}

#[test]
fn test_error_type_get_table() {
let c = make();

let res = c.get_table("debug");

let path: PathBuf = ["tests", "Settings.toml"].iter().collect();

assert!(res.is_err());
assert_eq!(
res.unwrap_err().to_string(),
format!(
"invalid type: boolean `true`, expected a map for key `debug` in {}",
path.display()
)
);
}

#[test]
fn test_error_type_get_array() {
let c = make();

let res = c.get_array("debug");

let path: PathBuf = ["tests", "Settings.toml"].iter().collect();

assert!(res.is_err());
assert_eq!(
res.unwrap_err().to_string(),
format!(
"invalid type: boolean `true`, expected an array for key `debug` in {}",
path.display()
)
);
}

#[test]
fn test_error_enum_de() {
#[derive(Debug, Deserialize, PartialEq, Eq)]
Expand Down