sarif_rust 0.3.0

A comprehensive Rust library for parsing, generating, and manipulating SARIF (Static Analysis Results Interchange Format) v2.1.0 files
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//! Builders for Location and PhysicalLocation objects

use crate::parser::{SarifValidator, ValidationResult};
use crate::types::location::ArtifactContent;
use crate::types::{
    Address, ArtifactLocation, Location, LogicalLocation, Message, PhysicalLocation, Region,
};

/// Fluent builder for creating SARIF Location objects
#[derive(Debug, Clone)]
pub struct LocationBuilder {
    id: Option<i32>,
    physical_location: Option<PhysicalLocation>,
    logical_locations: Vec<LogicalLocation>,
    message: Option<Message>,
    // annotations: Vec<Annotation>, // TODO: Add when Annotation type exists
    // relationships: Vec<LocationRelationship>, // TODO: Add when LocationRelationship type exists
}

impl LocationBuilder {
    /// Create a new Location builder
    pub fn new() -> Self {
        Self {
            id: None,
            physical_location: None,
            logical_locations: Vec::new(),
            message: None,
            // annotations: Vec::new(),
            // relationships: Vec::new(),
        }
    }

    /// Set the location ID
    pub fn with_id(mut self, id: i32) -> Self {
        self.id = Some(id);
        self
    }

    /// Set the physical location
    pub fn with_physical_location(mut self, physical_location: PhysicalLocation) -> Self {
        self.physical_location = Some(physical_location);
        self
    }

    /// Set the physical location using a builder function
    pub fn with_physical_location_builder<F>(mut self, f: F) -> Self
    where
        F: FnOnce(PhysicalLocationBuilder) -> PhysicalLocationBuilder,
    {
        let builder = PhysicalLocationBuilder::new();
        self.physical_location = Some(f(builder).build());
        self
    }

    /// Add a simple file location
    pub fn with_file_location(self, file_path: impl Into<String>) -> Self {
        self.with_physical_location(PhysicalLocation::with_artifact_location(
            ArtifactLocation::new(file_path),
        ))
    }

    /// Add a file location with region
    pub fn with_file_region(
        self,
        file_path: impl Into<String>,
        start_line: i32,
        start_column: i32,
        end_line: i32,
        end_column: i32,
    ) -> Self {
        self.with_physical_location(
            PhysicalLocation::with_artifact_location(ArtifactLocation::new(file_path)).with_region(
                Region::from_coordinates(start_line, start_column, end_line, end_column),
            ),
        )
    }

    /// Add a logical location
    pub fn add_logical_location(mut self, logical_location: LogicalLocation) -> Self {
        self.logical_locations.push(logical_location);
        self
    }

    /// Add multiple logical locations
    pub fn add_logical_locations(
        mut self,
        logical_locations: impl IntoIterator<Item = LogicalLocation>,
    ) -> Self {
        self.logical_locations.extend(logical_locations);
        self
    }

    /// Set the message
    pub fn with_message(mut self, message: Message) -> Self {
        self.message = Some(message);
        self
    }

    /// Set a simple text message
    pub fn with_text_message(mut self, text: impl Into<String>) -> Self {
        self.message = Some(Message::new(text));
        self
    }

    // TODO: Add annotation and relationship methods when types exist

    /// Validate the builder state
    pub fn validate(&self, validator: &SarifValidator) -> ValidationResult<()> {
        if let Some(ref physical_location) = self.physical_location {
            validator.validate_physical_location(physical_location)?;
        }

        for logical_location in &self.logical_locations {
            validator.validate_logical_location(logical_location)?;
        }

        if let Some(ref message) = self.message {
            validator.validate_message(message)?;
        }

        Ok(())
    }

    /// Build the Location object
    pub fn build(self) -> Location {
        let mut location = Location::new();

        location.id = self.id;
        location.physical_location = self.physical_location;
        location.message = self.message;

        if !self.logical_locations.is_empty() {
            location.logical_locations = Some(self.logical_locations);
        }
        // TODO: Set annotations and relationships when available

        location
    }

