cageforge_backend_api/diagnostic.rs
1// SPDX-License-Identifier: Apache-2.0
2
3//! Shared metadata for native backend diagnostics.
4
5/// Stable presentation metadata for one native backend failure.
6///
7/// The metadata is deliberately smaller than the backend's error enum. The
8/// native backend remains the owner of the typed error and its platform
9/// details; this value gives adapters a stable code and, when applicable, the
10/// portable configuration field that caused the failure.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub struct BackendDiagnosticMetadata {
13 code: &'static str,
14 field: Option<&'static str>,
15}
16
17impl BackendDiagnosticMetadata {
18 /// Creates metadata for a backend failure.
19 pub const fn new(code: &'static str, field: Option<&'static str>) -> Self {
20 Self { code, field }
21 }
22
23 /// Returns the stable diagnostic code.
24 pub const fn code(self) -> &'static str {
25 self.code
26 }
27
28 /// Returns the portable configuration field associated with the failure.
29 pub const fn field(self) -> Option<&'static str> {
30 self.field
31 }
32}
33
34/// Supplies stable presentation metadata for a concrete backend error.
35///
36/// Implement this for the backend's public top-level error and for nested
37/// error types when callers may receive them directly. The implementation is
38/// responsible for preserving the backend-specific typed error; this trait is
39/// only an adapter-facing classification layer.
40pub trait BackendDiagnostic {
41 /// Classifies this error without replacing or flattening the typed error.
42 fn diagnostic_metadata(&self) -> BackendDiagnosticMetadata;
43}