diagprint 0.8.1

A Rust diagnostics lifecycle framework for structured diagnostics, rendering, remediation, CI, editors, and telemetry.
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
use crate::{
    Applicability, Cause, Diagnostic, DiagnosticAttribute, DiagnosticValue, Label, LabelKind,
    REDACTED, Severity, SourceRevision, is_sensitive_key, sanitize_path, sanitize_url,
};
use chrono::{DateTime, Local};
use serde::Serialize;
use std::path::{Component, Path, PathBuf};
use uuid::Uuid;

/// Policy for free-form text in an external diagnostic export.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum ExportText {
    /// Preserve diagnostic text.
    #[default]
    Preserve,

    /// Replace diagnostic text with the standard redaction marker.
    Redact,
}

impl ExportText {
    fn apply(self, value: &str) -> String {
        match self {
            Self::Preserve => value.to_owned(),
            Self::Redact => REDACTED.to_owned(),
        }
    }
}

/// Policy for source paths in an external diagnostic export.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum ExportPath {
    /// Omit source paths entirely.
    Omit,

    /// Export only the final filename component.
    #[default]
    FileName,

    /// Export the original source path.
    ///
    /// This can expose usernames, workspace paths, temporary directories, or
    /// other machine-specific information and therefore requires explicit
    /// opt-in.
    FullPath,

    /// Export paths relative to the supplied repository/workspace root.
    ///
    /// Absolute paths outside the root fail closed to their filename. Safe
    /// relative input paths are preserved.
    RepositoryRelative(PathBuf),
}

impl ExportPath {
    fn apply(&self, value: &str) -> Option<String> {
        match self {
            Self::Omit => None,

            Self::FileName => Some(sanitize_path(value)),

            Self::FullPath => Some(value.to_owned()),

            Self::RepositoryRelative(root) => {
                let path = Path::new(value);

                if path.is_relative() {
                    return safe_relative_path(path).or_else(|| Some(sanitize_path(value)));
                }

                path.strip_prefix(root)
                    .ok()
                    .and_then(safe_relative_path)
                    .or_else(|| Some(sanitize_path(value)))
            }
        }
    }
}

fn safe_relative_path(path: &Path) -> Option<String> {
    if path.as_os_str().is_empty() {
        return None;
    }

    let mut parts = Vec::new();

    for component in path.components() {
        match component {
            Component::Normal(part) => {
                parts.push(part.to_string_lossy().into_owned());
            }

            Component::CurDir => {}

            Component::ParentDir | Component::RootDir | Component::Prefix(_) => {
                return None;
            }
        }
    }

    if parts.is_empty() {
        None
    } else {
        Some(parts.join("/"))
    }
}

/// Policy for arbitrary structured diagnostic attributes.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum ExportAttributes {
    /// Export only the structural attribute count.
    #[default]
    Omit,

    /// Export attribute names while redacting every value.
    Redact,

    /// Export non-sensitive attribute values while redacting values whose keys
    /// match diagprint's common sensitive-key rules.
    ///
    /// This is opt-in because arbitrary non-sensitive-looking attributes may
    /// still contain application-specific private information.
    RedactSensitive,

    /// Export every structured attribute value unchanged.
    ///
    /// This is an explicit trust-boundary opt-in.
    Full,
}

/// Policy for remediation information in an external diagnostic export.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum ExportRemediation {
    /// Omit suggestions entirely.
    Omit,

    /// Export suggestion metadata and edit/command counts only.
    ///
    /// Source expectations, replacement text, inserted text, deleted text, and
    /// command contents are never part of the safe export representation.
    #[default]
    MetadataOnly,
}

/// Policy for documentation URLs in an external diagnostic export.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum ExportUrl {
    /// Omit URLs entirely.
    Omit,

    /// Remove URL user information, query parameters, and fragments.
    #[default]
    Sanitize,

    /// Export the original URL.
    ///
    /// This can expose credentials or sensitive query parameters and therefore
    /// requires explicit opt-in.
    Full,
}

impl ExportUrl {
    fn apply(self, value: &str) -> Option<String> {
        match self {
            Self::Omit => None,
            Self::Sanitize => Some(sanitize_url(value)),
            Self::Full => Some(value.to_owned()),
        }
    }
}

