1use std::str::FromStr;
2
3use flow_expression_parser::ast::{self};
4use serde::Deserialize;
5
6use crate::error::ManifestError;
7use crate::{v1, Error};
8
9type Result<T> = std::result::Result<T, Error>;
10
11pub(crate) fn vec_connection<'de, D>(deserializer: D) -> std::result::Result<Vec<crate::v1::FlowExpression>, D::Error>
12where
13 D: serde::Deserializer<'de>,
14{
15 struct Visitor;
16 impl<'de> serde::de::Visitor<'de> for Visitor {
17 type Value = Vec<crate::v1::FlowExpression>;
18 fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
19 write!(f, "a list of connections")
20 }
21
22 fn visit_seq<A: serde::de::SeqAccess<'de>>(
23 self,
24 mut seq: A,
25 ) -> std::result::Result<Vec<crate::v1::FlowExpression>, A::Error> {
26 let mut v = vec![];
27 while let Some(thing) = seq.next_element::<serde_value::Value>()? {
28 let result = match thing {
29 serde_value::Value::String(s) => ast::FlowExpression::from_str(&s)
30 .map_err(|e| serde::de::Error::custom(e.to_string()))?
31 .try_into()
32 .map_err(|e: ManifestError| serde::de::Error::custom(e.to_string()))?,
33 serde_value::Value::Map(map) => {
34 crate::v1::FlowExpression::deserialize(serde_value::ValueDeserializer::new(serde_value::Value::Map(map)))?
35 }
36 _ => {
37 return Err(serde::de::Error::invalid_type(
38 serde::de::Unexpected::Other("other"),
39 &self,
40 ))
41 }
42 };
43 v.push(result);
44 }
45 Ok(v)
46 }
47 }
48
49 deserializer.deserialize_seq(Visitor)
50}
51
52pub(crate) fn vec_component_operation<'de, D>(
53 deserializer: D,
54) -> std::result::Result<Vec<crate::v1::ComponentOperationExpression>, D::Error>
55where
56 D: serde::Deserializer<'de>,
57{
58 struct Visitor;
59 impl<'de> serde::de::Visitor<'de> for Visitor {
60 type Value = Vec<crate::v1::ComponentOperationExpression>;
61 fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
62 write!(f, "a list of operations")
63 }
64
65 fn visit_seq<A: serde::de::SeqAccess<'de>>(
66 self,
67 mut seq: A,
68 ) -> std::result::Result<Vec<crate::v1::ComponentOperationExpression>, A::Error> {
69 let mut v = vec![];
70 while let Some(thing) = seq.next_element::<serde_value::Value>()? {
71 let result = match thing {
72 serde_value::Value::String(s) => crate::v1::ComponentOperationExpression::from_str(&s)
73 .map_err(|e| serde::de::Error::custom(e.to_string()))?,
74 serde_value::Value::Map(map) => crate::v1::ComponentOperationExpression::deserialize(
75 serde_value::ValueDeserializer::new(serde_value::Value::Map(map)),
76 )?,
77 _ => {
78 return Err(serde::de::Error::invalid_type(
79 serde::de::Unexpected::Other("other"),
80 &self,
81 ))
82 }
83 };
84 v.push(result);
85 }
86 Ok(v)
87 }
88 }
89
90 deserializer.deserialize_seq(Visitor)
91}
92
93pub(crate) fn component_operation_syntax<'de, D>(
94 deserializer: D,
95) -> std::result::Result<crate::v1::ComponentOperationExpression, D::Error>
96where
97 D: serde::Deserializer<'de>,
98{
99 struct ComponentOperationExpressionVisitor;
100
101 impl<'de> serde::de::Visitor<'de> for ComponentOperationExpressionVisitor {
102 type Value = crate::v1::ComponentOperationExpression;
103
104 fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
105 formatter.write_str("a connection target definition")
106 }
107
108 fn visit_str<E>(self, s: &str) -> std::result::Result<Self::Value, E>
109 where
110 E: serde::de::Error,
111 {
112 crate::v1::ComponentOperationExpression::from_str(s).map_err(|e| serde::de::Error::custom(e.to_string()))
113 }
114
115 fn visit_map<A>(self, map: A) -> std::result::Result<Self::Value, A::Error>
116 where
117 A: serde::de::MapAccess<'de>,
118 {
119 crate::v1::ComponentOperationExpression::deserialize(serde::de::value::MapAccessDeserializer::new(map))
120 }
121 }
122
123 deserializer.deserialize_any(ComponentOperationExpressionVisitor)
124}
125
126impl FromStr for v1::ComponentOperationExpression {
127 type Err = Error;
128
129 fn from_str(s: &str) -> Result<Self> {
130 let mut parts = s.split("::");
131
132 let id = parts
133 .next()
134 .ok_or_else(|| crate::Error::InvalidOperationExpression(s.to_owned()))?
135 .to_owned();
136 let operation = parts
137 .next()
138 .ok_or_else(|| crate::Error::InvalidOperationExpression(s.to_owned()))?
139 .to_owned();
140
141 Ok(Self {
142 name: operation,
143 component: crate::v1::ComponentDefinition::ComponentReference(crate::v1::ComponentReference { id }),
144 with: None,
145 timeout: None,
146 })
147 }
148}
149
150impl Default for v1::ComponentDefinition {
151 fn default() -> Self {
152 Self::ComponentReference(crate::v1::ComponentReference {
153 id: "<anonymous>".to_owned(),
154 })
155 }
156}
157
158pub(crate) fn component_shortform<'de, D>(
159 deserializer: D,
160) -> std::result::Result<crate::v1::ComponentDefinition, D::Error>
161where
162 D: serde::Deserializer<'de>,
163{
164 struct Visitor;
165 impl<'de> serde::de::Visitor<'de> for Visitor {
166 type Value = crate::v1::ComponentDefinition;
167 fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
168 write!(
169 f,
170 "a component definition structure or path pointing to a WebAssembly module"
171 )
172 }
173
174 fn visit_str<E>(self, s: &str) -> std::result::Result<Self::Value, E>
175 where
176 E: serde::de::Error,
177 {
178 Ok(crate::v1::ComponentDefinition::ComponentReference(
179 crate::v1::ComponentReference { id: s.to_owned() },
180 ))
181 }
182
183 fn visit_string<E>(self, v: String) -> std::result::Result<Self::Value, E>
184 where
185 E: serde::de::Error,
186 {
187 Ok(crate::v1::ComponentDefinition::ComponentReference(
188 crate::v1::ComponentReference { id: v },
189 ))
190 }
191
192 fn visit_map<A>(self, map: A) -> std::result::Result<Self::Value, A::Error>
193 where
194 A: serde::de::MapAccess<'de>,
195 {
196 crate::v1::ComponentDefinition::deserialize(serde::de::value::MapAccessDeserializer::new(map))
197 }
198
199 fn visit_borrowed_str<E>(self, v: &'de str) -> std::result::Result<Self::Value, E>
200 where
201 E: serde::de::Error,
202 {
203 Ok(crate::v1::ComponentDefinition::ComponentReference(
204 crate::v1::ComponentReference { id: v.to_owned() },
205 ))
206 }
207 }
208
209 deserializer.deserialize_any(Visitor)
210}