1#[derive(Debug, Clone, Default, PartialEq, Eq)]
12pub struct Annotation {
13 pub id: Option<std::string::String>,
18 pub extension: Vec<super::extension::Extension>,
27 pub author: Option<AnnotationAuthor>,
31 pub time: Option<super::primitives::DateTime>,
35 pub text: super::primitives::Markdown,
39}
40
41impl super::super::codec::Json for Annotation {
42 fn to_json(&self) -> Result<super::super::codec::Object, super::super::codec::EncodeError> {
43 let mut object = super::super::codec::Object::new();
44 if let Some(v) = &self.id {
45 object.insert(
46 std::string::String::from("id"),
47 super::super::codec::Value::String(v.clone()),
48 );
49 }
50 if !self.extension.is_empty() {
51 let mut items = Vec::with_capacity(self.extension.len());
52 for item in &self.extension {
53 items.push(super::super::codec::Value::Object(
54 super::super::codec::Json::to_json(item)?,
55 ));
56 }
57 object.insert(
58 std::string::String::from("extension"),
59 super::super::codec::Value::Array(items),
60 );
61 }
62 if let Some(item) = &self.author {
63 let (suffix, value, element) = item.to_json_parts()?;
64 if let Some(value) = value {
65 object.insert(format!("author{suffix}"), value);
66 }
67 if let Some(element) = element {
68 object.insert(format!("_author{suffix}"), element);
69 }
70 }
71 if let Some(item) = &self.time {
72 if let Some(v) = super::super::codec::Primitive::value_json(item)? {
73 object.insert(std::string::String::from("time"), v);
74 }
75 if let Some(e) = super::super::codec::Primitive::element_json(item)? {
76 object.insert(std::string::String::from("_time"), e);
77 }
78 }
79 if let Some(v) = super::super::codec::Primitive::value_json(&self.text)? {
80 object.insert(std::string::String::from("text"), v);
81 }
82 if let Some(e) = super::super::codec::Primitive::element_json(&self.text)? {
83 object.insert(std::string::String::from("_text"), e);
84 }
85 Ok(object)
86 }
87
88 fn from_json(
89 object: &super::super::codec::Object,
90 path: &mut super::super::codec::Path,
91 ) -> Result<Self, super::super::codec::DecodeError> {
92 let mut raw_id: Option<&super::super::codec::Value> = None;
93 let mut raw_extension: Option<&super::super::codec::Value> = None;
94 let mut raw_author = super::super::codec::ChoiceSlot::default();
95 let mut raw_time: Option<&super::super::codec::Value> = None;
96 let mut raw_time_element: Option<&super::super::codec::Value> = None;
97 let mut raw_text: Option<&super::super::codec::Value> = None;
98 let mut raw_text_element: Option<&super::super::codec::Value> = None;
99 for (key, value) in object {
100 match key.as_str() {
101 "id" => {
102 raw_id = Some(value);
103 }
104 "extension" => {
105 raw_extension = Some(value);
106 }
107 "authorReference" => {
108 raw_author.value("Reference", value, path)?;
109 }
110 "_authorReference" => {
111 raw_author.element("Reference", value, path)?;
112 }
113 "authorString" => {
114 raw_author.value("String", value, path)?;
115 }
116 "_authorString" => {
117 raw_author.element("String", value, path)?;
118 }
119 "time" => {
120 raw_time = Some(value);
121 }
122 "_time" => {
123 raw_time_element = Some(value);
124 }
125 "text" => {
126 raw_text = Some(value);
127 }
128 "_text" => {
129 raw_text_element = Some(value);
130 }
131 other => {
132 return path.with(other, |path| {
133 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
134 });
135 }
136 }
137 }
138 let field_id = raw_id
139 .map(|value| {
140 path.with("id", |path| {
141 let value = super::super::codec::expect_single(value, path)?;
142 super::primitives::checked_string(
143 super::super::codec::expect_string(value, path)?,
144 path,
145 )
146 })
147 })
148 .transpose()?;
149 let mut field_extension = Vec::new();
150 if let Some(raw) = raw_extension {
151 for (index, value) in path
152 .with("extension", |path| {
153 super::super::codec::expect_array(raw, path)
154 })?
155 .iter()
156 .enumerate()
157 {
158 field_extension.push(path.with_index("extension", index, |path| {
159 super::super::codec::Json::from_json(
160 super::super::codec::expect_object(value, path)?,
161 path,
162 )
163 })?);
164 }
165 }
166 let field_author = match raw_author.suffix {
167 None => None,
168 Some(suffix) => Some(path.with("author", |path| {
169 AnnotationAuthor::from_json_parts(
170 suffix,
171 raw_author.value,
172 raw_author.element,
173 path,
174 )
175 })?),
176 };
177 let field_time = match (raw_time, raw_time_element) {
178 (None, None) => None,
179 (value, element) => Some(path.with("time", |path| {
180 super::super::codec::Primitive::from_json_parts(value, element, path)
181 })?),
182 };
183 let field_text = path.with("text", |path| {
184 super::super::codec::Primitive::from_json_parts(raw_text, raw_text_element, path)
185 })?;
186 Ok(Self {
187 id: field_id,
188 extension: field_extension,
189 author: field_author,
190 time: field_time,
191 text: field_text,
192 })
193 }
194}
195
196impl serde::Serialize for Annotation {
197 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
198 let mut map = serde::Serializer::serialize_map(serializer, None)?;
199 if let Some(AnnotationAuthor::String(inner)) = &self.author {
200 super::super::codec::element_entry(&mut map, "_authorString", inner)?;
201 }
202 super::super::codec::element_entry(&mut map, "_text", &self.text)?;
203 if let Some(item) = &self.time {
204 super::super::codec::element_entry(&mut map, "_time", item)?;
205 }
206 match &self.author {
207 Some(AnnotationAuthor::Reference(inner)) => {
208 serde::ser::SerializeMap::serialize_entry(
209 &mut map,
210 "authorReference",
211 inner.as_ref(),
212 )?;
213 }
214 Some(AnnotationAuthor::String(inner)) => {
215 super::super::codec::value_entry(&mut map, "authorString", inner)?;
216 }
217 None => {}
218 }
219 if !self.extension.is_empty() {
220 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
221 }
222 if let Some(v) = &self.id {
223 serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
224 }
225 super::super::codec::value_entry(&mut map, "text", &self.text)?;
226 if let Some(item) = &self.time {
227 super::super::codec::value_entry(&mut map, "time", item)?;
228 }
229 serde::ser::SerializeMap::end(map)
230 }
231}
232
233impl<'de> serde::Deserialize<'de> for Annotation {
234 fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
235 let value = <super::super::codec::Value as serde::Deserialize>::deserialize(deserializer)?;
236 let mut path = super::super::codec::Path::root("Annotation");
237 let object =
238 super::super::codec::expect_object(&value, &path).map_err(serde::de::Error::custom)?;
239 super::super::codec::Json::from_json(object, &mut path).map_err(serde::de::Error::custom)
240 }
241}
242
243#[derive(Debug, Clone, PartialEq, Eq)]
247pub enum AnnotationAuthor {
248 Reference(Box<super::reference::Reference>),
250 String(super::primitives::String),
252}
253
254impl AnnotationAuthor {
255 pub fn to_json_parts(
261 &self,
262 ) -> Result<
263 (
264 &'static str,
265 Option<super::super::codec::Value>,
266 Option<super::super::codec::Value>,
267 ),
268 super::super::codec::EncodeError,
269 > {
270 match self {
271 Self::Reference(inner) => Ok((
272 "Reference",
273 Some(super::super::codec::Value::Object(
274 super::super::codec::Json::to_json(inner.as_ref())?,
275 )),
276 None,
277 )),
278 Self::String(inner) => Ok((
279 "String",
280 super::super::codec::Primitive::value_json(inner)?,
281 super::super::codec::Primitive::element_json(inner)?,
282 )),
283 }
284 }
285
286 pub fn from_json_parts(
292 suffix: &str,
293 value: Option<&super::super::codec::Value>,
294 element: Option<&super::super::codec::Value>,
295 path: &mut super::super::codec::Path,
296 ) -> Result<Self, super::super::codec::DecodeError> {
297 match suffix {
298 "Reference" => {
299 if element.is_some() {
300 return Err(path.error(super::super::codec::DecodeErrorKind::WrongType {
301 expected: "no underscore form for a complex type",
302 }));
303 }
304 let value = value.ok_or_else(|| {
305 path.error(super::super::codec::DecodeErrorKind::MissingProperty)
306 })?;
307 let inner = super::super::codec::Json::from_json(
308 super::super::codec::expect_object(
309 super::super::codec::expect_single(value, path)?,
310 path,
311 )?,
312 path,
313 )?;
314 Ok(Self::Reference(Box::new(inner)))
315 }
316 "String" => Ok(Self::String(
317 super::super::codec::Primitive::from_json_parts(value, element, path)?,
318 )),
319 _ => Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty)),
320 }
321 }
322}