1use std::collections::HashMap;
7use std::sync::atomic::{AtomicU64, Ordering};
8use std::sync::{Arc, Mutex, OnceLock};
9
10pub static BIFROST_REGISTRY: OnceLock<Arc<Mutex<BifrostRegistry>>> = OnceLock::new();
12
13pub fn bifrost_registry() -> Arc<Mutex<BifrostRegistry>> {
15 BIFROST_REGISTRY
16 .get_or_init(|| Arc::new(Mutex::new(BifrostRegistry::new())))
17 .clone()
18}
19
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
22#[allow(dead_code)]
23pub struct NodeId(u64);
24
25static NEXT_NODE_ID: AtomicU64 = AtomicU64::new(1);
26
27impl NodeId {
28 pub fn generate() -> Self {
30 Self(NEXT_NODE_ID.fetch_add(1, Ordering::SeqCst))
31 }
32}
33
34#[allow(dead_code)]
36pub struct BifrostRegistry {
37 bridges: HashMap<String, NodeId>,
39 geometry_cache: HashMap<NodeId, crate::Rect>,
41}
42
43impl Default for BifrostRegistry {
44 fn default() -> Self {
45 Self::new()
46 }
47}
48
49impl BifrostRegistry {
50 pub fn new() -> Self {
51 Self {
52 bridges: HashMap::new(),
53 geometry_cache: HashMap::new(),
54 }
55 }
56
57 pub fn get_or_create_bridge(&mut self, bridge_id: &str) -> NodeId {
59 *self
60 .bridges
61 .entry(bridge_id.to_string())
62 .or_insert_with(NodeId::generate)
63 }
64
65 pub fn update_geometry(&mut self, node_id: NodeId, rect: crate::Rect) {
67 self.geometry_cache.insert(node_id, rect);
68 }
69
70 pub fn get_geometry(&self, node_id: NodeId) -> Option<crate::Rect> {
72 self.geometry_cache.get(&node_id).copied()
73 }
74}