use std::collections::BTreeSet;
use rustc_ast::Path;
use crate::macro_path::{matches_any, parse_path_list};
const CONFIG_KEY: &str = "perfectionist::print_macro_split";
const DEFAULT_MAX_LINE_WIDTH: usize = 100;
const DEFAULT_TARGET_MACROS: &[&str] = &[
"println", "eprintln", "print", "eprint", "writeln", "write", "log", "error", "warn", "info",
"debug", "trace",
];
#[derive(Debug, serde::Deserialize)]
#[serde(default, deny_unknown_fields, rename_all = "snake_case")]
pub(super) struct Config {
pub max_line_width: usize,
pub target_macros: Vec<String>,
}
impl Default for Config {
fn default() -> Self {
Self {
max_line_width: DEFAULT_MAX_LINE_WIDTH,
target_macros: DEFAULT_TARGET_MACROS
.iter()
.map(|name| (*name).to_owned())
.collect(),
}
}
}
pub(super) struct PrintMacroSplit {
max_line_width: usize,
target_macros: BTreeSet<Vec<String>>,
}
impl PrintMacroSplit {
pub(super) fn new() -> Self {
let config: Config = dylint_linting::config_or_default(CONFIG_KEY);
let target_macros = if config.target_macros.is_empty() {
BTreeSet::new()
} else {
parse_path_list(&config.target_macros)
};
Self {
max_line_width: config.max_line_width,
target_macros,
}
}
pub(super) fn max_line_width(&self) -> usize {
self.max_line_width
}
pub(super) fn should_check_path(&self, path: &Path) -> bool {
matches_any(path, &self.target_macros)
}
}