Skip to content

Remove ValueWithKey struct #109

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

Merged
merged 1 commit into from
May 13, 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
7 changes: 4 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ser::ConfigSerializer;
use source::Source;

use path;
use value::{Table, Value, ValueKind, ValueWithKey};
use value::{Table, Value, ValueKind};

#[derive(Clone, Debug)]
enum ConfigKind {
Expand Down Expand Up @@ -151,7 +151,7 @@ impl Config {
self.refresh()
}

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

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

None => Err(ConfigError::NotFound(key.into())),
Expand Down
128 changes: 1 addition & 127 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,133 +3,7 @@ use error::*;
use serde::de;
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

impl<'de> de::Deserializer<'de> for ValueWithKey<'de> {
type Error = ConfigError;

#[inline]
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
// Deserialize based on the underlying type
match self.0.kind {
ValueKind::Nil => visitor.visit_unit(),
ValueKind::Integer(i) => visitor.visit_i64(i),
ValueKind::Boolean(b) => visitor.visit_bool(b),
ValueKind::Float(f) => visitor.visit_f64(f),
ValueKind::String(s) => visitor.visit_string(s),
ValueKind::Array(values) => visitor.visit_seq(SeqAccess::new(values)),
ValueKind::Table(map) => visitor.visit_map(MapAccess::new(map)),
}
}

#[inline]
fn deserialize_bool<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
visitor.visit_bool(self.into_bool()?)
}

#[inline]
fn deserialize_i8<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
// FIXME: This should *fail* if the value does not fit in the requets integer type
visitor.visit_i8(self.into_int()? as i8)
}

#[inline]
fn deserialize_i16<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
// FIXME: This should *fail* if the value does not fit in the requets integer type
visitor.visit_i16(self.into_int()? as i16)
}

#[inline]
fn deserialize_i32<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
// FIXME: This should *fail* if the value does not fit in the requets integer type
visitor.visit_i32(self.into_int()? as i32)
}

#[inline]
fn deserialize_i64<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
visitor.visit_i64(self.into_int()?)
}

#[inline]
fn deserialize_u8<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
// FIXME: This should *fail* if the value does not fit in the requets integer type
visitor.visit_u8(self.into_int()? as u8)
}

#[inline]
fn deserialize_u16<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
// FIXME: This should *fail* if the value does not fit in the requets integer type
visitor.visit_u16(self.into_int()? as u16)
}

#[inline]
fn deserialize_u32<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
// FIXME: This should *fail* if the value does not fit in the requets integer type
visitor.visit_u32(self.into_int()? as u32)
}

#[inline]
fn deserialize_u64<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
// FIXME: This should *fail* if the value does not fit in the requets integer type
visitor.visit_u64(self.into_int()? as u64)
}

#[inline]
fn deserialize_f32<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
visitor.visit_f32(self.into_float()? as f32)
}

#[inline]
fn deserialize_f64<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
visitor.visit_f64(self.into_float()?)
}

#[inline]
fn deserialize_str<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
visitor.visit_string(self.into_str()?)
}

#[inline]
fn deserialize_string<V: de::Visitor<'de>>(self, visitor: V) -> Result<V::Value> {
visitor.visit_string(self.into_str()?)
}

#[inline]
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
// Match an explicit nil as None and everything else as Some
match self.0.kind {
ValueKind::Nil => visitor.visit_none(),
_ => visitor.visit_some(self),
}
}

fn deserialize_enum<V>(
self,
name: &'static str,
variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
// FIXME: find a way to extend_with_key
visitor.visit_enum(EnumAccess{ value: self.0, name: name, variants: variants })
}

forward_to_deserialize_any! {
char seq
bytes byte_buf map struct unit newtype_struct
identifier ignored_any unit_struct tuple_struct tuple
}
}
use value::{Value, ValueKind, Table};

impl<'de> de::Deserializer<'de> for Value {
type Error = ConfigError;
Expand Down
2 changes: 1 addition & 1 deletion src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::Display;
use std::mem;

use error::*;
use value::{Value, ValueKind, ValueWithKey};
use value::{Value, ValueKind};
use Config;

#[derive(Default, Debug)]
Expand Down
55 changes: 0 additions & 55 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,58 +542,3 @@ impl Display for Value {
write!(f, "{}", self.kind)
}
}

pub struct ValueWithKey<'a>(pub Value, &'a str);

impl<'a> ValueWithKey<'a> {
pub fn new(value: Value, key: &'a str) -> Self {
ValueWithKey(value, key)
}

pub fn into_bool(self) -> Result<bool> {
match self.0.into_bool() {
Ok(value) => Ok(value),
Err(error) => Err(error.extend_with_key(self.1)),
}
}

/// Returns `self` into an i64, if possible.
pub fn into_int(self) -> Result<i64> {
match self.0.into_int() {
Ok(value) => Ok(value),
Err(error) => Err(error.extend_with_key(self.1)),
}
}

/// Returns `self` into a f64, if possible.
pub fn into_float(self) -> Result<f64> {
match self.0.into_float() {
Ok(value) => Ok(value),
Err(error) => Err(error.extend_with_key(self.1)),
}
}

/// Returns `self` into a str, if possible.
pub fn into_str(self) -> Result<String> {
match self.0.into_str() {
Ok(value) => Ok(value),
Err(error) => Err(error.extend_with_key(self.1)),
}
}

/// Returns `self` into an array, if possible
pub fn into_array(self) -> Result<Vec<Value>> {
match self.0.into_array() {
Ok(value) => Ok(value),
Err(error) => Err(error.extend_with_key(self.1)),
}
}

/// If the `Value` is a Table, returns the associated Map.
pub fn into_table(self) -> Result<HashMap<String, Value>> {
match self.0.into_table() {
Ok(value) => Ok(value),
Err(error) => Err(error.extend_with_key(self.1)),
}
}
}