Skip to content

Commit ad29f8f

Browse files
authored
Merge pull request #109 from gnieto/task/remove-value-with-key
Remove ValueWithKey struct
2 parents eb2c80e + 4cf99ea commit ad29f8f

File tree

4 files changed

+6
-186
lines changed

4 files changed

+6
-186
lines changed

src/config.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use ser::ConfigSerializer;
1010
use source::Source;
1111

1212
use path;
13-
use value::{Table, Value, ValueKind, ValueWithKey};
13+
use value::{Table, Value, ValueKind};
1414

1515
#[derive(Clone, Debug)]
1616
enum ConfigKind {
@@ -151,7 +151,7 @@ impl Config {
151151
self.refresh()
152152
}
153153

154-
pub fn get<'de, T: Deserialize<'de>>(&self, key: &'de str) -> Result<T> {
154+
pub fn get<'de, T: Deserialize<'de>>(&self, key: &str) -> Result<T> {
155155
// Parse the key into a path expression
156156
let expr: path::Expression = key.to_lowercase().parse()?;
157157

@@ -161,7 +161,8 @@ impl Config {
161161
match value {
162162
Some(value) => {
163163
// Deserialize the received value into the requested type
164-
T::deserialize(ValueWithKey::new(value, key))
164+
T::deserialize(value)
165+
.map_err(|e| e.extend_with_key(key))
165166
}
166167

167168
None => Err(ConfigError::NotFound(key.into())),

src/de.rs

Lines changed: 1 addition & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -3,133 +3,7 @@ use error::*;
33
use serde::de;
44
use std::collections::{HashMap, VecDeque};
55
use std::iter::Enumerate;
6-
use value::{Value, ValueKind, ValueWithKey, Table};
7-
8-
// TODO: Use a macro or some other magic to reduce the code duplication here
9-
10-
impl<'de> de::Deserializer<'de> for ValueWithKey<'de> {
11-
type Error = ConfigError;
12-
13-
#[inline]
14-
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
15-
where
16-
V: de::Visitor<'de>,
17-
{
18-
// Deserialize based on the underlying type
19-
match self.0.kind {
20-
ValueKind::Nil => visitor.visit_unit(),
21-
ValueKind::Integer(i) => visitor.visit_i64(i),
22-
ValueKind::Boolean(b) => visitor.visit_bool(b),
23-
ValueKind::Float(f) => visitor.visit_f64(f),
24-
ValueKind::String(s) => visitor.visit_string(s),
25-
ValueKind::Array(values) => visitor.visit_seq(SeqAccess::new(values)),
26-
ValueKind::Table(map) => visitor.visit_map(MapAccess::new(map)),
27-
}
28-
}
29-
30-
#[inline]
31-
fn deserialize_bool<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
32-
visitor.visit_bool(self.into_bool()?)
33-
}
34-
35-
#[inline]
36-
fn deserialize_i8<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
37-
// FIXME: This should *fail* if the value does not fit in the requets integer type
38-
visitor.visit_i8(self.into_int()? as i8)
39-
}
40-
41-
#[inline]
42-
fn deserialize_i16<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
43-
// FIXME: This should *fail* if the value does not fit in the requets integer type
44-
visitor.visit_i16(self.into_int()? as i16)
45-
}
46-
47-
#[inline]
48-
fn deserialize_i32<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
49-
// FIXME: This should *fail* if the value does not fit in the requets integer type
50-
visitor.visit_i32(self.into_int()? as i32)
51-
}
52-
53-
#[inline]
54-
fn deserialize_i64<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
55-
visitor.visit_i64(self.into_int()?)
56-
}
57-
58-
#[inline]
59-
fn deserialize_u8<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
60-
// FIXME: This should *fail* if the value does not fit in the requets integer type
61-
visitor.visit_u8(self.into_int()? as u8)
62-
}
63-
64-
#[inline]
65-
fn deserialize_u16<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
66-
// FIXME: This should *fail* if the value does not fit in the requets integer type
67-
visitor.visit_u16(self.into_int()? as u16)
68-
}
69-
70-
#[inline]
71-
fn deserialize_u32<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
72-
// FIXME: This should *fail* if the value does not fit in the requets integer type
73-
visitor.visit_u32(self.into_int()? as u32)
74-
}
75-
76-
#[inline]
77-
fn deserialize_u64<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
78-
// FIXME: This should *fail* if the value does not fit in the requets integer type
79-
visitor.visit_u64(self.into_int()? as u64)
80-
}
81-
82-
#[inline]
83-
fn deserialize_f32<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
84-
visitor.visit_f32(self.into_float()? as f32)
85-
}
86-
87-
#[inline]
88-
fn deserialize_f64<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
89-
visitor.visit_f64(self.into_float()?)
90-
}
91-
92-
#[inline]
93-
fn deserialize_str<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
94-
visitor.visit_string(self.into_str()?)
95-
}
96-
97-
#[inline]
98-
fn deserialize_string<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
99-
visitor.visit_string(self.into_str()?)
100-
}
101-
102-
#[inline]
103-
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
104-
where
105-
V: de::Visitor<'de>,
106-
{
107-
// Match an explicit nil as None and everything else as Some
108-
match self.0.kind {
109-
ValueKind::Nil => visitor.visit_none(),
110-
_ => visitor.visit_some(self),
111-
}
112-
}
113-
114-
fn deserialize_enum<V>(
115-
self,
116-
name: &'static str,
117-
variants: &'static [&'static str],
118-
visitor: V,
119-
) -> Result<V::Value>
120-
where
121-
V: de::Visitor<'de>,
122-
{
123-
// FIXME: find a way to extend_with_key
124-
visitor.visit_enum(EnumAccess{ value: self.0, name: name, variants: variants })
125-
}
126-
127-
forward_to_deserialize_any! {
128-
char seq
129-
bytes byte_buf map struct unit newtype_struct
130-
identifier ignored_any unit_struct tuple_struct tuple
131-
}
132-
}
6+
use value::{Value, ValueKind, Table};
1337

