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
use crate::CowStr;
use super::MagicString;
impl<'text> MagicString<'text> {
pub fn append(&mut self, source: impl Into<CowStr<'text>>) -> &mut Self {
self.append_outro(source.into());
self
}
/// # Errors
/// Returns `Err` if `text_index` falls inside a chunk that has already been edited, since the
/// chunk cannot be split there.
///
/// # Example
///```rust
/// use string_wizard::MagicString;
/// let mut s = MagicString::new("01234");
/// s.append_left(2, "a").unwrap();
/// s.append_left(2, "b").unwrap();
/// assert_eq!(s.to_string(), "01ab234")
///```
pub fn append_left(
&mut self,
text_index: u32,
content: impl Into<CowStr<'text>>,
) -> Result<&mut Self, String> {
match self.by_end_mut(text_index)? {
Some(chunk) => {
chunk.append_outro(content.into());
}
None => self.append_intro(content.into()),
}
Ok(self)
}
/// # Errors
/// Returns `Err` if `text_index` falls inside a chunk that has already been edited, since the
/// chunk cannot be split there.
///
/// # Example
/// ```rust
/// use string_wizard::MagicString;
/// let mut s = MagicString::new("01234");
/// s.append_right(2, "A").unwrap();
/// s.append_right(2, "B").unwrap();
/// s.append_left(2, "a").unwrap();
/// s.append_left(2, "b").unwrap();
/// assert_eq!(s.to_string(), "01abAB234")
///```
pub fn append_right(
&mut self,
text_index: u32,
content: impl Into<CowStr<'text>>,
) -> Result<&mut Self, String> {
match self.by_start_mut(text_index)? {
Some(chunk) => {
chunk.append_intro(content.into());
}
None => self.append_outro(content.into()),
}
Ok(self)
}
}