cortexkit_usage_steps/
ops.rs1use serde::{Deserialize, Serialize};
2
3use crate::{codes, Cursor, StepRow};
4
5#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
8#[serde(deny_unknown_fields)]
9#[non_exhaustive]
10pub struct SubscribeRequest {}
11
12impl SubscribeRequest {
13 pub fn new() -> Self {
14 Self {}
15 }
16}
17
18#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
25#[non_exhaustive]
26pub struct Notice {
27 pub through: Cursor,
28}
29
30impl Notice {
31 pub fn new(through: impl Into<Cursor>) -> Self {
32 Self {
33 through: through.into(),
34 }
35 }
36
37 pub fn check_closed(value: &serde_json::Value) -> Result<(), crate::NotClosed> {
40 crate::row::check_members("notice", value, &["through"])
41 }
42}
43
44#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
52#[serde(deny_unknown_fields)]
53#[non_exhaustive]
54pub struct ReadRequest {
55 #[serde(default, skip_serializing_if = "Option::is_none")]
56 pub after: Option<Cursor>,
57 #[serde(default, skip_serializing_if = "Option::is_none")]
58 pub limit: Option<u32>,
59}
60
61impl ReadRequest {
62 pub fn new() -> Self {
64 Self::default()
65 }
66
67 pub fn after(mut self, cursor: impl Into<Cursor>) -> Self {
69 self.after = Some(cursor.into());
70 self
71 }
72
73 pub fn with_limit(mut self, limit: u32) -> Self {
75 self.limit = Some(limit);
76 self
77 }
78}
79
80#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
82#[non_exhaustive]
83pub struct ReadReply {
84 pub rows: Vec<StepRow>,
86 #[serde(default)]
89 pub next: Option<Cursor>,
90 pub through: Cursor,
92}
93
94impl ReadReply {
95 pub fn new(rows: Vec<StepRow>, next: Option<Cursor>, through: impl Into<Cursor>) -> Self {
96 Self {
97 rows,
98 next,
99 through: through.into(),
100 }
101 }
102
103 pub fn resume_after(&self) -> &Cursor {
106 self.next.as_ref().unwrap_or(&self.through)
107 }
108
109 pub fn check_closed(value: &serde_json::Value) -> Result<(), crate::NotClosed> {
113 crate::row::check_members("reply", value, &["rows", "next", "through"])?;
114 if let Some(serde_json::Value::Array(rows)) = value.get("rows") {
115 for row in rows {
116 StepRow::check_closed(row)?;
117 }
118 }
119 Ok(())
120 }
121}
122
123#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
131#[non_exhaustive]
132pub struct Refusal {
133 pub code: String,
134 #[serde(default)]
135 pub message: String,
136 #[serde(default, skip_serializing_if = "Option::is_none")]
137 pub detail: Option<serde_json::Value>,
138}
139
140impl Refusal {
141 pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
143 Self {
144 code: code.into(),
145 message: message.into(),
146 detail: None,
147 }
148 }
149
150 pub fn lagged(through: impl Into<Cursor>, message: impl Into<String>) -> Self {
152 Self::new(codes::LAGGED, message).with_through(through)
153 }
154
155 pub fn cancelled(message: impl Into<String>) -> Self {
158 Self::new(codes::CANCELLED, message)
159 }
160
161 pub fn cursor_expired(through: impl Into<Cursor>, message: impl Into<String>) -> Self {
163 Self::new(codes::CURSOR_EXPIRED, message).with_through(through)
164 }
165
166 fn with_through(mut self, through: impl Into<Cursor>) -> Self {
167 self.detail = Some(serde_json::json!({ "through": through.into() }));
168 self
169 }
170
171 pub fn through(&self) -> Option<Cursor> {
173 self.detail
174 .as_ref()?
175 .get("through")?
176 .as_str()
177 .map(Cursor::new)
178 }
179}