summaryrefslogtreecommitdiffstats
path: root/src/error.rs
diff options
context:
space:
mode:
authorRyan Leckey <leckey.ryan@gmail.com>2019-01-03 11:05:38 -0800
committerGitHub <noreply@github.com>2019-01-03 11:05:38 -0800
commitc7448652727de308a3830f78408fc20e759b782a (patch)
tree29c4d09169f08df2597f985efcfaf71267cc16e6 /src/error.rs
parent05bc8dee889dd95663a3441bd58fb5abdf322d9e (diff)
parent85f9735978433c9d49a24f51fa76b28f45beeb7c (diff)
Merge pull request #89 from vorner/err-path
Error path
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs37
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`.