Skip to main content

fresh_core/
overlay.rs

1use serde::{Deserialize, Serialize};
2use std::sync::atomic::{AtomicU64, Ordering};
3
4/// Opaque handle for an overlay
5#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, ts_rs::TS)]
6#[ts(export)]
7pub struct OverlayHandle(pub String);
8
9impl OverlayHandle {
10    pub fn new() -> Self {
11        static NEXT_HANDLE: AtomicU64 = AtomicU64::new(1);
12        Self(format!(
13            "ovl_{}",
14            NEXT_HANDLE.fetch_add(1, Ordering::Relaxed)
15        ))
16    }
17
18    pub fn from_string(s: String) -> Self {
19        Self(s)
20    }
21
22    pub fn as_str(&self) -> &str {
23        &self.0
24    }
25}
26
27impl Default for OverlayHandle {
28    fn default() -> Self {
29        Self::new()
30    }
31}
32
33/// Namespace for grouping overlays
34#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, ts_rs::TS)]
35#[ts(export)]
36pub struct OverlayNamespace(pub String);
37
38impl OverlayNamespace {
39    pub fn new() -> Self {
40        static NEXT_NAMESPACE: AtomicU64 = AtomicU64::new(1);
41        Self(format!(
42            "ns_{}",
43            NEXT_NAMESPACE.fetch_add(1, Ordering::Relaxed)
44        ))
45    }
46
47    pub fn from_string(s: String) -> Self {
48        Self(s)
49    }
50
51    pub fn as_str(&self) -> &str {
52        &self.0
53    }
54}
55
56impl Default for OverlayNamespace {
57    fn default() -> Self {
58        Self::new()
59    }
60}