Skip to content

Commit 024ef23

Browse files
committed
fill the hashmap as it's parsed instead of collecting a Vec
1 parent 9ae2096 commit 024ef23

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

json/nom/src/main.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ named!(
6565
separated_pair!(ws!(string), char!(':'), value)
6666
);
6767

68+
/*
6869
named!(
6970
hash<HashMap<&str, JsonValue>>,
7071
map!(
@@ -78,6 +79,42 @@ named!(
7879
.collect()
7980
)
8081
);
82+
*/
83+
84+
fn hash_internal(input: &[u8]) -> nom::IResult<&[u8], HashMap<&str, JsonValue>> {
85+
match key_value(input) {
86+
Err(nom::Err::Error(_)) => Ok((input, HashMap::default())),
87+
Err(e) => Err(e),
88+
Ok((i, (key, value))) => {
89+
let mut map = HashMap::default();
90+
map.insert(key, value);
91+
92+
let mut input = i;
93+
loop {
94+
match do_parse!(input, sp >> char!(',') >> kv: key_value >> (kv)) {
95+
Err(nom::Err::Error(_)) => break Ok((input, map)),
96+
Err(e) => break Err(e),
97+
Ok((i, (key, value))) => {
98+
map.insert(key, value);
99+
input = i;
100+
}
101+
}
102+
}
103+
}
104+
}
105+
106+
}
107+
108+
named!(
109+
hash<HashMap<&str, JsonValue>>,
110+
delimited!(
111+
char!('{'),
112+
return_error!(
113+
hash_internal
114+
),
115+
preceded!(sp, char!('}'))
116+
)
117+
);
81118

82119
named!(
83120
value<JsonValue>,

0 commit comments

Comments
 (0)