1 use rustc_data_structures::fx::FxHashSet;
3 use std::hash::{Hash, Hasher};
6 use rustc_errors::Handler;
11 macro_rules! try_something {
12 ($e:expr, $diag:expr, $out:expr) => {{
16 $diag.struct_err(&e.to_string()).emit();
23 #[derive(Debug, Clone, Eq)]
26 pub children: FxHashSet<CssPath>,
29 // This PartialEq implementation IS NOT COMMUTATIVE!!!
31 // The order is very important: the second object must have all first's rules.
32 // However, the first doesn't require to have all second's rules.
33 impl PartialEq for CssPath {
34 fn eq(&self, other: &CssPath) -> bool {
35 if self.name != other.name {
38 for child in &self.children {
39 if !other.children.iter().any(|c| child == c) {
48 impl Hash for CssPath {
49 fn hash<H: Hasher>(&self, state: &mut H) {
50 self.name.hash(state);
51 for x in &self.children {
58 fn new(name: String) -> CssPath {
59 CssPath { name, children: FxHashSet::default() }
63 /// All variants contain the position they occur.
64 #[derive(Debug, Clone, Copy)]
66 StartLineComment(usize),
74 fn get_pos(&self) -> usize {
76 Events::StartLineComment(p)
77 | Events::StartComment(p)
78 | Events::EndComment(p)
80 | Events::OutBlock(p) => p,
84 fn is_comment(&self) -> bool {
86 Events::StartLineComment(_) | Events::StartComment(_) | Events::EndComment(_) => true,
92 fn previous_is_line_comment(events: &[Events]) -> bool {
93 if let Some(&Events::StartLineComment(_)) = events.last() { true } else { false }
96 fn is_line_comment(pos: usize, v: &[u8], events: &[Events]) -> bool {
97 if let Some(&Events::StartComment(_)) = events.last() {
103 fn load_css_events(v: &[u8]) -> Vec<Events> {
105 let mut events = Vec::with_capacity(100);
107 while pos + 1 < v.len() {
109 b'/' if v[pos + 1] == b'*' => {
110 events.push(Events::StartComment(pos));
113 b'/' if is_line_comment(pos, v, &events) => {
114 events.push(Events::StartLineComment(pos));
117 b'\n' if previous_is_line_comment(&events) => {
118 events.push(Events::EndComment(pos));
120 b'*' if v[pos + 1] == b'/' => {
121 events.push(Events::EndComment(pos + 2));
124 b'{' if !previous_is_line_comment(&events) => {
125 if let Some(&Events::StartComment(_)) = events.last() {
129 events.push(Events::InBlock(pos + 1));
131 b'}' if !previous_is_line_comment(&events) => {
132 if let Some(&Events::StartComment(_)) = events.last() {
136 events.push(Events::OutBlock(pos + 1));
145 fn get_useful_next(events: &[Events], pos: &mut usize) -> Option<Events> {
146 while *pos < events.len() {
147 if !events[*pos].is_comment() {
148 return Some(events[*pos]);
155 fn get_previous_positions(events: &[Events], mut pos: usize) -> Vec<usize> {
156 let mut ret = Vec::with_capacity(3);
158 ret.push(events[pos].get_pos());
163 if pos < 1 || !events[pos].is_comment() {
164 let x = events[pos].get_pos();
165 if *ret.last().unwrap() != x {
172 ret.push(events[pos].get_pos());
175 if ret.len() & 1 != 0 && events[pos].is_comment() {
178 ret.iter().rev().cloned().collect()
181 fn build_rule(v: &[u8], positions: &[usize]) -> String {
182 minifier::css::minify(
185 .map(|x| ::std::str::from_utf8(&v[x[0]..x[1]]).unwrap_or(""))
194 .filter(|s| !s.is_empty())
195 .collect::<Vec<&str>>()
198 .unwrap_or_else(|_| String::new())
201 fn inner(v: &[u8], events: &[Events], pos: &mut usize) -> FxHashSet<CssPath> {
202 let mut paths = Vec::with_capacity(50);
204 while *pos < events.len() {
205 if let Some(Events::OutBlock(_)) = get_useful_next(events, pos) {
209 if let Some(Events::InBlock(_)) = get_useful_next(events, pos) {
210 paths.push(CssPath::new(build_rule(v, &get_previous_positions(events, *pos))));
213 while let Some(Events::InBlock(_)) = get_useful_next(events, pos) {
214 if let Some(ref mut path) = paths.last_mut() {
215 for entry in inner(v, events, pos).iter() {
216 path.children.insert(entry.clone());
220 if let Some(Events::OutBlock(_)) = get_useful_next(events, pos) {
224 paths.iter().cloned().collect()
227 pub fn load_css_paths(v: &[u8]) -> CssPath {
228 let events = load_css_events(v);
231 let mut parent = CssPath::new("parent".to_owned());
232 parent.children = inner(v, &events, &mut pos);
236 pub fn get_differences(against: &CssPath, other: &CssPath, v: &mut Vec<String>) {
237 if against.name != other.name {
240 for child in &against.children {
241 let mut found = false;
242 let mut found_working = false;
243 let mut tmp = Vec::new();
245 for other_child in &other.children {
246 if child.name == other_child.name {
247 if child != other_child {
248 get_differences(child, other_child, &mut tmp);
250 found_working = true;
257 v.push(format!(" Missing \"{}\" rule", child.name));
258 } else if !found_working {
259 v.extend(tmp.iter().cloned());
265 pub fn test_theme_against<P: AsRef<Path>>(
269 ) -> (bool, Vec<String>) {
270 let data = try_something!(fs::read(f), diag, (false, vec![]));
272 let paths = load_css_paths(&data);
273 let mut ret = vec![];
274 get_differences(against, &paths, &mut ret);