Skip to main content

string_wizard/magic_string/
reset.rs

1use crate::MagicString;
2
3impl<'text> MagicString<'text> {
4  /// Resets the portion of the string from `start` to `end` to its original content.
5  /// This undoes any modifications (updates, overwrites, intro/outro additions) made to that range.
6  ///
7  /// # Errors
8  /// - If `start` is greater than to `end`
9  /// - If the range is out of bounds
10  pub fn reset(&mut self, start: u32, end: u32) -> Result<&mut Self, String> {
11    if start == end {
12      return Ok(self);
13    }
14
15    if start > end {
16      return Err("end must be greater than start".to_string());
17    }
18
19    if (end as usize) > self.source.len() {
20      return Err("Character is out of bounds".to_string());
21    }
22
23    self.split_at(start)?;
24    self.split_at(end)?;
25
26    let mut chunk_idx = self.chunk_by_start.get(&start).copied();
27
28    while let Some(idx) = chunk_idx {
29      let chunk = &mut self.chunks[idx];
30      chunk.reset();
31      let chunk_end = chunk.end();
32      chunk_idx = if end > chunk_end { self.chunk_by_start.get(&chunk_end).copied() } else { None };
33    }
34
35    Ok(self)
36  }
37}