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
pub mod differ; pub mod sequencematcher; mod utils; use sequencematcher::{Sequence, SequenceMatcher}; use std::collections::HashMap; use std::fmt::Display; use utils::{format_range_context, format_range_unified}; pub fn get_close_matches<'a>( word: &str, possibilities: Vec<&'a str>, n: usize, cutoff: f32, ) -> Vec<&'a str> { if !(0.0 <= cutoff && cutoff <= 1.0) { panic!("Cutoff must be greater than 0.0 and lower than 1.0"); } let mut res: Vec<(f32, &str)> = Vec::new(); let mut matcher = SequenceMatcher::new("", word); for i in &possibilities { matcher.set_first_seq(i); let ratio = matcher.ratio(); if ratio >= cutoff { res.push((ratio, i)); } } res.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap()); res.truncate(n); res.iter().map(|x| x.1).collect() } pub fn unified_diff<T: Sequence + Display>( first_sequence: &[T], second_sequence: &[T], from_file: &str, to_file: &str, from_file_date: &str, to_file_date: &str, n: usize, ) -> Vec<String> { let mut res = Vec::new(); let lineterm = '\n'; let mut started = false; let mut matcher = SequenceMatcher::new(first_sequence, second_sequence); for group in &matcher.get_grouped_opcodes(n) { if !started { started = true; let from_date = format!("\t{}", from_file_date); let to_date = format!("\t{}", to_file_date); res.push(format!("--- {}{}{}", from_file, from_date, lineterm)); res.push(format!("+++ {}{}{}", to_file, to_date, lineterm)); } let (first, last) = (group.first().unwrap(), group.last().unwrap()); let file1_range = format_range_unified(first.first_start, last.first_end); let file2_range = format_range_unified(first.second_start, last.second_end); res.push(format!( "@@ -{} +{} @@{}", file1_range, file2_range, lineterm )); for code in group { if code.tag == "equal" { for item in first_sequence .iter() .take(code.first_end) .skip(code.first_start) { res.push(format!(" {}", item)); } continue; } if code.tag == "replace" || code.tag == "delete" { for item in first_sequence .iter() .take(code.first_end) .skip(code.first_start) { res.push(format!("-{}", item)); } } if code.tag == "replace" || code.tag == "insert" { for item in second_sequence .iter() .take(code.second_end) .skip(code.second_start) { res.push(format!("+{}", item)); } } } } res } pub fn context_diff<T: Sequence + Display>( first_sequence: &[T], second_sequence: &[T], from_file: &str, to_file: &str, from_file_date: &str, to_file_date: &str, n: usize, ) -> Vec<String> { let mut res = Vec::new(); let lineterm = '\n'; let mut prefix: HashMap<String, String> = HashMap::new(); prefix.insert(String::from("insert"), String::from("+ ")); prefix.insert(String::from("delete"), String::from("- ")); prefix.insert(String::from("replace"), String::from("! ")); prefix.insert(String::from("equal"), String::from(" ")); let mut started = false; let mut matcher = SequenceMatcher::new(first_sequence, second_sequence); for group in &matcher.get_grouped_opcodes(n) { if !started { started = true; let from_date = format!("\t{}", from_file_date); let to_date = format!("\t{}", to_file_date); res.push(format!("*** {}{}{}", from_file, from_date, lineterm)); res.push(format!("--- {}{}{}", to_file, to_date, lineterm)); } let (first, last) = (group.first().unwrap(), group.last().unwrap()); res.push(format!("***************{}", lineterm)); let file1_range = format_range_context(first.first_start, last.first_end); res.push(format!("*** {} ****{}", file1_range, lineterm)); let mut any = false; for opcode in group { if opcode.tag == "replace" || opcode.tag == "delete" { any = true; break; } } if any { for opcode in group { if opcode.tag != "insert" { for item in first_sequence .iter() .take(opcode.first_end) .skip(opcode.first_start) { res.push(format!("{}{}", &prefix[&opcode.tag], item)); } } } } let file2_range = format_range_context(first.second_start, last.second_end); res.push(format!("--- {} ----{}", file2_range, lineterm)); any = false; for opcode in group { if opcode.tag == "replace" || opcode.tag == "insert" { any = true; break; } } if any { for opcode in group { if opcode.tag != "delete" { for item in second_sequence .iter() .take(opcode.second_end) .skip(opcode.second_start) { res.push(format!("{}{}", prefix[&opcode.tag], item)); } } } } } res }