Skip to content

Commit 60fd1e1

Browse files
authored
Merge branch 'main' into feat/retire-aimdb-ws-protocol
2 parents 9d0930d + 136a98e commit 60fd1e1

10 files changed

Lines changed: 102 additions & 56 deletions

File tree

Cargo.lock

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aimdb-core/src/log.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
//! `Vec<String>`). The few sites that mirror events to defmt (router.rs)
1515
//! keep explicit `#[cfg(feature = "defmt")]` gates next to these macros.
1616
17+
#[macro_export]
1718
macro_rules! log_debug {
1819
($s:literal $(, $x:expr)* $(,)?) => {{
1920
#[cfg(feature = "tracing")]
@@ -23,6 +24,7 @@ macro_rules! log_debug {
2324
}};
2425
}
2526

27+
#[macro_export]
2628
macro_rules! log_info {
2729
($s:literal $(, $x:expr)* $(,)?) => {{
2830
#[cfg(feature = "tracing")]
@@ -32,6 +34,7 @@ macro_rules! log_info {
3234
}};
3335
}
3436

37+
#[macro_export]
3538
macro_rules! log_warn {
3639
($s:literal $(, $x:expr)* $(,)?) => {{
3740
#[cfg(feature = "tracing")]
@@ -41,6 +44,7 @@ macro_rules! log_warn {
4144
}};
4245
}
4346

47+
#[macro_export]
4448
macro_rules! log_error {
4549
($s:literal $(, $x:expr)* $(,)?) => {{
4650
#[cfg(feature = "tracing")]

aimdb-sync/Cargo.toml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ categories = ["database", "api-bindings"]
1313
# Core dependencies
1414
aimdb-core = { path = "../aimdb-core", version = "1.1.0" }
1515
aimdb-tokio-adapter = { path = "../aimdb-tokio-adapter", version = "0.6.0" }
16+
tracing = { workspace = true, optional = true }
1617

1718
# Tokio for channels and runtime
1819
tokio = { version = "1.40", features = ["sync", "rt", "time", "macros"] }
1920

2021
# Error handling
21-
thiserror = "1.0"
22+
thiserror = { version = "2.0.16", default-features = false }
2223

2324
# Optional: Settable::set (feature `data-contracts`) for the `set_value` family.
2425
# aimdb-sync depends on the contracts crate, never the reverse — the contracts
@@ -35,10 +36,12 @@ serde = { version = "1.0", features = ["derive"] }
3536
serde_json = "1.0"
3637

3738
[features]
38-
default = []
39+
default = ["std"]
40+
41+
std = []
3942

4043
# Enable tracing for debugging
41-
tracing = ["aimdb-core/tracing"]
44+
tracing = ["dep:tracing", "aimdb-core/tracing"]
4245

4346
# SyncProducer::set_value / try_set_value / set_value_at for Settable types
4447
data-contracts = ["dep:aimdb-data-contracts"]

aimdb-sync/src/consumer.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::time::Duration;
2424
/// # use serde::{Serialize, Deserialize};
2525
/// # #[derive(Debug, Clone, Serialize, Deserialize)]
2626
/// # struct Temperature { celsius: f32 }
27-
/// # fn example(consumer: &SyncConsumer<Temperature>) -> Result<(), Box<dyn std::error::Error>> {
27+
/// # fn example(consumer: &SyncConsumer<Temperature>) -> SyncResult<()> {
2828
/// // Get value (blocks until available)
2929
/// let temp = consumer.get()?;
3030
/// println!("Temperature: {}°C", temp.celsius);
@@ -81,13 +81,14 @@ where
8181
///
8282
/// ```no_run
8383
/// use aimdb_core::AimDbBuilder;
84-
/// use aimdb_sync::AimDbBuilderSyncExt;
84+
/// use aimdb_sync::{AimDbBuilderSyncExt, SyncResult};
8585
/// use aimdb_tokio_adapter::TokioAdapter;
8686
/// use std::sync::Arc;
8787
///
8888
/// # #[derive(Debug, Clone)]
8989
/// # struct MyData { value: i32 }
90-
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
90+
/// # #[cfg(feature = "std")]
91+
/// # fn main() -> SyncResult<()> {
9192
/// let handle = AimDbBuilder::new()
9293
/// .runtime(Arc::new(TokioAdapter))
9394
/// .attach()?;
@@ -119,14 +120,15 @@ where
119120
///
120121
/// ```no_run
121122
/// use aimdb_core::AimDbBuilder;
122-
/// use aimdb_sync::AimDbBuilderSyncExt;
123+
/// use aimdb_sync::{AimDbBuilderSyncExt, SyncResult};
123124
/// use aimdb_tokio_adapter::TokioAdapter;
124125
/// use std::sync::Arc;
125126
/// use std::time::Duration;
126127
///
127128
/// # #[derive(Debug, Clone)]
128129
/// # struct MyData { value: i32 }
129-
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
130+
/// # #[cfg(feature = "std")]
131+
/// # fn main() -> SyncResult<()> {
130132
/// let handle = AimDbBuilder::new()
131133
/// .runtime(Arc::new(TokioAdapter))
132134
/// .attach()?;
@@ -160,13 +162,14 @@ where
160162
///
161163
/// ```no_run
162164
/// use aimdb_core::AimDbBuilder;
163-
/// use aimdb_sync::AimDbBuilderSyncExt;
165+
/// use aimdb_sync::{AimDbBuilderSyncExt, SyncResult};
164166
/// use aimdb_tokio_adapter::TokioAdapter;
165167
/// use std::sync::Arc;
166168
///
167169
/// # #[derive(Debug, Clone)]
168170
/// # struct MyData { value: i32 }
169-
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
171+
/// # #[cfg(feature = "std")]
172+
/// # fn main() -> SyncResult<()> {
170173
/// let handle = AimDbBuilder::new()
171174
/// .runtime(Arc::new(TokioAdapter))
172175
/// .attach()?;
@@ -207,13 +210,14 @@ where
207210
///
208211
/// ```no_run
209212
/// use aimdb_core::AimDbBuilder;
210-
/// use aimdb_sync::AimDbBuilderSyncExt;
213+
/// use aimdb_sync::{AimDbBuilderSyncExt, SyncResult};
211214
/// use aimdb_tokio_adapter::TokioAdapter;
212215
/// use std::sync::Arc;
213216
///
214217
/// # #[derive(Debug, Clone)]
215218
/// # struct MyData { value: i32 }
216-
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
219+
/// # #[cfg(feature = "std")]
220+
/// # fn main() -> SyncResult<()> {
217221
/// let handle = AimDbBuilder::new()
218222
/// .runtime(Arc::new(TokioAdapter))
219223
/// .attach()?;
@@ -258,14 +262,15 @@ where
258262
///
259263
/// ```no_run
260264
/// use aimdb_core::AimDbBuilder;
261-
/// use aimdb_sync::AimDbBuilderSyncExt;
265+
/// use aimdb_sync::{AimDbBuilderSyncExt, SyncResult};
262266
/// use aimdb_tokio_adapter::TokioAdapter;
263267
/// use std::sync::Arc;
264268
/// use std::time::Duration;
265269
///
266270
/// # #[derive(Debug, Clone)]
267271
/// # struct MyData { value: i32 }
268-
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
272+
/// # #[cfg(feature = "std")]
273+
/// # fn main() -> SyncResult<()> {
269274
/// let handle = AimDbBuilder::new()
270275
/// .runtime(Arc::new(TokioAdapter))
271276
/// .attach()?;

aimdb-sync/src/error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! Errors of the blocking facade.
2+
use alloc::string::String;
23

34
use aimdb_core::DbError;
45

aimdb-sync/src/handle.rs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! AimDB handle for managing the sync API runtime thread.
22
33
use crate::{SyncError, SyncResult};
4-
use aimdb_core::{AimDb, AimDbBuilder, DbError, DbResult};
4+
use aimdb_core::{log_error, log_warn, AimDb, AimDbBuilder, DbError, DbResult};
55
use std::fmt::Debug;
66
use std::sync::Arc;
77
use std::thread::{self, JoinHandle};
@@ -46,11 +46,12 @@ pub trait AimDbBuilderSyncExt {
4646
/// ```no_run
4747
/// use aimdb_core::AimDbBuilder;
4848
/// use aimdb_tokio_adapter::TokioAdapter;
49-
/// use aimdb_sync::AimDbBuilderSyncExt;
49+
/// use aimdb_sync::{AimDbBuilderSyncExt, SyncResult};
5050
/// use std::sync::Arc;
5151
///
5252
/// # #[derive(Debug, Clone)] struct MyData { value: f32 }
53-
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
53+
/// # #[cfg(feature = "std")]
54+
/// # fn main() -> SyncResult<()> {
5455
/// let mut builder = AimDbBuilder::new()
5556
/// .runtime(Arc::new(TokioAdapter::new()?));
5657
/// builder.configure::<MyData>("my.data", |reg| {
@@ -87,10 +88,10 @@ pub trait AimDbSyncExt {
8788
///
8889
/// ```no_run
8990
/// use aimdb_core::AimDb;
90-
/// use aimdb_sync::AimDbSyncExt;
91+
/// use aimdb_sync::{AimDbSyncExt, SyncResult};
9192
///
9293
/// // `db` comes out of an async `AimDbBuilder::build()` elsewhere
93-
/// # fn demo(db: AimDb) -> Result<(), Box<dyn std::error::Error>> {
94+
/// # fn demo(db: AimDb) -> SyncResult<()> {
9495
/// let handle = db.attach()?;
9596
/// # Ok(())
9697
/// # }
@@ -156,7 +157,7 @@ impl AimDbHandle {
156157
let runtime = match tokio::runtime::Runtime::new() {
157158
Ok(rt) => rt,
158159
Err(e) => {
159-
eprintln!("Failed to create Tokio runtime: {}", e);
160+
log_error!("Failed to create Tokio runtime: {}", e);
160161
return;
161162
}
162163
};
@@ -166,7 +167,7 @@ impl AimDbHandle {
166167

167168
// Send the runtime handle to the main thread
168169
if handle_tx.blocking_send(rt_handle).is_err() {
169-
eprintln!("Failed to send runtime handle to main thread");
170+
log_error!("Failed to send runtime handle to main thread");
170171
return;
171172
}
172173

@@ -175,14 +176,14 @@ impl AimDbHandle {
175176
let (db, runner) = match builder.build().await {
176177
Ok(d) => (Arc::new(d.0), d.1),
177178
Err(e) => {
178-
eprintln!("Failed to build database: {}", e);
179+
log_error!("Failed to build database: {}", e);
179180
return;
180181
}
181182
};
182183

183184
// Send the database to the main thread
184185
if db_tx.send(db.clone()).await.is_err() {
185-
eprintln!("Failed to send database to main thread");
186+
log_error!("Failed to send database to main thread");
186187
return;
187188
}
188189

@@ -242,7 +243,7 @@ impl AimDbHandle {
242243
let runtime = match tokio::runtime::Runtime::new() {
243244
Ok(rt) => rt,
244245
Err(e) => {
245-
eprintln!("Failed to create Tokio runtime: {}", e);
246+
log_error!("Failed to create Tokio runtime: {}", e);
246247
return;
247248
}
248249
};
@@ -302,7 +303,7 @@ impl AimDbHandle {
302303
/// # use serde::{Serialize, Deserialize};
303304
/// # #[derive(Debug, Clone, Serialize, Deserialize)]
304305
/// # struct Temperature { celsius: f32 }
305-
/// # fn example(handle: &AimDbHandle) -> Result<(), Box<dyn std::error::Error>> {
306+
/// # fn example(handle: &AimDbHandle) -> SyncResult<()> {
306307
/// let producer = handle.producer::<Temperature>("sensor::temp")?;
307308
/// producer.set(Temperature { celsius: 25.0 })?;
308309
/// # Ok(())
@@ -337,7 +338,7 @@ impl AimDbHandle {
337338
/// # use serde::{Serialize, Deserialize};
338339
/// # #[derive(Clone, Debug, Serialize, Deserialize)]
339340
/// # struct Temperature { celsius: f32 }
340-
/// # fn example(handle: &AimDbHandle) -> Result<(), Box<dyn std::error::Error>> {
341+
/// # fn example(handle: &AimDbHandle) -> SyncResult<()> {
341342
/// let consumer = handle.consumer::<Temperature>("sensor::temp")?;
342343
/// let temp = consumer.get()?;
343344
/// # Ok(())
@@ -376,7 +377,7 @@ impl AimDbHandle {
376377
/// # use serde::{Serialize, Deserialize};
377378
/// # #[derive(Debug, Clone, Serialize, Deserialize)]
378379
/// # struct HighFrequencySensor { value: f32 }
379-
/// # fn example(handle: &AimDbHandle) -> Result<(), Box<dyn std::error::Error>> {
380+
/// # fn example(handle: &AimDbHandle) -> SyncResult<()> {
380381
/// // High-frequency sensor needs larger buffer
381382
/// let producer = handle.producer_with_capacity::<HighFrequencySensor>("sensor::high_freq", 1000)?;
382383
/// producer.set(HighFrequencySensor { value: 42.0 })?;
@@ -438,7 +439,7 @@ impl AimDbHandle {
438439
/// # use serde::{Serialize, Deserialize};
439440
/// # #[derive(Clone, Debug, Serialize, Deserialize)]
440441
/// # struct RareEvent { id: u32 }
441-
/// # fn example(handle: &AimDbHandle) -> Result<(), Box<dyn std::error::Error>> {
442+
/// # fn example(handle: &AimDbHandle) -> SyncResult<()> {
442443
/// // Rare events need smaller buffer
443444
/// let consumer = handle.consumer_with_capacity::<RareEvent>("events::rare", 10)?;
444445
/// let event = consumer.get()?;
@@ -482,7 +483,7 @@ impl AimDbHandle {
482483
Err(DbError::BufferLagged { lag_count, .. }) => {
483484
// Consumer fell behind - this is not fatal
484485
// Log warning but continue receiving
485-
eprintln!(
486+
log_warn!(
486487
"Warning: Consumer for {} lagged by {} messages",
487488
std::any::type_name::<T>(),
488489
lag_count
@@ -495,7 +496,7 @@ impl AimDbHandle {
495496
}
496497
Err(e) => {
497498
// Other unexpected errors - log and stop
498-
eprintln!(
499+
log_error!(
499500
"Error reading from buffer for {}: {}",
500501
std::any::type_name::<T>(),
501502
e
@@ -506,7 +507,7 @@ impl AimDbHandle {
506507
}
507508
}
508509
Err(e) => {
509-
eprintln!(
510+
log_error!(
510511
"Failed to subscribe to record type {}: {}",
511512
std::any::type_name::<T>(),
512513
e
@@ -541,7 +542,7 @@ impl AimDbHandle {
541542
///
542543
/// ```rust,no_run
543544
/// # use aimdb_sync::*;
544-
/// # fn example(handle: AimDbHandle) -> Result<(), Box<dyn std::error::Error>> {
545+
/// # fn example(handle: AimDbHandle) -> SyncResult<()> {
545546
/// handle.detach()?;
546547
/// # Ok(())
547548
/// # }
@@ -568,7 +569,7 @@ impl AimDbHandle {
568569
/// ```rust,no_run
569570
/// # use aimdb_sync::*;
570571
/// # use std::time::Duration;
571-
/// # fn example(handle: AimDbHandle) -> Result<(), Box<dyn std::error::Error>> {
572+
/// # fn example(handle: AimDbHandle) -> SyncResult<()> {
572573
/// handle.detach_timeout(Duration::from_secs(5))?;
573574
/// # Ok(())
574575
/// # }
@@ -641,12 +642,12 @@ impl Drop for AimDbHandle {
641642
/// If shutdown fails, the runtime thread may be left running.
642643
fn drop(&mut self) {
643644
if self.thread_handle.is_some() {
644-
eprintln!("Warning: AimDbHandle dropped without calling detach()");
645-
eprintln!("Attempting emergency shutdown with 5 second timeout");
645+
log_warn!("Warning: AimDbHandle dropped without calling detach()");
646+
log_warn!("Attempting emergency shutdown with 5 second timeout");
646647

647648
let timeout = Duration::from_secs(5);
648649
if let Err(e) = self.detach_internal(Some(timeout)) {
649-
eprintln!("Error during emergency shutdown: {}", e);
650+
log_error!("Error during emergency shutdown: {}", e);
650651
}
651652
}
652653
}

0 commit comments

Comments
 (0)