#![allow(clippy::missing_const_for_fn)]
use super::{FormatterConfig, IoConfig, OutputConfig, ParallelConfig};
use crate::cli::Cli;
#[derive(Debug, Clone, Default)]
pub struct CommonConfig {
pub output: OutputConfig,
pub formatter: FormatterConfig,
pub io: IoConfig,
pub parallel: ParallelConfig,
}
impl CommonConfig {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn from_cli(cli: &Cli) -> Self {
Self {
output: OutputConfig::from_cli(cli.quiet, cli.verbose, cli.no_color),
formatter: FormatterConfig::default(),
io: IoConfig::new()
.with_in_place(cli.in_place)
.with_output_path(cli.output.clone()),
parallel: ParallelConfig::default(),
}
}
#[must_use]
pub fn with_output(mut self, output: OutputConfig) -> Self {
self.output = output;
self
}
#[must_use]
pub fn with_formatter(mut self, formatter: FormatterConfig) -> Self {
self.formatter = formatter;
self
}
#[must_use]
pub fn with_io(mut self, io: IoConfig) -> Self {
self.io = io;
self
}
#[must_use]
pub fn with_parallel(mut self, parallel: ParallelConfig) -> Self {
self.parallel = parallel;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
#[test]
fn test_default_config() {
let config = CommonConfig::default();
assert!(!config.output.is_quiet());
assert!(!config.output.is_verbose());
assert_eq!(config.formatter.indent(), 2);
assert!(!config.io.is_in_place());
}
#[test]
fn test_new() {
let config = CommonConfig::new();
assert!(!config.output.is_quiet());
assert!(!config.output.is_verbose());
assert_eq!(config.formatter.indent(), 2);
assert!(!config.io.is_in_place());
}
#[test]
fn test_with_output() {
let output = OutputConfig::new().with_quiet(true);
let config = CommonConfig::new().with_output(output);
assert!(config.output.is_quiet());
}
#[test]
fn test_with_formatter() {
let formatter = FormatterConfig::new().with_indent(4);
let config = CommonConfig::new().with_formatter(formatter);
assert_eq!(config.formatter.indent(), 4);
}
#[test]
fn test_with_io() {
let io = IoConfig::new().with_in_place(true);
let config = CommonConfig::new().with_io(io);
assert!(config.io.is_in_place());
}
#[test]
fn test_builder_chaining() {
let output = OutputConfig::new().with_verbose(true);
let formatter = FormatterConfig::new().with_indent(4);
let io = IoConfig::new().with_output_path(Some(PathBuf::from("out.yaml")));
let config = CommonConfig::new()
.with_output(output)
.with_formatter(formatter)
.with_io(io);
assert!(config.output.is_verbose());
assert_eq!(config.formatter.indent(), 4);
assert_eq!(
config.io.output_path(),
Some(PathBuf::from("out.yaml").as_path())
);
}
}