mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:24:57 +00:00
factor out DiagnosticFormat::try_from(OutputFormat)
This commit is contained in:
parent
e5eb00e320
commit
79204caba6
2 changed files with 38 additions and 49 deletions
|
@ -18,7 +18,7 @@ use ruff_linter::logging::LogLevel;
|
||||||
use ruff_linter::message::{Emitter, EmitterContext, GroupedEmitter, SarifEmitter};
|
use ruff_linter::message::{Emitter, EmitterContext, GroupedEmitter, SarifEmitter};
|
||||||
use ruff_linter::notify_user;
|
use ruff_linter::notify_user;
|
||||||
use ruff_linter::settings::flags::{self};
|
use ruff_linter::settings::flags::{self};
|
||||||
use ruff_linter::settings::types::{OutputFormat, UnsafeFixes};
|
use ruff_linter::settings::types::{OutputFormat, RuffOutputFormat, UnsafeFixes};
|
||||||
|
|
||||||
use crate::diagnostics::{Diagnostics, FixMap};
|
use crate::diagnostics::{Diagnostics, FixMap};
|
||||||
|
|
||||||
|
@ -233,64 +233,24 @@ impl Printer {
|
||||||
.fix_applicability(self.unsafe_fixes.required_applicability())
|
.fix_applicability(self.unsafe_fixes.required_applicability())
|
||||||
.show_fix_diff(preview);
|
.show_fix_diff(preview);
|
||||||
|
|
||||||
match self.format {
|
match DiagnosticFormat::try_from(self.format) {
|
||||||
OutputFormat::Json => {
|
Ok(format) => {
|
||||||
let config = config.format(DiagnosticFormat::Json);
|
let config = config.format(format);
|
||||||
let value = DisplayDiagnostics::new(&context, &config, &diagnostics.inner);
|
let value = DisplayDiagnostics::new(&context, &config, &diagnostics.inner);
|
||||||
write!(writer, "{value}")?;
|
write!(writer, "{value}")?;
|
||||||
}
|
}
|
||||||
OutputFormat::Rdjson => {
|
Err(RuffOutputFormat::Github) => {
|
||||||
let config = config.format(DiagnosticFormat::Rdjson);
|
let renderer = GithubRenderer::new(&context, "Ruff");
|
||||||
let value = DisplayDiagnostics::new(&context, &config, &diagnostics.inner);
|
let value = DisplayGithubDiagnostics::new(&renderer, &diagnostics.inner);
|
||||||
write!(writer, "{value}")?;
|
write!(writer, "{value}")?;
|
||||||
}
|
}
|
||||||
OutputFormat::JsonLines => {
|
Err(RuffOutputFormat::Grouped) => {
|
||||||
let config = config.format(DiagnosticFormat::JsonLines);
|
|
||||||
let value = DisplayDiagnostics::new(&context, &config, &diagnostics.inner);
|
|
||||||
write!(writer, "{value}")?;
|
|
||||||
}
|
|
||||||
OutputFormat::Junit => {
|
|
||||||
let config = config.format(DiagnosticFormat::Junit);
|
|
||||||
let value = DisplayDiagnostics::new(&context, &config, &diagnostics.inner);
|
|
||||||
write!(writer, "{value}")?;
|
|
||||||
}
|
|
||||||
OutputFormat::Concise => {
|
|
||||||
let config = config.format(DiagnosticFormat::Concise);
|
|
||||||
let value = DisplayDiagnostics::new(&context, &config, &diagnostics.inner);
|
|
||||||
write!(writer, "{value}")?;
|
|
||||||
}
|
|
||||||
OutputFormat::Full => {
|
|
||||||
let config = config.format(DiagnosticFormat::Full);
|
|
||||||
let value = DisplayDiagnostics::new(&context, &config, &diagnostics.inner);
|
|
||||||
write!(writer, "{value}")?;
|
|
||||||
}
|
|
||||||
OutputFormat::Grouped => {
|
|
||||||
GroupedEmitter::default()
|
GroupedEmitter::default()
|
||||||
.with_show_fix_status(show_fix_status(self.fix_mode, fixables.as_ref()))
|
.with_show_fix_status(show_fix_status(self.fix_mode, fixables.as_ref()))
|
||||||
.with_unsafe_fixes(self.unsafe_fixes)
|
.with_unsafe_fixes(self.unsafe_fixes)
|
||||||
.emit(writer, &diagnostics.inner, &context)?;
|
.emit(writer, &diagnostics.inner, &context)?;
|
||||||
}
|
}
|
||||||
OutputFormat::Github => {
|
Err(RuffOutputFormat::Sarif) => {
|
||||||
let renderer = GithubRenderer::new(&context, "Ruff");
|
|
||||||
let value = DisplayGithubDiagnostics::new(&renderer, &diagnostics.inner);
|
|
||||||
write!(writer, "{value}")?;
|
|
||||||
}
|
|
||||||
OutputFormat::Gitlab => {
|
|
||||||
let config = config.format(DiagnosticFormat::Gitlab);
|
|
||||||
let value = DisplayDiagnostics::new(&context, &config, &diagnostics.inner);
|
|
||||||
write!(writer, "{value}")?;
|
|
||||||
}
|
|
||||||
OutputFormat::Pylint => {
|
|
||||||
let config = config.format(DiagnosticFormat::Pylint);
|
|
||||||
let value = DisplayDiagnostics::new(&context, &config, &diagnostics.inner);
|
|
||||||
write!(writer, "{value}")?;
|
|
||||||
}
|
|
||||||
OutputFormat::Azure => {
|
|
||||||
let config = config.format(DiagnosticFormat::Azure);
|
|
||||||
let value = DisplayDiagnostics::new(&context, &config, &diagnostics.inner);
|
|
||||||
write!(writer, "{value}")?;
|
|
||||||
}
|
|
||||||
OutputFormat::Sarif => {
|
|
||||||
SarifEmitter.emit(writer, &diagnostics.inner, &context)?;
|
SarifEmitter.emit(writer, &diagnostics.inner, &context)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ use anyhow::{Context, Result, bail};
|
||||||
use globset::{Glob, GlobMatcher, GlobSet, GlobSetBuilder};
|
use globset::{Glob, GlobMatcher, GlobSet, GlobSetBuilder};
|
||||||
use log::debug;
|
use log::debug;
|
||||||
use pep440_rs::{VersionSpecifier, VersionSpecifiers};
|
use pep440_rs::{VersionSpecifier, VersionSpecifiers};
|
||||||
|
use ruff_db::diagnostic::DiagnosticFormat;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use serde::{Deserialize, Deserializer, Serialize, de};
|
use serde::{Deserialize, Deserializer, Serialize, de};
|
||||||
use strum_macros::EnumIter;
|
use strum_macros::EnumIter;
|
||||||
|
@ -553,6 +554,34 @@ impl Display for OutputFormat {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The subset of output formats only implemented in Ruff, not in `ruff_db` via `DisplayDiagnostics`.
|
||||||
|
pub enum RuffOutputFormat {
|
||||||
|
Github,
|
||||||
|
Grouped,
|
||||||
|
Sarif,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<OutputFormat> for DiagnosticFormat {
|
||||||
|
type Error = RuffOutputFormat;
|
||||||
|
|
||||||
|
fn try_from(format: OutputFormat) -> std::result::Result<Self, Self::Error> {
|
||||||
|
match format {
|
||||||
|
OutputFormat::Concise => Ok(DiagnosticFormat::Concise),
|
||||||
|
OutputFormat::Full => Ok(DiagnosticFormat::Full),
|
||||||
|
OutputFormat::Json => Ok(DiagnosticFormat::Json),
|
||||||
|
OutputFormat::JsonLines => Ok(DiagnosticFormat::JsonLines),
|
||||||
|
OutputFormat::Junit => Ok(DiagnosticFormat::Junit),
|
||||||
|
OutputFormat::Gitlab => Ok(DiagnosticFormat::Gitlab),
|
||||||
|
OutputFormat::Pylint => Ok(DiagnosticFormat::Pylint),
|
||||||
|
OutputFormat::Rdjson => Ok(DiagnosticFormat::Rdjson),
|
||||||
|
OutputFormat::Azure => Ok(DiagnosticFormat::Azure),
|
||||||
|
OutputFormat::Github => Err(RuffOutputFormat::Github),
|
||||||
|
OutputFormat::Grouped => Err(RuffOutputFormat::Grouped),
|
||||||
|
OutputFormat::Sarif => Err(RuffOutputFormat::Sarif),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
|
||||||
#[serde(try_from = "String")]
|
#[serde(try_from = "String")]
|
||||||
pub struct RequiredVersion(VersionSpecifiers);
|
pub struct RequiredVersion(VersionSpecifiers);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue