strands/types/
interrupt.rs1use serde::{Deserialize, Serialize};
4
5use super::json::JsonValue;
6
7#[derive(Debug, Clone, PartialEq)]
11#[non_exhaustive]
12pub struct InterruptParams {
13 pub name: String,
15 pub reason: Option<JsonValue>,
17 pub response: Option<JsonValue>,
20}
21
22impl InterruptParams {
23 pub fn new(name: impl Into<String>) -> Self {
25 Self {
26 name: name.into(),
27 reason: None,
28 response: None,
29 }
30 }
31
32 #[must_use]
34 pub fn with_reason(mut self, reason: impl Into<JsonValue>) -> Self {
35 self.reason = Some(reason.into());
36 self
37 }
38
39 #[must_use]
41 pub fn with_response(mut self, response: impl Into<JsonValue>) -> Self {
42 self.response = Some(response.into());
43 self
44 }
45}
46
47impl From<&str> for InterruptParams {
48 fn from(name: &str) -> Self {
49 Self::new(name)
50 }
51}
52
53impl From<String> for InterruptParams {
54 fn from(name: String) -> Self {
55 Self::new(name)
56 }
57}
58
59#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
61#[serde(rename_all = "camelCase")]
62#[non_exhaustive]
63pub struct InterruptResponse {
64 pub interrupt_id: String,
66 pub response: JsonValue,
68}
69
70impl InterruptResponse {
71 pub fn new(interrupt_id: impl Into<String>, response: impl Into<JsonValue>) -> Self {
73 Self {
74 interrupt_id: interrupt_id.into(),
75 response: response.into(),
76 }
77 }
78}
79
80#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
84#[serde(rename_all = "camelCase")]
85#[non_exhaustive]
86pub struct InterruptResponseContent {
87 pub interrupt_response: InterruptResponse,
89}
90
91impl InterruptResponseContent {
92 pub fn new(interrupt_response: InterruptResponse) -> Self {
94 Self { interrupt_response }
95 }
96}
97
98impl From<InterruptResponse> for InterruptResponseContent {
99 fn from(interrupt_response: InterruptResponse) -> Self {
100 Self::new(interrupt_response)
101 }
102}
103
104#[cfg(test)]
105mod tests {
106 use serde_json::json;
107
108 use super::*;
109
110 #[test]
111 fn interrupt_response_content_uses_the_wrapped_wire_shape() {
112 let content = InterruptResponseContent::new(InterruptResponse::new("int-1", "approved"));
113 assert_eq!(
114 serde_json::to_value(&content).unwrap(),
115 json!({"interruptResponse": {"interruptId": "int-1", "response": "approved"}})
116 );
117 }
118
119 #[test]
120 fn interrupt_params_convert_from_a_name() {
121 let params = InterruptParams::from("confirm").with_reason(json!("Please confirm"));
122 assert_eq!(params.name, "confirm");
123 assert_eq!(params.reason, Some(json!("Please confirm")));
124 assert_eq!(
125 InterruptParams::from(String::from("confirm")),
126 InterruptParams::new("confirm")
127 );
128 }
129}