Skip to content

Commit 6b4cdf1

Browse files
committed
WIP
1 parent db33f30 commit 6b4cdf1

13 files changed

+57
-150
lines changed

src/array.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ impl IntoIterator for Array {
340340
self.values
341341
.into_iter()
342342
.filter(|v| v.is_value())
343-
.map(|v| v.into_value().unwrap()),
343+
.map(|v| v.into_value()),
344344
)
345345
}
346346
}

src/de/item.rs

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ impl<'de, 'a> serde::Deserializer<'de> for crate::Item {
9999
V: serde::de::Visitor<'de>,
100100
{
101101
match self {
102-
crate::Item::None => visitor.visit_none(),
103102
crate::Item::Value(v) => v.deserialize_any(visitor),
104103
crate::Item::Table(v) => visitor.visit_map(crate::de::TableMapAccess::new(v)),
105104
crate::Item::ArrayOfTables(v) => {

src/index.rs

+7-27
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use std::ops;
22

33
use crate::document::Document;
4-
use crate::key::Key;
5-
use crate::table::TableKeyValue;
6-
use crate::{value, InlineTable, InternalString, Item, Table, Value};
4+
use crate::{InlineTable, Item, Table, Value};
75

86
// copied from
97
// https://github.com/serde-rs/json/blob/master/src/value/index.rs
@@ -39,34 +37,16 @@ impl Index for str {
3937
Item::Value(ref v) => v
4038
.as_inline_table()
4139
.and_then(|t| t.items.get(self))
42-
.and_then(|kv| {
43-
if !kv.value.is_none() {
44-
Some(&kv.value)
45-
} else {
46-
None
47-
}
48-
}),
40+
.map(|kv| &kv.value),
4941
_ => None,
5042
}
5143
}
5244
fn index_mut<'v>(&self, v: &'v mut Item) -> Option<&'v mut Item> {
53-
if let Item::None = *v {
54-
let mut t = InlineTable::default();
55-
t.items.insert(
56-
InternalString::from(self),
57-
TableKeyValue::new(Key::new(self), Item::None),
58-
);
59-
*v = value(Value::InlineTable(t));
60-
}
6145
match *v {
62-
Item::Table(ref mut t) => Some(t.entry(self).or_insert(Item::None)),
63-
Item::Value(ref mut v) => v.as_inline_table_mut().map(|t| {
64-
&mut t
65-
.items
66-
.entry(InternalString::from(self))
67-
.or_insert_with(|| TableKeyValue::new(Key::new(self), Item::None))
68-
.value
69-
}),
46+
Item::Table(ref mut t) => t.get_mut(self),
47+
Item::Value(ref mut v) => v
48+
.as_inline_table_mut()
49+
.and_then(|t| t.items.get_mut(self).map(|kv| &mut kv.value)),
7050
_ => None,
7151
}
7252
}
@@ -123,7 +103,7 @@ impl<'s> ops::Index<&'s str> for Table {
123103

124104
impl<'s> ops::IndexMut<&'s str> for Table {
125105
fn index_mut(&mut self, key: &'s str) -> &mut Item {
126-
self.entry(key).or_insert(Item::None)
106+
self.get_mut(key).expect("index not found")
127107
}
128108
}
129109

src/inline_table.rs

+20-28
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,8 @@ impl InlineTable {
205205
match self.items.entry(key.into()) {
206206
indexmap::map::Entry::Occupied(mut entry) => {
207207
// Ensure it is a `Value` to simplify `InlineOccupiedEntry`'s code.
208-
let scratch = std::mem::take(&mut entry.get_mut().value);
209-
let scratch = Item::Value(
210-
scratch
211-
.into_value()
212-
// HACK: `Item::None` is a corner case of a corner case, let's just pick a
213-
// "safe" value
214-
.unwrap_or_else(|_| Value::InlineTable(Default::default())),
215-
);
208+
let scratch = std::mem::replace(&mut entry.get_mut().value, crate::value(0));
209+
let scratch = Item::Value(scratch.into_value());
216210
entry.get_mut().value = scratch;
217211

218212
InlineEntry::Occupied(InlineOccupiedEntry { entry })
@@ -229,14 +223,8 @@ impl InlineTable {
229223
match self.items.entry(key.get().into()) {
230224
indexmap::map::Entry::Occupied(mut entry) => {
231225
// Ensure it is a `Value` to simplify `InlineOccupiedEntry`'s code.
232-
let scratch = std::mem::take(&mut entry.get_mut().value);
233-
let scratch = Item::Value(
234-
scratch
235-
.into_value()
236-
// HACK: `Item::None` is a corner case of a corner case, let's just pick a
237-
// "safe" value
238-
.unwrap_or_else(|_| Value::InlineTable(Default::default())),
239-
);
226+
let scratch = std::mem::replace(&mut entry.get_mut().value, crate::value(0));
227+
let scratch = Item::Value(scratch.into_value());
240228
entry.get_mut().value = scratch;
241229

242230
InlineEntry::Occupied(InlineOccupiedEntry { entry })
@@ -285,7 +273,7 @@ impl InlineTable {
285273
let kv = TableKeyValue::new(Key::new(key), Item::Value(value));
286274
self.items
287275
.insert(InternalString::from(key), kv)
288-
.and_then(|kv| kv.value.into_value().ok())
276+
.map(|kv| kv.value.into_value())
289277
}
290278

291279
/// Inserts a key-value pair into the map.
@@ -294,21 +282,20 @@ impl InlineTable {
294282
self.items
295283
.insert(InternalString::from(key.get()), kv)
296284
.filter(|kv| kv.value.is_value())
297-
.map(|kv| kv.value.into_value().unwrap())
285+
.map(|kv| kv.value.into_value())
298286
}
299287

300288
/// Removes an item given the key.
301289
pub fn remove(&mut self, key: &str) -> Option<Value> {
302-
self.items
303-
.shift_remove(key)
304-
.and_then(|kv| kv.value.into_value().ok())
290+
self.items.shift_remove(key).map(|kv| kv.value.into_value())
305291
}
306292

307293
/// Removes a key from the map, returning the stored key and value if the key was previously in the map.
308294
pub fn remove_entry(&mut self, key: &str) -> Option<(Key, Value)> {
309-
self.items.shift_remove(key).and_then(|kv| {
295+
self.items.shift_remove(key).map(|kv| {
310296
let key = kv.key;
311-
kv.value.into_value().ok().map(|value| (key, value))
297+
let value = kv.value.into_value();
298+
(key, value)
312299
})
313300
}
314301
}
@@ -351,7 +338,7 @@ impl IntoIterator for InlineTable {
351338
self.items
352339
.into_iter()
353340
.filter(|(_, kv)| kv.value.is_value())
354-
.map(|(k, kv)| (k, kv.value.into_value().unwrap())),
341+
.map(|(k, kv)| (k, kv.value.into_value())),
355342
)
356343
}
357344
}
@@ -395,6 +382,12 @@ impl TableLike for InlineTable {
395382
.map(|(_, kv)| (kv.key.as_mut(), &mut kv.value)),
396383
)
397384
}
385+
fn len(&self) -> usize {
386+
self.len()
387+
}
388+
fn is_empty(&self) -> bool {
389+
self.is_empty()
390+
}
398391
fn get<'s>(&'s self, key: &str) -> Option<&'s Item> {
399392
self.items.get(key).map(|kv| &kv.value)
400393
}
@@ -405,8 +398,7 @@ impl TableLike for InlineTable {
405398
self.contains_key(key)
406399
}
407400
fn insert(&mut self, key: &str, value: Item) -> Option<Item> {
408-
self.insert(key, value.into_value().unwrap())
409-
.map(Item::Value)
401+
self.insert(key, value.into_value()).map(Item::Value)
410402
}
411403
fn remove(&mut self, key: &str) -> Option<Item> {
412404
self.remove(key).map(Item::Value)
@@ -526,12 +518,12 @@ impl<'a> InlineOccupiedEntry<'a> {
526518
pub fn insert(&mut self, value: Value) -> Value {
527519
let mut value = Item::Value(value);
528520
std::mem::swap(&mut value, &mut self.entry.get_mut().value);
529-
value.into_value().unwrap()
521+
value.into_value()
530522
}
531523

532524
/// Takes the value out of the entry, and returns it
533525
pub fn remove(self) -> Value {
534-
self.entry.shift_remove().value.into_value().unwrap()
526+
self.entry.shift_remove().value.into_value()
535527
}
536528
}
537529

src/item.rs

+7-33
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ use crate::{Array, InlineTable, Table, Value};
88
/// Type representing either a value, a table, an array of tables, or none.
99
#[derive(Debug, Clone)]
1010
pub enum Item {
11-
/// Type representing none.
12-
None,
1311
/// Type representing value.
1412
Value(Value),
1513
/// Type representing table.
@@ -18,24 +16,12 @@ pub enum Item {
1816
ArrayOfTables(ArrayOfTables),
1917
}
2018

21-
impl Item {
22-
/// Sets `self` to the given item iff `self` is none and
23-
/// returns a mutable reference to `self`.
24-
pub fn or_insert(&mut self, item: Item) -> &mut Item {
25-
if self.is_none() {
26-
*self = item
27-
}
28-
self
29-
}
30-
}
31-
3219
// TODO: This should be generated by macro or derive
3320
/// Downcasting
3421
impl Item {
3522
/// Text description of value type
3623
pub fn type_name(&self) -> &'static str {
3724
match self {
38-
Item::None => "none",
3925
Item::Value(v) => v.type_name(),
4026
Item::Table(..) => "table",
4127
Item::ArrayOfTables(..) => "array of tables",
@@ -113,24 +99,23 @@ impl Item {
11399
}
114100
}
115101
/// Casts `self` to value.
116-
pub fn into_value(self) -> Result<Value, Self> {
102+
pub fn into_value(self) -> Value {
117103
match self {
118-
Item::None => Err(self),
119-
Item::Value(v) => Ok(v),
104+
Item::Value(v) => v,
120105
Item::Table(v) => {
121106
let v = v.into_inline_table();
122-
Ok(Value::InlineTable(v))
107+
Value::InlineTable(v)
123108
}
124109
Item::ArrayOfTables(v) => {
125110
let v = v.into_array();
126-
Ok(Value::Array(v))
111+
Value::Array(v)
127112
}
128113
}
129114
}
130115
/// In-place convert to a value
131116
pub fn make_value(&mut self) {
132-
let other = std::mem::take(self);
133-
let other = other.into_value().map(Item::Value).unwrap_or(Item::None);
117+
let other = std::mem::replace(self, value(0));
118+
let other = Item::Value(other.into_value());
134119
*self = other;
135120
}
136121
/// Casts `self` to table.
@@ -164,7 +149,7 @@ impl Item {
164149
}
165150
// Starting private because the name is unclear
166151
pub(crate) fn make_item(&mut self) {
167-
let other = std::mem::take(self);
152+
let other = std::mem::replace(self, value(0));
168153
let other = match other.into_table().map(crate::Item::Table) {
169154
Ok(i) => i,
170155
Err(i) => i,
@@ -187,10 +172,6 @@ impl Item {
187172
pub fn is_array_of_tables(&self) -> bool {
188173
self.as_array_of_tables().is_some()
189174
}
190-
/// Returns true iff `self` is `None`.
191-
pub fn is_none(&self) -> bool {
192-
matches!(*self, Item::None)
193-
}
194175

195176
// Duplicate Value downcasting API
196177

@@ -296,12 +277,6 @@ impl Item {
296277
}
297278
}
298279

299-
impl Default for Item {
300-
fn default() -> Self {
301-
Item::None
302-
}
303-
}
304-
305280
impl FromStr for Item {
306281
type Err = crate::TomlError;
307282

@@ -315,7 +290,6 @@ impl FromStr for Item {
315290
impl std::fmt::Display for Item {
316291
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
317292
match &self {
318-
Item::None => Ok(()),
319293
Item::Value(v) => v.fmt(f),
320294
Item::Table(v) => v.fmt(f),
321295
Item::ArrayOfTables(v) => v.fmt(f),

src/parser/table.rs

-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ impl TomlParser {
113113
Item::Table(ref mut sweet_child_of_mine) => {
114114
table = sweet_child_of_mine;
115115
}
116-
_ => unreachable!(),
117116
}
118117
}
119118
Ok(table)

src/ser/pretty.rs

-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ impl crate::visit_mut::VisitMut for Pretty {
4545

4646
fn remove_table_prefix(node: &mut crate::Item) {
4747
match node {
48-
crate::Item::None => {}
4948
crate::Item::Value(_) => {}
5049
crate::Item::Table(t) => t.decor_mut().set_prefix(""),
5150
crate::Item::ArrayOfTables(a) => {

src/ser/table.rs

+8-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{Error, ErrorKind, KeySerializer};
1+
use super::{Error, KeySerializer};
22

33
#[doc(hidden)]
44
pub struct SerializeItemTable {
@@ -111,17 +111,12 @@ impl serde::ser::SerializeMap for SerializeKeyValuePairs {
111111
let item = match res {
112112
Ok(item) => item,
113113
Err(e) => {
114-
if e.kind != ErrorKind::UnsupportedNone {
115-
return Err(e);
116-
}
117-
crate::Item::None
114+
return Err(e);
118115
}
119116
};
120-
if !item.is_none() {
121-
let key = self.key.take().unwrap();
122-
let kv = crate::table::TableKeyValue::new(crate::Key::new(&key), item);
123-
self.items.insert(key, kv);
124-
}
117+
let key = self.key.take().unwrap();
118+
let kv = crate::table::TableKeyValue::new(crate::Key::new(&key), item);
119+
self.items.insert(key, kv);
125120
Ok(())
126121
}
127122

@@ -146,16 +141,11 @@ impl serde::ser::SerializeStruct for SerializeKeyValuePairs {
146141
let item = match res {
147142
Ok(item) => item,
148143
Err(e) => {
149-
if e.kind != ErrorKind::UnsupportedNone {
150-
return Err(e);
151-
}
152-
crate::Item::None
144+
return Err(e);
153145
}
154146
};
155-
if !item.is_none() {
156-
let kv = crate::table::TableKeyValue::new(crate::Key::new(key), item);
157-
self.items.insert(crate::InternalString::from(key), kv);
158-
}
147+
let kv = crate::table::TableKeyValue::new(crate::Key::new(key), item);
148+
self.items.insert(crate::InternalString::from(key), kv);
159149
Ok(())
160150
}
161151

0 commit comments

Comments
 (0)