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> {
    /// Find all nodes, beginning at the input, that match the given CSS selector
    /// and return only those
    Only(CssSelectorList<'a>),
    /// Find all nodes, beginning at the input, that match the given CSS selector
    /// and remove them from their parent nodes.
    /// Returns the input as result.
    Filter(CssSelectorList<'a>),
    // Map(String, Pipeline),
    // GetAttribute(String),
    // SetAttribute(String, Pipeline),
    // RemoveAttribute(String),
    // GetText(),
    // SetText(Pipeline),
    // RemoveText(),
}

impl<'a> Command<'a> {
    /// perform the action defined by the command on the set of nodes
    /// and return the calculated results.
    /// For some command the output can be equal to the input,
    /// others change the result-set
    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> {
    //TODO: Actually implement it
    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
                }
            }
        }
    }
}