Skip to content

Commit

Permalink
fix: uppercase=false does not lowercase the query
Browse files Browse the repository at this point in the history
  • Loading branch information
wugeer committed Sep 6, 2024
1 parent 3b6e8e1 commit e034089
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 15 deletions.
25 changes: 13 additions & 12 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,12 @@ impl<'a> Formatter<'a> {
{
self.trim_spaces_end(query);
}
if self.options.uppercase {
query.push_str(&token.value.to_uppercase());
} else {
query.push_str(token.value);
let value = match self.options.uppercase {
Some(true) => token.value.to_uppercase(),
Some(false) => token.value.to_lowercase(),
_ => token.value.to_string(),
};
query.push_str(&value);

self.inline_block.begin_if_possible(self.tokens, self.index);

Expand All @@ -178,10 +179,10 @@ impl<'a> Formatter<'a> {
// Closing parentheses decrease the block indent level
fn format_closing_parentheses(&mut self, token: &Token<'_>, query: &mut String) {
let mut token = token.clone();
let value = if self.options.uppercase {
token.value.to_uppercase()
} else {
token.value.to_string()
let value = match self.options.uppercase {
Some(true) => token.value.to_uppercase(),
Some(false) => token.value.to_lowercase(),
_ => token.value.to_string(),
};
token.value = &value;

Expand Down Expand Up @@ -278,10 +279,10 @@ impl<'a> Formatter<'a> {
}

fn format_reserved_word<'t>(&self, token: &'t str) -> Cow<'t, str> {
if self.options.uppercase {
Cow::Owned(token.to_uppercase())
} else {
Cow::Borrowed(token)
match self.options.uppercase {
Some(true) => Cow::Owned(token.to_uppercase()),
Some(false) => Cow::Owned(token.to_lowercase()),
_ => Cow::Borrowed(token),
}
}

Expand Down
50 changes: 47 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct FormatOptions {
/// When set, changes reserved keywords to ALL CAPS
///
/// Default: false
pub uppercase: bool,
uppercase: Option<bool>,
/// Controls the number of line breaks after a query
///
/// Default: 1
Expand All @@ -43,7 +43,7 @@ impl Default for FormatOptions {
fn default() -> Self {
FormatOptions {
indent: Indent::Spaces(2),
uppercase: false,
uppercase: None,
lines_between_queries: 1,
}
}
Expand Down Expand Up @@ -741,7 +741,7 @@ mod tests {
fn it_converts_keywords_to_uppercase_when_option_passed_in() {
let input = "select distinct * frOM foo left join bar WHERe cola > 1 and colb = 3";
let options = FormatOptions {
uppercase: true,
uppercase: Some(true),
..FormatOptions::default()
};
let expected = indoc!(
Expand Down Expand Up @@ -1658,4 +1658,48 @@ mod tests {

assert_eq!(format(input, &QueryParams::None, options), expected);
}

#[test]
fn it_converts_keywords_to_lowercase_when_option_passed_in() {
let input = "select distinct * frOM foo left join bar WHERe cola > 1 and colb = 3";
let options = FormatOptions {
uppercase: Some(false),
..FormatOptions::default()
};
let expected = indoc!(
"
select
distinct *
from
foo
left join bar
where
cola > 1
and colb = 3"
);

assert_eq!(format(input, &QueryParams::None, options), expected);
}

#[test]
fn it_converts_keywords_nothing_when_no_option_passed_in() {
let input = "select distinct * frOM foo left join bar WHERe cola > 1 and colb = 3";
let options = FormatOptions {
uppercase: None,
..FormatOptions::default()
};
let expected = indoc!(
"
select
distinct *
frOM
foo
left join bar
WHERe
cola > 1
and colb = 3"
);

assert_eq!(format(input, &QueryParams::None, options), expected);
}
}

0 comments on commit e034089

Please sign in to comment.