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
|
// Take a look at the license at the top of the repository in the LICENSE file.
use crate::json::json_minifier::JsonMinifier;
use std::fmt;
use std::str::Chars;
#[derive(Clone)]
pub struct JsonMultiFilter<'a, P: Clone> {
minifier: JsonMinifier,
iter: Chars<'a>,
predicate: P,
initialized: bool,
item1: Option<<Chars<'a> as Iterator>::Item>,
}
impl<'a, P: Clone> JsonMultiFilter<'a, P> {
#[inline]
pub fn new(iter: Chars<'a>, predicate: P) -> Self {
JsonMultiFilter {
minifier: JsonMinifier::default(),
iter,
predicate,
initialized: false,
item1: None,
}
}
}
impl<'a, P: Clone> fmt::Debug for JsonMultiFilter<'a, P> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Filter")
.field("minifier", &self.minifier)
.field("iter", &self.iter)
.field("initialized", &self.initialized)
.finish()
}
}
impl<'a, P: Clone> Iterator for JsonMultiFilter<'a, P>
where
P: FnMut(
&mut JsonMinifier,
&<Chars<'a> as Iterator>::Item,
Option<&<Chars<'a> as Iterator>::Item>,
) -> bool,
{
type Item = <Chars<'a> as Iterator>::Item;
#[inline]
fn next(&mut self) -> Option<<Chars<'a> as Iterator>::Item> {
if !self.initialized {
self.item1 = self.iter.next();
self.initialized = true;
}
while let Some(item) = self.item1.take() {
self.item1 = self.iter.next();
if (self.predicate)(&mut self.minifier, &item, self.item1.as_ref()) {
return Some(item);
}
}
None
}
}
impl<'a, P> JsonMultiFilter<'a, P>
where
P: FnMut(
&mut JsonMinifier,
&<Chars<'a> as Iterator>::Item,
Option<&<Chars<'a> as Iterator>::Item>,
) -> bool
+ Clone,
{
pub(super) fn write<W: std::io::Write>(self, mut w: W) -> std::io::Result<()> {
for token in self {
write!(w, "{}", token)?;
}
Ok(())
}
}
impl<'a, P> fmt::Display for JsonMultiFilter<'a, P>
where
P: FnMut(
&mut JsonMinifier,
&<Chars<'a> as Iterator>::Item,
Option<&<Chars<'a> as Iterator>::Item>,
) -> bool
+ Clone,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = (*self).clone();
for token in s {
write!(f, "{}", token)?;
}
Ok(())
}
}
|