Skip to content

Commit d9cb1e8

Browse files
committed
Improve usage with u32 and usize
1 parent 0ea028d commit d9cb1e8

File tree

5 files changed

+77
-39
lines changed

5 files changed

+77
-39
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "string_wizard"
3-
version = "0.0.6"
3+
version = "0.0.7"
44
edition = "2021"
55
license = "MIT"
66
description = "manipulate string like wizards"

src/basic_types.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::borrow::Cow;
1+
use std::{borrow::Cow, fmt::Debug};
22

33
#[derive(Debug, Clone)]
44
pub struct BasicCowStr<'text> {
@@ -38,3 +38,18 @@ impl<'text, T: Into<Cow<'text, str>>> From<T> for BasicCowStr<'text> {
3838
Self::new(value.into())
3939
}
4040
}
41+
42+
// This is basically doing the same thing as `TryInto<u32>`.
43+
// If we use `TryInto<u32>`, we need to put `where <T as TryInto<u32>>::Error: Debug` everywhere.
44+
pub trait AssertIntoU32 {
45+
fn assert_into_u32(self) -> u32;
46+
}
47+
48+
impl<T: TryInto<u32>> AssertIntoU32 for T
49+
where
50+
<T as TryInto<u32>>::Error: Debug,
51+
{
52+
fn assert_into_u32(self) -> u32 {
53+
self.try_into().unwrap()
54+
}
55+
}

src/magic_string/mod.rs

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::collections::VecDeque;
77
use rustc_hash::FxHashMap;
88

99
use crate::{
10+
basic_types::AssertIntoU32,
1011
chunk::{Chunk, ChunkIdx, ChunkVec},
1112
span::Span,
1213
CowStr, TextSize,
@@ -31,14 +32,14 @@ pub struct MagicString<'s> {
3132
chunk_by_end: FxHashMap<TextSize, ChunkIdx>,
3233
}
3334

