Skip to content

Commit 25152be

Browse files
authored
feat: make Debug readable (#70)
1 parent 4f19605 commit 25152be

File tree

5 files changed

+84
-5
lines changed

5 files changed

+84
-5
lines changed

src/cached_source.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ use crate::{helpers::StreamChunks, MapOptions, Source, SourceMap};
4343
/// "Hello World\nconsole.log('test');\nconsole.log('test2');\nHello2\n"
4444
/// );
4545
/// ```
46-
#[derive(Debug)]
4746
pub struct CachedSource<T> {
4847
inner: Arc<T>,
4948
cached_buffer: OnceCell<Vec<u8>>,
@@ -159,6 +158,20 @@ impl<T: PartialEq> PartialEq for CachedSource<T> {
159158

160159
impl<T: Eq> Eq for CachedSource<T> {}
161160

161+
impl<T: std::fmt::Debug> std::fmt::Debug for CachedSource<T> {
162+
fn fmt(
163+
&self,
164+
f: &mut std::fmt::Formatter<'_>,
165+
) -> Result<(), std::fmt::Error> {
166+
f.debug_struct("CachedSource")
167+
.field("inner", self.inner.as_ref())
168+
.field("cached_buffer", &self.cached_buffer.get().is_some())
169+
.field("cached_source", &self.cached_source.get().is_some())
170+
.field("cached_maps", &(!self.cached_maps.is_empty()))
171+
.finish()
172+
}
173+
}
174+
162175
#[cfg(test)]
163176
mod tests {
164177
use crate::{

src/original_source.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use crate::{
3434
/// "AAAA;AACA",
3535
/// );
3636
/// ```
37-
#[derive(Debug, Clone, Eq)]
37+
#[derive(Clone, Eq)]
3838
pub struct OriginalSource {
3939
value: String,
4040
name: String,
@@ -86,6 +86,18 @@ impl PartialEq for OriginalSource {
8686
}
8787
}
8888

89+
impl std::fmt::Debug for OriginalSource {
90+
fn fmt(
91+
&self,
92+
f: &mut std::fmt::Formatter<'_>,
93+
) -> Result<(), std::fmt::Error> {
94+
f.debug_struct("OriginalSource")
95+
.field("name", &self.name)
96+
.field("value", &self.value.chars().take(50).collect::<String>())
97+
.finish()
98+
}
99+
}
100+
89101
impl StreamChunks for OriginalSource {
90102
fn stream_chunks(
91103
&self,

src/raw_source.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::{
2424
/// assert_eq!(s.map(&MapOptions::default()), None);
2525
/// assert_eq!(s.size(), 16);
2626
/// ```
27-
#[derive(Debug, Clone, Eq)]
27+
#[derive(Clone, Eq)]
2828
pub enum RawSource {
2929
/// Represent buffer.
3030
Buffer(Vec<u8>),
@@ -114,6 +114,27 @@ impl PartialEq for RawSource {
114114
}
115115
}
116116

117+
impl std::fmt::Debug for RawSource {
118+
fn fmt(
119+
&self,
120+
f: &mut std::fmt::Formatter<'_>,
121+
) -> Result<(), std::fmt::Error> {
122+
let mut d = f.debug_struct("RawSource");
123+
match self {
124+
Self::Buffer(buffer) => {
125+
d.field(
126+
"buffer",
127+
&buffer.iter().take(50).copied().collect::<Vec<u8>>(),
128+
);
129+
}
130+
Self::Source(string) => {
131+
d.field("source", &string.chars().take(50).collect::<String>());
132+
}
133+
}
134+
d.finish()
135+
}
136+
}
137+
117138
impl StreamChunks for RawSource {
118139
fn stream_chunks(
119140
&self,

src/replace_source.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use crate::{
3434
///
3535
/// assert_eq!(source.source(), "start1\nstart2\nreplaced!\nend1\nend2");
3636
/// ```
37-
#[derive(Debug)]
3837
pub struct ReplaceSource<T> {
3938
inner: Arc<T>,
4039
inner_source_code: OnceCell<Box<str>>,
@@ -188,6 +187,28 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> Source for ReplaceSource<T> {
188187
}
189188
}
190189

190+
impl<T: std::fmt::Debug> std::fmt::Debug for ReplaceSource<T> {
191+
fn fmt(
192+
&self,
193+
f: &mut std::fmt::Formatter<'_>,
194+
) -> Result<(), std::fmt::Error> {
195+
f.debug_struct("ReplaceSource")
196+
.field("inner", self.inner.as_ref())
197+
.field(
198+
"inner_source_code",
199+
&self
200+
.inner_source_code
201+
.get()
202+
.map(|s| s.chars().take(50).collect::<String>()),
203+
)
204+
.field(
205+
"replacements",
206+
&self.replacements.lock().iter().take(3).collect::<Vec<_>>(),
207+
)
208+
.finish()
209+
}
210+
}
211+
191212
impl<T: Source> StreamChunks for ReplaceSource<T> {
192213
fn mappings_size_hint(&self) -> usize {
193214
self.replacements.lock().len() * 2

src/source_map_source.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<V, N> From<WithoutOriginalOptions<V, N>> for SourceMapSourceOptions<V, N> {
5757
/// source map for the original source.
5858
///
5959
/// - [webpack-sources docs](https://github.com/webpack/webpack-sources/#sourcemapsource).
60-
#[derive(Debug, Clone, Eq)]
60+
#[derive(Clone, Eq)]
6161
pub struct SourceMapSource {
6262
value: String,
6363
name: String,
@@ -134,6 +134,18 @@ impl PartialEq for SourceMapSource {
134134
}
135135
}
136136

137+
impl std::fmt::Debug for SourceMapSource {
138+
fn fmt(
139+
&self,
140+
f: &mut std::fmt::Formatter<'_>,
141+
) -> Result<(), std::fmt::Error> {
142+
f.debug_struct("SourceMapSource")
143+
.field("name", &self.name)
144+
.field("value", &self.value.chars().take(50).collect::<String>())
145+
.finish()
146+
}
147+
}
148+
137149
impl StreamChunks for SourceMapSource {
138150
fn stream_chunks(
139151
&self,

0 commit comments

Comments
 (0)