use std::collections::{BTreeMap, BTreeSet};
use rustc_span::Symbol;
const CONFIG_KEY: &str = "perfectionist::prefer_derive_more_over_thiserror";
const THISERROR_PATHS: &[&str] = &["thiserror::Error"];
#[derive(Debug, Default, serde::Deserialize)]
#[serde(default, deny_unknown_fields, rename_all = "snake_case")]
struct Config {}
pub(super) struct PreferDeriveMoreOverThiserror {
pub(super) thiserror_paths: Vec<Vec<Symbol>>,
pub(super) thiserror_crates: BTreeSet<Symbol>,
pub(super) aliases: BTreeSet<Symbol>,
pub(super) crate_aliases: BTreeMap<Symbol, Symbol>,
}
impl PreferDeriveMoreOverThiserror {
pub(super) fn new() -> Self {
let _config: Config = dylint_linting::config_or_default(CONFIG_KEY);
let thiserror_paths: Vec<Vec<Symbol>> = THISERROR_PATHS
.iter()
.map(|path| {
path.split("::")
.filter(|segment| !segment.is_empty())
.map(Symbol::intern)
.collect()
})
.filter(|segments: &Vec<Symbol>| !segments.is_empty())
.collect();
let thiserror_crates = thiserror_paths
.iter()
.filter_map(|segments| segments.first().copied())
.collect();
Self {
thiserror_paths,
thiserror_crates,
aliases: BTreeSet::new(),
crate_aliases: BTreeMap::new(),
}
}
}
pub(super) fn path_matches_thiserror(recognised: &[Vec<Symbol>], path: &[Symbol]) -> bool {
recognised.iter().any(|known| known.as_slice() == path)
}