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/value.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/value.rs')
-rw-r--r-- | src/value.rs | 55 |
1 files changed, 0 insertions, 55 deletions
diff --git a/src/value.rs b/src/value.rs index 1bba78c..2db195d 100644 --- a/src/value.rs +++ b/src/value.rs @@ -542,58 +542,3 @@ impl Display for Value { write!(f, "{}", self.kind) } } - -pub struct ValueWithKey<'a>(pub Value, &'a str); - -impl<'a> ValueWithKey<'a> { - pub fn new(value: Value, key: &'a str) -> Self { - ValueWithKey(value, key) - } - - pub fn into_bool(self) -> Result<bool> { - match self.0.into_bool() { - Ok(value) => Ok(value), - Err(error) => Err(error.extend_with_key(self.1)), - } - } - - /// Returns `self` into an i64, if possible. - pub fn into_int(self) -> Result<i64> { - match self.0.into_int() { - Ok(value) => Ok(value), - Err(error) => Err(error.extend_with_key(self.1)), - } - } - - /// Returns `self` into a f64, if possible. - pub fn into_float(self) -> Result<f64> { - match self.0.into_float() { - Ok(value) => Ok(value), - Err(error) => Err(error.extend_with_key(self.1)), - } - } - - /// Returns `self` into a str, if possible. - pub fn into_str(self) -> Result<String> { - match self.0.into_str() { - Ok(value) => Ok(value), - Err(error) => Err(error.extend_with_key(self.1)), - } - } - - /// Returns `self` into an array, if possible - pub fn into_array(self) -> Result<Vec<Value>> { - match self.0.into_array() { - Ok(value) => Ok(value), - Err(error) => Err(error.extend_with_key(self.1)), - } - } - - /// If the `Value` is a Table, returns the associated Map. - pub fn into_table(self) -> Result<HashMap<String, Value>> { - match self.0.into_table() { - Ok(value) => Ok(value), - Err(error) => Err(error.extend_with_key(self.1)), - } - } -} |