Replace cache bool with an enum

This commit is contained in:
Charlie Marsh 2022-12-16 15:44:13 -05:00
parent 1e19142d0e
commit 5f67ee93f7
7 changed files with 50 additions and 72 deletions

View File

@ -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,

View File

@ -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<Message>,
}
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<bool> 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<Vec<Message>> {
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;
};

View File

@ -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<Diagnostics> {
// 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<String> {
pub fn run_stdin(
strategy: &PyprojectDiscovery,
filename: &Path,
autofix: &fixer::Mode,
autofix: fixer::Mode,
) -> Result<Diagnostics> {
let stdin = read_from_stdin()?;
let settings = match strategy {

View File

@ -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<Diagnostics> {
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<Diagnostics> {
// 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<Message>)> {
// Track the number of fixed errors across iterations.
let mut fixed = 0;

View File

@ -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<ExitCode> {
&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<ExitCode> {
&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<ExitCode> {
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<ExitCode> {
// 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.

View File

@ -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(());
}

View File

@ -17,8 +17,8 @@ impl From<bool> for Autofix {
}
}
impl From<&fixer::Mode> for Autofix {
fn from(value: &fixer::Mode) -> Self {
impl From<fixer::Mode> 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<bool> for Noqa {
}
}
}
#[derive(Debug, Copy, Clone)]
pub enum Cache {
Enabled,
Disabled,
}
impl From<bool> for Cache {
fn from(value: bool) -> Self {
if value {
Cache::Enabled
} else {
Cache::Disabled
}
}
}