diff --git a/src/autofix/fixer.rs b/src/autofix/fixer.rs index 5568fbab90..b6855ff82a 100644 --- a/src/autofix/fixer.rs +++ b/src/autofix/fixer.rs @@ -10,7 +10,7 @@ use crate::autofix::Fix; use crate::checks::Check; use crate::source_code_locator::SourceCodeLocator; -#[derive(Debug, Hash)] +#[derive(Debug, Copy, Clone, Hash)] pub enum Mode { Generate, Apply, diff --git a/src/cache.rs b/src/cache.rs index d8638fd1da..5e463ec232 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -13,7 +13,7 @@ use serde::{Deserialize, Serialize}; use crate::autofix::fixer; use crate::message::Message; -use crate::settings::Settings; +use crate::settings::{flags, Settings}; const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -34,43 +34,6 @@ struct CheckResult { messages: Vec, } -pub enum Mode { - ReadWrite, - ReadOnly, - WriteOnly, - None, -} - -impl Mode { - fn allow_read(&self) -> bool { - match self { - Mode::ReadWrite => true, - Mode::ReadOnly => true, - Mode::WriteOnly => false, - Mode::None => false, - } - } - - fn allow_write(&self) -> bool { - match self { - Mode::ReadWrite => true, - Mode::ReadOnly => false, - Mode::WriteOnly => true, - Mode::None => false, - } - } -} - -impl From for Mode { - fn from(value: bool) -> Self { - if value { - Mode::ReadWrite - } else { - Mode::None - } - } -} - fn cache_dir() -> &'static str { "./.ruff_cache" } @@ -79,7 +42,7 @@ fn content_dir() -> &'static str { "content" } -fn cache_key(path: &Path, settings: &Settings, autofix: &fixer::Mode) -> u64 { +fn cache_key(path: &Path, settings: &Settings, autofix: fixer::Mode) -> u64 { let mut hasher = DefaultHasher::new(); CARGO_PKG_VERSION.hash(&mut hasher); path.absolutize().unwrap().hash(&mut hasher); @@ -132,10 +95,10 @@ pub fn get( path: &Path, metadata: &Metadata, settings: &Settings, - autofix: &fixer::Mode, - mode: &Mode, + autofix: fixer::Mode, + cache: flags::Cache, ) -> Option> { - if !mode.allow_read() { + if matches!(cache, flags::Cache::Disabled) { return None; }; @@ -161,11 +124,11 @@ pub fn set( path: &Path, metadata: &Metadata, settings: &Settings, - autofix: &fixer::Mode, + autofix: fixer::Mode, messages: &[Message], - mode: &Mode, + cache: flags::Cache, ) { - if !mode.allow_write() { + if matches!(cache, flags::Cache::Disabled) { return; }; diff --git a/src/commands.rs b/src/commands.rs index 513d88fa25..ae2516a956 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -19,6 +19,7 @@ use crate::linter::{add_noqa_to_path, autoformat_path, lint_path, lint_stdin, Di use crate::message::Message; use crate::resolver; use crate::resolver::{FileDiscovery, PyprojectDiscovery}; +use crate::settings::flags; use crate::settings::types::SerializationFormat; /// Run the linter over a collection of files. @@ -27,8 +28,8 @@ pub fn run( pyproject_strategy: &PyprojectDiscovery, file_strategy: &FileDiscovery, overrides: &Overrides, - cache: bool, - autofix: &fixer::Mode, + cache: flags::Cache, + autofix: fixer::Mode, ) -> Result { // Collect all the files to check. let start = Instant::now(); @@ -44,7 +45,7 @@ pub fn run( Ok(entry) => { let path = entry.path(); let settings = resolver.resolve(path, pyproject_strategy); - lint_path(path, settings, &cache.into(), autofix) + lint_path(path, settings, cache, autofix) .map_err(|e| (Some(path.to_owned()), e.to_string())) } Err(e) => Err(( @@ -102,7 +103,7 @@ fn read_from_stdin() -> Result { pub fn run_stdin( strategy: &PyprojectDiscovery, filename: &Path, - autofix: &fixer::Mode, + autofix: fixer::Mode, ) -> Result { let stdin = read_from_stdin()?; let settings = match strategy { diff --git a/src/linter.rs b/src/linter.rs index 69d90efe0c..cad4638e4d 100644 --- a/src/linter.rs +++ b/src/linter.rs @@ -148,13 +148,13 @@ const MAX_ITERATIONS: usize = 100; pub fn lint_path( path: &Path, settings: &Settings, - mode: &cache::Mode, - autofix: &fixer::Mode, + cache: flags::Cache, + autofix: fixer::Mode, ) -> Result { let metadata = path.metadata()?; // Check the cache. - if let Some(messages) = cache::get(path, &metadata, settings, autofix, mode) { + if let Some(messages) = cache::get(path, &metadata, settings, autofix, cache) { debug!("Cache hit for: {}", path.to_string_lossy()); return Ok(Diagnostics::new(messages)); } @@ -166,7 +166,7 @@ pub fn lint_path( let (contents, fixed, messages) = lint(contents, path, settings, autofix)?; // Re-populate the cache. - cache::set(path, &metadata, settings, autofix, &messages, mode); + cache::set(path, &metadata, settings, autofix, &messages, cache); // If we applied any fixes, write the contents back to disk. if fixed > 0 { @@ -241,7 +241,7 @@ pub fn lint_stdin( path: &Path, stdin: &str, settings: &Settings, - autofix: &fixer::Mode, + autofix: fixer::Mode, ) -> Result { // Read the file from disk. let contents = stdin.to_string(); @@ -261,7 +261,7 @@ fn lint( mut contents: String, path: &Path, settings: &Settings, - autofix: &fixer::Mode, + autofix: fixer::Mode, ) -> Result<(String, usize, Vec)> { // Track the number of fixed errors across iterations. let mut fixed = 0; diff --git a/src/main.rs b/src/main.rs index fc58eadaad..22d92f7ac8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,10 +17,10 @@ use std::process::ExitCode; use std::sync::mpsc::channel; use ::ruff::autofix::fixer; -use ::ruff::cli::{extract_log_level, Cli}; +use ::ruff::cli::{extract_log_level, Cli, Overrides}; use ::ruff::logging::{set_up_logging, LogLevel}; use ::ruff::printer::Printer; -use ::ruff::resolver::PyprojectDiscovery; +use ::ruff::resolver::{resolve_settings, FileDiscovery, PyprojectDiscovery, Relativity}; use ::ruff::settings::configuration::Configuration; use ::ruff::settings::types::SerializationFormat; use ::ruff::settings::{pyproject, Settings}; @@ -32,8 +32,6 @@ use clap::{CommandFactory, Parser}; use colored::Colorize; use notify::{recommended_watcher, RecursiveMode, Watcher}; use path_absolutize::path_dedot; -use ruff::cli::Overrides; -use ruff::resolver::{resolve_settings, FileDiscovery, Relativity}; /// Resolve the relevant settings strategy and defaults for the current /// invocation. @@ -153,8 +151,8 @@ fn inner_main() -> Result { &pyproject_strategy, &file_strategy, &overrides, - cache_enabled, - &fixer::Mode::None, + cache_enabled.into(), + fixer::Mode::None, )?; printer.write_continuously(&messages)?; @@ -183,8 +181,8 @@ fn inner_main() -> Result { &pyproject_strategy, &file_strategy, &overrides, - cache_enabled, - &fixer::Mode::None, + cache_enabled.into(), + fixer::Mode::None, )?; printer.write_continuously(&messages)?; } @@ -211,15 +209,15 @@ fn inner_main() -> Result { let diagnostics = if is_stdin { let filename = cli.stdin_filename.unwrap_or_else(|| "-".to_string()); let path = Path::new(&filename); - commands::run_stdin(&pyproject_strategy, path, &autofix)? + commands::run_stdin(&pyproject_strategy, path, autofix)? } else { commands::run( &cli.files, &pyproject_strategy, &file_strategy, &overrides, - cache_enabled, - &autofix, + cache_enabled.into(), + autofix, )? }; @@ -227,7 +225,7 @@ fn inner_main() -> Result { // unless we're writing fixes via stdin (in which case, the transformed // source code goes to stdout). if !(is_stdin && matches!(autofix, fixer::Mode::Apply)) { - printer.write_once(&diagnostics, &autofix)?; + printer.write_once(&diagnostics, autofix)?; } // Check for updates if we're in a non-silent log level. diff --git a/src/printer.rs b/src/printer.rs index ce795653c1..0f7c71f4d1 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -57,7 +57,7 @@ impl<'a> Printer<'a> { } } - fn post_text(&self, num_fixable: usize, autofix: &fixer::Mode) { + fn post_text(&self, num_fixable: usize, autofix: fixer::Mode) { if self.log_level >= &LogLevel::Default { if num_fixable > 0 && !matches!(autofix, fixer::Mode::Apply) { println!("{num_fixable} potentially fixable with the --fix option."); @@ -65,7 +65,7 @@ impl<'a> Printer<'a> { } } - pub fn write_once(&self, diagnostics: &Diagnostics, autofix: &fixer::Mode) -> Result<()> { + pub fn write_once(&self, diagnostics: &Diagnostics, autofix: fixer::Mode) -> Result<()> { if matches!(self.log_level, LogLevel::Silent) { return Ok(()); } diff --git a/src/settings/flags.rs b/src/settings/flags.rs index a25335f4b4..bcf39d96d2 100644 --- a/src/settings/flags.rs +++ b/src/settings/flags.rs @@ -17,8 +17,8 @@ impl From for Autofix { } } -impl From<&fixer::Mode> for Autofix { - fn from(value: &fixer::Mode) -> Self { +impl From for Autofix { + fn from(value: fixer::Mode) -> Self { match value { fixer::Mode::Generate | fixer::Mode::Apply => Autofix::Enabled, fixer::Mode::None => Autofix::Disabled, @@ -41,3 +41,19 @@ impl From for Noqa { } } } + +#[derive(Debug, Copy, Clone)] +pub enum Cache { + Enabled, + Disabled, +} + +impl From for Cache { + fn from(value: bool) -> Self { + if value { + Cache::Enabled + } else { + Cache::Disabled + } + } +}