Skip to content

Commit

Permalink
feat: make Debug readable
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Nov 28, 2023
1 parent 4f19605 commit 46c332f
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 5 deletions.
15 changes: 14 additions & 1 deletion src/cached_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ use crate::{helpers::StreamChunks, MapOptions, Source, SourceMap};
/// "Hello World\nconsole.log('test');\nconsole.log('test2');\nHello2\n"
/// );
/// ```
#[derive(Debug)]
pub struct CachedSource<T> {
inner: Arc<T>,
cached_buffer: OnceCell<Vec<u8>>,
Expand Down Expand Up @@ -159,6 +158,20 @@ impl<T: PartialEq> PartialEq for CachedSource<T> {

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

impl<T: std::fmt::Debug> std::fmt::Debug for CachedSource<T> {
fn fmt(
&self,
f: &mut std::fmt::Formatter<'_>,
) -> Result<(), std::fmt::Error> {
f.debug_struct("CachedSource")
.field("inner", self.inner.as_ref())
.field("cached_buffer", &self.cached_buffer.get().is_some())
.field("cached_source", &self.cached_source.get().is_some())
.field("cached_maps", &(!self.cached_maps.is_empty()))
.finish()
}
}

#[cfg(test)]
mod tests {
use crate::{
Expand Down
14 changes: 13 additions & 1 deletion src/original_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::{
/// "AAAA;AACA",
/// );
/// ```
#[derive(Debug, Clone, Eq)]
#[derive(Clone, Eq)]
pub struct OriginalSource {
value: String,
name: String,
Expand Down Expand Up @@ -86,6 +86,18 @@ impl PartialEq for OriginalSource {
}
}

impl std::fmt::Debug for OriginalSource {
fn fmt(
&self,
f: &mut std::fmt::Formatter<'_>,
) -> Result<(), std::fmt::Error> {
f.debug_struct("OriginalSource")
.field("name", &self.name)
.field("value", &self.value.chars().take(50).collect::<String>())
.finish()
}
}

impl StreamChunks for OriginalSource {
fn stream_chunks(
&self,
Expand Down
23 changes: 22 additions & 1 deletion src/raw_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
/// assert_eq!(s.map(&MapOptions::default()), None);
/// assert_eq!(s.size(), 16);
/// ```
#[derive(Debug, Clone, Eq)]
#[derive(Clone, Eq)]
pub enum RawSource {
/// Represent buffer.
Buffer(Vec<u8>),
Expand Down Expand Up @@ -114,6 +114,27 @@ impl PartialEq for RawSource {
}
}

impl std::fmt::Debug for RawSource {
fn fmt(
&self,
f: &mut std::fmt::Formatter<'_>,
) -> Result<(), std::fmt::Error> {
let mut d = f.debug_struct("RawSource");
match self {
Self::Buffer(buffer) => {
d.field(
"buffer",
&buffer.iter().take(50).copied().collect::<Vec<u8>>(),
);
}
Self::Source(string) => {
d.field("source", &string.chars().take(50).collect::<String>());
}
}
d.finish()
}
}

impl StreamChunks for RawSource {
fn stream_chunks(
&self,
Expand Down
23 changes: 22 additions & 1 deletion src/replace_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ use crate::{
///
/// assert_eq!(source.source(), "start1\nstart2\nreplaced!\nend1\nend2");
/// ```
#[derive(Debug)]
pub struct ReplaceSource<T> {
inner: Arc<T>,
inner_source_code: OnceCell<Box<str>>,
Expand Down Expand Up @@ -188,6 +187,28 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> Source for ReplaceSource<T> {
}
}

impl<T: std::fmt::Debug> std::fmt::Debug for ReplaceSource<T> {
fn fmt(
&self,
f: &mut std::fmt::Formatter<'_>,
) -> Result<(), std::fmt::Error> {
f.debug_struct("ReplaceSource")
.field("inner", self.inner.as_ref())
.field(
"inner_source_code",
&self
.inner_source_code
.get()
.map(|s| s.chars().take(50).collect::<String>()),
)
.field(
"replacements",
&self.replacements.lock().iter().take(3).collect::<Vec<_>>(),
)
.finish()
}
}

impl<T: Source> StreamChunks for ReplaceSource<T> {
fn mappings_size_hint(&self) -> usize {
self.replacements.lock().len() * 2
Expand Down
14 changes: 13 additions & 1 deletion src/source_map_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<V, N> From<WithoutOriginalOptions<V, N>> for SourceMapSourceOptions<V, N> {
/// source map for the original source.
///
/// - [webpack-sources docs](https://github.com/webpack/webpack-sources/#sourcemapsource).
#[derive(Debug, Clone, Eq)]
#[derive(Clone, Eq)]
pub struct SourceMapSource {
value: String,
name: String,
Expand Down Expand Up @@ -134,6 +134,18 @@ impl PartialEq for SourceMapSource {
}
}

impl std::fmt::Debug for SourceMapSource {
fn fmt(
&self,
f: &mut std::fmt::Formatter<'_>,
) -> Result<(), std::fmt::Error> {
f.debug_struct("SourceMapSource")
.field("name", &self.name)
.field("value", &self.value.chars().take(50).collect::<String>())
.finish()
}
}

impl StreamChunks for SourceMapSource {
fn stream_chunks(
&self,
Expand Down

0 comments on commit 46c332f

Please sign in to comment.