Skip to content

Commit e7dc5b1

Browse files
committed
Improve API
1 parent 9a25bc3 commit e7dc5b1

File tree

4 files changed

+77
-14
lines changed

4 files changed

+77
-14
lines changed

src/chunk.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl Default for EditOptions {
2626
}
2727
}
2828

29-
#[derive(Debug, Default)]
29+
#[derive(Debug, Default, Clone)]
3030
pub struct Chunk<'str> {
3131
pub intro: VecDeque<CowStr<'str>>,
3232
pub outro: VecDeque<CowStr<'str>>,
@@ -78,6 +78,9 @@ impl<'str> Chunk<'str> {
7878
pub fn split<'a>(&'a mut self, text_index: TextSize) -> Chunk<'str> {
7979
debug_assert!(text_index > self.start());
8080
debug_assert!(text_index < self.end());
81+
if self.edited_content.is_some() {
82+
panic!("Cannot split a chunk that has already been edited")
83+
}
8184
let first_slice_span = Span(self.start(), text_index);
8285
let last_slice_span = Span(text_index, self.end());
8386
let mut new_chunk = Chunk::new(last_slice_span);

src/magic_string/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct MagicStringOptions {
1515
pub filename: Option<String>,
1616
}
1717

18+
#[derive(Debug, Clone)]
1819
pub struct MagicString<'s> {
1920
pub filename: Option<String>,
2021
intro: VecDeque<CowStr<'s>>,

src/magic_string/mutation.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,20 @@ use crate::{chunk::EditOptions, CowStr, MagicString, TextSize};
33
#[derive(Debug, Default, Clone)]
44
pub struct UpdateOptions {
55
pub store_name: bool,
6+
7+
/// `true` will clear the `intro` and `outro` for the corresponding range.
68
pub overwrite: bool,
79
}
810

911
impl<'text> MagicString<'text> {
12+
/// A shorthand for `update_with(start, end, content, Default::default())`;
1013
pub fn update(
1114
&mut self,
1215
start: TextSize,
1316
end: TextSize,
1417
content: impl Into<CowStr<'text>>,
1518
) -> &mut Self {
16-
self.update_with(
17-
start,
18-
end,
19-
content,
20-
UpdateOptions {
21-
store_name: false,
22-
overwrite: true,
23-
},
24-
)
19+
self.update_with(start, end, content, Default::default())
2520
}
2621

2722
pub fn update_with(

tests/magic_string.rs

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
1-
use string_wizard::MagicString;
1+
use std::borrow::Cow;
22

3+
use string_wizard::MagicString;
34
use string_wizard::MagicStringOptions;
5+
use string_wizard::UpdateOptions;
6+
7+
trait MagicStringExt<'text> {
8+
fn overwrite(
9+
&mut self,
10+
start: usize,
11+
end: usize,
12+
content: impl Into<Cow<'text, str>>,
13+
) -> &mut Self;
14+
}
15+
16+
impl<'text> MagicStringExt<'text> for MagicString<'text> {
17+
fn overwrite(
18+
&mut self,
19+
start: usize,
20+
end: usize,
21+
content: impl Into<Cow<'text, str>>,
22+
) -> &mut Self {
23+
self.update_with(
24+
start,
25+
end,
26+
content,
27+
UpdateOptions {
28+
overwrite: true,
29+
..Default::default()
30+
},
31+
)
32+
}
33+
}
34+
435
mod options {
536
use super::*;
637
#[test]
@@ -17,7 +48,7 @@ mod options {
1748

1849
mod append {
1950
use super::*;
20-
51+
2152
#[test]
2253
fn should_append_content() {
2354
// should append content
@@ -44,7 +75,7 @@ mod prepend_append_left_right {
4475
s.prepend_right(5, "c");
4576

4677
assert_eq!(s.to_string(), "01234ABCcba56789");
47-
78+
4879
s.prepend_left(5, "<");
4980
s.prepend_left(5, "{");
5081
assert_eq!(s.to_string(), "01234{<ABCcba56789");
@@ -83,12 +114,45 @@ mod prepend_append_left_right {
83114

84115
assert_eq!(s.to_string(), "x4213");
85116
}
117+
}
86118

119+
mod clone {
120+
use super::*;
87121

122+
#[test]
123+
fn should_clone_a_magic_string() {
124+
let mut s = MagicString::new("abcdefghijkl");
125+
s.overwrite(3, 9, "XYZ");
126+
let c = s.clone();
127+
128+
assert_eq!(c.to_string(), "abcXYZjkl")
129+
}
88130
}
131+
132+
mod overwrite {
133+
use super::*;
134+
135+
#[test]
136+
fn should_replace_characters() {
137+
let mut s = MagicString::new("abcdefghijkl");
138+
s.overwrite(5, 8, "FGH");
139+
assert_eq!(s.to_string(), "abcdeFGHijkl");
140+
}
141+
142+
// #[test]
143+
// fn should_throw_an_error_if_overlapping_replacements_are_attempted() {
144+
// let mut s = MagicString::new("abcdefghijkl");
145+
// s.overwrite(7, 11, "xx");
146+
// assert!(std::panic::catch_unwind(|| {
147+
// s.clone().overwrite(8, 12, "yy");
148+
// })
149+
// .is_err())
150+
// }
151+
}
152+
89153
mod misc {
90154
use super::*;
91-
155+
92156
#[test]
93157
fn should_append_content() {
94158
// should append content

0 commit comments

Comments
 (0)