1348
impl<'de> de::Deserializer<'de> for Value {
1359
type Error = ConfigError;

src/ser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt::Display;
33
use std::mem;
44

55
use error::*;
6-
use value::{Value, ValueKind, ValueWithKey};
6+
use value::{Value, ValueKind};
77
use Config;
88

99
#[derive(Default, Debug)]

src/value.rs

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -542,58 +542,3 @@ impl Display for Value {
542542
write!(f, "{}", self.kind)
543543
}
544544
}
545-
546-
pub struct ValueWithKey<'a>(pub Value, &'a str);
547-
548-
impl<'a> ValueWithKey<'a> {
549-
pub fn new(value: Value, key: &'a str) -> Self {
550-
ValueWithKey(value, key)
551-
}
552-
553-
pub fn into_bool(self) -> Result<bool> {
554-
match self.0.into_bool() {
555-
Ok(value) => Ok(value),
556-
Err(error) => Err(error.extend_with_key(self.1)),
557-
}
558-
}
559-
560-
/// Returns `self` into an i64, if possible.
561-
pub fn into_int(self) -> Result<i64> {
562-
match self.0.into_int() {
563-
Ok(value) => Ok(value),
564-
Err(error) => Err(error.extend_with_key(self.1)),
565-
}
566-
}
567-
568-
/// Returns `self` into a f64, if possible.
569-
pub fn into_float(self) -> Result<f64> {
570-
match self.0.into_float() {
571-
Ok(value) => Ok(value),
572-
Err(error) => Err(error.extend_with_key(self.1)),
573-
}
574-
}
575-
576-
/// Returns `self` into a str, if possible.
577-
pub fn into_str(self) -> Result<String> {
578-
match self.0.into_str() {
579-
Ok(value) => Ok(value),
580-
Err(error) => Err(error.extend_with_key(self.1)),
581-
}
582-
}
583-
584-
/// Returns `self` into an array, if possible
585-
pub fn into_array(self) -> Result<Vec<Value>> {
586-
match self.0.into_array() {
587-
Ok(value) => Ok(value),
588-
Err(error) => Err(error.extend_with_key(self.1)),
589-
}
590-
}
591-
592-
/// If the `Value` is a Table, returns the associated Map.
593-
pub fn into_table(self) -> Result<HashMap<String, Value>> {
594-
match self.0.into_table() {
595-
Ok(value) => Ok(value),
596-
Err(error) => Err(error.extend_with_key(self.1)),
597-
}
598-
}
599-
}

0 commit comments

Comments
 (0)