@@ -117,14 +117,7 @@ impl Serialize for SarifRule<'_> {
117117 }
118118}
119119
120- fn serialize_rule_code < S > ( code : & RuleCode < ' _ > , serializer : S ) -> Result < S :: Ok , S :: Error >
121- where
122- S : Serializer ,
123- {
124- serializer. serialize_str ( code. as_str ( ) )
125- }
126-
127- #[ derive( Debug , Serialize ) ]
120+ #[ derive( Debug ) ]
128121enum RuleCode < ' a > {
129122 SecondaryCode ( & ' a SecondaryCode ) ,
130123 LintId ( & ' a str ) ,
@@ -146,6 +139,15 @@ impl RuleCode<'_> {
146139 }
147140}
148141
142+ impl Serialize for RuleCode < ' _ > {
143+ fn serialize < S > ( & self , serializer : S ) -> std:: result:: Result < S :: Ok , S :: Error >
144+ where
145+ S : Serializer ,
146+ {
147+ serializer. serialize_str ( self . as_str ( ) )
148+ }
149+ }
150+
149151impl < ' a > From < & ' a Diagnostic > for RuleCode < ' a > {
150152 fn from ( code : & ' a Diagnostic ) -> Self {
151153 match code. secondary_code ( ) {
@@ -162,7 +164,6 @@ impl<'a> From<&'a Diagnostic> for RuleCode<'a> {
162164#[ derive( Debug , Serialize ) ]
163165#[ serde( rename_all = "camelCase" ) ]
164166struct SarifResult < ' a > {
165- #[ serde( serialize_with = "serialize_rule_code" ) ]
166167 rule_id : RuleCode < ' a > ,
167168 level : String ,
168169 message : SarifMessage ,
@@ -277,28 +278,8 @@ impl<'a> SarifResult<'a> {
277278 . collect ( )
278279 }
279280
280- fn sarif_region ( message : & ' a Diagnostic ) -> SarifRegion {
281- let start_location = message. expect_ruff_start_location ( ) ;
282- let end_location = message. expect_ruff_end_location ( ) ;
283- SarifRegion {
284- start_line : start_location. line ,
285- start_column : start_location. column ,
286- end_line : end_location. line ,
287- end_column : end_location. column ,
288- }
289- }
290-
291- fn location ( uri : String , region : SarifRegion ) -> SarifLocation {
292- SarifLocation {
293- physical_location : SarifPhysicalLocation {
294- artifact_location : SarifArtifactLocation { uri } ,
295- region,
296- } ,
297- }
298- }
299-
300- fn message_uri ( message : & Diagnostic ) -> Result < String > {
301- let path = normalize_path ( & * message. expect_ruff_filename ( ) ) ;
281+ fn uri ( diagnostic : & Diagnostic ) -> Result < String > {
282+ let path = normalize_path ( & * diagnostic. expect_ruff_filename ( ) ) ;
302283 #[ cfg( not( target_arch = "wasm32" ) ) ]
303284 return url:: Url :: from_file_path ( & path)
304285 . map_err ( |( ) | anyhow:: anyhow!( "Failed to convert path to URL: {}" , path. display( ) ) )
@@ -307,20 +288,31 @@ impl<'a> SarifResult<'a> {
307288 return Ok ( format ! ( "file://{}" , path. display( ) ) ) ;
308289 }
309290
310- fn from_message ( message : & ' a Diagnostic ) -> Result < Self > {
311- let sarif_region = Self :: sarif_region ( message) ;
312- let uri = Self :: message_uri ( message) ?;
313- let fixes = Self :: fixes ( message, sarif_region, & uri) ;
314- let locations = vec ! [ Self :: location( uri, sarif_region) ] ;
291+ fn from_message ( diagnostic : & ' a Diagnostic ) -> Result < Self > {
292+ let start_location = diagnostic. expect_ruff_start_location ( ) ;
293+ let end_location = diagnostic. expect_ruff_end_location ( ) ;
294+ let region = SarifRegion {
295+ start_line : start_location. line ,
296+ start_column : start_location. column ,
297+ end_line : end_location. line ,
298+ end_column : end_location. column ,
299+ } ;
300+
301+ let uri = Self :: uri ( diagnostic) ?;
315302
316303 Ok ( Self {
317- rule_id : RuleCode :: from ( message ) ,
304+ rule_id : RuleCode :: from ( diagnostic ) ,
318305 level : "error" . to_string ( ) ,
319306 message : SarifMessage {
320- text : message . body ( ) . to_string ( ) ,
307+ text : diagnostic . body ( ) . to_string ( ) ,
321308 } ,
322- locations,
323- fixes,
309+ fixes : Self :: fixes ( diagnostic, region, & uri) ,
310+ locations : vec ! [ SarifLocation {
311+ physical_location: SarifPhysicalLocation {
312+ artifact_location: SarifArtifactLocation { uri } ,
313+ region,
314+ } ,
315+ } ] ,
324316 } )
325317 }
326318}
0 commit comments