/// Shared privacy policy for external diagnostic serialization.
///
/// The default policy preserves useful diagnostic text while excluding common
/// machine- and application-sensitive data:
///
/// - source paths become filenames;
/// - arbitrary attribute values are omitted;
/// - remediation exports metadata only;
/// - documentation URLs are sanitized;
/// - hostname and process ID are omitted;
/// - application identity remains available;
/// - edit payloads and suggested command contents are never exported.
///
/// [`ExportDiagnostic::from`] uses this default policy.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExportPolicy {
    text: ExportText,
    paths: ExportPath,
    attributes: ExportAttributes,
    remediation: ExportRemediation,
    urls: ExportUrl,

    include_application: bool,
    include_hostname: bool,
    include_process_id: bool,
}

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

impl ExportPolicy {
    /// Creates the conservative default external-export policy.
    pub fn new() -> Self {
        Self {
            text: ExportText::Preserve,
            paths: ExportPath::FileName,
            attributes: ExportAttributes::Omit,
            remediation: ExportRemediation::MetadataOnly,
            urls: ExportUrl::Sanitize,

            include_application: true,
            include_hostname: false,
            include_process_id: false,
        }
    }

    pub fn with_text(mut self, policy: ExportText) -> Self {
        self.text = policy;
        self
    }

    pub fn with_paths(mut self, policy: ExportPath) -> Self {
        self.paths = policy;
        self
    }

    pub fn with_attributes(mut self, policy: ExportAttributes) -> Self {
        self.attributes = policy;
        self
    }

    pub fn with_remediation(mut self, policy: ExportRemediation) -> Self {
        self.remediation = policy;
        self
    }

    pub fn with_urls(mut self, policy: ExportUrl) -> Self {
        self.urls = policy;
        self
    }

    pub fn with_application(mut self, enabled: bool) -> Self {
        self.include_application = enabled;
        self
    }

    pub fn with_hostname(mut self, enabled: bool) -> Self {
        self.include_hostname = enabled;
        self
    }

    pub fn with_process_id(mut self, enabled: bool) -> Self {
        self.include_process_id = enabled;
        self
    }

    pub const fn text(&self) -> ExportText {
        self.text
    }

    pub fn paths(&self) -> &ExportPath {
        &self.paths
    }

    pub const fn attributes(&self) -> ExportAttributes {
        self.attributes
    }

    pub const fn remediation(&self) -> ExportRemediation {
        self.remediation
    }

    pub const fn urls(&self) -> ExportUrl {
        self.urls
    }

    pub const fn includes_application(&self) -> bool {
        self.include_application
    }

    pub const fn includes_hostname(&self) -> bool {
        self.include_hostname
    }

    pub const fn includes_process_id(&self) -> bool {
        self.include_process_id
    }

    pub(crate) fn apply_text(&self, value: &str) -> String {
        self.text.apply(value)
    }

    pub(crate) fn apply_path(&self, value: &str) -> Option<String> {
        self.paths.apply(value)
    }
}

/// External, privacy-conscious representation of a diagnostic.
///
/// This representation is deliberately separate from [`struct@Diagnostic`].
/// Internal diagnostics retain complete remediation and process context while
/// exporters choose an explicit [`ExportPolicy`].
#[derive(Debug, Clone, Serialize)]
pub struct ExportDiagnostic {
    pub report_id: Uuid,
    pub session_id: Uuid,
    pub timestamp: DateTime<Local>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub application: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub pid: Option<u32>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub hostname: Option<String>,

    pub severity: Severity,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub code: Option<String>,

    pub message: String,

    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub attributes: Vec<DiagnosticAttribute>,

    /// Number of structured attributes present on the internal diagnostic.
    pub attribute_count: usize,

    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub labels: Vec<ExportLabel>,

    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub notes: Vec<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub help: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub cause: Option<Cause>,

    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub suggestions: Vec<ExportSuggestion>,
}

impl ExportDiagnostic {
    /// Builds an external representation using the supplied policy.
    pub fn with_policy(diagnostic: &Diagnostic, policy: &ExportPolicy) -> Self {
        Self {
            report_id: diagnostic.report_id,

            session_id: diagnostic.session_id,

            timestamp: diagnostic.timestamp,

            application: policy
                .include_application
                .then(|| diagnostic.application.clone()),

            pid: policy.include_process_id.then_some(diagnostic.pid),

            hostname: policy.include_hostname.then(|| diagnostic.hostname.clone()),

            severity: diagnostic.severity,

            code: diagnostic.code.clone(),

            message: policy.text.apply(&diagnostic.message),

            attributes: export_attributes(&diagnostic.attributes, policy.attributes),

            attribute_count: diagnostic.attributes.len(),

            labels: diagnostic
                .labels
                .iter()
                .map(|label| ExportLabel::with_policy(label, policy))
                .collect(),

            notes: diagnostic
                .notes
                .iter()
                .map(|note| policy.text.apply(note))
                .collect(),

            help: diagnostic
                .help
                .as_deref()
                .map(|help| policy.text.apply(help)),

            cause: diagnostic
                .cause
                .as_ref()
                .map(|cause| export_cause(cause, policy.text)),

            suggestions: match policy.remediation {
                ExportRemediation::Omit => Vec::new(),

                ExportRemediation::MetadataOnly => diagnostic
                    .suggestions
                    .iter()
                    .map(|suggestion| ExportSuggestion::with_policy(suggestion, policy))
                    .collect(),
            },
        }
    }
}

