Skip to content

Commit 5d91ad6

Browse files
authored
Support fmt-like braced arguments (#112)
1 parent 1949e63 commit 5d91ad6

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/formatter.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ pub(crate) fn format(
173173
.collect::<String>();
174174
s.into()
175175
}
176+
TokenKind::Placeholder => format!("{} / {:?}", token.value, token.key).into(),
176177
_ => Cow::Borrowed(token.value),
177178
};
178179
anstream::eprintln!("{k}{:21}{rk}: {d}{:50}{rd} {line}", kind, value);

src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,6 +1764,29 @@ mod tests {
17641764
);
17651765
}
17661766

1767+
#[test]
1768+
fn it_recognizes_braced_placeholders_with_param_values() {
1769+
let input = "SELECT {a}, {b}, {c};";
1770+
let params = vec![
1771+
("a".to_string(), "first".to_string()),
1772+
("b".to_string(), "second".to_string()),
1773+
("c".to_string(), "third".to_string()),
1774+
];
1775+
let options = FormatOptions::default();
1776+
let expected = indoc!(
1777+
"
1778+
SELECT
1779+
first,
1780+
second,
1781+
third;"
1782+
);
1783+
1784+
assert_eq!(
1785+
format(input, &QueryParams::Named(params), &options),
1786+
expected
1787+
);
1788+
}
1789+
17671790
#[test]
17681791
fn it_formats_query_with_go_batch_separator() {
17691792
let input = "SELECT 1 GO SELECT 2";

src/tokenizer.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::borrow::Cow;
22
use unicode_categories::UnicodeCategories;
33
use winnow::ascii::{digit0, digit1, till_line_ending, Caseless};
4-
use winnow::combinator::{alt, dispatch, eof, fail, opt, peek, terminated};
4+
use winnow::combinator::{alt, delimited, dispatch, eof, fail, opt, peek, terminated};
55
use winnow::error::ContextError;
66
use winnow::error::ParserError;
77
use winnow::prelude::*;
@@ -326,13 +326,15 @@ fn get_placeholder_token<'i>(
326326
get_ident_named_placeholder_token,
327327
|input: &mut _| get_string_named_placeholder_token(input, dialect),
328328
get_indexed_placeholder_token,
329+
get_braced_named_placeholder_token,
329330
))
330331
.parse_next(input)
331332
} else {
332333
alt((
333334
get_indexed_placeholder_token,
334335
get_ident_named_placeholder_token,
335336
|input: &mut _| get_string_named_placeholder_token(input, dialect),
337+
get_braced_named_placeholder_token,
336338
))
337339
.parse_next(input)
338340
}
@@ -381,6 +383,25 @@ fn get_ident_named_placeholder_token<'i>(input: &mut &'i str) -> Result<Token<'i
381383
})
382384
}
383385

386+
fn get_braced_named_placeholder_token<'i>(input: &mut &'i str) -> Result<Token<'i>> {
387+
delimited(
388+
'{',
389+
take_while(1.., |c: char| c.is_alphanumeric() || c == '_'),
390+
'}',
391+
)
392+
.with_taken()
393+
.parse_next(input)
394+
.map(|(index, token)| {
395+
let index = Cow::Borrowed(index);
396+
Token {
397+
kind: TokenKind::Placeholder,
398+
value: token,
399+
key: Some(PlaceholderKind::Named(index)),
400+
alias: token,
401+
}
402+
})
403+
}
404+
384405
fn get_string_named_placeholder_token<'i>(
385406
input: &mut &'i str,
386407
dialect: Dialect,

0 commit comments

Comments
 (0)