Skip to main content

switchyard_translation/
diagnostic.rs

1// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Diagnostics emitted when translation preserves, drops, or degrades data.
5
6use serde::{Deserialize, Serialize};
7
8use crate::format::FormatId;
9
10/// Severity level for translation diagnostics.
11#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
12pub enum DiagnosticSeverity {
13    Info,
14    Warning,
15    Error,
16}
17
18/// Structured diagnostic attached to translation output.
19#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
20pub struct TranslationDiagnostic {
21    pub severity: DiagnosticSeverity,
22    pub code: String,
23    pub message: String,
24    pub source: Option<FormatId>,
25    pub target: Option<FormatId>,
26    pub path: Option<String>,
27}
28
29impl TranslationDiagnostic {
30    /// Creates a warning diagnostic.
31    pub fn warning(code: impl Into<String>, message: impl Into<String>) -> Self {
32        Self {
33            severity: DiagnosticSeverity::Warning,
34            code: code.into(),
35            message: message.into(),
36            source: None,
37            target: None,
38            path: None,
39        }
40    }
41
42    /// Adds a JSON-path-like location to a diagnostic.
43    pub fn at_path(mut self, path: impl Into<String>) -> Self {
44        self.path = Some(path.into());
45        self
46    }
47
48    /// Adds source and target format metadata to a diagnostic.
49    pub fn with_formats(
50        mut self,
51        source: impl Into<FormatId>,
52        target: impl Into<FormatId>,
53    ) -> Self {
54        self.source = Some(source.into());
55        self.target = Some(target.into());
56        self
57    }
58}