From 1c1d3c5755369e6e7c011e0d4812b8669efbd8c9 Mon Sep 17 00:00:00 2001 From: cool-mist Date: Mon, 20 Jul 2026 19:03:33 +0530 Subject: [PATCH] WIP --- .gitignore | 10 +++++ Cargo.lock | 7 ++++ Cargo.toml | 5 +++ README.md | 3 ++ crates/hd-bus/Cargo.toml | 6 +++ crates/hd-bus/src/lib.rs | 80 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 111 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 crates/hd-bus/Cargo.toml create mode 100644 crates/hd-bus/src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f88b862 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +/target/ +**/target/ +**/*.rs.bk +*.pdb +.idea/ +.vscode/ +*.iml +.DS_Store +Thumbs.db +.env diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..3837a10 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "hd-bus" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..7bef8be --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,5 @@ +[workspace] +members = [ + "crates/*" +] +resolver = "2" diff --git a/README.md b/README.md new file mode 100644 index 0000000..5259512 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Heimdall + +A Pub-Sub system for home automation diff --git a/crates/hd-bus/Cargo.toml b/crates/hd-bus/Cargo.toml new file mode 100644 index 0000000..5596574 --- /dev/null +++ b/crates/hd-bus/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "hd-bus" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/crates/hd-bus/src/lib.rs b/crates/hd-bus/src/lib.rs new file mode 100644 index 0000000..6ee221a --- /dev/null +++ b/crates/hd-bus/src/lib.rs @@ -0,0 +1,80 @@ +use std::{ + collections::VecDeque, sync::{RwLock, mpsc::{Receiver, Sender}}, +}; + +pub enum HError { + BusLockPoisoned(String), +} + +pub struct Event { + id: u64, + topic_name: String, + data_utf8: Vec, +} + +pub struct EventBus { + queue: RwLock>, + subscriptions: RwLock>, + processor: Sender, +} + +pub trait SubscriptionEndpoint { + fn deliver(&self, evt: &Event); +} + +struct ConsoleEndpoint; +impl SubscriptionEndpoint for ConsoleEndpoint { + fn deliver(&self, evt: &Event) { + let data = String::from_utf8_lossy(&evt.data_utf8); + println!("({}, {}, {})", evt.id, evt.topic_name, data); + } +} + +pub struct Subscription { + topic_pattern: String, + /// from_id is inclusive + from_id: u64, + endpoint: Box, +} + +impl EventBus { + pub fn new() -> Self { + Self { + queue: RwLock::new(VecDeque::new()), + subscriptions: RwLock::new(Vec::new()), + } + } + + pub fn publish(&self, evt: Event) -> Result<(), HError> { + let mut queue = self + .queue + .write() + .map_err(|e| HError::BusLockPoisoned(e.to_string()))?; + queue.push_back(evt); + Ok(()) + } + + pub fn subscribe(&self, request: Subscription) -> Result<(), HError> { + let mut subscriptions = self + .subscriptions + .write() + .map_err(|e| HError::BusLockPoisoned(e.to_string()))?; + subscriptions.push(request); + Ok(()) + } +} + +struct EventProcessRequest { + id: u64, +} + +struct EventBusProcessor { + last_processed_id: u64, + work: Receiver, +} + +#[cfg(test)] +mod tests { + #[test] + fn it_works() {} +}