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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use crate::svg_data::Tag;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;

fn default_true() -> bool {
    true
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct MatchingAttrRule {
    pub included_attr: Option<HashSet<String>>,
    pub exclude_attr: HashSet<String>,
    #[serde(default = "default_true")]
    pub with_pos: bool,
    #[serde(default = "default_true")]
    pub with_style: bool,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct MatchingRule {
    pub name: String,
    pub apply_to_tags: Option<HashSet<String>>,
    #[serde(default = "HashSet::new")]
    pub dont_apply_to_tags: HashSet<String>,
    pub attr: Option<MatchingAttrRule>,
    #[serde(default = "default_true")]
    pub include_text: bool,
    #[serde(default = "default_true")]
    pub recursive: bool,
    pub childrens_rule: Option<String>,
    #[serde(default = "default_true")]
    pub sort_children: bool,
    pub prev_sibling_rule: Option<String>,
    pub next_sibling_rule: Option<String>,
}

impl MatchingRule {
    pub fn new_all_rule() -> MatchingRule {
        MatchingRule {
            name: "all".to_string(),
            apply_to_tags: None,
            dont_apply_to_tags: HashSet::new(),
            attr: Some(MatchingAttrRule {
                included_attr: None,
                exclude_attr: HashSet::new(),
                with_pos: true,
                with_style: true,
            }),
            include_text: true,
            recursive: true,
            childrens_rule: None,
            sort_children: false,
            prev_sibling_rule: None,
            next_sibling_rule: None,
        }
    }

    pub fn new_all_without_subtrees_rule() -> MatchingRule {
        MatchingRule {
            name: "all".to_string(),
            apply_to_tags: None,
            dont_apply_to_tags: HashSet::new(),
            attr: Some(MatchingAttrRule {
                included_attr: None,
                exclude_attr: HashSet::new(),
                with_pos: true,
                with_style: true,
            }),
            include_text: true,
            recursive: false,
            childrens_rule: None,
            sort_children: false,
            prev_sibling_rule: None,
            next_sibling_rule: None,
        }
    }

    pub fn new_all_subtrees_rule() -> MatchingRule {
        MatchingRule {
            name: "all_children".to_string(),
            apply_to_tags: None,
            dont_apply_to_tags: HashSet::new(),
            attr: None,
            include_text: false,
            recursive: true,
            childrens_rule: Some("all".to_string()),
            sort_children: false,
            prev_sibling_rule: None,
            next_sibling_rule: None,
        }
    }

    pub fn default_rules() -> Vec<MatchingRule> {
        vec![
            MatchingRule {
                name: "next_is_same_text".to_string(),
                apply_to_tags: None,
                dont_apply_to_tags: Default::default(),
                attr: Some(MatchingAttrRule {
                    included_attr: Some(HashSet::new()),
                    exclude_attr: HashSet::new(),
                    with_pos: true,
                    with_style: true,
                }),
                include_text: true,
                recursive: false,
                childrens_rule: None,
                sort_children: false,
                prev_sibling_rule: None,
                next_sibling_rule: Some("same_text_in_text".to_string()),
            },
            MatchingRule {
                name: "with_reorder".to_string(),
                apply_to_tags: None,
                dont_apply_to_tags: HashSet::new(),
                attr: Some(MatchingAttrRule {
                    included_attr: None,
                    exclude_attr: HashSet::new(),
                    with_pos: true,
                    with_style: true,
                }),
                include_text: true,
                recursive: true,
                childrens_rule: None,
                sort_children: true,
                prev_sibling_rule: None,
                next_sibling_rule: None,
            },
            MatchingRule {
                name: "without_attr".to_string(),
                apply_to_tags: None,
                dont_apply_to_tags: HashSet::new(),
                attr: None,
                include_text: true,
                recursive: true,
                childrens_rule: None,
                sort_children: true,
                prev_sibling_rule: None,
                next_sibling_rule: None,
            },
            MatchingRule {
                name: "without_text".to_string(),
                apply_to_tags: None,
                dont_apply_to_tags: HashSet::new(),
                attr: Some(MatchingAttrRule {
                    included_attr: None,
                    exclude_attr: HashSet::new(),
                    with_pos: true,
                    with_style: true,
                }),
                include_text: false,
                recursive: true,
                childrens_rule: None,
                sort_children: true,
                prev_sibling_rule: None,
                next_sibling_rule: None,
            },
            MatchingRule {
                name: "only_tag".to_string(),
                apply_to_tags: None,
                dont_apply_to_tags: HashSet::new(),
                attr: None,
                include_text: false,
                recursive: true,
                childrens_rule: None,
                sort_children: true,
                prev_sibling_rule: None,
                next_sibling_rule: None,
            },
        ]
    }

    pub fn applies_to_tag(&self, tag: &Tag) -> bool {
        if let Some(includes) = &self.apply_to_tags {
            if !includes.contains(&tag.name.to_string()) {
                return false;
            }
        }
        !self.dont_apply_to_tags.contains(&tag.name.to_string())
    }

    fn attr_is_excluded(&self, attr: &str) -> bool {
        if let Some(attr_opt) = self.attr.as_ref() {
            if attr_opt.exclude_attr.contains(attr) {
                return true;
            }
            if !attr_opt.with_pos && ["x", "y", "cx", "cy"].contains(&attr) {
                return true;
            }
            if !attr_opt.with_style && ["fill", "stroke"].contains(&attr) {
                return true;
            }
            false
        } else {
            true
        }
    }

    pub fn included_sorted_attr<'a>(&self, tag: &'a Tag) -> Vec<&'a String> {
        let mut res = Vec::new();
        if let Some(attr_opt) = &self.attr {
            if let Some(inc_attr) = &attr_opt.included_attr {
                for attr in tag.args.keys() {
                    if inc_attr.contains(attr) && !self.attr_is_excluded(attr) {
                        res.push(attr);
                    }
                }
            } else {
                for attr in tag.args.keys() {
                    if !self.attr_is_excluded(attr) {
                        res.push(attr);
                    }
                }
            }
            res.sort();
        }
        res
    }
}