mod draft;
#[cfg(feature = "cems")]
pub mod meter;
mod report;
use std::{collections::BTreeSet, fmt, io};
use serde::{Deserialize, Deserializer, Serialize, de::DeserializeOwned};
use crate::{Severity, status_severity};
pub use crate::NodeId;
pub use draft::draft_contract;
pub use report::{Attribute, AttributeValue, Counts, Finding, Outcome, Report, assess, compare};
use {SignalObservation as Observed, SignalRequirement as Expected};
pub const MAX_DOCUMENT_BYTES: usize = 4 * 1024 * 1024;
pub const MAX_SIGNALS: usize = 1024;
#[derive(Clone, Debug)]
pub struct Error(String);
impl Error {
fn new(message: impl Into<String>) -> Self {
Self(message.into())
}
fn at(self, path: &str) -> Self {
Self(format!("{path}: {self}"))
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl std::error::Error for Error {}
impl From<crate::NodeIdError> for Error {
fn from(error: crate::NodeIdError) -> Self {
Self::new(error.to_string())
}
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct Document<T> {
schema_version: u32,
device: String,
application_uri: String,
principal: String,
signals: Vec<T>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SignalRequirement {
pub name: String,
pub node: NodeId,
pub data_type: NodeId,
#[serde(
default,
skip_serializing_if = "Option::is_none",
deserialize_with = "present"
)]
pub value_rank: Option<i32>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
deserialize_with = "present"
)]
pub engineering_units: Option<Unit>,
}
impl SignalRequirement {
pub fn new(name: impl Into<String>, node: NodeId, data_type: NodeId) -> Self {
Self {
name: name.into(),
node,
data_type,
value_rank: None,
engineering_units: None,
}
}
}
pub use crate::EngineeringUnit as Unit;
impl Unit {
fn validate(&self) -> Result<(), Error> {
if self.namespace_uri.chars().any(char::is_control) {
return Err(Error::new("namespaceUri contains control characters"));
}
Ok(())
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields, bound(deserialize = "T: Deserialize<'de>"))]
pub struct Evidence<T> {
pub status: u32,
#[serde(
default,
skip_serializing_if = "Option::is_none",
deserialize_with = "present"
)]
pub value: Option<T>,
}
fn present<'de, D, T>(deserializer: D) -> Result<Option<T>, D::Error>
where
D: Deserializer<'de>,
T: Deserialize<'de>,
{
T::deserialize(deserializer).map(Some)
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SignalObservation {
pub name: String,
pub node: NodeId,
#[serde(
default,
skip_serializing_if = "Option::is_none",
deserialize_with = "present"
)]
pub data_type: Option<Evidence<NodeId>>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
deserialize_with = "present"
)]
pub value_rank: Option<Evidence<i32>>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
deserialize_with = "present"
)]
pub engineering_units: Option<Evidence<Unit>>,
}
impl SignalObservation {
pub fn new(name: impl Into<String>, node: NodeId) -> Self {
Self {
name: name.into(),
node,
data_type: None,
value_rank: None,
engineering_units: None,
}
}
}
#[derive(Debug, Serialize)]
#[serde(transparent)]
pub struct Contract(Document<SignalRequirement>);
impl Contract {
pub fn new(
device: impl Into<String>,
application_uri: impl Into<String>,
principal: impl Into<String>,
signals: Vec<SignalRequirement>,
) -> Result<Self, Error> {
validate_contract(Document::new(device, application_uri, principal, signals)?)
}
pub fn device(&self) -> &str {
&self.0.device
}
pub fn application_uri(&self) -> &str {
&self.0.application_uri
}
pub fn principal(&self) -> &str {
&self.0.principal
}
pub fn signals(&self) -> &[SignalRequirement] {
&self.0.signals
}
pub fn signal_count(&self) -> usize {
self.0.signals.len()
}
}
#[derive(Debug, Serialize)]
#[serde(transparent)]
pub struct Snapshot(Document<SignalObservation>);
impl Snapshot {
pub fn new(
device: impl Into<String>,
application_uri: impl Into<String>,
principal: impl Into<String>,
signals: Vec<SignalObservation>,
) -> Result<Self, Error> {
validate_snapshot(Document::new(device, application_uri, principal, signals)?)
}
pub fn device(&self) -> &str {
&self.0.device
}
pub fn application_uri(&self) -> &str {
&self.0.application_uri
}
pub fn principal(&self) -> &str {
&self.0.principal
}
pub fn signals(&self) -> &[SignalObservation] {
&self.0.signals
}
}
#[cfg(feature = "client")]
pub(crate) struct SnapshotCollector {
document: Document<SignalObservation>,
budget: ByteBudget,
}
#[cfg(feature = "client")]
impl SnapshotCollector {
pub(crate) fn new(contract: &Contract) -> Result<Self, Error> {
let document = Document {
schema_version: 1,
device: contract.device().to_owned(),
application_uri: contract.application_uri().to_owned(),
principal: contract.principal().to_owned(),
signals: Vec::with_capacity(contract.signal_count()),
};
let mut budget = ByteBudget(MAX_DOCUMENT_BYTES - 1);
serde_json::to_writer(&mut budget, &document).map_err(|e| Error::new(e.to_string()))?;
Ok(Self { document, budget })
}
pub(crate) fn push(&mut self, row: SignalObservation) -> Result<(), Error> {
use io::Write;
if !self.document.signals.is_empty() {
self.budget
.write_all(b",")
.map_err(|e| Error::new(e.to_string()))?;
}
serde_json::to_writer(&mut self.budget, &row).map_err(|e| Error::new(e.to_string()))?;
self.document.signals.push(row);
Ok(())
}
pub(crate) fn finish(self, contract: &Contract) -> Result<Snapshot, Error> {
let snapshot = validate_snapshot(self.document)?;
contract.validate_capture(&snapshot)?;
Ok(snapshot)
}
}
struct ByteBudget(usize);
impl io::Write for ByteBudget {
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
self.0 = self
.0
.checked_sub(bytes.len())
.ok_or_else(|| io::Error::other("document exceeds 4 MiB"))?;
Ok(bytes.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
impl<T: Serialize> Document<T> {
fn new(
device: impl Into<String>,
application_uri: impl Into<String>,
principal: impl Into<String>,
signals: Vec<T>,
) -> Result<Self, Error> {
let doc = Self {
schema_version: 1,
device: device.into(),
application_uri: application_uri.into(),
principal: principal.into(),
signals,
};
validate_header(&doc)?;
serde_json::to_writer(ByteBudget(MAX_DOCUMENT_BYTES), &doc)
.map_err(|error| Error::new(error.to_string()))?;
Ok(doc)
}
}
fn parse<T: DeserializeOwned>(bytes: &[u8]) -> Result<Document<T>, Error> {
if bytes.len() > MAX_DOCUMENT_BYTES {
return Err(Error::new("document exceeds 4 MiB"));
}
let doc: Document<T> = serde_json::from_slice(bytes).map_err(|error| {
let message: String = error
.to_string()
.chars()
.take(180)
.flat_map(char::escape_debug)
.collect();
Error::new(format!(
"invalid JSON at line {}, column {}: {message}",
error.line(),
error.column()
))
})?;
validate_header(&doc)?;
Ok(doc)
}
fn validate_header<T>(doc: &Document<T>) -> Result<(), Error> {
if doc.schema_version != 1 {
return Err(Error::new("unsupported schemaVersion (expected 1)"));
}
for (path, value) in [
("device", &doc.device),
("applicationUri", &doc.application_uri),
("principal", &doc.principal),
] {
validate_label(value).map_err(|e| e.at(path))?;
}
if doc.signals.len() > MAX_SIGNALS {
return Err(Error::new("signals exceeds 1024 entries"));
}
Ok(())
}
fn validate_label(label: &str) -> Result<(), Error> {
if label.trim().is_empty() || label.chars().any(char::is_control) {
return Err(Error::new(
"expected nonempty text without control characters",
));
}
Ok(())
}
fn validate_rank(rank: i32) -> Result<(), Error> {
if rank < -3 {
return Err(Error::new("ValueRank must be -3 or greater"));
}
Ok(())
}
fn unique<'a>(items: impl Iterator<Item = (&'a str, &'a NodeId)>) -> Result<(), Error> {
let mut names = BTreeSet::new();
let mut nodes = BTreeSet::new();
for (i, (name, node)) in items.enumerate() {
if !names.insert(name) {
return Err(Error::new(format!(
"signals[{i}].name duplicates another signal name"
)));
}
if !nodes.insert(node) {
return Err(Error::new(format!(
"signals[{i}].node duplicates another node identity"
)));
}
}
Ok(())
}
pub fn parse_contract(bytes: &[u8]) -> Result<Contract, Error> {
validate_contract(parse(bytes)?)
}
fn validate_contract(doc: Document<SignalRequirement>) -> Result<Contract, Error> {
if doc.signals.is_empty() {
return Err(Error::new("contract must specify at least one signal"));
}
for (i, s) in doc.signals.iter().enumerate() {
validate_label(&s.name).map_err(|e| e.at(&format!("signals[{i}].name")))?;
if let Some(rank) = s.value_rank {
validate_rank(rank).map_err(|e| e.at(&format!("signals[{i}].valueRank")))?;
}
if let Some(unit) = &s.engineering_units {
let path = format!("signals[{i}].engineeringUnits");
unit.validate().map_err(|e| e.at(&path))?;
if !unit.is_identified() {
return Err(
Error::new("expected nonempty namespaceUri and unitId other than -1").at(&path),
);
}
}
}
unique(doc.signals.iter().map(|s| (s.name.as_str(), &s.node)))?;
Ok(Contract(doc))
}
fn validate_observation<T>(
evidence: &Evidence<T>,
validate: impl FnOnce(&T) -> Result<(), Error>,
) -> Result<(), Error> {
let good = status_severity(evidence.status) == Severity::Good;
let result = match (good, evidence.value.as_ref()) {
(true, Some(value)) => validate(value),
(_, None) => Ok(()),
(false, Some(_)) => Err(Error::new("non-Good status requires omitting the value")),
};
result.map_err(|e| Error::new(format!("{e} (status 0x{:08X})", evidence.status)))
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
struct WireObservation {
name: String,
node: NodeId,
#[serde(default, deserialize_with = "present")]
data_type: Option<Evidence<crate::identity::WireNode>>,
#[serde(default, deserialize_with = "present")]
value_rank: Option<Evidence<i32>>,
#[serde(default, deserialize_with = "present")]
engineering_units: Option<Evidence<Unit>>,
}
impl WireObservation {
fn into_observation(self, index: usize) -> Result<SignalObservation, Error> {
let data_type = self
.data_type
.map(|evidence| -> Result<_, Error> {
let value = evidence
.value
.map(NodeId::try_from)
.transpose()
.map_err(|err| {
Error::new(format!(
"signals[{index}].dataType: {err} (status 0x{:08X})",
evidence.status
))
})?;
Ok(Evidence {
status: evidence.status,
value,
})
})
.transpose()?;
Ok(SignalObservation {
name: self.name,
node: self.node,
data_type,
value_rank: self.value_rank,
engineering_units: self.engineering_units,
})
}
}
pub fn parse_snapshot(bytes: &[u8]) -> Result<Snapshot, Error> {
let doc: Document<WireObservation> = parse(bytes)?;
let signals = doc
.signals
.into_iter()
.enumerate()
.map(|(index, row)| row.into_observation(index))
.collect::<Result<_, _>>()?;
validate_snapshot(Document {
schema_version: doc.schema_version,
device: doc.device,
application_uri: doc.application_uri,
principal: doc.principal,
signals,
})
}
fn validate_snapshot(doc: Document<SignalObservation>) -> Result<Snapshot, Error> {
for (i, s) in doc.signals.iter().enumerate() {
validate_label(&s.name).map_err(|e| e.at(&format!("signals[{i}].name")))?;
if let Some(evidence) = &s.data_type {
validate_observation(evidence, |_| Ok(()))
.map_err(|e| e.at(&format!("signals[{i}].dataType")))?;
}
if let Some(evidence) = &s.value_rank {
validate_observation(evidence, |n| validate_rank(*n))
.map_err(|e| e.at(&format!("signals[{i}].valueRank")))?;
}
if let Some(evidence) = &s.engineering_units {
validate_observation(evidence, |unit| unit.validate())
.map_err(|e| e.at(&format!("signals[{i}].engineeringUnits")))?;
}
}
unique(doc.signals.iter().map(|s| (s.name.as_str(), &s.node)))?;
Ok(Snapshot(doc))
}