Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error path #89

Merged
merged 2 commits into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions src/de.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use config::Config;
use error::*;
use serde::de;
use std::borrow::Cow;
use std::collections::hash_map::Drain;
use std::collections::HashMap;
use std::iter::Peekable;
use std::collections::{HashMap, VecDeque};
use std::iter::Enumerate;
use value::{Value, ValueKind, ValueWithKey, Table};

// TODO: Use a macro or some other magic to reduce the code duplication here
Expand Down Expand Up @@ -287,13 +285,13 @@ impl<'de, 'a> de::Deserializer<'de> for StrDeserializer<'a> {
}

struct SeqAccess {
elements: ::std::vec::IntoIter<Value>,
elements: Enumerate<::std::vec::IntoIter<Value>>,
}

impl SeqAccess {
fn new(elements: Vec<Value>) -> Self {
SeqAccess {
elements: elements.into_iter(),
elements: elements.into_iter().enumerate(),
}
}
}
Expand All @@ -306,7 +304,11 @@ impl<'de> de::SeqAccess<'de> for SeqAccess {
T: de::DeserializeSeed<'de>,
{
match self.elements.next() {
Some(value) => seed.deserialize(value).map(Some),
Some((idx, value)) => {
seed.deserialize(value)
.map(Some)
.map_err(|e| e.prepend_index(idx))
}
None => Ok(None),
}
}
Expand All @@ -320,15 +322,13 @@ impl<'de> de::SeqAccess<'de> for SeqAccess {
}

struct MapAccess {
elements: Vec<(String, Value)>,
index: usize,
elements: VecDeque<(String, Value)>,
}

impl MapAccess {
fn new(mut table: HashMap<String, Value>) -> Self {
fn new(table: HashMap<String, Value>) -> Self {
MapAccess {
elements: table.drain().collect(),
index: 0,
elements: table.into_iter().collect(),
}
}
}
Expand All @@ -340,22 +340,23 @@ impl<'de> de::MapAccess<'de> for MapAccess {
where
K: de::DeserializeSeed<'de>,
{
if self.index >= self.elements.len() {
return Ok(None);
}

let key_s = &(self.elements[0].0);
let key_de = StrDeserializer(key_s);
let key = de::DeserializeSeed::deserialize(seed, key_de)?;
if let Some(&(ref key_s, _)) = self.elements.front() {
let key_de = StrDeserializer(key_s);
let key = de::DeserializeSeed::deserialize(seed, key_de)?;

Ok(Some(key))
Ok(Some(key))
} else {
Ok(None)
}
}

fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
where
V: de::DeserializeSeed<'de>,
{
de::DeserializeSeed::deserialize(seed, self.elements.remove(0).1)
let (key, value) = self.elements.pop_front().unwrap();
de::DeserializeSeed::deserialize(seed, value)
.map_err(|e| e.prepend_key(key))
}
}

Expand Down
37 changes: 37 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
25 changes: 25 additions & 0 deletions tests/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,28 @@ fn test_error_enum_de() {
);
}

#[test]
fn error_with_path() {
#[derive(Debug, Deserialize)]
struct Inner {
test: i32,
}

#[derive(Debug, Deserialize)]
struct Outer {
inner: Inner,
}
const CFG: &str = r#"
inner:
test: ABC
"#;

let mut cfg = Config::new();
cfg.merge(File::from_str(CFG, FileFormat::Yaml)).unwrap();
let e = cfg.try_into::<Outer>().unwrap_err();
if let ConfigError::Type { key: Some(path), .. } = e {
assert_eq!(path, "inner.test");
} else {
panic!("Wrong error {:?}", e);
}
}