use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum IndentStyle {
#[default]
Spaces,
Tabs,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FormatConfig {
pub indent_style: IndentStyle,
pub indent_width: usize,
pub max_line_width: usize,
pub trailing_newline: bool,
pub preserve_comments: bool,
pub sort_imports: bool,
}
impl Default for FormatConfig {
fn default() -> Self {
Self {
indent_style: IndentStyle::Spaces,
indent_width: 4,
max_line_width: 100,
trailing_newline: true,
preserve_comments: true,
sort_imports: true,
}
}
}
impl FormatConfig {
pub fn new() -> Self {
Self::default()
}
pub fn with_indent_style(mut self, style: IndentStyle) -> Self {
self.indent_style = style;
self
}
pub fn with_indent_width(mut self, width: usize) -> Self {
self.indent_width = width;
self
}
pub fn with_tabs(mut self) -> Self {
self.indent_style = IndentStyle::Tabs;
self
}
pub fn indent_string(&self) -> String {
match self.indent_style {
IndentStyle::Spaces => " ".repeat(self.indent_width),
IndentStyle::Tabs => "\t".to_string(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = FormatConfig::default();
assert_eq!(config.indent_style, IndentStyle::Spaces);
assert_eq!(config.indent_width, 4);
assert_eq!(config.max_line_width, 100);
assert!(config.trailing_newline);
assert!(config.preserve_comments);
assert!(config.sort_imports);
}
#[test]
fn test_indent_string_spaces() {
let config = FormatConfig::default().with_indent_width(2);
assert_eq!(config.indent_string(), " ");
}
#[test]
fn test_indent_string_tabs() {
let config = FormatConfig::default().with_tabs();
assert_eq!(config.indent_string(), "\t");
}
#[test]
fn test_builder_pattern() {
let config = FormatConfig::new()
.with_indent_style(IndentStyle::Tabs)
.with_indent_width(8);
assert_eq!(config.indent_style, IndentStyle::Tabs);
assert_eq!(config.indent_width, 8);
}
}