serde_filter/filter.rs
1/// A Filter transforms a JSON value into a Filter::Output
2pub trait Filter {
3 type Output;
4 fn filter(&self, json: serde_json::Value) -> Result<Self::Output, anyhow::Error>;
5}
6
7/// Runs a filter function on a JSON value
8/// ### Example
9/// ```no_run
10/// use serde_filter::prelude::*;
11/// let json = serde_json::json!({
12/// "explanation": "test",
13/// "date": "2020-01-01",
14/// "title": "test",
15/// "url": "test",
16/// });
17/// let values = filter::<Match<String>>(json, &Match::new("explanation")).unwrap();
18/// assert_eq!(values, vec!["test".to_string()]);
19/// ```
20pub fn filter<F>(json: serde_json::Value, filter: &F) -> Result<F::Output, anyhow::Error>
21where
22 F: crate::filter::Filter,
23{
24 filter.filter(json)
25}