Skip to content

feat: make Debug readable #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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