impl From<&Diagnostic> for ExportDiagnostic {
    fn from(diagnostic: &Diagnostic) -> Self {
        Self::with_policy(diagnostic, &ExportPolicy::default())
    }
}

fn export_attributes(
    attributes: &[DiagnosticAttribute],
    policy: ExportAttributes,
) -> Vec<DiagnosticAttribute> {
    match policy {
        ExportAttributes::Omit => Vec::new(),

        ExportAttributes::Redact => attributes
            .iter()
            .map(|attribute| {
                DiagnosticAttribute::new(attribute.name.clone(), DiagnosticValue::from(REDACTED))
            })
            .collect(),

        ExportAttributes::RedactSensitive => attributes
            .iter()
            .map(|attribute| {
                if is_sensitive_key(&attribute.name) {
                    DiagnosticAttribute::new(
                        attribute.name.clone(),
                        DiagnosticValue::from(REDACTED),
                    )
                } else {
                    attribute.clone()
                }
            })
            .collect(),

        ExportAttributes::Full => attributes.to_vec(),
    }
}

fn export_cause(cause: &Cause, policy: ExportText) -> Cause {
    Cause {
        message: policy.apply(&cause.message),

        source: cause
            .source
            .as_deref()
            .map(|source| Box::new(export_cause(source, policy))),
    }
}

/// Privacy-conscious source label.
#[derive(Debug, Clone, Serialize)]
pub struct ExportLabel {
    #[serde(skip_serializing_if = "label_kind_is_primary")]
    pub kind: LabelKind,

    pub location: ExportSourceLocation,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub length: Option<usize>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
}

fn label_kind_is_primary(kind: &LabelKind) -> bool {
    *kind == LabelKind::Primary
}

impl ExportLabel {
    fn with_policy(label: &Label, policy: &ExportPolicy) -> Self {
        Self {
            kind: label.kind,

            location: ExportSourceLocation {
                file: policy.paths.apply(&label.location.file),

                line: label.location.line,

                column: label.location.column,

                revision: label.location.revision,
            },

            length: label.length,

            message: label
                .message
                .as_deref()
                .map(|message| policy.text.apply(message)),
        }
    }
}

impl From<&Label> for ExportLabel {
    fn from(label: &Label) -> Self {
        Self::with_policy(label, &ExportPolicy::default())
    }
}

/// Privacy-conscious source location.
#[derive(Debug, Clone, Serialize)]
pub struct ExportSourceLocation {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file: Option<String>,

    pub line: u32,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub column: Option<u32>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub revision: Option<SourceRevision>,
}

/// Metadata-only representation of a remediation suggestion.
///
/// Edit and command payloads are intentionally not represented by this type.
#[derive(Debug, Clone, Serialize)]
pub struct ExportSuggestion {
    pub title: String,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub explanation: Option<String>,

    pub applicability: Applicability,

    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub documentation: Vec<ExportDocumentationLink>,

    pub edit_count: usize,
    pub command_count: usize,
}

impl ExportSuggestion {
    fn with_policy(suggestion: &crate::Suggestion, policy: &ExportPolicy) -> Self {
        Self {
            title: policy.text.apply(&suggestion.title),

            explanation: suggestion
                .explanation
                .as_deref()
                .map(|explanation| policy.text.apply(explanation)),

            applicability: suggestion.applicability,

            documentation: suggestion
                .documentation
                .iter()
                .map(|link| ExportDocumentationLink {
                    label: policy.text.apply(&link.label),

                    url: policy.urls.apply(&link.url),

                    language_hint: link.language_hint.clone(),
                })
                .collect(),

            edit_count: suggestion.edits.len(),

            command_count: suggestion.commands.len(),
        }
    }
}

impl From<&crate::Suggestion> for ExportSuggestion {
    fn from(suggestion: &crate::Suggestion) -> Self {
        Self::with_policy(suggestion, &ExportPolicy::default())
    }
}

/// Sanitized documentation link.
#[derive(Debug, Clone, Serialize)]
pub struct ExportDocumentationLink {
    pub label: String,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub language_hint: Option<String>,
}