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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! Builders for Tool and ToolComponent objects

use crate::parser::{SarifValidator, ValidationResult};
use crate::types::{
    ArtifactLocation, MultiformatMessage, ReportingDescriptor, Tool, ToolComponent,
    ToolComponentContents, ToolComponentReference, TranslationMetadata,
};
use std::collections::HashMap;

/// Fluent builder for creating SARIF Tool objects
#[derive(Debug, Clone)]
pub struct ToolBuilder {
    driver: ToolComponent,
    extensions: Vec<ToolComponent>,
}

impl ToolBuilder {
    /// Create a new Tool builder with a driver name
    pub fn new(driver_name: impl Into<String>) -> Self {
        Self {
            driver: ToolComponent::new(driver_name),
            extensions: Vec::new(),
        }
    }

    /// Create a Tool builder with a pre-built driver
    pub fn with_driver(driver: ToolComponent) -> Self {
        Self {
            driver,
            extensions: Vec::new(),
        }
    }

    /// Modify the driver using a builder function
    pub fn with_driver_builder<F>(mut self, f: F) -> Self
    where
        F: FnOnce(ToolComponentBuilder) -> ToolComponentBuilder,
    {
        let builder = ToolComponentBuilder::from_component(self.driver);
        self.driver = f(builder).build();
        self
    }

    /// Set driver version
    pub fn with_version(mut self, version: impl Into<String>) -> Self {
        self.driver.version = Some(version.into());
        self
    }

    /// Set driver organization
    pub fn with_organization(mut self, organization: impl Into<String>) -> Self {
        self.driver.organization = Some(organization.into());
        self
    }

    /// Add a rule to the driver
    pub fn add_rule(mut self, rule: ReportingDescriptor) -> Self {
        if self.driver.rules.is_none() {
            self.driver.rules = Some(Vec::new());
        }
        self.driver.rules.as_mut().unwrap().push(rule);
        self
    }

    /// Add multiple rules to the driver
    pub fn add_rules(mut self, rules: impl IntoIterator<Item = ReportingDescriptor>) -> Self {
        if self.driver.rules.is_none() {
            self.driver.rules = Some(Vec::new());
        }
        self.driver.rules.as_mut().unwrap().extend(rules);
        self
    }

    /// Add a simple rule with ID and name
    pub fn add_simple_rule(self, id: impl Into<String>, name: impl Into<String>) -> Self {
        let rule = ReportingDescriptor::new(id.into()).with_name(name.into());
        self.add_rule(rule)
    }

    /// Add an extension
    pub fn add_extension(mut self, extension: ToolComponent) -> Self {
        self.extensions.push(extension);
        self
    }

    /// Add multiple extensions
    pub fn add_extensions(mut self, extensions: impl IntoIterator<Item = ToolComponent>) -> Self {
        self.extensions.extend(extensions);
        self
    }

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

