Skip to main content

mads_core/auto_configuration/
report.rs

1use std::path::Path;
2
3use crate::SourceLocation;
4
5const REDACTED: &str = "<redacted>";
6
7fn safe_source_label(value: &str) -> &'static str {
8    match value {
9        "defaults" => "defaults",
10        "environment" => "environment",
11        "mads.toml" => "mads.toml",
12        "test" => "test",
13        source
14            if Path::new(source)
15                .file_name()
16                .is_some_and(|name| name == "mads.toml") =>
17        {
18            "mads.toml"
19        }
20        _ => REDACTED,
21    }
22}
23
24fn safe_explanation(reason_code: AutoConfigurationReasonCode, explanation: &str) -> &'static str {
25    if explanation == "Database is required and configured" {
26        return "Database is required and configured";
27    }
28    if explanation == "Database is required and configured with startup migrations disabled" {
29        return "Database is required and configured with startup migrations disabled";
30    }
31
32    match reason_code.as_str() {
33        "conditions_matched" => "Auto-configuration conditions matched",
34        "requirement_absent" => "No provider requires this auto-configuration",
35        "user_override" => "An application provider overrides this auto-configuration",
36        "missing_configuration" => "Required configuration is missing",
37        "invalid_configuration" => "Required configuration is invalid",
38        "missing_migration_source" => "A required migration source is missing",
39        "provisioning_failed" => "Auto-configuration provisioning failed",
40        "duplicate_identifier" => "An auto-configuration identifier is duplicated",
41        "conflicting_default" => "Auto-configuration defaults conflict",
42        _ => REDACTED,
43    }
44}
45
46/// The outcome of evaluating an auto-configuration descriptor.
47#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
48pub enum AutoConfigurationStatus {
49    /// The descriptor produced its configured provider.
50    Active,
51    /// The descriptor did not apply to the application.
52    Skipped,
53    /// An explicit provider took precedence over the descriptor.
54    Overridden,
55    /// The descriptor matched but could not produce its provider.
56    Failed,
57}
58
59/// A stable, machine-readable reason for an auto-configuration outcome.
60#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
61pub struct AutoConfigurationReasonCode(&'static str);
62
63impl AutoConfigurationReasonCode {
64    /// Creates a reason code from a stable string.
65    #[doc(hidden)]
66    pub const fn new(value: &'static str) -> Self {
67        Self(value)
68    }
69
70    /// Returns the stable string representation of this reason code.
71    pub const fn as_str(self) -> &'static str {
72        self.0
73    }
74}
75
76/// A provider requirement that contributed to an auto-configuration decision.
77#[derive(Clone, Debug, Eq, PartialEq)]
78pub struct AutoConfigurationRequirement {
79    provider_type_name: &'static str,
80    location: Option<SourceLocation>,
81}
82
83impl AutoConfigurationRequirement {
84    /// Creates redacted provider requirement evidence for an integration.
85    #[doc(hidden)]
86    pub const fn new(provider_type_name: &'static str, location: Option<SourceLocation>) -> Self {
87        Self {
88            provider_type_name,
89            location,
90        }
91    }
92
93    /// Returns the provider type name that established the requirement.
94    pub const fn provider_type_name(&self) -> &'static str {
95        self.provider_type_name
96    }
97
98    /// Returns the source location that established the requirement, when known.
99    pub const fn location(&self) -> Option<SourceLocation> {
100        self.location
101    }
102}
103
104/// Redacted evidence that a configuration key was present in a named source.
105///
106/// Configuration values are never stored in evidence. The optional source is
107/// a fixed, recognized human-readable source label. Unknown labels are
108/// redacted before storage.
109#[derive(Clone, Debug, Eq, PartialEq)]
110pub struct AutoConfigurationConfigEvidence {
111    key: &'static str,
112    source: Option<String>,
113}
114
115impl AutoConfigurationConfigEvidence {
116    /// Creates redacted configuration evidence from a configuration source label.
117    #[doc(hidden)]
118    pub fn new(key: &'static str, source: Option<&str>) -> Self {
119        Self {
120            key,
121            source: source.map(|source| safe_source_label(source).to_owned()),
122        }
123    }
124
125    /// Returns the configuration key whose presence contributed to the decision.
126    pub const fn key(&self) -> &'static str {
127        self.key
128    }
129
130    /// Returns the configuration source label, when one is available.
131    pub fn source(&self) -> Option<&str> {
132        self.source.as_deref()
133    }
134}
135
136/// A redacted report describing one auto-configuration decision.
137///
138/// Reports contain only stable identifiers, type and key names, source labels,
139/// and explanatory text. They never store resolved configuration values.
140#[derive(Clone, Debug, Eq, PartialEq)]
141pub struct AutoConfigurationReport {
142    identifier: &'static str,
143    output_type_name: &'static str,
144    status: AutoConfigurationStatus,
145    reason_code: AutoConfigurationReasonCode,
146    explanation: String,
147    requirements: Vec<AutoConfigurationRequirement>,
148    configuration: Vec<AutoConfigurationConfigEvidence>,
149}
150
151impl AutoConfigurationReport {
152    /// Creates an auto-configuration report and redacts sensitive explanation text.
153    #[doc(hidden)]
154    #[allow(clippy::too_many_arguments)]
155    pub fn new(
156        identifier: &'static str,
157        output_type_name: &'static str,
158        status: AutoConfigurationStatus,
159        reason_code: AutoConfigurationReasonCode,
160        explanation: impl AsRef<str>,
161        requirements: Vec<AutoConfigurationRequirement>,
162        configuration: Vec<AutoConfigurationConfigEvidence>,
163    ) -> Self {
164        Self {
165            identifier,
166            output_type_name,
167            status,
168            reason_code,
169            explanation: safe_explanation(reason_code, explanation.as_ref()).to_owned(),
170            requirements,
171            configuration,
172        }
173    }
174
175    /// Returns the stable descriptor identifier.
176    pub const fn identifier(&self) -> &'static str {
177        self.identifier
178    }
179
180    /// Returns the type name of the provider the descriptor can produce.
181    pub const fn output_type_name(&self) -> &'static str {
182        self.output_type_name
183    }
184
185    /// Returns the outcome of evaluating the descriptor.
186    pub const fn status(&self) -> AutoConfigurationStatus {
187        self.status
188    }
189
190    /// Returns the stable reason code for the outcome.
191    pub const fn reason_code(&self) -> AutoConfigurationReasonCode {
192        self.reason_code
193    }
194
195    /// Returns the human-readable explanation of the outcome.
196    pub fn explanation(&self) -> &str {
197        &self.explanation
198    }
199
200    /// Returns the provider requirements that contributed to the decision.
201    pub fn requirements(&self) -> &[AutoConfigurationRequirement] {
202        &self.requirements
203    }
204
205    /// Returns redacted configuration evidence that contributed to the decision.
206    pub fn configuration(&self) -> &[AutoConfigurationConfigEvidence] {
207        &self.configuration
208    }
209}