diff options
author | Michal 'vorner' Vaner <vorner@vorner.cz> | 2018-12-30 19:06:28 +0100 |
---|---|---|
committer | Michal 'vorner' Vaner <vorner@vorner.cz> | 2018-12-30 19:06:28 +0100 |
commit | 85f9735978433c9d49a24f51fa76b28f45beeb7c (patch) | |
tree | 29c4d09169f08df2597f985efcfaf71267cc16e6 /src/error.rs | |
parent | 2eca1ad50c899f52c7a589105180f85817f94049 (diff) |
Tracking a path where an error happens during deserialization
Related to #83, but doesn't solve that specific problem :-(. That
specific error message ("missing field") comes from somewhere else than
this library.
Diffstat (limited to 'src/error.rs')
-rw-r--r-- | src/error.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs index 1d348b6..e305750 100644 --- a/src/error.rs +++ b/src/error.rs @@ -114,6 +114,43 @@ impl ConfigError { _ => self, } } + + fn prepend(self, segment: String, add_dot: bool) -> Self { + let concat = |key: Option<String>| { + let key = key.unwrap_or_else(String::new); + let dot = if add_dot && key.as_bytes().get(0).unwrap_or(&b'[') != &b'[' { + "." + } else { + "" + }; + format!("{}{}{}", segment, dot, key) + }; + match self { + ConfigError::Type { + origin, + unexpected, + expected, + key, + } => { + ConfigError::Type { + origin, + unexpected, + expected, + key: Some(concat(key)), + } + } + ConfigError::NotFound(key) => ConfigError::NotFound(concat(Some(key))), + _ => self, + } + } + + pub(crate) fn prepend_key(self, key: String) -> Self { + self.prepend(key, true) + } + + pub(crate) fn prepend_index(self, idx: usize) -> Self { + self.prepend(format!("[{}]", idx), false) + } } /// Alias for a `Result` with the error type set to `ConfigError`. |