Skip to main content

astarte_device_sdk/
lib.rs

1// This file is part of Astarte.
2//
3// Copyright 2021-2026 SECO Mind Srl
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9//    http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17// SPDX-License-Identifier: Apache-2.0
18
19#![doc = include_str!("../README.md")]
20#![doc(
21    html_logo_url = "https://raw.githubusercontent.com/astarte-platform/astarte-device-sdk-rust/refs/heads/master/assets/logos/clea-24.svg"
22)]
23#![doc(
24    html_favicon_url = "https://raw.githubusercontent.com/astarte-platform/astarte-device-sdk-rust/refs/heads/master/assets/logos/clea-24.ico"
25)]
26#![warn(
27    clippy::dbg_macro,
28    clippy::todo,
29    missing_docs,
30    rustdoc::missing_crate_level_docs
31)]
32#![cfg_attr(astarte_device_sdk_docsrs, feature(doc_cfg))]
33#![cfg_attr(__coverage, feature(coverage_attribute))]
34
35pub mod _docs;
36
37pub mod aggregate;
38pub mod builder;
39pub mod client;
40pub mod connection;
41pub mod error;
42pub mod event;
43mod interfaces;
44pub mod introspection;
45pub(crate) mod logging;
46pub mod prelude;
47pub mod properties;
48pub mod retention;
49mod retry;
50pub mod session;
51pub(crate) mod state;
52pub mod store;
53pub mod transport;
54pub mod types;
55mod validate;
56
57/// Re-exported internal structs
58pub use crate::client::{Client, DeviceClient};
59pub use crate::connection::{DeviceConnection, EventLoop};
60pub use crate::error::Error;
61pub use crate::event::Value;
62pub use crate::event::{DeviceEvent, FromEvent};
63pub use crate::types::AstarteData;
64
65// Re-export rumqttc since we return its types in some methods
66pub use astarte_interfaces;
67pub use chrono;
68pub use rumqttc;
69
70/// Timestamp returned in the astarte payload
71pub(crate) type Timestamp = chrono::DateTime<chrono::Utc>;
72
73#[cfg(feature = "derive")]
74#[cfg_attr(astarte_device_sdk_docsrs, doc(cfg(feature = "derive")))]
75pub use astarte_device_sdk_derive::*;
76
77#[cfg(test)]
78mod test {
79    // Interfaces
80    pub(crate) const DEVICE_OBJECT: &str = include_str!(
81        "../examples/object_datastream/interfaces/org.astarte-platform.rust.examples.object-datastream.DeviceDatastream.json"
82    );
83    pub(crate) const DEVICE_PROPERTIES: &str = include_str!(
84        "../examples/individual_properties/interfaces/org.astarte-platform.rust.examples.individual-properties.DeviceProperties.json"
85    );
86    pub(crate) const DEVICE_PROPERTIES_NAME: &str =
87        "org.astarte-platform.rust.examples.individual-properties.DeviceProperties";
88
89    pub(crate) const SERVER_OBJECT: &str = include_str!(
90        "../examples/object_datastream/interfaces/org.astarte-platform.rust.examples.object-datastream.ServerDatastream.json"
91    );
92    pub(crate) const SERVER_OBJECT_NAME: &str =
93        "org.astarte-platform.rust.examples.object-datastream.ServerDatastream";
94    pub(crate) const SERVER_PROPERTIES: &str = include_str!(
95        "../examples/individual_properties/interfaces/org.astarte-platform.rust.examples.individual-properties.ServerProperties.json"
96    );
97    #[cfg(feature = "message-hub")]
98    pub(crate) const SERVER_PROPERTIES_NAME: &str =
99        "org.astarte-platform.rust.examples.individual-properties.ServerProperties";
100    pub(crate) const SERVER_INDIVIDUAL: &str = include_str!(
101        "../examples/individual_datastream/interfaces/org.astarte-platform.rust.examples.individual-datastream.ServerDatastream.json"
102    );
103    pub(crate) const SERVER_INDIVIDUAL_NAME: &str =
104        "org.astarte-platform.rust.examples.individual-datastream.ServerDatastream";
105
106    // E2E Interfaces
107    pub(crate) const E2E_DEVICE_DATASTREAM: &str = include_str!(
108        "../e2e-test/interfaces/org.astarte-platform.rust.e2etest.DeviceDatastream.json"
109    );
110    pub(crate) const E2E_DEVICE_DATASTREAM_NAME: &str =
111        "org.astarte-platform.rust.e2etest.DeviceDatastream";
112
113    pub(crate) const E2E_SERVER_DATASTREAM: &str = include_str!(
114        "../e2e-test/interfaces/additional/org.astarte-platform.rust.e2etest.ServerDatastream.json"
115    );
116    pub(crate) const E2E_SERVER_DATASTREAM_NAME: &str =
117        "org.astarte-platform.rust.e2etest.ServerDatastream";
118    pub(crate) const E2E_DEVICE_AGGREGATE: &str = include_str!(
119        "../e2e-test/interfaces/org.astarte-platform.rust.e2etest.DeviceAggregate.json"
120    );
121    pub(crate) const E2E_DEVICE_AGGREGATE_NAME: &str =
122        "org.astarte-platform.rust.e2etest.DeviceAggregate";
123    pub(crate) const E2E_DEVICE_PROPERTY: &str = include_str!(
124        "../e2e-test/interfaces/additional/org.astarte-platform.rust.e2etest.DeviceProperty.json"
125    );
126    pub(crate) const E2E_DEVICE_PROPERTY_NAME: &str =
127        "org.astarte-platform.rust.e2etest.DeviceProperty";
128    pub(crate) const E2E_SERVER_PROPERTY: &str = include_str!(
129        "../e2e-test/interfaces/additional/org.astarte-platform.rust.e2etest.ServerProperty.json"
130    );
131    pub(crate) const E2E_SERVER_PROPERTY_NAME: &str =
132        "org.astarte-platform.rust.e2etest.ServerProperty";
133
134    pub(crate) mod for_update {
135        pub(crate) const E2E_DEVICE_DATASTREAM_NAME: &str =
136            "org.astarte-platform.rust.e2etest.ForUpdateDeviceDatastream";
137        pub(crate) const E2E_DEVICE_DATASTREAM_0_1: &str = include_str!(
138            "../e2e-test/interfaces/org.astarte-platform.rust.e2etest.ForUpdateDeviceDatastream.json"
139        );
140        pub(crate) const E2E_DEVICE_DATASTREAM_1_0: &str = include_str!(
141            "../e2e-test/interfaces/update/org.astarte-platform.rust.e2etest.ForUpdateDeviceDatastream.json"
142        );
143    }
144
145    // Interfaces with retention
146    pub(crate) const VOLATILE_DEVICE_DATASTREAM: &str = include_str!(
147        "../examples/retention/interfaces/org.astarte-platform.rust.examples.individual-datastream.VolatileDeviceDatastream.json"
148    );
149    pub(crate) const VOLATILE_DEVICE_DATASTREAM_NAME: &str =
150        "org.astarte-platform.rust.examples.individual-datastream.VolatileDeviceDatastream";
151    pub(crate) const STORED_DEVICE_DATASTREAM: &str = include_str!(
152        "../examples/retention/interfaces/org.astarte-platform.rust.examples.individual-datastream.StoredDeviceDatastream.json"
153    );
154    pub(crate) const STORED_DEVICE_DATASTREAM_NAME: &str =
155        "org.astarte-platform.rust.examples.individual-datastream.StoredDeviceDatastream";
156    pub(crate) const VOLATILE_DEVICE_OBJECT: &str = include_str!(
157        "../examples/retention/interfaces/org.astarte-platform.rust.examples.individual-datastream.VolatileDeviceObject.json"
158    );
159    pub(crate) const VOLATILE_DEVICE_OBJECT_NAME: &str =
160        "org.astarte-platform.rust.examples.individual-datastream.VolatileDeviceObject";
161    pub(crate) const STORED_DEVICE_OBJECT: &str = include_str!(
162        "../examples/retention/interfaces/org.astarte-platform.rust.examples.individual-datastream.StoredDeviceObject.json"
163    );
164    pub(crate) const STORED_DEVICE_OBJECT_NAME: &str =
165        "org.astarte-platform.rust.examples.individual-datastream.StoredDeviceObject";
166
167    pub(crate) const DEVICE_PROPERTIES_NO_UNSET: &str = r#"{
168    "interface_name": "org.astarte-platform.rust.examples.individual-properties.DevicePropertyNoUnset",
169    "version_major": 0,
170    "version_minor": 1,
171    "type": "properties",
172    "ownership": "device",
173    "mappings": [{
174        "endpoint": "/%{sensor_id}/enable",
175        "type": "boolean",
176        "allow_unset": false
177    }]
178}"#;
179
180    pub(crate) const SERVER_PROPERTIES_NO_UNSET: &str = r#"{
181    "interface_name": "org.astarte-platform.rust.examples.individual-properties.ServerPropertyNoUnset",
182    "version_major": 0,
183    "version_minor": 1,
184    "type": "properties",
185    "ownership": "server",
186    "mappings": [{
187        "endpoint": "/%{sensor_id}/enable",
188        "type": "boolean",
189        "allow_unset": false
190    }]
191}"#;
192    pub(crate) const SERVER_PROPERTIES_NO_UNSET_NAME: &str =
193        "org.astarte-platform.rust.examples.individual-properties.ServerPropertyNoUnset";
194}