Skip to content

Commit 1857149

Browse files
committed
reworking macros into generic trait impls, stage 1
1 parent 3bc4e49 commit 1857149

File tree

2 files changed

+19
-68
lines changed

2 files changed

+19
-68
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "json"
3-
version = "0.11.3"
3+
version = "0.11.4"
44
authors = ["Maciej Hirsz <[email protected]>"]
55
description = "JSON implementation in Rust"
66
repository = "https://github.com/maciejhirsz/json-rust"

src/value/implements.rs

Lines changed: 18 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,6 @@ use object::Object;
1010

1111
use { JsonValue, Null };
1212

13-
macro_rules! implement_extras {
14-
($from:ty) => {
15-
impl From<Option<$from>> for JsonValue {
16-
fn from(val: Option<$from>) -> JsonValue {
17-
match val {
18-
Some(value) => value.into(),
19-
None => Null,
20-
}
21-
}
22-
}
23-
24-
impl From<Vec<$from>> for JsonValue {
25-
fn from(mut val: Vec<$from>) -> JsonValue {
26-
JsonValue::Array(
27-
val.drain(..)
28-
.map(|value| value.into())
29-
.collect()
30-
)
31-
}
32-
}
33-
34-
impl From<Vec<Option<$from>>> for JsonValue {
35-
fn from(mut val: Vec<Option<$from>>) -> JsonValue {
36-
JsonValue::Array(
37-
val.drain(..)
38-
.map(|item| item.into())
39-
.collect()
40-
)
41-
}
42-
}
43-
}
44-
}
45-
4613
macro_rules! implement_eq {
4714
($to:ident, $from:ty) => {
4815
impl PartialEq<$from> for JsonValue {
@@ -83,7 +50,7 @@ macro_rules! implement {
8350
}
8451

8552
implement_eq!($to, $from);
86-
implement_extras!($from);
53+
// implement_extras!($from);
8754
};
8855
($to:ident, $from:ty) => {
8956
impl From<$from> for JsonValue {
@@ -93,7 +60,7 @@ macro_rules! implement {
9360
}
9461

9562
implement_eq!($to, $from);
96-
implement_extras!($from);
63+
// implement_extras!($from);
9764
}
9865
}
9966

@@ -107,15 +74,27 @@ impl<'a> From<&'a str> for JsonValue {
10774
}
10875
}
10976

110-
impl<'a> From<Option<&'a str>> for JsonValue {
111-
fn from(val: Option<&'a str>) -> JsonValue {
77+
impl<T: Into<JsonValue>> From<Option<T>> for JsonValue {
78+
fn from(val: Option<T>) -> JsonValue {
11279
match val {
113-
Some(value) => value.into(),
114-
None => Null,
80+
Some(val) => val.into(),
81+
None => JsonValue::Null,
11582
}
11683
}
11784
}
11885

86+
impl<T: Into<JsonValue>> From<Vec<T>> for JsonValue {
87+
fn from(val: Vec<T>) -> JsonValue {
88+
let mut array = Vec::with_capacity(val.len());
89+
90+
for val in val {
91+
array.push(val.into());
92+
}
93+
94+
JsonValue::Array(array)
95+
}
96+
}
97+
11998
impl From<HashMap<String, JsonValue>> for JsonValue {
12099
fn from(mut val: HashMap<String, JsonValue>) -> JsonValue {
121100
let mut object = Object::with_capacity(val.len());
@@ -128,15 +107,6 @@ impl From<HashMap<String, JsonValue>> for JsonValue {
128107
}
129108
}
130109

131-
impl From<Option<HashMap<String, JsonValue>>> for JsonValue {
132-
fn from(val: Option<HashMap<String, JsonValue>>) -> JsonValue {
133-
match val {
134-
Some(value) => value.into(),
135-
None => Null,
136-
}
137-
}
138-
}
139-
140110
impl From<BTreeMap<String, JsonValue>> for JsonValue {
141111
fn from(mut val: BTreeMap<String, JsonValue>) -> JsonValue {
142112
let mut object = Object::with_capacity(val.len());
@@ -153,24 +123,6 @@ impl From<BTreeMap<String, JsonValue>> for JsonValue {
153123
}
154124
}
155125

156-
impl From<Option<BTreeMap<String, JsonValue>>> for JsonValue {
157-
fn from(val: Option<BTreeMap<String, JsonValue>>) -> JsonValue {
158-
match val {
159-
Some(value) => value.into(),
160-
None => Null,
161-
}
162-
}
163-
}
164-
165-
impl From<Option<JsonValue>> for JsonValue {
166-
fn from(val: Option<JsonValue>) -> JsonValue {
167-
match val {
168-
Some(value) => value,
169-
None => Null,
170-
}
171-
}
172-
}
173-
174126
impl<'a> PartialEq<&'a str> for JsonValue {
175127
fn eq(&self, other: &&str) -> bool {
176128
match *self {
@@ -226,5 +178,4 @@ implement!(Number, f32 as num);
226178
implement!(Number, f64 as num);
227179
implement!(Number, Number);
228180
implement!(Object, Object);
229-
implement!(Array, Vec<JsonValue>);
230181
implement!(Boolean, bool);

0 commit comments

Comments
 (0)