1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::json::json_minifier::JsonMinifier;
4
5use std::fmt;
6use std::str::Chars;
7
8#[derive(Clone)]
9pub struct JsonMultiFilter<'a, P: Clone> {
10 minifier: JsonMinifier,
11 iter: Chars<'a>,
12 predicate: P,
13 initialized: bool,
14 item1: Option<<Chars<'a> as Iterator>::Item>,
15}
16
17impl<'a, P: Clone> JsonMultiFilter<'a, P> {
18 #[inline]
19 pub fn new(iter: Chars<'a>, predicate: P) -> Self {
20 JsonMultiFilter {
21 minifier: JsonMinifier::default(),
22 iter,
23 predicate,
24 initialized: false,
25 item1: None,
26 }
27 }
28}
29
30impl<'a, P: Clone> fmt::Debug for JsonMultiFilter<'a, P> {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 f&mut DebugStruct<'_, '_>.debug_struct("Filter")
33 .field("minifier", &self.minifier)
34 .field("iter", &self.iter)
35 .field(name:"initialized", &self.initialized)
36 .finish()
37 }
38}
39
40impl<'a, P: Clone> Iterator for JsonMultiFilter<'a, P>
41where
42 P: FnMut(
43 &mut JsonMinifier,
44 &<Chars<'a> as Iterator>::Item,
45 Option<&<Chars<'a> as Iterator>::Item>,
46 ) -> bool,
47{
48 type Item = <Chars<'a> as Iterator>::Item;
49
50 #[inline]
51 fn next(&mut self) -> Option<<Chars<'a> as Iterator>::Item> {
52 if !self.initialized {
53 self.item1 = self.iter.next();
54 self.initialized = true;
55 }
56
57 while let Some(item: char) = self.item1.take() {
58 self.item1 = self.iter.next();
59 if (self.predicate)(&mut self.minifier, &item, self.item1.as_ref()) {
60 return Some(item);
61 }
62 }
63 None
64 }
65}
66
67impl<'a, P> JsonMultiFilter<'a, P>
68where
69 P: FnMut(
70 &mut JsonMinifier,
71 &<Chars<'a> as Iterator>::Item,
72 Option<&<Chars<'a> as Iterator>::Item>,
73 ) -> bool
74 + Clone,
75{
76 pub(super) fn write<W: std::io::Write>(self, mut w: W) -> std::io::Result<()> {
77 for token: char in self {
78 write!(w, "{}", token)?;
79 }
80 Ok(())
81 }
82}
83
84impl<'a, P> fmt::Display for JsonMultiFilter<'a, P>
85where
86 P: FnMut(
87 &mut JsonMinifier,
88 &<Chars<'a> as Iterator>::Item,
89 Option<&<Chars<'a> as Iterator>::Item>,
90 ) -> bool
91 + Clone,
92{
93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94 let s: JsonMultiFilter<'_, P> = (*self).clone();
95 for token: char in s {
96 write!(f, "{}", token)?;
97 }
98 Ok(())
99 }
100}
101