1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
use clouseau_core::{Queryable, Value}; use std::iter; #[derive(Debug, Clone, Copy)] pub struct Context<'a> { root: &'a dyn Queryable<'a>, } #[derive(Default, Debug)] pub struct Query { path: Path, } #[derive(Debug, Default)] pub struct Path { segments: Vec<Segment>, } #[derive(Debug)] pub struct Segment { segment_type: SegmentType, filter: Option<Pred>, } #[derive(Debug)] pub enum SegmentType { Root, Current, Child(Value), Children, } #[derive(Debug)] pub enum PathOrValue { Path(Path), Value(Value), } #[derive(Debug)] pub struct Pred { match_type: MatchType, path: Path, compare: Compare, rhs: PathOrValue, } #[derive(Debug)] pub enum MatchType { Any, All, } #[derive(Debug)] pub enum Compare { LessThan, LessThanEq, Eq, GreaterThan, GreaterThanEq, } impl Query { pub fn new(path: Path) -> Self { Self { path } } pub fn exec<'a>(&'a self, q: &'a dyn Queryable<'a>) -> impl Iterator<Item = Value> + 'a { let ctx = Context { root: q }; self.path.exec(ctx, q) } } impl Compare { fn compare(&self, a: &Value, b: &Value) -> bool { match (a, b) { (Value::String(a), Value::String(b)) => match self { Compare::LessThan => a < b, Compare::LessThanEq => a <= b, Compare::Eq => a == b, Compare::GreaterThan => a > b, Compare::GreaterThanEq => a >= b, }, (Value::Int(a), Value::Int(b)) => match self { Compare::LessThan => a < b, Compare::LessThanEq => a <= b, Compare::Eq => a == b, Compare::GreaterThan => a > b, Compare::GreaterThanEq => a >= b, }, _ => false, } } } impl Path { pub fn append(mut self, segment: Segment) -> Self { self.segments.push(segment); self } pub fn append_filter(mut self, filter: Pred) -> Self { if let Some(segment) = self.segments.last_mut() { segment.filter = Some(filter); } self } pub fn exec<'a>( &'a self, ctx: Context<'a>, q: &'a dyn Queryable<'a>, ) -> impl Iterator<Item = Value> + 'a { let init: Box<dyn Iterator<Item = &'a dyn Queryable<'a>>> = Box::new(iter::once(q as _)); self.segments .iter() .fold(init, move |i, segment| { Box::from(i.flat_map(move |qq| segment.exec(ctx, qq))) }) .flat_map(|qq| Box::from(qq.data().into_iter())) } } impl Segment { pub fn exec<'a>( &'a self, ctx: Context<'a>, q: &'a dyn Queryable<'a>, ) -> Box<dyn Iterator<Item = &'a dyn Queryable<'a>> + 'a> { if let Some(pred) = &self.filter { Box::from( self.segment_type .exec(ctx, q) .filter(move |&v| pred.filter(ctx, v)), ) } else { self.segment_type.exec(ctx, q) } } } impl Pred { pub fn new(path: Path, compare: Compare, rhs: PathOrValue, match_type: MatchType) -> Pred { Self { path, compare, rhs, match_type, } } pub fn filter<'a>(&'a self, ctx: Context<'a>, q: &'a dyn Queryable<'a>) -> bool { let mut values = self.path.exec(ctx, q).peekable(); match self.match_type { MatchType::Any => match &self.rhs { PathOrValue::Path(path) => { values.peek().is_some() && values.any(|v| { let mut rhs = path.exec(ctx, q).peekable(); rhs.peek().is_some() && rhs.any(|r| self.compare.compare(&v, &r)) }) } PathOrValue::Value(value) => { values.peek().is_some() && values.any(|v| self.compare.compare(&v, value)) } }, MatchType::All => match &self.rhs { PathOrValue::Path(path) => { values.peek().is_some() && values.any(|v| { let mut rhs = path.exec(ctx, q).peekable(); rhs.peek().is_some() && rhs.all(|r| self.compare.compare(&v, &r)) }) } PathOrValue::Value(value) => { values.peek().is_some() && values.all(|v| self.compare.compare(&v, value)) } }, } } } impl SegmentType { pub fn exec<'a>( &'a self, ctx: Context<'a>, q: &'a dyn Queryable<'a>, ) -> Box<dyn Iterator<Item = &'a dyn Queryable<'a>> + 'a> { match self { SegmentType::Child(s) => Box::from(q.member(&s).into_iter()), SegmentType::Children => q.all(), SegmentType::Current => Box::from(iter::once(q)), SegmentType::Root => Box::from(iter::once(ctx.root)), } } pub fn to_segment(self) -> Segment { Segment { segment_type: self, filter: None, } } } impl From<SegmentType> for Segment { fn from(segment_type: SegmentType) -> Self { Segment { segment_type, filter: None, } } }