greentic_types/
session.rs1use 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#[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 pub fn as_str(&self) -> &str {
23 &self.0
24 }
25
26 pub fn new(value: impl Into<String>) -> Self {
28 Self(value.into())
29 }
30
31 #[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#[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 pub node_pointer: String,
70 #[cfg_attr(
72 feature = "serde",
73 serde(default, skip_serializing_if = "Option::is_none")
74 )]
75 pub wait_reason: Option<String>,
76 #[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 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 pub fn with_wait_reason(mut self, reason: impl Into<String>) -> Self {
96 self.wait_reason = Some(reason.into());
97 self
98 }
99
100 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#[derive(Clone, Debug, PartialEq, Eq)]
109#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
110#[cfg_attr(feature = "schemars", derive(JsonSchema))]
111pub struct SessionData {
112 pub tenant_ctx: TenantCtx,
114 pub flow_id: FlowId,
116 pub cursor: SessionCursor,
118 pub context_json: String,
120}