use fast_yaml_core::EmitterConfig;
#[cfg(feature = "linter")]
use fast_yaml_linter::LintConfig;
#[derive(Debug, Clone)]
pub struct FormatterConfig {
indent: u8,
width: usize,
}
impl FormatterConfig {
pub const MIN_INDENT: u8 = 2;
pub const MAX_INDENT: u8 = 8;
pub const DEFAULT_WIDTH: usize = 80;
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn with_indent(mut self, indent: u8) -> Self {
self.indent = indent.clamp(Self::MIN_INDENT, Self::MAX_INDENT);
self
}
#[must_use]
pub const fn with_width(mut self, width: usize) -> Self {
self.width = width;
self
}
#[must_use]
pub const fn indent(&self) -> u8 {
self.indent
}
#[must_use]
pub const fn width(&self) -> usize {
self.width
}
#[must_use]
pub fn to_emitter_config(&self) -> EmitterConfig {
EmitterConfig::new()
.with_indent(self.indent as usize)
.with_width(self.width)
}
#[cfg(feature = "linter")]
#[must_use]
pub fn to_lint_config(&self, max_line_length: usize) -> LintConfig {
LintConfig::new()
.with_indent_size(self.indent as usize)
.with_max_line_length(Some(max_line_length))
}
}
impl Default for FormatterConfig {
fn default() -> Self {
Self {
indent: 2,
width: Self::DEFAULT_WIDTH,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = FormatterConfig::default();
assert_eq!(config.indent(), 2);
assert_eq!(config.width(), FormatterConfig::DEFAULT_WIDTH);
}
#[test]
fn test_new() {
let config = FormatterConfig::new();
assert_eq!(config.indent(), 2);
assert_eq!(config.width(), 80);
}
#[test]
fn test_with_indent() {
let config = FormatterConfig::new().with_indent(4);
assert_eq!(config.indent(), 4);
}
#[test]
fn test_with_width() {
let config = FormatterConfig::new().with_width(120);
assert_eq!(config.width(), 120);
}
#[test]
fn test_indent_clamping_min() {
let config = FormatterConfig::new().with_indent(1);
assert_eq!(config.indent(), FormatterConfig::MIN_INDENT);
}
#[test]
fn test_indent_clamping_max() {
let config = FormatterConfig::new().with_indent(10);
assert_eq!(config.indent(), FormatterConfig::MAX_INDENT);
}
#[test]
fn test_builder_chaining() {
let config = FormatterConfig::new().with_indent(4).with_width(100);
assert_eq!(config.indent(), 4);
assert_eq!(config.width(), 100);
}
#[test]
fn test_to_emitter_config() {
let config = FormatterConfig::new().with_indent(4).with_width(120);
let _emitter_config = config.to_emitter_config();
}
#[cfg(feature = "linter")]
#[test]
fn test_to_lint_config() {
let config = FormatterConfig::new().with_indent(4);
let _lint_config = config.to_lint_config(100);
}
}