34-
impl<'s> MagicString<'s> {
35+
impl<'text> MagicString<'text> {
3536
// --- public
3637

37-
pub fn new(source: impl Into<CowStr<'s>>) -> Self {
38+
pub fn new(source: impl Into<CowStr<'text>>) -> Self {
3839
Self::with_options(source, Default::default())
3940
}
4041

41-
pub fn with_options(source: impl Into<CowStr<'s>>, options: MagicStringOptions) -> Self {
42+
pub fn with_options(source: impl Into<CowStr<'text>>, options: MagicStringOptions) -> Self {
4243
let source = source.into();
4344
let initial_chunk = Chunk::new(Span(0, source.len()));
4445
let mut chunks = ChunkVec::with_capacity(1);
@@ -64,7 +65,7 @@ impl<'s> MagicString<'s> {
6465
magic_string
6566
}
6667

67-
pub fn append(&mut self, source: impl Into<CowStr<'s>>) -> &mut Self {
68+
pub fn append(&mut self, source: impl Into<CowStr<'text>>) -> &mut Self {
6869
self.append_outro(source.into());
6970
self
7071
}
@@ -79,10 +80,10 @@ impl<'s> MagicString<'s> {
7980
///```
8081
pub fn append_left(
8182
&mut self,
82-
text_index: TextSize,
83-
content: impl Into<CowStr<'s>>,
83+
text_index: impl AssertIntoU32,
84+
content: impl Into<CowStr<'text>>,
8485
) -> &mut Self {
85-
match self.by_end_mut(text_index) {
86+
match self.by_end_mut(text_index.assert_into_u32()) {
8687
Some(chunk) => {
8788
chunk.append_outro(content.into());
8889
}
@@ -103,10 +104,10 @@ impl<'s> MagicString<'s> {
103104
///```
104105
pub fn append_right(
105106
&mut self,
106-
text_index: TextSize,
107-
content: impl Into<CowStr<'s>>,
107+
text_index: impl AssertIntoU32,
108+
content: impl Into<CowStr<'text>>,
108109
) -> &mut Self {
109-
match self.by_start_mut(text_index) {
110+
match self.by_start_mut(text_index.assert_into_u32()) {
110111
Some(chunk) => {
111112
chunk.append_intro(content.into());
112113
}
@@ -115,17 +116,17 @@ impl<'s> MagicString<'s> {
115116
self
116117
}
117118

118-
pub fn prepend(&mut self, source: impl Into<CowStr<'s>>) -> &mut Self {
119+
pub fn prepend(&mut self, source: impl Into<CowStr<'text>>) -> &mut Self {
119120
self.prepend_intro(source.into());
120121
self
121122
}
122123

123124
pub fn prepend_left(
124125
&mut self,
125-
text_index: TextSize,
126-
content: impl Into<CowStr<'s>>,
126+
text_index: impl AssertIntoU32,
127+
content: impl Into<CowStr<'text>>,
127128
) -> &mut Self {
128-
match self.by_end_mut(text_index) {
129+
match self.by_end_mut(text_index.assert_into_u32()) {
129130
Some(chunk) => chunk.prepend_outro(content.into()),
130131
None => self.prepend_intro(content.into()),
131132
}
@@ -134,10 +135,10 @@ impl<'s> MagicString<'s> {
134135

135136
pub fn prepend_right(
136137
&mut self,
137-
text_index: TextSize,
138-
content: impl Into<CowStr<'s>>,
138+
text_index: impl AssertIntoU32,
139+
content: impl Into<CowStr<'text>>,
139140
) -> &mut Self {
140-
match self.by_start_mut(text_index) {
141+
match self.by_start_mut(text_index.assert_into_u32()) {
141142
Some(chunk) => {
142143
chunk.prepend_intro(content.into());
143144
}
@@ -159,19 +160,19 @@ impl<'s> MagicString<'s> {
159160

160161
// --- private
161162

162-
fn prepend_intro(&mut self, content: impl Into<CowStr<'s>>) {
163+
fn prepend_intro(&mut self, content: impl Into<CowStr<'text>>) {
163164
self.intro.push_front(content.into());
164165
}
165166

166-
fn append_outro(&mut self, content: impl Into<CowStr<'s>>) {
167+
fn append_outro(&mut self, content: impl Into<CowStr<'text>>) {
167168
self.outro.push_back(content.into());
168169
}
169170

170-
fn prepend_outro(&mut self, content: impl Into<CowStr<'s>>) {
171+
fn prepend_outro(&mut self, content: impl Into<CowStr<'text>>) {
171172
self.outro.push_front(content.into());
172173
}
173174

174-
fn append_intro(&mut self, content: impl Into<CowStr<'s>>) {
175+
fn append_intro(&mut self, content: impl Into<CowStr<'text>>) {
175176
self.intro.push_back(content.into());
176177
}
177178

@@ -182,7 +183,7 @@ impl<'s> MagicString<'s> {
182183
}
183184
}
184185

185-
pub(crate) fn fragments(&'s self) -> impl Iterator<Item = &'s str> {
186+
pub(crate) fn fragments(&'text self) -> impl Iterator<Item = &'text str> {
186187
let intro = self.intro.iter().map(|s| s.as_ref());
187188
let outro = self.outro.iter().map(|s| s.as_ref());
188189
let chunks = self.iter_chunks().flat_map(|c| c.fragments(&self.source));
@@ -236,7 +237,8 @@ impl<'s> MagicString<'s> {
236237
chunk_contains_index.next = Some(new_chunk_idx);
237238
}
238239

239-
fn by_start_mut(&mut self, text_index: TextSize) -> Option<&mut Chunk<'s>> {
240+
fn by_start_mut(&mut self, text_index: impl AssertIntoU32) -> Option<&mut Chunk<'text>> {
241+
let text_index = text_index.assert_into_u32();
240242
if text_index == self.source.len() {
241243
None
242244
} else {
@@ -247,7 +249,8 @@ impl<'s> MagicString<'s> {
247249
}
248250
}
249251

250-
fn by_end_mut(&mut self, text_index: TextSize) -> Option<&mut Chunk<'s>> {
252+
fn by_end_mut(&mut self, text_index: TextSize) -> Option<&mut Chunk<'text>> {
253+
let text_index = text_index.assert_into_u32();
251254
if text_index == 0 {
252255
None
253256
} else {
@@ -258,11 +261,11 @@ impl<'s> MagicString<'s> {
258261
}
259262
}
260263

261-
fn last_chunk(&self) -> &Chunk<'s> {
264+
fn last_chunk(&self) -> &Chunk<'text> {
262265
&self.chunks[self.last_chunk_idx]
263266
}
264267

265-
fn first_chunk(&self) -> &Chunk<'s> {
268+
fn first_chunk(&self) -> &Chunk<'text> {
266269
&self.chunks[self.first_chunk_idx]
267270
}
268271
}

src/magic_string/mutation.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{chunk::EditOptions, CowStr, MagicString};
1+
use crate::{basic_types::AssertIntoU32, chunk::EditOptions, CowStr, MagicString};
22

33
#[derive(Debug, Default, Clone)]
44
pub struct UpdateOptions {
@@ -13,28 +13,34 @@ impl<'text> MagicString<'text> {
1313
/// A shorthand for `update_with(start, end, content, Default::default())`;
1414
pub fn update(
1515
&mut self,
16-
start: usize,
17-
end: usize,
16+
start: impl AssertIntoU32,
17+
end: impl AssertIntoU32,
1818
content: impl Into<CowStr<'text>>,
1919
) -> &mut Self {
2020
self.update_with(start, end, content, Default::default())
2121
}
2222

2323
pub fn update_with(
2424
&mut self,
25-
start: usize,
26-
end: usize,
25+
start: impl AssertIntoU32,
26+
end: impl AssertIntoU32,
2727
content: impl Into<CowStr<'text>>,
2828
opts: UpdateOptions,
2929
) -> &mut Self {
30-
self.update_with_inner(start, end, content.into(), opts, true);
30+
self.update_with_inner(
31+
start.assert_into_u32(),
32+
end.assert_into_u32(),
33+
content.into(),
34+
opts,
35+
true,
36+
);
3137
self
3238
}
3339

34-
pub fn remove(&mut self, start: usize, end: usize) -> &mut Self {
40+
pub fn remove(&mut self, start: impl AssertIntoU32, end: impl AssertIntoU32) -> &mut Self {
3541
self.update_with_inner(
36-
start,
37-
end,
42+
start.assert_into_u32(),
43+
end.assert_into_u32(),
3844
"".into(),
3945
UpdateOptions {
4046
keep_original: false,
@@ -50,8 +56,8 @@ impl<'text> MagicString<'text> {
5056

5157
fn update_with_inner(
5258
&mut self,
53-
start: usize,
54-
end: usize,
59+
start: u32,
60+
end: u32,
5561
content: CowStr<'text>,
5662
opts: UpdateOptions,
5763
panic_if_start_equal_end: bool,

tests/usage.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use string_wizard::MagicString;
2+
3+
#[test]
4+
pub fn should_alow_passing_u32_or_usize() {
5+
let mut s = MagicString::new("x");
6+
let start_u32 = 0u32;
7+
let end_u32 = 1u32;
8+
s.update(start_u32, end_u32, "y");
9+
assert_eq!(s.to_string(), "y");
10+
let start_u32 = 0usize;
11+
let end_u32 = 1usize;
12+
s.update(start_u32, end_u32, "z");
13+
assert_eq!(s.to_string(), "z");
14+
}

0 commit comments

Comments
 (0)