Skip to content

Commit

Permalink
refactor(de): Consolidate array logic (toml-rs#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
epage authored Sep 9, 2021
1 parent 613b5ff commit 0908e81
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 64 deletions.
14 changes: 11 additions & 3 deletions src/easy/de/array.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
use crate::easy::de::Error;

pub(crate) struct ArraySeqAccess {
iter: crate::ArrayIntoIter,
iter: std::vec::IntoIter<crate::Item>,
}

impl ArraySeqAccess {
pub(crate) fn new(input: crate::Array) -> Self {
pub(crate) fn new(input: Vec<crate::Item>) -> Self {
Self {
iter: input.into_iter(),
}
}

pub(crate) fn with_array(input: crate::Array) -> Self {
Self::new(input.values)
}

pub(crate) fn with_array_of_tables(input: crate::ArrayOfTables) -> Self {
Self::new(input.values)
}
}

impl<'de> serde::de::SeqAccess<'de> for ArraySeqAccess {
Expand All @@ -21,7 +29,7 @@ impl<'de> serde::de::SeqAccess<'de> for ArraySeqAccess {
{
match self.iter.next() {
Some(v) => seed
.deserialize(&mut crate::easy::de::ValueDeserializer::new(v))
.deserialize(&mut crate::easy::de::ItemDeserializer::new(v))
.map(Some),
None => Ok(None),
}
Expand Down
29 changes: 0 additions & 29 deletions src/easy/de/array_of_tables.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/easy/de/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<'de, 'a> serde::Deserializer<'de> for &'a mut ItemDeserializer {
}
crate::Item::Table(v) => visitor.visit_map(crate::easy::de::TableMapAccess::new(v)),
crate::Item::ArrayOfTables(v) => {
visitor.visit_seq(crate::easy::de::ArrayOfTablesSeqAccess::new(v))
visitor.visit_seq(crate::easy::de::ArraySeqAccess::with_array_of_tables(v))
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/easy/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
use serde::Deserialize;

mod array;
mod array_of_tables;
mod inline_table;
mod item;
mod table;
mod value;

use array::*;
use array_of_tables::*;
use inline_table::*;
use item::*;
use table::*;
Expand Down
28 changes: 0 additions & 28 deletions src/easy/de/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,6 @@ use serde::de::IntoDeserializer;

use crate::easy::de::Error;

pub(crate) struct TableDeserializer {
input: crate::Table,
}

impl TableDeserializer {
pub(crate) fn new(input: crate::Table) -> Self {
Self { input }
}
}

impl<'de, 'a> serde::Deserializer<'de> for &'a mut TableDeserializer {
type Error = Error;

fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
V: serde::de::Visitor<'de>,
{
let mut input = crate::Table::new();
std::mem::swap(&mut input, &mut self.input);
visitor.visit_map(TableMapAccess::new(input))
}

serde::forward_to_deserialize_any! {
bool u8 u16 u32 u64 i8 i16 i32 i64 f32 f64 char str string seq
bytes byte_buf map option unit newtype_struct
ignored_any unit_struct tuple_struct tuple enum identifier struct
}
}
pub(crate) struct TableMapAccess {
iter: linked_hash_map::IntoIter<crate::repr::InternalString, crate::table::TableKeyValue>,
value: Option<crate::Item>,
Expand Down
4 changes: 3 additions & 1 deletion src/easy/de/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ impl<'de, 'a> serde::Deserializer<'de> for &'a mut ValueDeserializer {
crate::Value::LocalDateTime(v) => visitor.visit_string(v.into_value().to_string()),
crate::Value::LocalDate(v) => visitor.visit_string(v.into_value().to_string()),
crate::Value::LocalTime(v) => visitor.visit_string(v.into_value().to_string()),
crate::Value::Array(v) => visitor.visit_seq(crate::easy::de::ArraySeqAccess::new(v)),
crate::Value::Array(v) => {
visitor.visit_seq(crate::easy::de::ArraySeqAccess::with_array(v))
}
crate::Value::InlineTable(v) => {
visitor.visit_map(crate::easy::de::InlineTableMapAccess::new(v))
}
Expand Down

0 comments on commit 0908e81

Please sign in to comment.