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
use super::*;

pub struct Layers<'a, P: Processor> {
    layers: &'a [&'a str],
    ignored: usize,
    h: &'a mut P,
}

impl<'a, H: Processor> Layers<'a, H> {
    pub fn new(layers: &'a[&'a str], h: &'a mut H) -> Self {
        Layers {
            layers,
            ignored: 0,
            h,
        }
    }
}

impl<'a, P: Processor> Processor for Layers<'a, P> {
    fn process(&mut self, elt: Element) -> Result<(), failure::Error> {
        debug!("elt = {:?} {:?}", elt, self.ignored);
        if self.ignored > 0 {
            match elt {
                Element::EndGroup => self.ignored -= 1,
                Element::BeginGroup { .. } => self.ignored += 1,
                _ => {}
            }
            Ok(())
        } else {
            match elt {
                Element::EndGroup => {
                    self.h.process(Element::EndGroup)
                }
                Element::BeginGroup { id } => {
                    debug!("{:?} {:?}", id, self.layers);
                    // while we have empty id groups (i.e. top level),
                    // recurse.
                    if id.is_empty() || self.layers.iter().any(|id_| *id_ == id) {
                        self.h.process(Element::BeginGroup { id })?
                    } else {
                        self.ignored += 1;
                    }
                    Ok(())
                },
                e => self.h.process(e)
            }
        }
    }
    fn finish(&mut self) -> Result<(), failure::Error> {
        self.h.finish()
    }
}