use structs instead of json!

This commit is contained in:
Brent Westbrook 2025-08-27 12:05:45 -04:00
parent 6a652d2ff3
commit e4c4bee35d

View file

@ -4,8 +4,8 @@ use std::{
path::Path, path::Path,
}; };
use ruff_source_file::LineColumn;
use serde::{Serialize, Serializer, ser::SerializeSeq}; use serde::{Serialize, Serializer, ser::SerializeSeq};
use serde_json::json;
use crate::diagnostic::Diagnostic; use crate::diagnostic::Diagnostic;
@ -98,21 +98,21 @@ impl Serialize for SerializedMessages<'_> {
let description = diagnostic.body(); let description = diagnostic.body();
let check_name = diagnostic.secondary_code_or_id(); let check_name = diagnostic.secondary_code_or_id();
let value = json!({ let value = Message {
"check_name": check_name, check_name,
// GitLab doesn't display the separate `check_name` field in a Code Quality report, // GitLab doesn't display the separate `check_name` field in a Code Quality report,
// so prepend it to the description too. // so prepend it to the description too.
"description": format!("{check_name}: {description}"), description: format!("{check_name}: {description}"),
"severity": "major", severity: "major",
"fingerprint": format!("{:x}", message_fingerprint), fingerprint: format!("{:x}", message_fingerprint),
"location": { location: Location {
"path": path, path,
"positions": { positions: Position {
"begin": start_location, begin: start_location,
"end": end_location, end: end_location,
}, },
}, },
}); };
s.serialize_element(&value)?; s.serialize_element(&value)?;
} }
@ -122,6 +122,27 @@ impl Serialize for SerializedMessages<'_> {
} }
} }
#[derive(Serialize)]
struct Message<'a> {
check_name: &'a str,
description: String,
severity: &'static str,
fingerprint: String,
location: Location,
}
#[derive(Serialize)]
struct Location {
path: String,
positions: Position,
}
#[derive(Serialize)]
struct Position {
begin: LineColumn,
end: LineColumn,
}
/// Generate a unique fingerprint to identify a violation. /// Generate a unique fingerprint to identify a violation.
fn fingerprint(message: &Diagnostic, project_path: &str, salt: u64) -> u64 { fn fingerprint(message: &Diagnostic, project_path: &str, salt: u64) -> u64 {
let mut hasher = DefaultHasher::new(); let mut hasher = DefaultHasher::new();