Skip to content

Commit 0c431ac

Browse files
committed
Add some initial debugging capabilities
1 parent da0183b commit 0c431ac

File tree

5 files changed

+72
-1
lines changed

5 files changed

+72
-1
lines changed

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ description = "Formats whitespace in a SQL string to make it easier to read"
1212
keywords = ["sql"]
1313
categories = ["development-tools"]
1414

15+
[features]
16+
debug = ["dep:anstream", "dep:anstyle", "dep:okhsl"]
17+
1518
[dependencies]
19+
anstream = { version = "0.6.21", optional = true }
20+
anstyle = { version = "1.0.13", optional = true }
21+
okhsl = { version = "1.0.1", optional = true }
1622
unicode_categories = "0.1.1"
1723
winnow = { version = "0.7.0", features = ["simd"] }
1824

src/debug.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use std::hash::{DefaultHasher, Hash, Hasher};
2+
3+
use anstyle::RgbColor;
4+
5+
pub(crate) trait ToColor {
6+
fn to_color(&self) -> RgbColor;
7+
}
8+
9+
impl<T: Hash> ToColor for T {
10+
fn to_color(&self) -> RgbColor {
11+
let mut s = DefaultHasher::new();
12+
self.hash(&mut s);
13+
14+
let v = s.finish();
15+
16+
let v = ((v >> 32) as u32) ^ (v as u32);
17+
18+
let h = v as f64 / u32::MAX as f64;
19+
20+
let c = okhsl::Okhsv { h, s: 0.7, v: 0.9 };
21+
let r = c.to_srgb();
22+
23+
RgbColor(r.r, r.g, r.b)
24+
}
25+
}

src/formatter.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,43 @@ pub(crate) fn format(
140140
}
141141
},
142142
}
143+
144+
#[cfg(feature = "debug")]
145+
{
146+
use crate::debug::*;
147+
let b = anstyle::Style::new().bold();
148+
let k = b.fg_color(Some(token.kind.to_color().into()));
149+
let rk = k.render_reset();
150+
let k = k.render();
151+
let d = anstyle::Style::new().dimmed();
152+
let rd = d.render_reset();
153+
let d = d.render();
154+
155+
let kind = format!("{:?}", token.kind);
156+
157+
let mut lines = formatted_query
158+
.lines()
159+
.rev()
160+
.filter(|l| !l.trim().is_empty());
161+
let line = lines.next().unwrap_or(formatted_query.as_str());
162+
let value = match token.kind {
163+
TokenKind::Whitespace => {
164+
let s = token
165+
.value
166+
.chars()
167+
.map(|c| match c {
168+
'\n' => Cow::Borrowed(r"\n"),
169+
'\t' => Cow::Borrowed(r"\t"),
170+
'\r' => Cow::Borrowed(r"\r"),
171+
_ => c.to_string().into(),
172+
})
173+
.collect::<String>();
174+
s.into()
175+
}
176+
_ => Cow::Borrowed(token.value),
177+
};
178+
anstream::eprintln!("{k}{:21}{rk}: {d}{:50}{rd} {line}", kind, value);
179+
}
143180
}
144181
formatted_query.trim().to_string()
145182
}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ mod inline_block;
1515
mod params;
1616
mod tokenizer;
1717

18+
#[cfg(feature = "debug")]
19+
mod debug;
20+
1821
/// Formats whitespace in a SQL string to make it easier to read.
1922
/// Optionally replaces parameter placeholders with `params`.
2023
pub fn format(query: &str, params: &QueryParams, options: &FormatOptions) -> String {

src/tokenizer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub(crate) struct Token<'a> {
7474
pub alias: &'a str,
7575
}
7676

77-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
77+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7878
pub(crate) enum TokenKind {
7979
TypeSpecifier,
8080
Whitespace,

0 commit comments

Comments
 (0)