patch_apply/
ast.rs

1use std::borrow::Cow;
2use std::fmt;
3
4use chrono::{DateTime, FixedOffset};
5
6use crate::parser::{parse_multiple_patches, parse_single_patch, ParseError};
7
8/// A complete patch summarizing the differences between two files
9#[derive(Debug, Clone, Eq, PartialEq)]
10pub struct Patch<'a> {
11    /// The file information of the `-` side of the diff, line prefix: `---`
12    pub old: File<'a>,
13    /// The file information of the `+` side of the diff, line prefix: `+++`
14    pub new: File<'a>,
15    /// hunks of differences; each hunk shows one area where the files differ
16    pub hunks: Vec<Hunk<'a>>,
17    // true if the last line of the file ends in a newline character
18    //
19    // This will only be false if at the end of the patch we encounter the text:
20    // `\ No newline at end of file`
21    // pub end_newline: bool,
22}
23
24impl<'a> fmt::Display for Patch<'a> {
25    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26        // Display implementations typically hold up the invariant that there is no trailing
27        // newline. This isn't enforced, but it allows them to work well with `println!`
28
29        write!(f, "--- {}", self.old)?;
30        write!(f, "\n+++ {}", self.new)?;
31        for hunk in &self.hunks {
32            write!(f, "\n{}", hunk)?;
33        }
34        // if !self.end_newline {
35        //     write!(f, "\n\\ No newline at end of file")?;
36        // }
37        Ok(())
38    }
39}
40
41impl<'a> Patch<'a> {
42    #[allow(clippy::tabs_in_doc_comments)]
43    /// Attempt to parse a patch from the given string.
44    ///
45    /// # Example
46    ///
47    /// ```
48    /// # fn main() -> Result<(), patch::ParseError<'static>> {
49    /// # use patch::Patch;
50    /// let sample = "\
51    /// --- lao	2002-02-21 23:30:39.942229878 -0800
52    /// +++ tzu	2002-02-21 23:30:50.442260588 -0800
53    /// @@ -1,7 +1,6 @@
54    /// -The Way that can be told of is not the eternal Way;
55    /// -The name that can be named is not the eternal name.
56    ///  The Nameless is the origin of Heaven and Earth;
57    /// -The Named is the mother of all things.
58    /// +The named is the mother of all things.
59    /// +
60    ///  Therefore let there always be non-being,
61    ///  so we may see their subtlety,
62    ///  And let there always be being,
63    /// @@ -9,3 +8,6 @@
64    ///  The two are the same,
65    ///  But after they are produced,
66    ///  they have different names.
67    /// +They both may be called deep and profound.
68    /// +Deeper and more profound,
69    /// +The door of all subtleties!
70    /// \\ No newline at end of file\n";
71    ///
72    /// let patch = Patch::from_single(sample)?;
73    /// assert_eq!(&patch.old.path, "lao");
74    /// assert_eq!(&patch.new.path, "tzu");
75    /// assert_eq!(patch.end_newline, false);
76    /// # Ok(())
77    /// # }
78    /// ```
79    pub fn from_single(s: &'a str) -> Result<Self, ParseError<'a>> {
80        parse_single_patch(s)
81    }
82
83    /// Attempt to parse as many patches as possible from the given string. This is useful for when
84    /// you have a complete diff of many files. String must contain at least one patch.
85    ///
86    /// # Example
87    ///
88    /// ```
89    /// # fn main() -> Result<(), patch::ParseError<'static>> {
90    /// # use patch::Patch;
91    /// let sample = "\
92    /// diff --git a/src/generator/place_items.rs b/src/generator/place_items.rs
93    /// index 508f4e9..31a167e 100644
94    /// --- a/src/generator/place_items.rs
95    /// +++ b/src/generator/place_items.rs
96    /// @@ -233,7 +233,7 @@ impl<'a> GameGenerator<'a> {
97    ///          //     oooooooo
98    ///          //
99    ///          // x would pass all of the previous checks but get caught by this one
100    /// -        if grid.adjacent_positions(inner_room_tile).find(|&pt| grid.is_room_entrance(pt)).is_some() {
101    /// +        if grid.adjacent_positions(inner_room_tile).any(|&pt| grid.is_room_entrance(pt)) {
102    ///              return None;
103    ///          }
104    ///
105    /// diff --git a/src/ui/level_screen.rs b/src/ui/level_screen.rs
106    /// index 81fe540..166bb2b 100644
107    /// --- a/src/ui/level_screen.rs
108    /// +++ b/src/ui/level_screen.rs
109    /// @@ -48,7 +48,7 @@ impl<'a, 'b> LevelScreen<'a, 'b> {
110    ///          // Find the empty position adjacent to this staircase. There should only be one.
111    ///          let map = self.world.read_resource::<FloorMap>();
112    ///          let tile_pos = map.world_to_tile_pos(pos);
113    /// -        let empty = map.grid().adjacent_positions(tile_pos).find(|&p| !map.grid().get(p).is_wall())
114    /// +        let empty = map.grid().adjacents(tile_pos).find(|t| !t.is_wall())
115    ///              .expect(\"bug: should be one empty position adjacent to a staircase\");
116    ///          empty.center(map.tile_size() as i32)
117    ///      }
118    /// @@ -64,7 +64,7 @@ impl<'a, 'b> LevelScreen<'a, 'b> {
119    ///          // Find the empty position adjacent to this staircase. There should only be one.
120    ///          let map = self.world.read_resource::<FloorMap>();
121    ///          let tile_pos = map.world_to_tile_pos(pos);
122    /// -        let empty = map.grid().adjacent_positions(tile_pos).find(|&p| !map.grid().get(p).is_wall())
123    /// +        let empty = map.grid().adjacents(tile_pos).find(|t| !t.is_wall())
124    ///              .expect(\"bug: should be one empty position adjacent to a staircase\");
125    ///          empty.center(map.tile_size() as i32)
126    ///      }\n";
127    ///
128    /// let patches = Patch::from_multiple(sample)?;
129    /// assert_eq!(patches.len(), 2);
130    /// # Ok(())
131    /// # }
132    /// ```
133    pub fn from_multiple(s: &'a str) -> Result<Vec<Self>, ParseError<'a>> {
134        parse_multiple_patches(s)
135    }
136}
137
138/// Check if a string needs to be quoted, and format it accordingly
139fn maybe_escape_quote(f: &mut fmt::Formatter, s: &str) -> fmt::Result {
140    let quote = s
141        .chars()
142        .any(|ch| matches!(ch, ' ' | '\t' | '\r' | '\n' | '\"' | '\0' | '\\'));
143
144    if quote {
145        write!(f, "\"")?;
146        for ch in s.chars() {
147            match ch {
148                '\0' => write!(f, r"\0")?,
149                '\n' => write!(f, r"\n")?,
150                '\r' => write!(f, r"\r")?,
151                '\t' => write!(f, r"\t")?,
152                '"' => write!(f, r#"\""#)?,
153                '\\' => write!(f, r"\\")?,
154                _ => write!(f, "{}", ch)?,
155            }
156        }
157        write!(f, "\"")
158    } else {
159        write!(f, "{}", s)
160    }
161}
162
163/// The file path and any additional info of either the old file or the new file
164#[derive(Debug, Clone, Eq, PartialEq)]
165pub struct File<'a> {
166    /// The parsed path or file name of the file
167    ///
168    /// Avoids allocation if at all possible. Only allocates if the file path is a quoted string
169    /// literal. String literals are necessary in some cases, for example if the file path has
170    /// spaces in it. These literals can contain escaped characters which are initially seen as
171    /// groups of two characters by the parser (e.g. '\\' + 'n'). A newly allocated string is
172    /// used to unescape those characters (e.g. "\\n" -> '\n').
173    ///
174    /// **Note:** While this string is typically a file path, this library makes no attempt to
175    /// verify the format of that path. That means that **this field can potentially be any
176    /// string**. You should verify it before doing anything that may be security-critical.
177    pub path: Cow<'a, str>,
178    /// Any additional information provided with the file path
179    pub meta: Option<FileMetadata<'a>>,
180}
181
182impl<'a> fmt::Display for File<'a> {
183    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
184        maybe_escape_quote(f, &self.path)?;
185        if let Some(meta) = &self.meta {
186            write!(f, "\t{}", meta)?;
187        }
188        Ok(())
189    }
190}
191
192/// Additional metadata provided with the file path
193#[derive(Debug, Clone, Eq, PartialEq)]
194pub enum FileMetadata<'a> {
195    /// A complete datetime, e.g. `2002-02-21 23:30:39.942229878 -0800`
196    DateTime(DateTime<FixedOffset>),
197    /// Any other string provided after the file path, e.g. git hash, unrecognized timestamp, etc.
198    Other(Cow<'a, str>),
199}
200
201impl<'a> fmt::Display for FileMetadata<'a> {
202    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
203        match self {
204            FileMetadata::DateTime(datetime) => {
205                write!(f, "{}", datetime.format("%F %T%.f %z"))
206            }
207            FileMetadata::Other(data) => maybe_escape_quote(f, data),
208        }
209    }
210}
211
212/// One area where the files differ
213#[derive(Debug, Clone, Eq, PartialEq)]
214pub struct Hunk<'a> {
215    /// The range of lines in the old file that this hunk represents
216    pub old_range: Range,
217    /// The range of lines in the new file that this hunk represents
218    pub new_range: Range,
219    /// Any trailing text after the hunk's range information
220    pub range_hint: &'a str,
221    /// Each line of text in the hunk, prefixed with the type of change it represents
222    pub lines: Vec<Line<'a>>,
223}
224
225impl<'a> Hunk<'a> {
226    /// A nicer way to access the optional hint
227    pub fn hint(&self) -> Option<&str> {
228        let h = self.range_hint.trim_start();
229        if h.is_empty() {
230            None
231        } else {
232            Some(h)
233        }
234    }
235}
236
237impl<'a> fmt::Display for Hunk<'a> {
238    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
239        write!(
240            f,
241            "@@ -{} +{} @@{}",
242            self.old_range, self.new_range, self.range_hint
243        )?;
244
245        for line in &self.lines {
246            write!(f, "\n{}", line)?;
247        }
248
249        Ok(())
250    }
251}
252
253/// A range of lines in a given file
254#[derive(Debug, Clone, Eq, PartialEq)]
255pub struct Range {
256    /// The start line of the chunk in the old or new file
257    pub start: u64,
258    /// The chunk size (number of lines) in the old or new file
259    pub count: u64,
260}
261
262impl fmt::Display for Range {
263    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
264        write!(f, "{},{}", self.start, self.count)
265    }
266}
267
268/// A line of the old file, new file, or both
269#[derive(Debug, Clone, Eq, PartialEq)]
270pub enum Line<'a> {
271    /// A line added to the old file in the new file
272    Add(&'a str),
273    /// A line removed from the old file in the new file
274    Remove(&'a str),
275    /// A line provided for context in the diff (unchanged); from both the old and the new file
276    Context(&'a str),
277    /// End of file with an empty line
278    EndOfFile(&'a str),
279}
280
281impl<'a> fmt::Display for Line<'a> {
282    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
283        match self {
284            Line::Add(line) => write!(f, "+{}", line),
285            Line::Remove(line) => write!(f, "-{}", line),
286            Line::Context(line) => write!(f, " {}", line),
287            Line::EndOfFile(_) => write!(f, " end_of_file"),
288        }
289    }
290}
291
292#[cfg(test)]
293mod tests {
294    use super::*;
295    use pretty_assertions::assert_eq;
296
297    #[test]
298    fn test_hint_helper() {
299        let mut h = Hunk {
300            old_range: Range { start: 0, count: 0 },
301            new_range: Range { start: 0, count: 0 },
302            range_hint: "",
303            lines: vec![],
304        };
305        for (input, expected) in vec![
306            ("", None),
307            (" ", None),
308            ("  ", None),
309            ("x", Some("x")),
310            (" x", Some("x")),
311            ("x ", Some("x ")),
312            (" x ", Some("x ")),
313            ("  abc def ", Some("abc def ")),
314        ] {
315            h.range_hint = input;
316            assert_eq!(h.hint(), expected);
317        }
318    }
319}