Skip to main content

tpt_protocol_tup/
lib.rs

1//! TUP (Telemetry Unified Packet) schema: CBOR-based binary encoding with a JSON mirror format.
2#![no_std]
3#![cfg_attr(docsrs, feature(doc_cfg))]
4
5extern crate alloc;
6
7use alloc::string::String;
8use alloc::vec::Vec;
9
10pub use quality::Quality;
11pub use source_protocol::SourceProtocol;
12pub use value::TupValue;
13
14mod quality;
15mod source_protocol;
16mod value;
17
18#[cfg(test)]
19mod cddl_conformance;
20
21const SCHEMA_VERSION: u64 = 1;
22
23#[derive(Clone, Debug, PartialEq)]
24pub struct DataPoint {
25    pub source_protocol: SourceProtocol,
26    pub device_id: String,
27    pub point_id: String,
28    pub timestamp: u64,
29    pub value: TupValue,
30    pub unit: Option<String>,
31    pub quality: Quality,
32    pub raw: Option<Vec<u8>>,
33}
34
35impl DataPoint {
36    pub fn new(
37        source_protocol: SourceProtocol,
38        device_id: impl Into<String>,
39        point_id: impl Into<String>,
40        timestamp: u64,
41        value: TupValue,
42        quality: Quality,
43    ) -> Self {
44        Self {
45            source_protocol,
46            device_id: device_id.into(),
47            point_id: point_id.into(),
48            timestamp,
49            value,
50            unit: None,
51            quality,
52            raw: None,
53        }
54    }
55
56    pub fn with_unit(mut self, unit: impl Into<String>) -> Self {
57        self.unit = Some(unit.into());
58        self
59    }
60
61    pub fn with_raw(mut self, raw: Vec<u8>) -> Self {
62        self.raw = Some(raw);
63        self
64    }
65
66    pub fn with_timestamp(mut self, timestamp: u64) -> Self {
67        self.timestamp = timestamp;
68        self
69    }
70
71    pub fn with_quality(mut self, quality: Quality) -> Self {
72        self.quality = quality;
73        self
74    }
75
76    pub const fn schema_version(&self) -> u64 {
77        SCHEMA_VERSION
78    }
79}
80
81#[cfg(feature = "std")]
82#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
83pub mod json;
84
85#[cfg(feature = "alloc")]
86#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
87pub mod cbor;
88
89#[cfg(test)]
90mod schema_tests {
91    /// The worked example from SPEC-TUP.md §4, kept in sync with
92    /// `schema/tup-v1.schema.json`.
93    const WORKED_EXAMPLE: &str = r#"{
94      "schema_version": 1,
95      "source_protocol": "modbus",
96      "device_id": "modbus-tcp://10.0.4.12:502/unit-1",
97      "point_id": "holding-register:40001",
98      "timestamp": 1753900000000,
99      "value": 68.5,
100      "unit": "degF",
101      "quality": "good"
102    }"#;
103
104    #[test]
105    fn worked_example_matches_json_schema() {
106        let schema: serde_json::Value =
107            serde_json::from_str(include_str!("../../../schema/tup-v1.schema.json"))
108                .expect("schema file is valid JSON");
109        let instance: serde_json::Value =
110            serde_json::from_str(WORKED_EXAMPLE).expect("worked example is valid JSON");
111
112        let validator = jsonschema::validator_for(&schema).expect("schema compiles");
113        let result = validator.validate(&instance);
114        assert!(
115            result.is_ok(),
116            "worked example failed schema validation: {:?}",
117            result.as_ref().err()
118        );
119    }
120}