Skip to main content

better_url/util/parts/query/either/
iter.rs

1//! [`QueryIter`].
2
3use crate::prelude::*;
4
5/// A [`DoubleEndedIterator`] of [`QuerySegment`]s.
6#[derive(Debug, Clone)]
7pub struct QueryIter<'a> {
8    /** The [`SplitAmpersands`]. */ iter  : SplitAmpersands<'a>,
9    /** The [`QueryType`].       */ r#type: QueryType,
10}
11
12impl<'a> QueryIter<'a> {
13    /// Create a new [`Self`] from a [`SplitAmpersands`] without validity checks.
14    /// # Safety
15    /// Every output of [`SplitAmpersands::next`] must be a valid input to [`QuerySegment::new_unchecked`] with the specified [`QueryType`].
16    pub unsafe fn from_parts(iter: SplitAmpersands<'a>, r#type: QueryType) -> Self {
17        Self {iter, r#type}
18    }
19
20    /// Get the inner [`SplitAmpersands`] and [`QueryType`].
21    pub fn into_parts(self) -> (SplitAmpersands<'a>, QueryType) {
22        (self.iter, self.r#type)
23    }
24
25    /// The range of the remainder as a [`str`].
26    pub fn range_str<B: RangeBounds<isize>>(&self, range: B) -> Option<&'a str> {
27        self.iter.range(range)
28    }
29
30    /// The range of the remainder as a [`Query`].
31    pub fn range<B: RangeBounds<isize>>(&self, range: B) -> Option<Query<'a>> {
32        self.range_str(range).map(|x| unsafe {Query::new_unchecked(x, self.r#type)})
33    }
34
35    /// The remaining [`str`].
36    pub fn remainder_str(&self) -> Option<&'a str> {
37        self.iter.remainder()
38    }
39
40    /// The remaining [`Query`].
41    pub fn remainder(&self) -> Option<Query<'a>> {
42        Some(unsafe {Query::new_unchecked(self.remainder_str()?, self.r#type)})
43    }
44
45    /// The [`QueryType`].
46    pub fn r#type(&self) -> QueryType {
47        self.r#type
48    }
49}
50
51impl<'a> IntoIterator for &'a Query<'_> {
52    type IntoIter = QueryIter   <'a>;
53    type Item     = QuerySegment<'a>;
54
55    fn into_iter(self) -> Self::IntoIter {
56        QueryIter {
57            iter: SplitAmpersands(Some(self.as_str())),
58            r#type: self.r#type(),
59        }
60    }
61}
62
63impl<'a> IntoIterator for &'a MaybeQuery<'_> {
64    type IntoIter = QueryIter   <'a>;
65    type Item     = QuerySegment<'a>;
66
67    fn into_iter(self) -> Self::IntoIter {
68        QueryIter {
69            iter: SplitAmpersands(self.as_str()),
70            r#type: self.r#type(),
71        }
72    }
73}
74
75impl<'a> From<SpecialQueryIter   <'a>> for QueryIter<'a> {fn from(value: SpecialQueryIter   <'a>) -> Self {Self {iter: value.into_inner(), r#type: QueryType::Special   }}}
76impl<'a> From<NonSpecialQueryIter<'a>> for QueryIter<'a> {fn from(value: NonSpecialQueryIter<'a>) -> Self {Self {iter: value.into_inner(), r#type: QueryType::NonSpecial}}}
77
78impl<'a> Iterator for QueryIter<'a> {
79    type Item = QuerySegment<'a>;
80
81    fn next(&mut self) -> Option<Self::Item> {
82        self.iter.next().map(|x| unsafe {QuerySegment::new_unchecked(x, self.r#type)})
83    }
84
85    fn nth(&mut self, n: usize) -> Option<Self::Item> {
86        self.iter.nth(n).map(|x| unsafe {QuerySegment::new_unchecked(x, self.r#type)})
87    }
88
89    fn count(self) -> usize {
90        self.iter.count()
91    }
92}
93
94impl<'a> DoubleEndedIterator for QueryIter<'a> {
95    fn next_back(&mut self) -> Option<Self::Item> {
96        self.iter.next_back().map(|x| unsafe {QuerySegment::new_unchecked(x, self.r#type)})
97    }
98
99    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
100        self.iter.nth_back(n).map(|x| unsafe {QuerySegment::new_unchecked(x, self.r#type)})
101    }
102}