greentic_types/
session.rs

1//! Session identity and cursor helpers.
2
3use alloc::borrow::ToOwned;
4use alloc::string::String;
5
6#[cfg(feature = "schemars")]
7use schemars::JsonSchema;
8#[cfg(feature = "serde")]
9use serde::{Deserialize, Serialize};
10
11use crate::{FlowId, TenantCtx};
12
13/// Unique key referencing a persisted session.
14#[derive(Clone, Debug, PartialEq, Eq, Hash)]
15#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
16#[cfg_attr(feature = "schemars", derive(JsonSchema))]
17#[cfg_attr(feature = "serde", serde(transparent))]
18pub struct SessionKey(pub String);
19
20impl SessionKey {
21    /// Returns the session key as a string slice.
22    pub fn as_str(&self) -> &str {
23        &self.0
24    }
25
26    /// Creates a new session key from the supplied string.
27    pub fn new(value: impl Into<String>) -> Self {
28        Self(value.into())
29    }
30
31    /// Generates a random session key using [`uuid`], when enabled.
32    #[cfg(feature = "uuid")]
33    pub fn generate() -> Self {
34        Self(uuid::Uuid::new_v4().to_string())
35    }
36}
37
38impl From<String> for SessionKey {
39    fn from(value: String) -> Self {
40        Self(value)
41    }
42}
43
44impl From<&str> for SessionKey {
45    fn from(value: &str) -> Self {
46        Self(value.to_owned())
47    }
48}
49
50impl core::fmt::Display for SessionKey {
51    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
52        f.write_str(self.as_str())
53    }
54}
55
56#[cfg(feature = "uuid")]
57impl From<uuid::Uuid> for SessionKey {
58    fn from(value: uuid::Uuid) -> Self {
59        Self(value.to_string())
60    }
61}
62
63/// Cursor pointing at a session's position in a flow graph.
64#[derive(Clone, Debug, PartialEq, Eq, Hash)]
65#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
66#[cfg_attr(feature = "schemars", derive(JsonSchema))]
67pub struct SessionCursor {
68    /// Identifier of the node currently owning the session.
69    pub node_pointer: String,
70    /// Optional wait reason emitted by the node.
71    #[cfg_attr(
72        feature = "serde",
73        serde(default, skip_serializing_if = "Option::is_none")
74    )]
75    pub wait_reason: Option<String>,
76    /// Optional marker describing pending outbox operations.
77    #[cfg_attr(
78        feature = "serde",
79        serde(default, skip_serializing_if = "Option::is_none")
80    )]
81    pub outbox_marker: Option<String>,
82}
83
84impl SessionCursor {
85    /// Creates a new cursor pointing at the provided node identifier.
86    pub fn new(node_pointer: impl Into<String>) -> Self {
87        Self {
88            node_pointer: node_pointer.into(),
89            wait_reason: None,
90            outbox_marker: None,
91        }
92    }
93
94    /// Assigns a wait reason to the cursor.
95    pub fn with_wait_reason(mut self, reason: impl Into<String>) -> Self {
96        self.wait_reason = Some(reason.into());
97        self
98    }
99
100    /// Assigns an outbox marker to the cursor.
101    pub fn with_outbox_marker(mut self, marker: impl Into<String>) -> Self {
102        self.outbox_marker = Some(marker.into());
103        self
104    }
105}
106
107/// Persisted session payload describing how to resume a flow.
108#[derive(Clone, Debug, PartialEq, Eq)]
109#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
110#[cfg_attr(feature = "schemars", derive(JsonSchema))]
111pub struct SessionData {
112    /// Tenant context associated with the session.
113    pub tenant_ctx: TenantCtx,
114    /// Flow identifier being executed.
115    pub flow_id: FlowId,
116    /// Cursor pinpointing where execution paused.
117    pub cursor: SessionCursor,
118    /// Serialized execution context/state snapshot.
119    pub context_json: String,
120}