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
|
#[derive(Clone, Debug, PartialEq)]
pub enum QueryComponent {
/// Every child element of the tag
TagName(String),
/// Only direct children with the tag
DirectTagName(String),
/// The child element with the ID
Id(String),
/// Only direct children with the tag
DirectId(String),
/// Every child that has the class
Class(String),
/// Only direct children with the class
DirectClass(String),
}
pub fn parse_query(mut raw: &str) -> Result<Vec<QueryComponent>, QueryParseError> {
let mut components = vec![];
let mut next_direct = false;
loop {
if raw.is_empty() {
break Ok(components);
}
let part = match raw.find(['>', ' ']) {
None => {
let part = raw;
raw = &raw[raw.len()..raw.len()];
part
}
Some(idx) => {
let part = &raw[..idx];
if &raw[idx..idx + 1] == ">" {
if next_direct {
return Err(QueryParseError::DoubleDirect);
} else {
next_direct = true;
}
}
raw = &raw[idx + 1..];
part
}
};
if part.is_empty() {
continue;
}
if let Some(id) = part.strip_prefix('#') {
if id.contains(['#', '.']) {
return Err(QueryParseError::UnknownComponent {
malformed: id.into(),
});
}
if next_direct {
components.push(QueryComponent::DirectId(id.into()));
next_direct = false;
} else {
components.push(QueryComponent::Id(id.into()));
}
} else if let Some(class) = part.strip_prefix('.') {
if class.contains(['#', '.']) {
return Err(QueryParseError::UnknownComponent {
malformed: class.into(),
});
}
if next_direct {
components.push(QueryComponent::DirectClass(class.into()));
next_direct = false;
} else {
components.push(QueryComponent::Class(class.into()));
}
} else {
if part.contains(['#', '.']) {
return Err(QueryParseError::UnknownComponent {
malformed: part.into(),
});
}
if next_direct {
components.push(QueryComponent::DirectTagName(part.into()));
next_direct = false;
} else {
components.push(QueryComponent::TagName(part.into()));
}
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum QueryParseError {
#[error("Query ends with '>' which does not make sense. Are you missing a selector?")]
EndsInDirect,
#[error("Two direct descendent selectors (>) appeard together")]
DoubleDirect,
#[error(
"The component {malformed} does not make sense. Valid selectors are #id, .class, and tag"
)]
UnknownComponent { malformed: String },
}
#[cfg(test)]
mod test {
use super::parse_query;
macro_rules! qc {
($tag:expr) => {
$crate::query::QueryComponent::TagName(String::from($tag))
};
(>$tag:expr) => {
$crate::query::QueryComponent::DirectTagName(String::from($tag))
};
(ID $tag:expr) => {
$crate::query::QueryComponent::Id(String::from($tag))
};
(>ID $tag:expr) => {
$crate::query::QueryComponent::DirectId(String::from($tag))
};
(. $tag:expr) => {
$crate::query::QueryComponent::Class(String::from($tag))
};
(>. $tag:expr) => {
$crate::query::QueryComponent::DirectClass(String::from($tag))
};
}
#[test]
fn parses_tags() {
let raw = "main section p";
let parse = parse_query(raw).unwrap();
assert_eq!(parse, vec![qc!("main"), qc!("section"), qc!("p")])
}
#[test]
fn parses_direct_tags() {
let raw = "main > section > p";
let parse = parse_query(raw).unwrap();
assert_eq!(parse, vec![qc!("main"), qc!(> "section"), qc!(> "p")])
}
#[test]
fn parses_id() {
let raw = "main #job";
let parse = parse_query(raw).unwrap();
assert_eq!(parse, vec![qc!("main"), qc!(ID "job")])
}
#[test]
fn parses_direct_id() {
let raw = "main > #job";
let parse = parse_query(raw).unwrap();
assert_eq!(parse, vec![qc!("main"), qc!(>ID "job")])
}
#[test]
fn parses_class() {
let raw = "main .post";
let parse = parse_query(raw).unwrap();
assert_eq!(parse, vec![qc!("main"), qc!(."post")])
}
#[test]
fn parses_direct_class() {
let raw = "main > .post";
let parse = parse_query(raw).unwrap();
assert_eq!(parse, vec![qc!("main"), qc!(>."post")])
}
#[test]
fn parses_complex() {
let raw = "main > article";
let parse = parse_query(raw).unwrap();
assert_eq!(parse, vec![qc!("main"), qc!(>."post")])
}
}
|