Skip to main content

cls_eedi/wire/
trust_report.rs

1//! Counts-only inspection of a validated local-device trust state.
2use super::{State, TrustEntry};
3use serde::Serialize;
4
5/// A snapshot of commissioning status suitable for operator diagnostics.
6///
7/// Counts refer to entries in this state message. A missing trust property
8/// remains absent information (JSON `null`), while an empty list yields zeros.
9#[derive(Debug, PartialEq, Eq, Serialize)]
10pub struct TrustReport {
11    pub timestamp: i64,
12    pub trust: Option<TrustSummary>,
13}
14
15/// Direct entries include SKIs resulting from certificate-based commissioning.
16#[derive(Debug, Default, PartialEq, Eq, Serialize)]
17#[serde(rename_all = "camelCase")]
18pub struct TrustSummary {
19    pub direct_ski: usize,
20    pub pairing_pending: usize,
21    pub pairing_established: usize,
22}
23
24impl TrustSummary {
25    /// Count entries from a validated state trust list.
26    pub fn from_entries(entries: &[TrustEntry]) -> Self {
27        let mut counts = Self::default();
28        for entry in entries {
29            if let Some(pairing) = &entry.ship_pairing_service {
30                if pairing.ski.is_some() {
31                    counts.pairing_established += 1;
32                } else {
33                    counts.pairing_pending += 1;
34                }
35            } else if entry.ski.is_some() {
36                counts.direct_ski += 1;
37            }
38        }
39        counts
40    }
41}
42
43impl TrustReport {
44    /// Project a state obtained from [`super::Message::decode`].
45    pub fn from_state(state: &State) -> Self {
46        Self {
47            timestamp: state.timestamp,
48            trust: state.trust.as_deref().map(TrustSummary::from_entries),
49        }
50    }
51}