aegis-runtime-mv
Native Rust agent runtime: Axum WebSocket/SSE/REST gateway, heartbeat scheduler, dependency-aware task workflow, SQLite storage. Attachment of AienOS. Pre-alpha.
Brought by drakestapleton
aegis-runtime-mv / src / events / id.rs
| 1 | use serde::{Deserialize, Serialize}; |
| 2 | use std::fmt; |
| 3 | use uuid::Uuid; |
| 4 | |
| 5 | #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] |
| 6 | pub struct EventId(String); |
| 7 | |
| 8 | impl EventId { |
| 9 | pub fn new() -> Self { |
| 10 | Self(format!("evt_{}", Uuid::new_v4().simple())) |
| 11 | } |
| 12 | |
| 13 | pub fn from_string(s: impl Into<String>) -> Self { |
| 14 | Self(s.into()) |
| 15 | } |
| 16 | |
| 17 | pub fn as_str(&self) -> &str { |
| 18 | &self.0 |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | impl Default for EventId { |
| 23 | fn default() -> Self { |
| 24 | Self::new() |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | impl fmt::Display for EventId { |
| 29 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 30 | write!(f, "{}", self.0) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | impl From<EventId> for aien_protocol_types::EventId { |
| 35 | fn from(e: EventId) -> Self { |
| 36 | let raw = e.as_str().strip_prefix("evt_").unwrap_or(e.as_str()); |
| 37 | if let Ok(u) = uuid::Uuid::parse_str(raw) { |
| 38 | aien_protocol_types::EventId(u) |
| 39 | } else { |
| 40 | aien_protocol_types::EventId(uuid::Uuid::new_v5( |
| 41 | &uuid::Uuid::NAMESPACE_OID, |
| 42 | e.as_str().as_bytes(), |
| 43 | )) |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | impl From<aien_protocol_types::EventId> for EventId { |
| 49 | fn from(id: aien_protocol_types::EventId) -> Self { |
| 50 | Self::from_string(format!("evt_{}", id.0.simple())) |
| 51 | } |
| 52 | } |