use std::hash::{Hash, Hasher};
use std::path::Path;
-use errors::Handler;
+use rustc_errors::Handler;
+
+#[cfg(test)]
+mod tests;
macro_rules! try_something {
- ($e:expr, $diag:expr, $out:expr) => ({
+ ($e:expr, $diag:expr, $out:expr) => {{
match $e {
Ok(c) => c,
Err(e) => {
return $out;
}
}
- })
+ }};
}
#[derive(Debug, Clone, Eq)]
impl CssPath {
fn new(name: String) -> CssPath {
- CssPath {
- name,
- children: FxHashSet::default(),
- }
+ CssPath { name, children: FxHashSet::default() }
}
}
impl Events {
fn get_pos(&self) -> usize {
match *self {
- Events::StartLineComment(p) |
- Events::StartComment(p) |
- Events::EndComment(p) |
- Events::InBlock(p) |
- Events::OutBlock(p) => p,
+ Events::StartLineComment(p)
+ | Events::StartComment(p)
+ | Events::EndComment(p)
+ | Events::InBlock(p)
+ | Events::OutBlock(p) => p,
}
}
fn is_comment(&self) -> bool {
match *self {
- Events::StartLineComment(_) |
- Events::StartComment(_) |
- Events::EndComment(_) => true,
+ Events::StartLineComment(_) | Events::StartComment(_) | Events::EndComment(_) => true,
_ => false,
}
}
}
fn previous_is_line_comment(events: &[Events]) -> bool {
- if let Some(&Events::StartLineComment(_)) = events.last() {
- true
- } else {
- false
- }
+ if let Some(&Events::StartLineComment(_)) = events.last() { true } else { false }
}
fn is_line_comment(pos: usize, v: &[u8], events: &[Events]) -> bool {
b'{' if !previous_is_line_comment(&events) => {
if let Some(&Events::StartComment(_)) = events.last() {
pos += 1;
- continue
+ continue;
}
events.push(Events::InBlock(pos + 1));
}
b'}' if !previous_is_line_comment(&events) => {
if let Some(&Events::StartComment(_)) = events.last() {
pos += 1;
- continue
+ continue;
}
events.push(Events::OutBlock(pos + 1));
}
} else {
ret.push(0);
}
- break
+ break;
}
ret.push(events[pos].get_pos());
pos -= 1;
}
fn build_rule(v: &[u8], positions: &[usize]) -> String {
- positions.chunks(2)
- .map(|x| ::std::str::from_utf8(&v[x[0]..x[1]]).unwrap_or(""))
- .collect::<String>()
- .trim()
- .replace("\n", " ")
- .replace("/", "")
- .replace("\t", " ")
- .replace("{", "")
- .replace("}", "")
- .split(' ')
- .filter(|s| s.len() > 0)
- .collect::<Vec<&str>>()
- .join(" ")
+ positions
+ .chunks(2)
+ .map(|x| ::std::str::from_utf8(&v[x[0]..x[1]]).unwrap_or(""))
+ .collect::<String>()
+ .trim()
+ .replace("\n", " ")
+ .replace("/", "")
+ .replace("\t", " ")
+ .replace("{", "")
+ .replace("}", "")
+ .split(' ')
+ .filter(|s| s.len() > 0)
+ .collect::<Vec<&str>>()
+ .join(" ")
}
fn inner(v: &[u8], events: &[Events], pos: &mut usize) -> FxHashSet<CssPath> {
while *pos < events.len() {
if let Some(Events::OutBlock(_)) = get_useful_next(events, pos) {
*pos += 1;
- break
+ break;
}
if let Some(Events::InBlock(_)) = get_useful_next(events, pos) {
paths.push(CssPath::new(build_rule(v, &get_previous_positions(events, *pos))));
pub fn get_differences(against: &CssPath, other: &CssPath, v: &mut Vec<String>) {
if against.name != other.name {
- return
+ return;
} else {
for child in &against.children {
let mut found = false;
found_working = true;
}
found = true;
- break
+ break;
}
}
if found == false {
diag: &Handler,
) -> (bool, Vec<String>) {
let data = try_something!(fs::read(f), diag, (false, vec![]));
+
let paths = load_css_paths(&data);
let mut ret = vec![];
get_differences(against, &paths, &mut ret);
(true, ret)
}
-
-#[cfg(test)]
-mod test {
- use super::*;
-
- #[test]
- fn test_comments_in_rules() {
- let text = r#"
-rule a {}
-
-rule b, c
-// a line comment
-{}
-
-rule d
-// another line comment
-e {}
-
-rule f/* a multine
-
-comment*/{}
-
-rule g/* another multine
-
-comment*/h
-
-i {}
-
-rule j/*commeeeeent
-
-you like things like "{}" in there? :)
-*/
-end {}"#;
-
- let against = r#"
-rule a {}
-
-rule b, c {}
-
-rule d e {}
-
-rule f {}
-
-rule gh i {}
-
-rule j end {}
-"#;
-
- let mut ret = Vec::new();
- get_differences(&load_css_paths(against.as_bytes()),
- &load_css_paths(text.as_bytes()),
- &mut ret);
- assert!(ret.is_empty());
- }
-
- #[test]
- fn test_text() {
- let text = r#"
-a
-/* sdfs
-*/ b
-c // sdf
-d {}
-"#;
- let paths = load_css_paths(text.as_bytes());
- assert!(paths.children.contains(&CssPath::new("a b c d".to_owned())));
- }
-
- #[test]
- fn test_comparison() {
- let x = r#"
-a {
- b {
- c {}
- }
-}
-"#;
-
- let y = r#"
-a {
- b {}
-}
-"#;
-
- let against = load_css_paths(y.as_bytes());
- let other = load_css_paths(x.as_bytes());
-
- let mut ret = Vec::new();
- get_differences(&against, &other, &mut ret);
- assert!(ret.is_empty());
- get_differences(&other, &against, &mut ret);
- assert_eq!(ret, vec![" Missing \"c\" rule".to_owned()]);
- }
-
- #[test]
- fn check_empty_css() {
- let events = load_css_events(&[]);
- assert_eq!(events.len(), 0);
- }
-
- #[test]
- fn check_invalid_css() {
- let events = load_css_events(b"*");
- assert_eq!(events.len(), 0);
- }
-}