ql.rs/src/lib.rs

83 lines
1.8 KiB
Rust
Raw Normal View History

2022-05-19 12:27:06 -04:00
/// A dynamic query structure.
pub trait DynamicQuery {
type Definition;
fn filter_ref(&mut self, def: &Self::Definition);
#[inline(always)]
2022-05-19 12:27:06 -04:00
fn filter(mut self, def: &Self::Definition) -> Self
where
Self: Sized,
{
self.filter_ref(def);
self
}
}
mod macros;
#[cfg(test)]
mod tests {
use serde::{Deserialize, Serialize};
use super::DynamicQuery;
2022-05-19 12:27:06 -04:00
// just tests that the macro compiles
crate::query_def_struct! {
2022-05-19 13:08:44 -04:00
/// Foo doc.
#[derive(PartialEq)]
Foo {
2022-05-19 13:08:44 -04:00
/// Field doc.
2022-05-19 13:03:53 -04:00
#[serde(flatten)]
bar: (Bar),
2022-05-19 12:27:06 -04:00
baz: (bool),
bars: 'vec (Bar),
bars_by_bazs: 'map (bool, Bar),
}
2022-05-19 13:08:44 -04:00
/// Bar doc.
#[derive(PartialEq)]
Bar {
2022-05-19 13:03:53 -04:00
baz_inner: (bool),
2022-05-19 12:27:06 -04:00
}
}
2022-05-19 13:03:53 -04:00
crate::query_def_enum! {
FooOrBar {
Foo {
value: 'dyn (Foo),
},
Bar {
value: 'dyn (Bar),
}
}
}
2022-05-19 13:03:53 -04:00
// make sure A. serde is working and B. we get expected structure
#[test]
fn test_json() {
let x = Foo {
2022-05-19 13:03:53 -04:00
bar: Some(Bar {
baz_inner: Some(true),
}),
baz: Some(false),
bars: Some(vec![]),
bars_by_bazs: Some(std::collections::HashMap::new()),
};
assert_eq!(serde_json::to_string(&x).ok(), Some(r#"{"baz_inner":true,"baz":false,"bars":[],"bars_by_bazs":{}}"#.to_owned()));
assert_eq!(x.clone().filter(&FooDef {
bar: false,
baz: true,
bars: Some(BarDef::default()),
bars_by_bazs: None,
}), Foo {
bar: None,
baz: Some(false),
bars: Some(vec![]),
bars_by_bazs: None,
})
2022-05-19 13:03:53 -04:00
}
2022-05-19 12:27:06 -04:00
}