Skip to content

Commit a08fac9

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

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,23 @@ 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 r = k.render_reset();
150+
let k = k.render();
151+
let kind = format!("{:?}", token.kind);
152+
153+
let mut lines = formatted_query
154+
.lines()
155+
.rev()
156+
.filter(|l| !l.trim().is_empty());
157+
let line = lines.next().unwrap_or(formatted_query.as_str());
158+
anstream::eprintln!("{k}{:21}{r}: {:50} {line}", kind, token.value);
159+
}
143160
}
144161
formatted_query.trim().to_string()
145162
}

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)