partiql_value/
bindings.rs

1use crate::datum::OwnedFieldView;
2use crate::{PairsIntoIter, PairsIter, Value};
3#[cfg(feature = "serde")]
4use serde::{Deserialize, Serialize};
5use std::borrow::Cow;
6use std::iter::Once;
7use unicase::UniCase;
8
9#[derive(Clone, Hash, Debug, Eq, PartialEq)]
10#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11pub enum BindingsName<'s> {
12    CaseSensitive(Cow<'s, str>),
13    CaseInsensitive(Cow<'s, str>),
14}
15
16impl<'s> BindingsName<'s> {
17    pub fn matcher(&'s self) -> BindingsMatcher<'s> {
18        BindingsMatcher::from(self)
19    }
20}
21
22#[derive(Clone, Hash, Debug, Eq, PartialEq)]
23pub enum BindingsMatcher<'s> {
24    CaseSensitive(&'s str),
25    CaseInsensitive(UniCase<&'s str>),
26}
27
28impl<'s> BindingsMatcher<'s> {
29    pub fn matches(&'s self, candidate: &str) -> bool {
30        match self {
31            BindingsMatcher::CaseSensitive(target) => *target == candidate,
32            BindingsMatcher::CaseInsensitive(target) => *target == UniCase::new(candidate),
33        }
34    }
35}
36
37impl<'s> From<&'s BindingsName<'s>> for BindingsMatcher<'s> {
38    fn from(name: &'s BindingsName<'_>) -> Self {
39        match name {
40            BindingsName::CaseSensitive(s) => BindingsMatcher::CaseSensitive(s.as_ref()),
41            BindingsName::CaseInsensitive(s) => {
42                BindingsMatcher::CaseInsensitive(UniCase::new(s.as_ref()))
43            }
44        }
45    }
46}
47
48#[derive(Debug, Clone)]
49pub enum BindingIter<'a> {
50    Tuple(PairsIter<'a>),
51    Single(Once<&'a Value>),
52    Empty,
53}
54
55impl<'a> Iterator for BindingIter<'a> {
56    type Item = (Option<&'a String>, &'a Value);
57
58    #[inline]
59    fn next(&mut self) -> Option<Self::Item> {
60        match self {
61            BindingIter::Tuple(t) => t.next().map(|(k, v)| (Some(k), v)),
62            BindingIter::Single(single) => single.next().map(|v| (None, v)),
63            BindingIter::Empty => None,
64        }
65    }
66
67    #[inline]
68    fn size_hint(&self) -> (usize, Option<usize>) {
69        match self {
70            BindingIter::Tuple(t) => t.size_hint(),
71            BindingIter::Single(_single) => (1, Some(1)),
72            BindingIter::Empty => (0, Some(0)),
73        }
74    }
75}
76
77pub enum BindingIntoIter {
78    Tuple(PairsIntoIter),
79    Single(Once<Value>),
80    Empty,
81    DynTuple(Box<dyn Iterator<Item = OwnedFieldView<Value>>>),
82}
83
84impl Iterator for BindingIntoIter {
85    type Item = (Option<String>, Value);
86
87    #[inline]
88    fn next(&mut self) -> Option<Self::Item> {
89        match self {
90            BindingIntoIter::Tuple(t) => t.next().map(|(k, v)| (Some(k), v)),
91            BindingIntoIter::Single(single) => single.next().map(|v| (None, v)),
92            BindingIntoIter::Empty => None,
93            BindingIntoIter::DynTuple(d) => d.next().map(|f| (Some(f.name), f.value)),
94        }
95    }
96
97    #[inline]
98    fn size_hint(&self) -> (usize, Option<usize>) {
99        match self {
100            BindingIntoIter::Tuple(t) => t.size_hint(),
101            BindingIntoIter::Single(_single) => (1, Some(1)),
102            BindingIntoIter::Empty => (0, Some(0)),
103            BindingIntoIter::DynTuple(d) => d.size_hint(),
104        }
105    }
106}