    /// Build and validate the Location object
    pub fn build_validated(self, validator: &SarifValidator) -> ValidationResult<Location> {
        self.validate(validator)?;
        Ok(self.build())
    }
}

impl Default for LocationBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Fluent builder for creating SARIF PhysicalLocation objects
#[derive(Debug, Clone)]
pub struct PhysicalLocationBuilder {
    artifact_location: ArtifactLocation,
    region: Option<Region>,
    context_region: Option<Region>,
    address: Option<Address>,
}

impl PhysicalLocationBuilder {
    /// Create a new PhysicalLocation builder
    pub fn new() -> Self {
        Self {
            artifact_location: ArtifactLocation::new(""),
            region: None,
            context_region: None,
            address: None,
        }
    }

    /// Create a new PhysicalLocation builder with an artifact location
    pub fn with_artifact_location(artifact_location: ArtifactLocation) -> Self {
        Self {
            artifact_location,
            region: None,
            context_region: None,
            address: None,
        }
    }

    /// Create a new PhysicalLocation builder with a file path
    pub fn with_file_path(file_path: impl Into<String>) -> Self {
        Self::with_artifact_location(ArtifactLocation::new(file_path))
    }

    /// Set the artifact location
    pub fn set_artifact_location(mut self, artifact_location: ArtifactLocation) -> Self {
        self.artifact_location = artifact_location;
        self
    }

    /// Set the file path (updates artifact location)
    pub fn set_file_path(mut self, file_path: impl Into<String>) -> Self {
        self.artifact_location = ArtifactLocation::new(file_path);
        self
    }

    /// Set the region
    pub fn with_region(mut self, region: Region) -> Self {
        self.region = Some(region);
        self
    }

    /// Set region using coordinates
    pub fn with_coordinates(
        mut self,
        start_line: i32,
        start_column: i32,
        end_line: i32,
        end_column: i32,
    ) -> Self {
        self.region = Some(Region::from_coordinates(
            start_line,
            start_column,
            end_line,
            end_column,
        ));
        self
    }

    /// Set region using character offset
    pub fn with_char_offset(mut self, char_offset: i32, char_length: i32) -> Self {
        self.region = Some(Region::from_char_offset(char_offset, char_length));
        self
    }

    /// Set region using a single line and column
    pub fn with_line_column(mut self, line: i32, column: i32) -> Self {
        self.region = Some(Region::from_coordinates(line, column, line, column));
        self
    }

    /// Set region with snippet text
    pub fn with_region_snippet(
        mut self,
        start_line: i32,
        start_column: i32,
        end_line: i32,
        end_column: i32,
        snippet_text: impl Into<String>,
    ) -> Self {
        let mut region = Region::from_coordinates(start_line, start_column, end_line, end_column);
        region.snippet = Some(ArtifactContent {
            text: Some(snippet_text.into()),
            binary: None,
            properties: None,
        });
        self.region = Some(region);
        self
    }

    /// Set the context region
    pub fn with_context_region(mut self, context_region: Region) -> Self {
        self.context_region = Some(context_region);
        self
    }

    /// Set the address
    pub fn with_address(mut self, address: Address) -> Self {
        self.address = Some(address);
        self
    }

    /// Validate the builder state
    pub fn validate(&self, validator: &SarifValidator) -> ValidationResult<()> {
        validator.validate_artifact_location(&self.artifact_location)?;

        if let Some(ref region) = self.region {
            validator.validate_region(region)?;
        }

        if let Some(ref context_region) = self.context_region {
            validator.validate_region(context_region)?;
        }

        Ok(())
    }

    /// Build the PhysicalLocation object
    pub fn build(self) -> PhysicalLocation {
        let mut physical_location =
            PhysicalLocation::with_artifact_location(self.artifact_location);
        physical_location.region = self.region;
        physical_location.context_region = self.context_region;
        physical_location.address = self.address;
        physical_location
    }

