12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- #[derive(PartialEq, Debug, Clone)]
- enum DiffOp {
- Insert,
- Delete,
- Equal,
- }
- #[derive(Clone, Debug)]
- struct Diff {
- operation: DiffOp,
- data: String,
- }
- struct DiffMatchPatch {
- diff_timeout: f64,
- diff_edit_cost: isize,
- match_threshold: f64,
- match_distance: isize,
- patch_delete_threshold: f64,
- patch_margin: isize,
- }
- impl DiffMatchPatch {
- fn new() -> DiffMatchPatch {
- DiffMatchPatch {
- diff_timeout: 1.0,
- diff_edit_cost: 4,
- match_threshold: 0.5,
- match_distance: 1000,
- patch_delete_threshold: 0.5,
- patch_margin: 4,
- }
- }
- fn diff_main(&self, text1: &str, text2: &str) -> Vec<Diff> {
- if text1 == text2 {
- if text1.is_empty() {
- Vec::new()
- } else {
- vec![Diff {
- operation: DiffOp::Equal,
- data: text1.to_string(),
- }]
- }
- } else {
- let mut diffs = Vec::new();
- if text1.is_empty() {
- diffs.push(Diff {
- operation: DiffOp::Insert,
- data: text2.to_string(),
- });
- } else if text2.is_empty() {
- diffs.push(Diff {
- operation: DiffOp::Delete,
- data: text1.to_string(),
- });
- } else {
- // Here, a real diff algorithm would be implemented
- diffs.push(Diff {
- operation: DiffOp::Delete,
- data: text1.to_string(),
- });
- diffs.push(Diff {
- operation: DiffOp::Insert,
- data: text2.to_string(),
- });
- }
- diffs
- }
- }
- }
- fn main() {
- let dmp = DiffMatchPatch::new();
- let text1 = "Hello World";
- let text2 = "Hello Rust!";
- let diffs = dmp.diff_main(text1, text2);
- println!("Diffs: {:?}", diffs);
- }
|