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
101
102
103
104
105
106
107
108
109
110
111
use snafu::{ResultExt, Snafu};
use std::collections::HashSet;
use std::fmt::{Debug, Formatter};
use tl::NodeHandle;
use crate::{CssSelectorList, HtmlIndex};
#[derive(Debug, Snafu)]
pub enum CommandError {
#[snafu(display("Failed run FILTER"))]
FilterFailed {
#[snafu(backtrace)]
source: FilterError,
},
}
#[derive(Debug, Snafu)]
pub enum FilterError {
#[snafu(display("Failed to remove HTML node"))]
RemovingNodeFailed {
#[snafu(backtrace)]
source: crate::html::IndexError,
},
}
pub enum Command<'a> {
Only(CssSelectorList<'a>),
Filter(CssSelectorList<'a>),
}
impl<'a> Command<'a> {
pub(crate) fn execute(
&self,
input: &HashSet<NodeHandle>,
index: &'_ HtmlIndex<'a>,
) -> Result<HashSet<NodeHandle>, CommandError> {
match self {
Command::Only(selector) => Self::only(input, index, selector),
Command::Filter(selector) => {
Self::filter(input, index, selector).context(FilterFailedSnafu)
}
}
}
fn only(
input: &HashSet<NodeHandle>,
index: &'_ HtmlIndex<'a>,
selector: &CssSelectorList<'a>,
) -> Result<HashSet<NodeHandle>, CommandError> {
Ok(selector.query(index, input))
}
fn filter(
input: &HashSet<NodeHandle>,
index: &'_ HtmlIndex<'a>,
selector: &CssSelectorList<'a>,
) -> Result<HashSet<NodeHandle>, FilterError> {
let findings = selector.query(index, input);
for node in findings.iter() {
index.remove(node).context(RemovingNodeFailedSnafu)?
}
Ok(input.clone())
}
}
impl<'a> Debug for Command<'a> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str("TODO")
}
}
impl<'a> PartialEq for Command<'a> {
fn eq(&self, other: &Self) -> bool {
match self {
Command::Only(selector) => {
if let Command::Only(other_selector) = other {
selector == other_selector
} else {
false
}
}
Command::Filter(selector) => {
if let Command::Filter(other_selector) = other {
selector == other_selector
} else {
false
}
}
}
}
}