/rust/registry/src/index.crates.io-1949cf8c6b5b557f/dmp-0.2.3/src/patch.rs
Line | Count | Source |
1 | | use crate::Diff; |
2 | | use std::fmt; |
3 | | use std::fmt::{Debug, Display, Formatter}; |
4 | | use urlencoding::encode; |
5 | | |
6 | | #[derive(Clone, PartialEq)] |
7 | | pub struct Patch { |
8 | | pub diffs: Vec<Diff>, |
9 | | pub start1: i32, |
10 | | pub start2: i32, |
11 | | pub length1: i32, |
12 | | pub length2: i32, |
13 | | } |
14 | | |
15 | | impl Patch { |
16 | | /// A new diff patch object created. |
17 | 0 | pub fn new(diffs: Vec<Diff>, start1: i32, start2: i32, length1: i32, length2: i32) -> Patch { |
18 | 0 | Patch { |
19 | 0 | diffs, |
20 | 0 | start1, |
21 | 0 | start2, |
22 | 0 | length1, |
23 | 0 | length2, |
24 | 0 | } |
25 | 0 | } |
26 | | } |
27 | | |
28 | | impl Debug for Patch { |
29 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
30 | 0 | write!( |
31 | 0 | f, |
32 | | "{{diffs:\n {:?},\n start1: {},\n start2: {},\n length1: {},\n length2: {} }}", |
33 | | self.diffs, self.start1, self.start2, self.length1, self.length2 |
34 | | ) |
35 | 0 | } |
36 | | } |
37 | | |
38 | | impl Display for Patch { |
39 | 0 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
40 | 0 | f.write_str(&self.build_string()) |
41 | 0 | } |
42 | | } |
43 | | |
44 | | impl Patch { |
45 | 0 | fn build_string(&self) -> String { |
46 | | // Convert patch to string. |
47 | 0 | let mut text = "@@ -".to_string(); |
48 | 0 | let mut start1: u32 = (self.start1 + 1) as u32; |
49 | 0 | if self.length1 == 0 && start1 == 1 { |
50 | 0 | start1 -= 1; |
51 | 0 | } |
52 | 0 | text += start1.to_string().as_str(); |
53 | 0 | if self.length1 > 0 || start1 == 0 { |
54 | 0 | text += ","; |
55 | 0 | let length1: u32 = self.length1 as u32; |
56 | 0 | text += length1.to_string().as_str(); |
57 | 0 | } |
58 | 0 | text += " +"; |
59 | 0 | let mut start2: u32 = (self.start2 + 1) as u32; |
60 | 0 | if self.length2 == 0 && start2 == 1 { |
61 | 0 | start2 -= 1; |
62 | 0 | } |
63 | 0 | text += start2.to_string().as_str(); |
64 | 0 | if self.length2 > 0 || start2 == 0 { |
65 | 0 | text += ","; |
66 | 0 | let length2: u32 = self.length2 as u32; |
67 | 0 | text += length2.to_string().as_str(); |
68 | 0 | } |
69 | 0 | text += " @@\n"; |
70 | 0 | for i in 0..self.diffs.len() { |
71 | | let ch: char; |
72 | 0 | if self.diffs[i].operation == 0 { |
73 | 0 | ch = ' '; |
74 | 0 | } else if self.diffs[i].operation == -1 { |
75 | 0 | ch = '-'; |
76 | 0 | } else { |
77 | 0 | ch = '+'; |
78 | 0 | } |
79 | 0 | text.push(ch); |
80 | 0 | let text_vec: Vec<char> = self.diffs[i].text.chars().collect(); |
81 | 0 | let temp5: Vec<char> = vec![ |
82 | | '!', '~', '*', '(', ')', ';', '/', '?', ':', '@', '&', '=', '+', '$', ',', '#', |
83 | | ' ', '\'', |
84 | | ]; |
85 | 0 | for text_vec_item in &text_vec { |
86 | 0 | let mut is: bool = false; |
87 | 0 | for temp5_item in &temp5 { |
88 | 0 | if *text_vec_item == *temp5_item { |
89 | 0 | is = true; |
90 | 0 | } |
91 | | } |
92 | 0 | if is { |
93 | 0 | text.push(*text_vec_item); |
94 | 0 | continue; |
95 | 0 | } else if *text_vec_item == '%' { |
96 | 0 | text += "%25"; |
97 | 0 | continue; |
98 | 0 | } |
99 | 0 | let mut temp6: String = "".to_string(); |
100 | 0 | temp6.push(*text_vec_item); |
101 | 0 | temp6 = encode(temp6.as_str()).into_owned(); |
102 | 0 | text += temp6.as_str(); |
103 | | } |
104 | 0 | text += "\n"; |
105 | | } |
106 | 0 | text |
107 | 0 | } |
108 | | } |