Skip to main content

poly_kv/
ids_compat.rs

1//! Feature-gated type aliases for stack-ids integration.
2//!
3//! When the `typed-ids` feature is active, `AgentId` is `stack_ids::EntityId`.
4//! When inactive, `AgentId` is a local newtype wrapping `String`.
5
6#[cfg(feature = "typed-ids")]
7pub use stack_ids::EntityId as AgentId;
8
9#[cfg(not(feature = "typed-ids"))]
10#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
11#[serde(transparent)]
12pub struct AgentId(pub String);
13
14#[cfg(not(feature = "typed-ids"))]
15impl AgentId {
16    /// Create from any string-like value.
17    pub fn new(value: impl Into<String>) -> Self {
18        Self(value.into())
19    }
20
21    /// Borrow as a string slice.
22    pub fn as_str(&self) -> &str {
23        &self.0
24    }
25
26    /// Returns true if the inner string is empty.
27    pub fn is_empty(&self) -> bool {
28        self.0.is_empty()
29    }
30}
31
32#[cfg(not(feature = "typed-ids"))]
33impl std::fmt::Display for AgentId {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        f.write_str(&self.0)
36    }
37}
38
39#[cfg(not(feature = "typed-ids"))]
40impl From<String> for AgentId {
41    fn from(value: String) -> Self {
42        Self(value)
43    }
44}
45
46#[cfg(not(feature = "typed-ids"))]
47impl From<&str> for AgentId {
48    fn from(value: &str) -> Self {
49        Self(value.to_string())
50    }
51}
52
53#[cfg(not(feature = "typed-ids"))]
54impl AsRef<str> for AgentId {
55    fn as_ref(&self) -> &str {
56        &self.0
57    }
58}