diff --git a/src/formatter.rs b/src/formatter.rs index bde4401..6ef83f6 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -186,19 +186,30 @@ impl<'a> Formatter<'a> { { self.trim_spaces_end(query); } - if self.options.uppercase - && !self - .options - .ignore_case_convert - .as_ref() - .map(|values| values.contains(&token.value)) - .unwrap_or(false) - { - query.push_str(&token.value.to_uppercase()); - } else { - query.push_str(token.value); + + let value = match ( + self.options.uppercase, + self.options.ignore_case_convert.as_ref(), + ) { + (Some(uppercase), Some(values)) if !values.contains(&token.value) => { + if uppercase { + Cow::Owned(token.value.to_uppercase()) + } else { + Cow::Owned(token.value.to_lowercase()) + } + } + (Some(uppercase), None) => { + if uppercase { + Cow::Owned(token.value.to_uppercase()) + } else { + Cow::Owned(token.value.to_lowercase()) + } + } + _ => Cow::Borrowed(token.value), }; + query.push_str(&value); + self.inline_block.begin_if_possible(self.tokens, self.index); if !self.inline_block.is_active() { @@ -210,18 +221,27 @@ 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 - && !self - .options - .ignore_case_convert - .as_ref() - .map(|values| values.contains(&token.value)) - .unwrap_or(false) - { - token.value.to_uppercase() - } else { - token.value.to_string() + let value = match ( + self.options.uppercase, + self.options.ignore_case_convert.as_ref(), + ) { + (Some(uppercase), Some(values)) if !values.contains(&token.value) => { + if uppercase { + Cow::Owned(token.value.to_uppercase()) + } else { + Cow::Owned(token.value.to_lowercase()) + } + } + (Some(uppercase), None) => { + if uppercase { + Cow::Owned(token.value.to_uppercase()) + } else { + Cow::Owned(token.value.to_lowercase()) + } + } + _ => Cow::Borrowed(token.value), }; + token.value = &value; if self.inline_block.is_active() { @@ -317,17 +337,25 @@ impl<'a> Formatter<'a> { } fn format_reserved_word<'t>(&self, token: &'t str) -> Cow<'t, str> { - if self.options.uppercase - && !self - .options - .ignore_case_convert - .as_ref() - .map(|values| values.contains(&token)) - .unwrap_or(false) - { - Cow::Owned(token.to_uppercase()) - } else { - Cow::Borrowed(token) + match ( + self.options.uppercase, + self.options.ignore_case_convert.as_ref(), + ) { + (Some(uppercase), Some(values)) if !values.contains(&token) => { + if uppercase { + Cow::Owned(token.to_uppercase()) + } else { + Cow::Owned(token.to_lowercase()) + } + } + (Some(uppercase), None) => { + if uppercase { + Cow::Owned(token.to_uppercase()) + } else { + Cow::Owned(token.to_lowercase()) + } + } + _ => Cow::Borrowed(token), } } diff --git a/src/lib.rs b/src/lib.rs index 29a97b4..712be2f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,7 +32,7 @@ pub struct FormatOptions<'a> { /// When set, changes reserved keywords to ALL CAPS /// /// Default: false - pub uppercase: bool, + pub uppercase: Option, /// Controls the number of line breaks after a query /// /// Default: 1 @@ -47,7 +47,7 @@ impl<'a> Default for FormatOptions<'a> { fn default() -> Self { FormatOptions { indent: Indent::Spaces(2), - uppercase: false, + uppercase: None, lines_between_queries: 1, ignore_case_convert: None, } @@ -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!( @@ -1663,7 +1663,7 @@ mod tests { fn it_uses_given_ignore_case_convert_config() { let input = "select count(*),Column1 from Table1;"; let options = FormatOptions { - uppercase: true, + uppercase: Some(true), ignore_case_convert: Some(vec!["from"]), ..FormatOptions::default() }; @@ -1717,4 +1717,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); + } }