Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 2a846aa

Browse files
Merge #719
719: Add more members of `filter_parser` to `milli::` & `From<&str>` implementation for `Token` r=Kerollmops a=GregoryConrad ## What does this PR do? The current `milli::Filter` and `milli::FilterCondition` APIs require working with some members of `filter_parser` directly that `milli::` does *not* re-export to its users (at least when not parsing input using `parse`). Also, using `filter_parser` does not make sense when using milli from an embedded context where there is no query to parse. Instead of reworking `milli::Filter` and `milli::FilterCondition`, this PR adds two non-breaking changes that ease the use of milli: - Re-exports more members of the dependent version of `filter_parser` in `milli` - Implements `From<&str>` for `filter_parser::Token` - This will also allow some basic tests that need to create a `Token` from a string to avoid some boilerplate. In conjunction, both of these will allow milli users to easily create a `Token` from a `&str` without needing to add `filter_parser` as an extra dependency. Note: I wanted to use `FromStr` for the `From` implementation; however, it requires returning a `Result` which is not needed for the conversion. Thus, I just left it as `From<&str>`. Co-authored-by: Gregory Conrad <[email protected]>
2 parents d6eacb2 + 50954d3 commit 2a846aa

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

filter-parser/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ impl<'a> From<Span<'a>> for Token<'a> {
115115
}
116116
}
117117

118+
/// Allow [Token] to be constructed from &[str]
119+
impl<'a> From<&'a str> for Token<'a> {
120+
fn from(s: &'a str) -> Self {
121+
Token::from(Span::new_extra(s, s))
122+
}
123+
}
124+
118125
#[derive(Debug, Clone, PartialEq, Eq)]
119126
pub enum FilterCondition<'a> {
120127
Not(Box<Self>),
@@ -664,6 +671,13 @@ pub mod tests {
664671
assert!(filter.token_at_depth(2).is_some());
665672
assert!(filter.token_at_depth(3).is_none());
666673
}
674+
675+
#[test]
676+
fn token_from_str() {
677+
let s = "test string that should not be parsed";
678+
let token: Token = s.into();
679+
assert_eq!(token.value(), s);
680+
}
667681
}
668682

669683
impl<'a> std::fmt::Display for FilterCondition<'a> {

milli/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::collections::{BTreeMap, HashMap};
2222
use std::convert::{TryFrom, TryInto};
2323
use std::hash::BuildHasherDefault;
2424

25-
pub use filter_parser::{Condition, FilterCondition};
25+
pub use filter_parser::{Condition, FilterCondition, Span, Token};
2626
use fxhash::{FxHasher32, FxHasher64};
2727
pub use grenad::CompressionType;
2828
use serde_json::Value;

0 commit comments

Comments
 (0)