    /// Build the Tool object
    pub fn build(self) -> Tool {
        Tool {
            driver: self.driver,
            extensions: if self.extensions.is_empty() {
                None
            } else {
                Some(self.extensions)
            },
            properties: None,
        }
    }

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

/// Fluent builder for creating SARIF ToolComponent objects
#[derive(Debug, Clone)]
pub struct ToolComponentBuilder {
    guid: Option<String>,
    name: String,
    organization: Option<String>,
    product: Option<String>,
    product_suite: Option<String>,
    short_description: Option<MultiformatMessage>,
    full_description: Option<MultiformatMessage>,
    full_name: Option<String>,
    version: Option<String>,
    semantic_version: Option<String>,
    dotted_quad_file_version: Option<String>,
    release_date_utc: Option<String>,
    download_uri: Option<String>,
    information_uri: Option<String>,
    global_message_strings: Option<HashMap<String, MultiformatMessage>>,
    notifications: Vec<ReportingDescriptor>,
    rules: Vec<ReportingDescriptor>,
    taxa: Vec<ReportingDescriptor>,
    locations: Vec<ArtifactLocation>,
    language: Option<String>,
    contents: Option<Vec<ToolComponentContents>>,
    is_comprehensive: Option<bool>,
    localized_data_semantic_version: Option<String>,
    minimum_required_localized_data_semantic_version: Option<String>,
    associated_component: Option<ToolComponentReference>,
    translation_metadata: Option<TranslationMetadata>,
    supported_taxonomies: Vec<ToolComponentReference>,
}

impl ToolComponentBuilder {
    /// Create a new ToolComponent builder
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            guid: None,
            name: name.into(),
            organization: None,
            product: None,
            product_suite: None,
            short_description: None,
            full_description: None,
            full_name: None,
            version: None,
            semantic_version: None,
            dotted_quad_file_version: None,
            release_date_utc: None,
            download_uri: None,
            information_uri: None,
            global_message_strings: None,
            notifications: Vec::new(),
            rules: Vec::new(),
            taxa: Vec::new(),
            locations: Vec::new(),
            language: None,
            contents: None,
            is_comprehensive: None,
            localized_data_semantic_version: None,
            minimum_required_localized_data_semantic_version: None,
            associated_component: None,
            translation_metadata: None,
            supported_taxonomies: Vec::new(),
        }
    }

    /// Create a builder from an existing ToolComponent
    pub fn from_component(component: ToolComponent) -> Self {
        Self {
            guid: component.guid,
            name: component.name,
            organization: component.organization,
            product: component.product,
            product_suite: component.product_suite,
            short_description: component.short_description,
            full_description: component.full_description,
            full_name: component.full_name,
            version: component.version,
            semantic_version: component.semantic_version,
            dotted_quad_file_version: component.dotted_quad_file_version,
            release_date_utc: component.release_date_utc,
            download_uri: component.download_uri,
            information_uri: component.information_uri,
            global_message_strings: component.global_message_strings,
            notifications: component.notifications.unwrap_or_default(),
            rules: component.rules.unwrap_or_default(),
            taxa: component.taxa.unwrap_or_default(),
            locations: component.locations.unwrap_or_default(),
            language: component.language,
            contents: component.contents,
            is_comprehensive: component.is_comprehensive,
            localized_data_semantic_version: component.localized_data_semantic_version,
            minimum_required_localized_data_semantic_version: component
                .minimum_required_localized_data_semantic_version,
            associated_component: component.associated_component,
            translation_metadata: component.translation_metadata,
            supported_taxonomies: component.supported_taxonomies.unwrap_or_default(),
        }
    }

    /// Set the GUID
    pub fn with_guid(mut self, guid: impl Into<String>) -> Self {
        self.guid = Some(guid.into());
        self
    }

    /// Set the organization
    pub fn with_organization(mut self, organization: impl Into<String>) -> Self {
        self.organization = Some(organization.into());
        self
    }

    /// Set the product name
    pub fn with_product(mut self, product: impl Into<String>) -> Self {
        self.product = Some(product.into());
        self
    }

    /// Set the product suite
    pub fn with_product_suite(mut self, product_suite: impl Into<String>) -> Self {
        self.product_suite = Some(product_suite.into());
        self
    }

    /// Set short description
    pub fn with_short_description(mut self, description: MultiformatMessage) -> Self {
        self.short_description = Some(description);
        self
    }

    /// Set short description from text
    pub fn with_short_description_text(mut self, text: impl Into<String>) -> Self {
        self.short_description = Some(MultiformatMessage {
            text: text.into(),
            markdown: None,
            properties: None,
        });
        self
    }

    /// Set full description
    pub fn with_full_description(mut self, description: MultiformatMessage) -> Self {
        self.full_description = Some(description);
        self
    }

    /// Set full description from text
    pub fn with_full_description_text(mut self, text: impl Into<String>) -> Self {
        self.full_description = Some(MultiformatMessage {
            text: text.into(),
            markdown: None,
            properties: None,
        });
        self
    }

    /// Set the full name
    pub fn with_full_name(mut self, full_name: impl Into<String>) -> Self {
        self.full_name = Some(full_name.into());
        self
    }

    /// Set the version
    pub fn with_version(mut self, version: impl Into<String>) -> Self {
        self.version = Some(version.into());
        self
    }

    /// Set the semantic version
    pub fn with_semantic_version(mut self, semantic_version: impl Into<String>) -> Self {
        self.semantic_version = Some(semantic_version.into());
        self
    }

    /// Set the download URI
    pub fn with_download_uri(mut self, uri: impl Into<String>) -> Self {
        self.download_uri = Some(uri.into());
        self
    }

    /// Set the information URI
    pub fn with_information_uri(mut self, uri: impl Into<String>) -> Self {
        self.information_uri = Some(uri.into());
        self
    }

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

    /// Set whether the tool is comprehensive
    pub fn with_is_comprehensive(mut self, is_comprehensive: bool) -> Self {
        self.is_comprehensive = Some(is_comprehensive);
        self
    }

    /// Add a rule
    pub fn add_rule(mut self, rule: ReportingDescriptor) -> Self {
        self.rules.push(rule);
        self
    }

    /// Add multiple rules
    pub fn add_rules(mut self, rules: impl IntoIterator<Item = ReportingDescriptor>) -> Self {
        self.rules.extend(rules);
        self
    }

    /// Add a simple rule with ID and name
    pub fn add_simple_rule(mut self, id: impl Into<String>, name: impl Into<String>) -> Self {
        let rule = ReportingDescriptor::new(id.into()).with_name(name.into());
        self.rules.push(rule);
        self
    }

    /// Add a notification
    pub fn add_notification(mut self, notification: ReportingDescriptor) -> Self {
        self.notifications.push(notification);
        self
    }

    /// Add a taxonomy
    pub fn add_taxon(mut self, taxon: ReportingDescriptor) -> Self {
        self.taxa.push(taxon);
        self
    }

    /// Add a location
    pub fn add_location(mut self, location: ArtifactLocation) -> Self {
        self.locations.push(location);
        self
    }

    /// Add a global message string
    pub fn add_global_message_string(
        mut self,
        key: impl Into<String>,
        message: MultiformatMessage,
    ) -> Self {
        if self.global_message_strings.is_none() {
            self.global_message_strings = Some(HashMap::new());
        }
        self.global_message_strings
            .as_mut()
            .unwrap()
            .insert(key.into(), message);
        self
    }

    /// Add a supported taxonomy
    pub fn add_supported_taxonomy(mut self, taxonomy: ToolComponentReference) -> Self {
        self.supported_taxonomies.push(taxonomy);
        self
    }

    /// Validate the builder state
    pub fn validate(&self, validator: &SarifValidator) -> ValidationResult<()> {
        // Validate URIs if present
        if let Some(ref uri) = self.download_uri {
            validator.validate_uri(uri)?;
        }
        if let Some(ref uri) = self.information_uri {
            validator.validate_uri(uri)?;
        }

        // Validate rules
        for rule in &self.rules {
            validator.validate_reporting_descriptor(rule)?;
        }

        // Validate notifications
        for notification in &self.notifications {
            validator.validate_reporting_descriptor(notification)?;
        }

        // Validate taxa
        for taxon in &self.taxa {
            validator.validate_reporting_descriptor(taxon)?;
        }

        // Validate locations
        for location in &self.locations {
            validator.validate_artifact_location(location)?;
        }

        Ok(())
    }

    /// Build the ToolComponent object
    pub fn build(self) -> ToolComponent {
        ToolComponent {
            guid: self.guid,
            name: self.name,
            organization: self.organization,
            product: self.product,
            product_suite: self.product_suite,
            short_description: self.short_description,
            full_description: self.full_description,
            full_name: self.full_name,
            version: self.version,
            semantic_version: self.semantic_version,
            dotted_quad_file_version: self.dotted_quad_file_version,
            release_date_utc: self.release_date_utc,
            download_uri: self.download_uri,
            information_uri: self.information_uri,
            global_message_strings: self.global_message_strings,
            notifications: if self.notifications.is_empty() {
                None
            } else {
                Some(self.notifications)
            },
            rules: if self.rules.is_empty() {
                None
            } else {
                Some(self.rules)
            },
            taxa: if self.taxa.is_empty() {
                None
            } else {
                Some(self.taxa)
            },
            locations: if self.locations.is_empty() {
                None
            } else {
                Some(self.locations)
            },
            language: self.language,
            contents: self.contents,
            is_comprehensive: self.is_comprehensive,
            localized_data_semantic_version: self.localized_data_semantic_version,
            minimum_required_localized_data_semantic_version: self
                .minimum_required_localized_data_semantic_version,
            associated_component: self.associated_component,
            translation_metadata: self.translation_metadata,
            supported_taxonomies: if self.supported_taxonomies.is_empty() {
                None
            } else {
                Some(self.supported_taxonomies)
            },
            properties: None,
        }
    }

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