Skip to content

Commit 0f0c9a7

Browse files
committed
Use ordermap to get (&K, &V) pairs
We won't need this when rust-lang/rust#46992 is implemented, but for now this is the easiest (and safe) way to untie Filter AST from its string representation, so it can live as long as the context does.
1 parent ffc2cb1 commit 0f0c9a7

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ version = "0.1.0"
55

66
[dependencies]
77
cidr = "0.0.3"
8+
ordermap = "0.3.5"
89
quick-error = "1.2.1"
910
regex = "0.2.5"
1011
serde = "1.0.27"

src/filter.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ use regex::bytes::Regex;
77
use serde::{Deserialize, Deserializer, Serialize, Serializer};
88
use serde::de::Error;
99
use types::{GetType, LhsValue, RhsValue, RhsValues, Type};
10+
use ordermap::OrderMap;
1011

1112
use std::borrow::{Borrow, Cow};
12-
use std::collections::HashMap;
1313
use std::hash::Hash;
1414
use std::iter::FromIterator;
1515

1616
pub struct Context<K, T> {
17-
fields: HashMap<K, T>,
17+
fields: OrderMap<K, T>,
1818
}
1919

2020
impl<K: Hash + Eq, T> FromIterator<(K, T)> for Context<K, T> {
2121
fn from_iter<I: IntoIterator<Item = (K, T)>>(iter: I) -> Self {
2222
Context {
23-
fields: HashMap::from_iter(iter),
23+
fields: OrderMap::from_iter(iter),
2424
}
2525
}
2626
}
@@ -32,8 +32,8 @@ fn combining_op(input: &str) -> (Option<CombiningOp>, &str) {
3232
}
3333
}
3434

35-
impl<K: Borrow<str> + Hash + Eq, T: GetType> Context<K, T> {
36-
fn simple_filter<'i>(&self, input: &'i str) -> LexResult<'i, Filter<'i>> {
35+
impl<'c, K: Borrow<str> + Hash + Eq, T: GetType> Context<K, T> {
36+
fn simple_filter<'i>(&'c self, input: &'i str) -> LexResult<'i, Filter<'c>> {
3737
if let Ok((op, input)) = UnaryOp::lex(input) {
3838
let input = input.trim_left();
3939
let (arg, input) = self.simple_filter(input)?;
@@ -52,10 +52,12 @@ impl<K: Borrow<str> + Hash + Eq, T: GetType> Context<K, T> {
5252

5353
let (lhs, input) = Field::lex(input)?;
5454

55-
let lhs_type = self.fields
56-
.get(lhs.path())
57-
.ok_or_else(|| (LexErrorKind::UnknownField, lhs.path()))?
58-
.get_type();
55+
let (_, lhs, lhs_type) = self.fields
56+
.get_full(lhs.path())
57+
.ok_or_else(|| (LexErrorKind::UnknownField, lhs.path()))?;
58+
59+
let lhs = Field::new(lhs.borrow());
60+
let lhs_type = lhs_type.get_type();
5961

6062
let input = input.trim_left();
6163

@@ -104,11 +106,11 @@ impl<K: Borrow<str> + Hash + Eq, T: GetType> Context<K, T> {
104106
}
105107

106108
fn filter_prec<'i>(
107-
&self,
108-
mut lhs: Filter<'i>,
109+
&'c self,
110+
mut lhs: Filter<'c>,
109111
min_prec: Option<CombiningOp>,
110112
mut lookahead: (Option<CombiningOp>, &'i str),
111-
) -> LexResult<'i, Filter<'i>> {
113+
) -> LexResult<'i, Filter<'c>> {
112114
while let Some(op) = lookahead.0 {
113115
let mut rhs = self.simple_filter(lookahead.1)?;
114116
loop {
@@ -135,13 +137,13 @@ impl<K: Borrow<str> + Hash + Eq, T: GetType> Context<K, T> {
135137
Ok((lhs, lookahead.1))
136138
}
137139

138-
fn combined_filter<'i>(&self, input: &'i str) -> LexResult<'i, Filter<'i>> {
140+
fn combined_filter<'i>(&'c self, input: &'i str) -> LexResult<'i, Filter<'c>> {
139141
let (lhs, input) = self.simple_filter(input)?;
140142
let lookahead = combining_op(input);
141143
self.filter_prec(lhs, None, lookahead)
142144
}
143145

144-
pub fn parse<'i>(&self, input: &'i str) -> Result<Filter<'i>, LexError<'i>> {
146+
pub fn parse<'i>(&'c self, input: &'i str) -> Result<Filter<'c>, LexError<'i>> {
145147
let (res, input) = self.combined_filter(input)?;
146148
if input.is_empty() {
147149
Ok(res)

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ extern crate serde_bytes;
1212
#[macro_use]
1313
extern crate serde_derive;
1414

15+
extern crate ordermap;
16+
1517
#[macro_use]
1618
pub mod lex;
1719

0 commit comments

Comments
 (0)