mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 14:21:53 +00:00
Implement ConfigProcessor on non-ref type (#6915)
This commit is contained in:
parent
f33277a057
commit
3f3494ad44
2 changed files with 9 additions and 14 deletions
|
@ -119,12 +119,13 @@ impl Resolver {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait ConfigProcessor: Copy + Send + Sync {
|
pub trait ConfigProcessor: Sync {
|
||||||
fn process_config(&self, config: &mut Configuration);
|
fn process_config(&self, config: &mut Configuration);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct NoOpProcessor;
|
struct NoOpProcessor;
|
||||||
impl ConfigProcessor for &NoOpProcessor {
|
|
||||||
|
impl ConfigProcessor for NoOpProcessor {
|
||||||
fn process_config(&self, _config: &mut Configuration) {}
|
fn process_config(&self, _config: &mut Configuration) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,7 +138,7 @@ impl ConfigProcessor for &NoOpProcessor {
|
||||||
pub fn resolve_configuration(
|
pub fn resolve_configuration(
|
||||||
pyproject: &Path,
|
pyproject: &Path,
|
||||||
relativity: &Relativity,
|
relativity: &Relativity,
|
||||||
processor: impl ConfigProcessor,
|
processor: &dyn ConfigProcessor,
|
||||||
) -> Result<Configuration> {
|
) -> Result<Configuration> {
|
||||||
let mut seen = FxHashSet::default();
|
let mut seen = FxHashSet::default();
|
||||||
let mut stack = vec![];
|
let mut stack = vec![];
|
||||||
|
@ -183,7 +184,7 @@ pub fn resolve_configuration(
|
||||||
pub fn resolve_scoped_settings(
|
pub fn resolve_scoped_settings(
|
||||||
pyproject: &Path,
|
pyproject: &Path,
|
||||||
relativity: &Relativity,
|
relativity: &Relativity,
|
||||||
processor: impl ConfigProcessor,
|
processor: &dyn ConfigProcessor,
|
||||||
) -> Result<(PathBuf, AllSettings)> {
|
) -> Result<(PathBuf, AllSettings)> {
|
||||||
let configuration = resolve_configuration(pyproject, relativity, processor)?;
|
let configuration = resolve_configuration(pyproject, relativity, processor)?;
|
||||||
let project_root = relativity.resolve(pyproject);
|
let project_root = relativity.resolve(pyproject);
|
||||||
|
@ -191,18 +192,12 @@ pub fn resolve_scoped_settings(
|
||||||
Ok((project_root, settings))
|
Ok((project_root, settings))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extract the [`Settings`] from a given `pyproject.toml`.
|
|
||||||
pub fn resolve_settings(pyproject: &Path, relativity: &Relativity) -> Result<AllSettings> {
|
|
||||||
let (_project_root, settings) = resolve_scoped_settings(pyproject, relativity, &NoOpProcessor)?;
|
|
||||||
Ok(settings)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Extract the [`Settings`] from a given `pyproject.toml` and process the
|
/// Extract the [`Settings`] from a given `pyproject.toml` and process the
|
||||||
/// configuration with the given [`ConfigProcessor`].
|
/// configuration with the given [`ConfigProcessor`].
|
||||||
pub fn resolve_settings_with_processor(
|
pub fn resolve_settings_with_processor(
|
||||||
pyproject: &Path,
|
pyproject: &Path,
|
||||||
relativity: &Relativity,
|
relativity: &Relativity,
|
||||||
processor: impl ConfigProcessor,
|
processor: &dyn ConfigProcessor,
|
||||||
) -> Result<AllSettings> {
|
) -> Result<AllSettings> {
|
||||||
let (_project_root, settings) = resolve_scoped_settings(pyproject, relativity, processor)?;
|
let (_project_root, settings) = resolve_scoped_settings(pyproject, relativity, processor)?;
|
||||||
Ok(settings)
|
Ok(settings)
|
||||||
|
@ -222,7 +217,7 @@ fn match_exclusion<P: AsRef<Path>, R: AsRef<Path>>(
|
||||||
pub fn python_files_in_path(
|
pub fn python_files_in_path(
|
||||||
paths: &[PathBuf],
|
paths: &[PathBuf],
|
||||||
pyproject_config: &PyprojectConfig,
|
pyproject_config: &PyprojectConfig,
|
||||||
processor: impl ConfigProcessor,
|
processor: &dyn ConfigProcessor,
|
||||||
) -> Result<(Vec<Result<DirEntry, ignore::Error>>, Resolver)> {
|
) -> Result<(Vec<Result<DirEntry, ignore::Error>>, Resolver)> {
|
||||||
// Normalize every path (e.g., convert from relative to absolute).
|
// Normalize every path (e.g., convert from relative to absolute).
|
||||||
let mut paths: Vec<PathBuf> = paths.iter().map(fs::normalize_path).unique().collect();
|
let mut paths: Vec<PathBuf> = paths.iter().map(fs::normalize_path).unique().collect();
|
||||||
|
@ -368,7 +363,7 @@ pub fn python_files_in_path(
|
||||||
pub fn python_file_at_path(
|
pub fn python_file_at_path(
|
||||||
path: &Path,
|
path: &Path,
|
||||||
pyproject_config: &PyprojectConfig,
|
pyproject_config: &PyprojectConfig,
|
||||||
processor: impl ConfigProcessor,
|
processor: &dyn ConfigProcessor,
|
||||||
) -> Result<bool> {
|
) -> Result<bool> {
|
||||||
if !pyproject_config.settings.lib.force_exclude {
|
if !pyproject_config.settings.lib.force_exclude {
|
||||||
return Ok(true);
|
return Ok(true);
|
||||||
|
|
|
@ -513,7 +513,7 @@ pub struct Overrides {
|
||||||
pub show_fixes: Option<bool>,
|
pub show_fixes: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConfigProcessor for &Overrides {
|
impl ConfigProcessor for Overrides {
|
||||||
fn process_config(&self, config: &mut ruff::settings::configuration::Configuration) {
|
fn process_config(&self, config: &mut ruff::settings::configuration::Configuration) {
|
||||||
if let Some(cache_dir) = &self.cache_dir {
|
if let Some(cache_dir) = &self.cache_dir {
|
||||||
config.cache_dir = Some(cache_dir.clone());
|
config.cache_dir = Some(cache_dir.clone());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue