diff options
author | Ryan Leckey <leckey.ryan@gmail.com> | 2019-01-03 11:05:38 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-03 11:05:38 -0800 |
commit | c7448652727de308a3830f78408fc20e759b782a (patch) | |
tree | 29c4d09169f08df2597f985efcfaf71267cc16e6 /src/error.rs | |
parent | 05bc8dee889dd95663a3441bd58fb5abdf322d9e (diff) | |
parent | 85f9735978433c9d49a24f51fa76b28f45beeb7c (diff) |
Merge pull request #89 from vorner/err-path
Error path
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`. |