    /// Build and validate the PhysicalLocation object
    pub fn build_validated(self, validator: &SarifValidator) -> ValidationResult<PhysicalLocation> {
        self.validate(validator)?;
        Ok(self.build())
    }
}

impl Default for PhysicalLocationBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Fluent builder for creating SARIF Region objects
#[derive(Debug, Clone)]
pub struct RegionBuilder {
    start_line: Option<i32>,
    start_column: Option<i32>,
    end_line: Option<i32>,
    end_column: Option<i32>,
    char_offset: Option<i32>,
    char_length: Option<i32>,
    byte_offset: Option<i32>,
    byte_length: Option<i32>,
    snippet: Option<ArtifactContent>,
    message: Option<Message>,
    source_language: Option<String>,
}

impl RegionBuilder {
    /// Create a new Region builder
    pub fn new() -> Self {
        Self {
            start_line: None,
            start_column: None,
            end_line: None,
            end_column: None,
            char_offset: None,
            char_length: None,
            byte_offset: None,
            byte_length: None,
            snippet: None,
            message: None,
            source_language: None,
        }
    }

    /// Set coordinates (line/column based)
    pub fn with_coordinates(
        mut self,
        start_line: i32,
        start_column: i32,
        end_line: i32,
        end_column: i32,
    ) -> Self {
        self.start_line = Some(start_line);
        self.start_column = Some(start_column);
        self.end_line = Some(end_line);
        self.end_column = Some(end_column);
        self
    }

    /// Set character offset and length
    pub fn with_char_offset(mut self, char_offset: i32, char_length: i32) -> Self {
        self.char_offset = Some(char_offset);
        self.char_length = Some(char_length);
        self
    }

    /// Set byte offset and length
    pub fn with_byte_offset(mut self, byte_offset: i32, byte_length: i32) -> Self {
        self.byte_offset = Some(byte_offset);
        self.byte_length = Some(byte_length);
        self
    }

    /// Set a single line and column
    pub fn with_line_column(mut self, line: i32, column: i32) -> Self {
        self.start_line = Some(line);
        self.start_column = Some(column);
        self.end_line = Some(line);
        self.end_column = Some(column);
        self
    }

    /// Set snippet text
    pub fn with_snippet_text(mut self, text: impl Into<String>) -> Self {
        self.snippet = Some(ArtifactContent {
            text: Some(text.into()),
            binary: None,
            properties: None,
        });
        self
    }

    /// Set snippet content
    pub fn with_snippet(mut self, snippet: ArtifactContent) -> Self {
        self.snippet = Some(snippet);
        self
    }

    /// Set message
    pub fn with_message(mut self, message: Message) -> Self {
        self.message = Some(message);
        self
    }

    /// Set simple text message
    pub fn with_text_message(mut self, text: impl Into<String>) -> Self {
        self.message = Some(Message::new(text));
        self
    }

    /// Set source language
    pub fn with_source_language(mut self, language: impl Into<String>) -> Self {
        self.source_language = Some(language.into());
        self
    }

    /// Validate the builder state
    pub fn validate(&self, validator: &SarifValidator) -> ValidationResult<()> {
        let region = self.clone().build();
        validator.validate_region(&region)
    }

    /// Build the Region object
    pub fn build(self) -> Region {
        Region {
            start_line: self.start_line,
            start_column: self.start_column,
            end_line: self.end_line,
            end_column: self.end_column,
            char_offset: self.char_offset,
            char_length: self.char_length,
            byte_offset: self.byte_offset,
            byte_length: self.byte_length,
            snippet: self.snippet,
            message: self.message,
            source_language: self.source_language,
            properties: None,
        }
    }

    /// Build and validate the Region object
    pub fn build_validated(self, validator: &SarifValidator) -> ValidationResult<Region> {
        let region = self.build();
        validator.validate_region(&region)?;
        Ok(region)
    }
}

impl Default for RegionBuilder {
    fn default() -> Self {
        Self::new()
    }
}