diff options
author | Guillem Nieto <gnieto@scopely.com> | 2019-05-05 23:53:30 +0200 |
---|---|---|
committer | Guillem Nieto <gnieto@scopely.com> | 2019-05-09 19:28:21 +0200 |
commit | 4cf99ea2a2a84b8a25822a26404af61f29ba87b5 (patch) | |
tree | c4436bcaa1df221e189c69d713fa9c848ebffcae /src/config.rs | |
parent | eb2c80ebf6b1425e660df5f0b379da4aafd8a444 (diff) |
Remove ValueWithKey struct
While using this library, I end up having lifetime issues with
`Config::get`. I've seen that current implementation forces the calleer
to match `key` lifetime to the output of the function.
My use case is, under some circumstances, return a suffixed version of
the config key. Something similar to:
```
if some_condition == true {
let key_name = format!("{}_suffix", key);
self.config.get(&key_name)
} else {
self.config.get(key)
}
```
This code is noy compiling for me due to conflicting lifetimes. To avoid
this, I've started looking to the code and I've found that `key` needed
this lifetime because of `ValueWithKey`. The purpouse of this struct
seems to be add more information to the errors that are returned to the
user.
To mitigate this lifetime coupling I've:
- Mapped the error on `Config::get` to include the originating key of
the current error
- Remove all the code related with `ValueWithKey`
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/src/config.rs b/src/config.rs index 2ce73f1..7e031d0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -10,7 +10,7 @@ use ser::ConfigSerializer; use source::Source; use path; -use value::{Table, Value, ValueKind, ValueWithKey}; +use value::{Table, Value, ValueKind}; #[derive(Clone, Debug)] enum ConfigKind { @@ -151,7 +151,7 @@ impl Config { self.refresh() } - pub fn get<'de, T: Deserialize<'de>>(&self, key: &'de str) -> Result<T> { + pub fn get<'de, T: Deserialize<'de>>(&self, key: &str) -> Result<T> { // Parse the key into a path expression let expr: path::Expression = key.to_lowercase().parse()?; @@ -161,7 +161,8 @@ impl Config { match value { Some(value) => { // Deserialize the received value into the requested type - T::deserialize(ValueWithKey::new(value, key)) + T::deserialize(value) + .map_err(|e| e.extend_with_key(key)) } None => Err(ConfigError::NotFound(key.into())), |