Skip to main content

subspace_core/diagnostics/
local.rs

1use super::{BuildIdentity, ErrorDetails, IssuePaths, RedactionContext, RuntimeDetails};
2
3/// Shell-owned snapshot. Collection happens once outside render and copy.
4/// Raw private inputs have no serialization or Debug implementation.
5#[derive(Clone)]
6pub struct LocalDiagnostics {
7    pub(super) identity: BuildIdentity,
8    pub(super) framework_version: String,
9    pub(super) os_version: Option<String>,
10    pub(super) runtime: Option<RuntimeDetails>,
11    pub(super) redaction: RedactionContext,
12}
13
14impl Default for LocalDiagnostics {
15    fn default() -> Self {
16        Self::new(
17            BuildIdentity::new("unavailable", "unavailable"),
18            "unavailable",
19        )
20    }
21}
22
23// qual:allow(dry, boilerplate) reason: "The shell injects optional local facts and a private non-serializable redaction context; the builders keep raw inputs out of the public record schema without a new dependency."
24impl LocalDiagnostics {
25    pub fn new(identity: BuildIdentity, framework_version: impl Into<String>) -> Self {
26        Self {
27            identity,
28            framework_version: framework_version.into(),
29            os_version: None,
30            runtime: None,
31            redaction: RedactionContext::default(),
32        }
33    }
34
35    pub fn with_os_version(mut self, version: impl Into<String>) -> Self {
36        self.os_version = Some(version.into());
37        self
38    }
39
40    pub fn with_runtime(mut self, runtime: RuntimeDetails) -> Self {
41        self.runtime = Some(runtime);
42        self
43    }
44
45    pub fn with_redaction(mut self, redaction: RedactionContext) -> Self {
46        self.redaction = redaction;
47        self
48    }
49
50    /// Project only the explicit per-report path allowlist, without error text
51    /// or the additional fields included by local copy.
52    pub fn issue_paths(&self, config_path: Option<&str>) -> IssuePaths {
53        IssuePaths::project(self.runtime.as_ref(), &self.redaction, config_path)
54    }
55
56    pub fn local_copy(&self, error: Option<&ErrorDetails>) -> LocalCopy {
57        let fields = [
58            ("Product", Some(self.identity.product_id())),
59            ("App version", Some(self.identity.app_version())),
60            ("Build channel", self.identity.channel()),
61            ("Commit", self.identity.commit()),
62            (
63                "Subspace Framework version",
64                Some(self.framework_version.as_str()),
65            ),
66            ("OS", self.identity.os()),
67            ("OS version", self.os_version.as_deref()),
68            ("Architecture", self.identity.architecture()),
69        ];
70        let mut fields: Vec<_> = fields
71            .into_iter()
72            .map(|(name, value)| {
73                (
74                    name,
75                    self.redaction.public_field(value.unwrap_or("unavailable")),
76                )
77            })
78            .collect();
79        fields.extend(super::markdown::runtime_fields(
80            self.runtime.as_ref(),
81            &self.redaction,
82        ));
83        let diagnostics = super::markdown::section("Diagnostics", &fields);
84        LocalCopy(super::markdown::render(self, error, diagnostics))
85    }
86}
87
88/// Sanitized local-copy projection ready for clipboard delivery. Free-text
89/// values are flattened to one line and capped at 4,096 Unicode characters
90/// after redaction, with a visible truncation marker.
91#[derive(Debug, Clone)]
92pub struct LocalCopy(String);
93
94impl LocalCopy {
95    pub fn markdown(&self) -> &str {
96        &self.0
97    }
98}