From 46c332fbeb643a7c299be5df644dc155a9486a07 Mon Sep 17 00:00:00 2001 From: Boshen Date: Tue, 28 Nov 2023 17:24:18 +0800 Subject: [PATCH] feat: make `Debug` readable --- src/cached_source.rs | 15 ++++++++++++++- src/original_source.rs | 14 +++++++++++++- src/raw_source.rs | 23 ++++++++++++++++++++++- src/replace_source.rs | 23 ++++++++++++++++++++++- src/source_map_source.rs | 14 +++++++++++++- 5 files changed, 84 insertions(+), 5 deletions(-) diff --git a/src/cached_source.rs b/src/cached_source.rs index 2d08bbf2..aebd2063 100644 --- a/src/cached_source.rs +++ b/src/cached_source.rs @@ -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 { inner: Arc, cached_buffer: OnceCell>, @@ -159,6 +158,20 @@ impl PartialEq for CachedSource { impl Eq for CachedSource {} +impl std::fmt::Debug for CachedSource { + 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::{ diff --git a/src/original_source.rs b/src/original_source.rs index dde88d98..746acee3 100644 --- a/src/original_source.rs +++ b/src/original_source.rs @@ -34,7 +34,7 @@ use crate::{ /// "AAAA;AACA", /// ); /// ``` -#[derive(Debug, Clone, Eq)] +#[derive(Clone, Eq)] pub struct OriginalSource { value: String, name: String, @@ -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::()) + .finish() + } +} + impl StreamChunks for OriginalSource { fn stream_chunks( &self, diff --git a/src/raw_source.rs b/src/raw_source.rs index ee0f1ee7..9e623c84 100644 --- a/src/raw_source.rs +++ b/src/raw_source.rs @@ -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), @@ -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::>(), + ); + } + Self::Source(string) => { + d.field("source", &string.chars().take(50).collect::()); + } + } + d.finish() + } +} + impl StreamChunks for RawSource { fn stream_chunks( &self, diff --git a/src/replace_source.rs b/src/replace_source.rs index d55bc724..1f1a8cd9 100644 --- a/src/replace_source.rs +++ b/src/replace_source.rs @@ -34,7 +34,6 @@ use crate::{ /// /// assert_eq!(source.source(), "start1\nstart2\nreplaced!\nend1\nend2"); /// ``` -#[derive(Debug)] pub struct ReplaceSource { inner: Arc, inner_source_code: OnceCell>, @@ -188,6 +187,28 @@ impl Source for ReplaceSource { } } +impl std::fmt::Debug for ReplaceSource { + 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::()), + ) + .field( + "replacements", + &self.replacements.lock().iter().take(3).collect::>(), + ) + .finish() + } +} + impl StreamChunks for ReplaceSource { fn mappings_size_hint(&self) -> usize { self.replacements.lock().len() * 2 diff --git a/src/source_map_source.rs b/src/source_map_source.rs index a892d3b0..b2d2a48f 100644 --- a/src/source_map_source.rs +++ b/src/source_map_source.rs @@ -57,7 +57,7 @@ impl From> for SourceMapSourceOptions { /// 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, @@ -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::()) + .finish() + } +} + impl StreamChunks for SourceMapSource { fn stream_chunks( &self,