mirror of https://github.com/astral-sh/ruff
rule 6/8: Remove Serialize & Deserialize impls for Rule
This commit is contained in:
parent
6cf770a692
commit
6d11ff3822
|
|
@ -11,7 +11,7 @@ use itertools::iterate;
|
||||||
use ruff::fs::relativize_path;
|
use ruff::fs::relativize_path;
|
||||||
use ruff::logging::LogLevel;
|
use ruff::logging::LogLevel;
|
||||||
use ruff::message::{Location, Message};
|
use ruff::message::{Location, Message};
|
||||||
use ruff::registry::RuleCode;
|
use ruff::registry::Rule;
|
||||||
use ruff::settings::types::SerializationFormat;
|
use ruff::settings::types::SerializationFormat;
|
||||||
use ruff::{fix, notify_user};
|
use ruff::{fix, notify_user};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
|
@ -35,7 +35,7 @@ struct ExpandedFix<'a> {
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct ExpandedMessage<'a> {
|
struct ExpandedMessage<'a> {
|
||||||
code: &'a RuleCode,
|
code: SerializeRuleAsCode<'a>,
|
||||||
message: String,
|
message: String,
|
||||||
fix: Option<ExpandedFix<'a>>,
|
fix: Option<ExpandedFix<'a>>,
|
||||||
location: Location,
|
location: Location,
|
||||||
|
|
@ -43,6 +43,23 @@ struct ExpandedMessage<'a> {
|
||||||
filename: &'a str,
|
filename: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct SerializeRuleAsCode<'a>(&'a Rule);
|
||||||
|
|
||||||
|
impl Serialize for SerializeRuleAsCode<'_> {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: serde::Serializer,
|
||||||
|
{
|
||||||
|
serializer.serialize_str(self.0.code())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> From<&'a Rule> for SerializeRuleAsCode<'a> {
|
||||||
|
fn from(rule: &'a Rule) -> Self {
|
||||||
|
Self(rule)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Printer<'a> {
|
pub struct Printer<'a> {
|
||||||
format: &'a SerializationFormat,
|
format: &'a SerializationFormat,
|
||||||
log_level: &'a LogLevel,
|
log_level: &'a LogLevel,
|
||||||
|
|
@ -143,7 +160,7 @@ impl<'a> Printer<'a> {
|
||||||
.messages
|
.messages
|
||||||
.iter()
|
.iter()
|
||||||
.map(|message| ExpandedMessage {
|
.map(|message| ExpandedMessage {
|
||||||
code: message.kind.rule(),
|
code: message.kind.rule().into(),
|
||||||
message: message.kind.body(),
|
message: message.kind.body(),
|
||||||
fix: message.fix.as_ref().map(|fix| ExpandedFix {
|
fix: message.fix.as_ref().map(|fix| ExpandedFix {
|
||||||
content: &fix.content,
|
content: &fix.content,
|
||||||
|
|
@ -280,7 +297,7 @@ impl<'a> Printer<'a> {
|
||||||
json!({
|
json!({
|
||||||
"description": format!("({}) {}", message.kind.rule().code(), message.kind.body()),
|
"description": format!("({}) {}", message.kind.rule().code(), message.kind.body()),
|
||||||
"severity": "major",
|
"severity": "major",
|
||||||
"fingerprint": message.kind.rule(),
|
"fingerprint": message.kind.rule().code(),
|
||||||
"location": {
|
"location": {
|
||||||
"path": message.filename,
|
"path": message.filename,
|
||||||
"lines": {
|
"lines": {
|
||||||
|
|
|
||||||
|
|
@ -55,8 +55,6 @@ pub fn define_rule_mapping(mapping: &Mapping) -> proc_macro2::TokenStream {
|
||||||
PartialEq,
|
PartialEq,
|
||||||
Eq,
|
Eq,
|
||||||
Clone,
|
Clone,
|
||||||
Serialize, // TODO(martin): Remove
|
|
||||||
Deserialize, // TODO(martin): Remove
|
|
||||||
Hash,
|
Hash,
|
||||||
PartialOrd,
|
PartialOrd,
|
||||||
Ord,
|
Ord,
|
||||||
|
|
|
||||||
|
|
@ -51,13 +51,30 @@ export interface Diagnostic {
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct ExpandedMessage {
|
struct ExpandedMessage {
|
||||||
code: RuleCode,
|
code: SerializeRuleAsCode,
|
||||||
message: String,
|
message: String,
|
||||||
location: Location,
|
location: Location,
|
||||||
end_location: Location,
|
end_location: Location,
|
||||||
fix: Option<ExpandedFix>,
|
fix: Option<ExpandedFix>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct SerializeRuleAsCode(RuleCode);
|
||||||
|
|
||||||
|
impl Serialize for SerializeRuleAsCode {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: serde::Serializer,
|
||||||
|
{
|
||||||
|
serializer.serialize_str(self.0.code())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Rule> for SerializeRuleAsCode {
|
||||||
|
fn from(rule: Rule) -> Self {
|
||||||
|
Self(rule)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct ExpandedFix {
|
struct ExpandedFix {
|
||||||
content: String,
|
content: String,
|
||||||
|
|
@ -182,7 +199,7 @@ pub fn check(contents: &str, options: JsValue) -> Result<JsValue, JsValue> {
|
||||||
let messages: Vec<ExpandedMessage> = diagnostics
|
let messages: Vec<ExpandedMessage> = diagnostics
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|diagnostic| ExpandedMessage {
|
.map(|diagnostic| ExpandedMessage {
|
||||||
code: diagnostic.kind.rule().clone(),
|
code: diagnostic.kind.rule().clone().into(),
|
||||||
message: diagnostic.kind.body(),
|
message: diagnostic.kind.body(),
|
||||||
location: diagnostic.location,
|
location: diagnostic.location,
|
||||||
end_location: diagnostic.end_location,
|
end_location: diagnostic.end_location,
|
||||||
|
|
@ -224,7 +241,7 @@ mod test {
|
||||||
"if (1, 2): pass",
|
"if (1, 2): pass",
|
||||||
r#"{}"#,
|
r#"{}"#,
|
||||||
[ExpandedMessage {
|
[ExpandedMessage {
|
||||||
code: RuleCode::F634,
|
code: RuleCode::F634.into(),
|
||||||
message: "If test is a tuple, which is always `True`".to_string(),
|
message: "If test is a tuple, which is always `True`".to_string(),
|
||||||
location: Location::new(1, 0),
|
location: Location::new(1, 0),
|
||||||
end_location: Location::new(1, 15),
|
end_location: Location::new(1, 15),
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue