ankurah_proto/
id.rs

1use base64::{engine::general_purpose, Engine as _};
2use serde::{Deserialize, Serialize};
3use std::fmt;
4use ulid::Ulid;
5
6#[cfg(feature = "wasm")]
7use wasm_bindgen::prelude::*;
8
9use crate::error::DecodeError;
10// TODO - split out the different id types. Presently there's a lot of not-entities that are using this type for their ID
11#[derive(PartialEq, Eq, Hash, Clone, Copy, Ord, PartialOrd)]
12#[cfg_attr(feature = "wasm", wasm_bindgen)]
13pub struct EntityId(pub(crate) Ulid);
14
15impl EntityId {
16    pub fn new() -> Self { EntityId(Ulid::new()) }
17
18    pub fn from_bytes(bytes: [u8; 16]) -> Self { EntityId(Ulid::from_bytes(bytes)) }
19
20    pub fn to_bytes(&self) -> [u8; 16] { self.0.to_bytes() }
21
22    pub fn from_base64<T: AsRef<[u8]>>(input: T) -> Result<Self, DecodeError> {
23        let decoded = general_purpose::URL_SAFE_NO_PAD.decode(input).map_err(DecodeError::InvalidBase64)?;
24        let bytes: [u8; 16] = decoded[..].try_into().map_err(|_| DecodeError::InvalidLength)?;
25
26        Ok(EntityId(Ulid::from_bytes(bytes)))
27    }
28
29    pub fn to_base64(&self) -> String { general_purpose::URL_SAFE_NO_PAD.encode(self.0.to_bytes()) }
30
31    pub fn to_base64_short(&self) -> String {
32        // take the last 6 characters of the base64 encoded string
33        let value = self.to_base64();
34        value[value.len() - 6..].to_string()
35    }
36
37    pub fn to_ulid(&self) -> Ulid { self.0 }
38    pub fn from_ulid(ulid: Ulid) -> Self { EntityId(ulid) }
39}
40
41#[cfg_attr(feature = "wasm", wasm_bindgen)]
42impl EntityId {
43    #[cfg_attr(feature = "wasm", wasm_bindgen(js_name = toString))]
44    pub fn to_string(&self) -> String { self.to_base64() }
45
46    #[cfg(feature = "wasm")]
47    #[wasm_bindgen(js_name = to_base64)]
48    pub fn to_base64_js(&self) -> String { general_purpose::URL_SAFE_NO_PAD.encode(self.0.to_bytes()) }
49
50    #[cfg(feature = "wasm")]
51    #[wasm_bindgen(js_name = from_base64)]
52    pub fn from_base64_js(s: &str) -> Result<Self, JsValue> { Self::from_base64(s).map_err(|e| JsValue::from_str(&e.to_string())) }
53}
54
55impl fmt::Display for EntityId {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
57        if f.alternate() {
58            write!(f, "{}", self.to_base64_short())
59        } else {
60            write!(f, "{}", self.to_base64())
61        }
62    }
63}
64impl std::fmt::Debug for EntityId {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.to_base64()) }
66}
67
68impl TryFrom<&str> for EntityId {
69    type Error = DecodeError;
70    fn try_from(id: &str) -> Result<Self, Self::Error> { Self::from_base64(id) }
71}
72
73impl TryFrom<String> for EntityId {
74    type Error = DecodeError;
75    fn try_from(id: String) -> Result<Self, Self::Error> { Self::try_from(id.as_str()) }
76}
77
78impl TryFrom<&String> for EntityId {
79    type Error = DecodeError;
80    fn try_from(id: &String) -> Result<Self, Self::Error> { Self::try_from(id.as_str()) }
81}
82
83impl TryInto<EntityId> for Vec<u8> {
84    type Error = DecodeError;
85    fn try_into(self) -> Result<EntityId, Self::Error> {
86        let bytes: [u8; 16] = self.try_into().map_err(|_| DecodeError::InvalidLength)?;
87        Ok(EntityId(Ulid::from_bytes(bytes)))
88    }
89}
90
91impl From<EntityId> for Ulid {
92    fn from(id: EntityId) -> Self { id.0 }
93}
94
95impl Default for EntityId {
96    fn default() -> Self { Self::new() }
97}
98
99impl Serialize for EntityId {
100    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
101    where S: serde::Serializer {
102        if serializer.is_human_readable() {
103            // Use base64 for human-readable formats like JSON
104            serializer.serialize_str(&self.to_base64())
105        } else {
106            // Use raw bytes as a fixed-size array for binary formats like bincode
107            self.to_bytes().serialize(serializer)
108        }
109    }
110}
111
112impl<'de> Deserialize<'de> for EntityId {
113    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
114    where D: serde::Deserializer<'de> {
115        if deserializer.is_human_readable() {
116            // Deserialize from base64 string for human-readable formats
117            let s = String::deserialize(deserializer)?;
118            EntityId::from_base64(s).map_err(serde::de::Error::custom)
119        } else {
120            // Deserialize from raw bytes as a fixed-size array for binary formats
121            let bytes = <[u8; 16]>::deserialize(deserializer)?;
122            Ok(EntityId::from_bytes(bytes))
123        }
124    }
125}
126
127#[cfg(test)]
128mod tests {
129    use super::*;
130
131    #[test]
132    fn test_entity_id_json_serialization() {
133        let id = EntityId::from_bytes([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
134        let json = serde_json::to_string(&id).unwrap();
135        assert_eq!(json, "\"AQIDBAUGBwgJCgsMDQ4PEA\"");
136        assert_eq!(id, serde_json::from_str(&json).unwrap());
137    }
138
139    #[test]
140    fn test_entity_id_bincode_serialization() {
141        let id = EntityId::from_bytes([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
142        let bytes = bincode::serialize(&id).unwrap();
143        assert_eq!(bytes, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
144        assert_eq!(id, bincode::deserialize(&bytes).unwrap());
145    }
146}
147
148// EntityId support for predicates
149
150impl From<EntityId> for ankql::ast::Expr {
151    fn from(id: EntityId) -> ankql::ast::Expr { ankql::ast::Expr::Literal(ankql::ast::Literal::EntityId(id.to_ulid())) }
152}
153
154impl From<&EntityId> for ankql::ast::Expr {
155    fn from(id: &EntityId) -> ankql::ast::Expr { ankql::ast::Expr::Literal(ankql::ast::Literal::EntityId(id.to_ulid())) }
156}