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
extern crate regex; use colored::*; use std::process::exit; use regex::RegexSet; use nom::IResult; use std::path::PathBuf; pub fn file_contents_to_regex(file: &str, file_path: &PathBuf) -> RegexSet { let processed_vec: Vec<&str> = process_to_vector(file); let processed_str: String = processed_vec.join(""); let lines = processed_str.split_whitespace(); debugln!("{:?}", lines.clone().collect::<Vec<&str>>()); let maybe_set = RegexSet::new(lines); if let Ok(s) = maybe_set { debugln!("{:?}",s); s } else { eprintln!("{}: failed to parse .gitignore at {:?}, ignoring", "Warning".yellow(), file_path); let empty: Vec<&str> = Vec::new(); RegexSet::new(empty) .expect("Error creating regex from empty vector") } } pub fn process_to_vector(input: &str) -> Vec<&str> { match process(input) { IResult::Done(_, result) => result, _ => { eprintln!("{}: Failed to parse gitignore", "Error".red()) ; exit(0xf001) } } } named!(process<&str, Vec<&str>>, do_parse!( opt!(first_line) >> r: many0!(options) >> (r) ) ); named!(options<&str, &str>, alt!( tag!("\n") | gitignore_comment | is_not!("*?.#") | parse_asterix | parse_period | parse_questionmark | parse_not_comment ) ); named!(parse_not_comment<&str, &str>, do_parse!( is_not!("\n") >> ("#") ) ); named!(first_line<&str, &str>, do_parse!( tag!("#") >> is_not!("\n") >> tag!("\n") >> ("\n") ) ); named!(gitignore_comment<&str, &str>, do_parse!( tag!("\n#") >> is_not!("\n") >> ("\n") ) ); named!(parse_period<&str, &str>, do_parse!( tag!(".") >> ("\\.") ) ); named!(parse_asterix<&str, &str>, do_parse!( tag!("*") >> opt!(do_parse!(tag!("\n") >> eof!() >> (""))) >> (".*") ) ); named!(parse_questionmark<&str, &str>, do_parse!( tag!("?") >> opt!(do_parse!(tag!("\n") >> eof!() >> (""))) >> (".") ) );