use crate::pg_formatter::PgFormatterConfig;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FormatStyle {
#[default]
SqlStyle,
PgFormatter,
Compact,
}
impl FormatStyle {
pub fn to_config(self) -> FormatConfig {
match self {
FormatStyle::SqlStyle => FormatConfig::sqlstyle(),
FormatStyle::PgFormatter => PgFormatterConfig::default().to_format_config(),
FormatStyle::Compact => FormatConfig::compact(),
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum KeywordCase {
#[default]
Upper,
Lower,
Preserve,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum IdentifierCase {
#[default]
Lower,
Preserve,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IndentStyle {
Spaces(usize),
Tabs,
}
impl Default for IndentStyle {
fn default() -> Self {
IndentStyle::Spaces(4)
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CommaStyle {
#[default]
Trailing,
Leading,
}
#[must_use]
#[derive(Debug, Clone)]
pub struct FormatConfig {
pub keyword_case: KeywordCase,
pub identifier_case: IdentifierCase,
pub indent: IndentStyle,
pub max_width: usize,
pub river_alignment: bool,
pub newline_before_logical: bool,
pub spaces_around_operators: bool,
pub comma_style: CommaStyle,
pub parentheses_spacing: bool,
pub align_select_items: bool,
pub river_width: usize,
}
impl Default for FormatConfig {
fn default() -> Self {
Self::sqlstyle()
}
}
impl FormatConfig {
pub fn sqlstyle() -> Self {
Self {
keyword_case: KeywordCase::Upper,
identifier_case: IdentifierCase::Lower,
indent: IndentStyle::Spaces(4),
max_width: 88,
river_alignment: true,
newline_before_logical: true,
spaces_around_operators: true,
comma_style: CommaStyle::Trailing,
parentheses_spacing: false,
align_select_items: true,
river_width: 10, }
}
pub fn compact() -> Self {
Self {
keyword_case: KeywordCase::Upper,
identifier_case: IdentifierCase::Preserve,
indent: IndentStyle::Spaces(2),
max_width: 120,
river_alignment: false,
newline_before_logical: false,
spaces_around_operators: true,
comma_style: CommaStyle::Trailing,
parentheses_spacing: false,
align_select_items: false,
river_width: 0,
}
}
pub fn with_keyword_case(mut self, case: KeywordCase) -> Self {
self.keyword_case = case;
self
}
pub fn with_identifier_case(mut self, case: IdentifierCase) -> Self {
self.identifier_case = case;
self
}
pub fn with_indent(mut self, indent: IndentStyle) -> Self {
self.indent = indent;
self
}
pub fn with_max_width(mut self, width: usize) -> Self {
self.max_width = width;
self
}
pub fn with_river_alignment(mut self, enabled: bool) -> Self {
self.river_alignment = enabled;
self
}
pub fn with_newline_before_logical(mut self, enabled: bool) -> Self {
self.newline_before_logical = enabled;
self
}
pub fn with_spaces_around_operators(mut self, enabled: bool) -> Self {
self.spaces_around_operators = enabled;
self
}
pub fn with_comma_style(mut self, style: CommaStyle) -> Self {
self.comma_style = style;
self
}
pub fn with_parentheses_spacing(mut self, enabled: bool) -> Self {
self.parentheses_spacing = enabled;
self
}
pub fn indent_str(&self) -> String {
match self.indent {
IndentStyle::Spaces(n) => " ".repeat(n),
IndentStyle::Tabs => "\t".to_string(),
}
}
pub fn indent_size(&self) -> usize {
match self.indent {
IndentStyle::Spaces(n) => n,
IndentStyle::Tabs => 4, }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sqlstyle_defaults() {
let config = FormatConfig::sqlstyle();
assert_eq!(config.keyword_case, KeywordCase::Upper);
assert!(config.river_alignment);
assert_eq!(config.comma_style, CommaStyle::Trailing);
}
#[test]
fn test_compact_config() {
let config = FormatConfig::compact();
assert!(!config.river_alignment);
assert_eq!(config.comma_style, CommaStyle::Trailing);
}
#[test]
fn test_builder_pattern() {
let config = FormatConfig::sqlstyle()
.with_max_width(100)
.with_river_alignment(false);
assert_eq!(config.max_width, 100);
assert!(!config.river_alignment);
}
#[test]
fn test_indent_str() {
let config = FormatConfig::sqlstyle();
assert_eq!(config.indent_str(), " ");
let config = FormatConfig::compact();
assert_eq!(config.indent_str(), " ");
}
}