1#[derive(Debug, Clone, Default, PartialEq, Eq)]
11pub struct Base64Binary {
12 pub id: Option<std::string::String>,
16 pub extension: Vec<super::extension::Extension>,
25 pub value: Option<std::string::String>,
29}
30impl From<std::string::String> for Base64Binary {
31 fn from(value: std::string::String) -> Self {
32 Self {
33 value: Some(value),
34 ..Self::default()
35 }
36 }
37}
38impl From<&str> for Base64Binary {
39 fn from(value: &str) -> Self {
40 Self::from(std::string::String::from(value))
41 }
42}
43
44#[expect(
50 clippy::expect_used,
51 reason = "the emitter compiles every lexical form it writes, so the pattern holds"
52)]
53static BASE64_BINARY_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> =
54 std::sync::LazyLock::new(|| {
55 regex::bytes::RegexBuilder::new(
56 r"^(?:(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?)$",
57 )
58 .unicode(false)
59 .build()
60 .expect("the lexical form of `base64Binary` should compile")
61 });
62
63pub(crate) fn checked_base64_binary(
69 text: std::string::String,
70 path: &super::super::codec::Path,
71) -> Result<std::string::String, super::super::codec::DecodeError> {
72 if BASE64_BINARY_LEXICAL_FORM.is_match(text.as_bytes()) {
73 Ok(text)
74 } else {
75 Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
76 }
77}
78
79impl Base64Binary {
80 pub fn check(text: &str) -> Result<(), super::super::codec::LexicalError> {
86 if BASE64_BINARY_LEXICAL_FORM.is_match(text.as_bytes()) {
87 Ok(())
88 } else {
89 Err(super::super::codec::LexicalError {
90 type_code: "base64Binary",
91 })
92 }
93 }
94}
95
96impl super::super::codec::Primitive for Base64Binary {
97 fn value_json(
98 &self,
99 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
100 Ok(self
101 .value
102 .as_ref()
103 .map(|v| super::super::codec::Value::String(v.clone())))
104 }
105
106 fn element_json(
107 &self,
108 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
109 if self.id.is_none() && self.extension.is_empty() {
110 return Ok(None);
111 }
112 let mut object = super::super::codec::Object::new();
113 if let Some(id) = &self.id {
114 object.insert(
115 std::string::String::from("id"),
116 super::super::codec::Value::String(id.clone()),
117 );
118 }
119 if !self.extension.is_empty() {
120 let mut items = Vec::with_capacity(self.extension.len());
121 for item in &self.extension {
122 items.push(super::super::codec::Value::Object(
123 super::super::codec::Json::to_json(item)?,
124 ));
125 }
126 object.insert(
127 std::string::String::from("extension"),
128 super::super::codec::Value::Array(items),
129 );
130 }
131 Ok(Some(super::super::codec::Value::Object(object)))
132 }
133
134 fn has_value(&self) -> bool {
135 self.value.is_some()
136 }
137
138 fn has_element(&self) -> bool {
139 self.id.is_some() || !self.extension.is_empty()
140 }
141
142 fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
143 match self.value.as_ref() {
144 Some(v) => serde::Serializer::serialize_str(serializer, v),
145 None => serde::Serializer::serialize_none(serializer),
146 }
147 }
148
149 fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
150 if self.id.is_none() && self.extension.is_empty() {
151 return serde::Serializer::serialize_none(serializer);
152 }
153 let mut map = serde::Serializer::serialize_map(serializer, None)?;
154 if !self.extension.is_empty() {
155 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
156 }
157 if let Some(id) = &self.id {
158 serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
159 }
160 serde::ser::SerializeMap::end(map)
161 }
162
163 fn from_json_parts(
164 value: Option<&super::super::codec::Value>,
165 element: Option<&super::super::codec::Value>,
166 path: &mut super::super::codec::Path,
167 ) -> Result<Self, super::super::codec::DecodeError> {
168 if value.is_none() && element.is_none() && !path.is_lenient() {
169 return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
170 }
171 let value = match value {
172 Some(value) => {
173 let value = super::super::codec::expect_single(value, path)?;
174 Some(checked_base64_binary(
175 super::super::codec::expect_string(value, path)?,
176 path,
177 )?)
178 }
179 None => None,
180 };
181 let mut id = None;
182 let mut extension = Vec::new();
183 if let Some(element) = element {
184 let object = super::super::codec::expect_object(element, path)?;
185 for (key, item) in object {
186 match key.as_str() {
187 "id" => {
188 id = Some(path.with("id", |path| {
189 super::primitives::checked_string(
190 super::super::codec::expect_string(item, path)?,
191 path,
192 )
193 })?);
194 }
195 "extension" => {
196 for (index, entry) in super::super::codec::expect_array(item, path)?
197 .iter()
198 .enumerate()
199 {
200 extension.push(path.with_index("extension", index, |path| {
201 super::super::codec::Json::from_json(
202 super::super::codec::expect_object(entry, path)?,
203 path,
204 )
205 })?);
206 }
207 }
208 _ => {
209 return path.with(key, |path| {
210 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
211 });
212 }
213 }
214 }
215 }
216 Ok(Self {
217 id,
218 extension,
219 value,
220 })
221 }
222}
223
224#[derive(Debug, Clone, Default, PartialEq, Eq)]
228pub struct Boolean {
229 pub id: Option<std::string::String>,
233 pub extension: Vec<super::extension::Extension>,
242 pub value: Option<bool>,
246}
247impl From<bool> for Boolean {
248 fn from(value: bool) -> Self {
249 Self {
250 value: Some(value),
251 ..Self::default()
252 }
253 }
254}
255
256impl super::super::codec::Primitive for Boolean {
257 fn value_json(
258 &self,
259 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
260 Ok(self
261 .value
262 .as_ref()
263 .map(|v| super::super::codec::Value::Bool(*v)))
264 }
265
266 fn element_json(
267 &self,
268 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
269 if self.id.is_none() && self.extension.is_empty() {
270 return Ok(None);
271 }
272 let mut object = super::super::codec::Object::new();
273 if let Some(id) = &self.id {
274 object.insert(
275 std::string::String::from("id"),
276 super::super::codec::Value::String(id.clone()),
277 );
278 }
279 if !self.extension.is_empty() {
280 let mut items = Vec::with_capacity(self.extension.len());
281 for item in &self.extension {
282 items.push(super::super::codec::Value::Object(
283 super::super::codec::Json::to_json(item)?,
284 ));
285 }
286 object.insert(
287 std::string::String::from("extension"),
288 super::super::codec::Value::Array(items),
289 );
290 }
291 Ok(Some(super::super::codec::Value::Object(object)))
292 }
293
294 fn has_value(&self) -> bool {
295 self.value.is_some()
296 }
297
298 fn has_element(&self) -> bool {
299 self.id.is_some() || !self.extension.is_empty()
300 }
301
302 fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
303 match self.value.as_ref() {
304 Some(v) => serde::Serializer::serialize_bool(serializer, *v),
305 None => serde::Serializer::serialize_none(serializer),
306 }
307 }
308
309 fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
310 if self.id.is_none() && self.extension.is_empty() {
311 return serde::Serializer::serialize_none(serializer);
312 }
313 let mut map = serde::Serializer::serialize_map(serializer, None)?;
314 if !self.extension.is_empty() {
315 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
316 }
317 if let Some(id) = &self.id {
318 serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
319 }
320 serde::ser::SerializeMap::end(map)
321 }
322
323 fn from_json_parts(
324 value: Option<&super::super::codec::Value>,
325 element: Option<&super::super::codec::Value>,
326 path: &mut super::super::codec::Path,
327 ) -> Result<Self, super::super::codec::DecodeError> {
328 if value.is_none() && element.is_none() && !path.is_lenient() {
329 return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
330 }
331 let value = match value {
332 Some(value) => {
333 let value = super::super::codec::expect_single(value, path)?;
334 Some(super::super::codec::expect_bool(value, path)?)
335 }
336 None => None,
337 };
338 let mut id = None;
339 let mut extension = Vec::new();
340 if let Some(element) = element {
341 let object = super::super::codec::expect_object(element, path)?;
342 for (key, item) in object {
343 match key.as_str() {
344 "id" => {
345 id = Some(path.with("id", |path| {
346 super::primitives::checked_string(
347 super::super::codec::expect_string(item, path)?,
348 path,
349 )
350 })?);
351 }
352 "extension" => {
353 for (index, entry) in super::super::codec::expect_array(item, path)?
354 .iter()
355 .enumerate()
356 {
357 extension.push(path.with_index("extension", index, |path| {
358 super::super::codec::Json::from_json(
359 super::super::codec::expect_object(entry, path)?,
360 path,
361 )
362 })?);
363 }
364 }
365 _ => {
366 return path.with(key, |path| {
367 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
368 });
369 }
370 }
371 }
372 }
373 Ok(Self {
374 id,
375 extension,
376 value,
377 })
378 }
379}
380
381#[derive(Debug, Clone, Default, PartialEq, Eq)]
385pub struct Canonical {
386 pub id: Option<std::string::String>,
390 pub extension: Vec<super::extension::Extension>,
399 pub value: Option<std::string::String>,
401}
402impl From<std::string::String> for Canonical {
403 fn from(value: std::string::String) -> Self {
404 Self {
405 value: Some(value),
406 ..Self::default()
407 }
408 }
409}
410impl From<&str> for Canonical {
411 fn from(value: &str) -> Self {
412 Self::from(std::string::String::from(value))
413 }
414}
415
416#[expect(
422 clippy::expect_used,
423 reason = "the emitter compiles every lexical form it writes, so the pattern holds"
424)]
425static CANONICAL_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> =
426 std::sync::LazyLock::new(|| {
427 regex::bytes::RegexBuilder::new(r"^(?:\S*)$")
428 .unicode(false)
429 .build()
430 .expect("the lexical form of `canonical` should compile")
431 });
432
433pub(crate) fn checked_canonical(
439 text: std::string::String,
440 path: &super::super::codec::Path,
441) -> Result<std::string::String, super::super::codec::DecodeError> {
442 if CANONICAL_LEXICAL_FORM.is_match(text.as_bytes()) {
443 Ok(text)
444 } else {
445 Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
446 }
447}
448
449impl Canonical {
450 pub fn check(text: &str) -> Result<(), super::super::codec::LexicalError> {
456 if CANONICAL_LEXICAL_FORM.is_match(text.as_bytes()) {
457 Ok(())
458 } else {
459 Err(super::super::codec::LexicalError {
460 type_code: "canonical",
461 })
462 }
463 }
464}
465
466impl super::super::codec::Primitive for Canonical {
467 fn value_json(
468 &self,
469 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
470 Ok(self
471 .value
472 .as_ref()
473 .map(|v| super::super::codec::Value::String(v.clone())))
474 }
475
476 fn element_json(
477 &self,
478 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
479 if self.id.is_none() && self.extension.is_empty() {
480 return Ok(None);
481 }
482 let mut object = super::super::codec::Object::new();
483 if let Some(id) = &self.id {
484 object.insert(
485 std::string::String::from("id"),
486 super::super::codec::Value::String(id.clone()),
487 );
488 }
489 if !self.extension.is_empty() {
490 let mut items = Vec::with_capacity(self.extension.len());
491 for item in &self.extension {
492 items.push(super::super::codec::Value::Object(
493 super::super::codec::Json::to_json(item)?,
494 ));
495 }
496 object.insert(
497 std::string::String::from("extension"),
498 super::super::codec::Value::Array(items),
499 );
500 }
501 Ok(Some(super::super::codec::Value::Object(object)))
502 }
503
504 fn has_value(&self) -> bool {
505 self.value.is_some()
506 }
507
508 fn has_element(&self) -> bool {
509 self.id.is_some() || !self.extension.is_empty()
510 }
511
512 fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
513 match self.value.as_ref() {
514 Some(v) => serde::Serializer::serialize_str(serializer, v),
515 None => serde::Serializer::serialize_none(serializer),
516 }
517 }
518
519 fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
520 if self.id.is_none() && self.extension.is_empty() {
521 return serde::Serializer::serialize_none(serializer);
522 }
523 let mut map = serde::Serializer::serialize_map(serializer, None)?;
524 if !self.extension.is_empty() {
525 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
526 }
527 if let Some(id) = &self.id {
528 serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
529 }
530 serde::ser::SerializeMap::end(map)
531 }
532
533 fn from_json_parts(
534 value: Option<&super::super::codec::Value>,
535 element: Option<&super::super::codec::Value>,
536 path: &mut super::super::codec::Path,
537 ) -> Result<Self, super::super::codec::DecodeError> {
538 if value.is_none() && element.is_none() && !path.is_lenient() {
539 return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
540 }
541 let value = match value {
542 Some(value) => {
543 let value = super::super::codec::expect_single(value, path)?;
544 Some(checked_canonical(
545 super::super::codec::expect_string(value, path)?,
546 path,
547 )?)
548 }
549 None => None,
550 };
551 let mut id = None;
552 let mut extension = Vec::new();
553 if let Some(element) = element {
554 let object = super::super::codec::expect_object(element, path)?;
555 for (key, item) in object {
556 match key.as_str() {
557 "id" => {
558 id = Some(path.with("id", |path| {
559 super::primitives::checked_string(
560 super::super::codec::expect_string(item, path)?,
561 path,
562 )
563 })?);
564 }
565 "extension" => {
566 for (index, entry) in super::super::codec::expect_array(item, path)?
567 .iter()
568 .enumerate()
569 {
570 extension.push(path.with_index("extension", index, |path| {
571 super::super::codec::Json::from_json(
572 super::super::codec::expect_object(entry, path)?,
573 path,
574 )
575 })?);
576 }
577 }
578 _ => {
579 return path.with(key, |path| {
580 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
581 });
582 }
583 }
584 }
585 }
586 Ok(Self {
587 id,
588 extension,
589 value,
590 })
591 }
592}
593
594#[derive(Debug, Clone, Default, PartialEq, Eq)]
600pub struct Code {
601 pub id: Option<std::string::String>,
605 pub extension: Vec<super::extension::Extension>,
614 pub value: Option<std::string::String>,
616}
617impl From<std::string::String> for Code {
618 fn from(value: std::string::String) -> Self {
619 Self {
620 value: Some(value),
621 ..Self::default()
622 }
623 }
624}
625impl From<&str> for Code {
626 fn from(value: &str) -> Self {
627 Self::from(std::string::String::from(value))
628 }
629}
630
631#[expect(
637 clippy::expect_used,
638 reason = "the emitter compiles every lexical form it writes, so the pattern holds"
639)]
640static CODE_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> =
641 std::sync::LazyLock::new(|| {
642 regex::bytes::RegexBuilder::new(r"^(?:[^\s]+( [^\s]+)*)$")
643 .unicode(false)
644 .build()
645 .expect("the lexical form of `code` should compile")
646 });
647
648pub(crate) fn checked_code(
654 text: std::string::String,
655 path: &super::super::codec::Path,
656) -> Result<std::string::String, super::super::codec::DecodeError> {
657 if CODE_LEXICAL_FORM.is_match(text.as_bytes()) {
658 Ok(text)
659 } else {
660 Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
661 }
662}
663
664impl Code {
665 pub fn check(text: &str) -> Result<(), super::super::codec::LexicalError> {
671 if CODE_LEXICAL_FORM.is_match(text.as_bytes()) {
672 Ok(())
673 } else {
674 Err(super::super::codec::LexicalError { type_code: "code" })
675 }
676 }
677}
678
679impl super::super::codec::Primitive for Code {
680 fn value_json(
681 &self,
682 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
683 Ok(self
684 .value
685 .as_ref()
686 .map(|v| super::super::codec::Value::String(v.clone())))
687 }
688
689 fn element_json(
690 &self,
691 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
692 if self.id.is_none() && self.extension.is_empty() {
693 return Ok(None);
694 }
695 let mut object = super::super::codec::Object::new();
696 if let Some(id) = &self.id {
697 object.insert(
698 std::string::String::from("id"),
699 super::super::codec::Value::String(id.clone()),
700 );
701 }
702 if !self.extension.is_empty() {
703 let mut items = Vec::with_capacity(self.extension.len());
704 for item in &self.extension {
705 items.push(super::super::codec::Value::Object(
706 super::super::codec::Json::to_json(item)?,
707 ));
708 }
709 object.insert(
710 std::string::String::from("extension"),
711 super::super::codec::Value::Array(items),
712 );
713 }
714 Ok(Some(super::super::codec::Value::Object(object)))
715 }
716
717 fn has_value(&self) -> bool {
718 self.value.is_some()
719 }
720
721 fn has_element(&self) -> bool {
722 self.id.is_some() || !self.extension.is_empty()
723 }
724
725 fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
726 match self.value.as_ref() {
727 Some(v) => serde::Serializer::serialize_str(serializer, v),
728 None => serde::Serializer::serialize_none(serializer),
729 }
730 }
731
732 fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
733 if self.id.is_none() && self.extension.is_empty() {
734 return serde::Serializer::serialize_none(serializer);
735 }
736 let mut map = serde::Serializer::serialize_map(serializer, None)?;
737 if !self.extension.is_empty() {
738 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
739 }
740 if let Some(id) = &self.id {
741 serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
742 }
743 serde::ser::SerializeMap::end(map)
744 }
745
746 fn from_json_parts(
747 value: Option<&super::super::codec::Value>,
748 element: Option<&super::super::codec::Value>,
749 path: &mut super::super::codec::Path,
750 ) -> Result<Self, super::super::codec::DecodeError> {
751 if value.is_none() && element.is_none() && !path.is_lenient() {
752 return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
753 }
754 let value = match value {
755 Some(value) => {
756 let value = super::super::codec::expect_single(value, path)?;
757 Some(checked_code(
758 super::super::codec::expect_string(value, path)?,
759 path,
760 )?)
761 }
762 None => None,
763 };
764 let mut id = None;
765 let mut extension = Vec::new();
766 if let Some(element) = element {
767 let object = super::super::codec::expect_object(element, path)?;
768 for (key, item) in object {
769 match key.as_str() {
770 "id" => {
771 id = Some(path.with("id", |path| {
772 super::primitives::checked_string(
773 super::super::codec::expect_string(item, path)?,
774 path,
775 )
776 })?);
777 }
778 "extension" => {
779 for (index, entry) in super::super::codec::expect_array(item, path)?
780 .iter()
781 .enumerate()
782 {
783 extension.push(path.with_index("extension", index, |path| {
784 super::super::codec::Json::from_json(
785 super::super::codec::expect_object(entry, path)?,
786 path,
787 )
788 })?);
789 }
790 }
791 _ => {
792 return path.with(key, |path| {
793 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
794 });
795 }
796 }
797 }
798 }
799 Ok(Self {
800 id,
801 extension,
802 value,
803 })
804 }
805}
806
807#[derive(Debug, Clone, Default, PartialEq, Eq)]
813pub struct Date {
814 pub id: Option<std::string::String>,
818 pub extension: Vec<super::extension::Extension>,
827 pub value: Option<std::string::String>,
831}
832impl From<std::string::String> for Date {
833 fn from(value: std::string::String) -> Self {
834 Self {
835 value: Some(value),
836 ..Self::default()
837 }
838 }
839}
840impl From<&str> for Date {
841 fn from(value: &str) -> Self {
842 Self::from(std::string::String::from(value))
843 }
844}
845
846#[expect(
852 clippy::expect_used,
853 reason = "the emitter compiles every lexical form it writes, so the pattern holds"
854)]
855static DATE_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> = std::sync::LazyLock::new(
856 || {
857 regex::bytes::RegexBuilder::new(r"^(?:([0-9]([0-9]([0-9][1-9]|[1-9]0)|[1-9]00)|[1-9]000)(-(0[1-9]|1[0-2])(-(0[1-9]|[1-2][0-9]|3[0-1]))?)?)$")
858 .unicode(false)
859 .build()
860 .expect("the lexical form of `date` should compile")
861 },
862);
863
864pub(crate) fn checked_date(
870 text: std::string::String,
871 path: &super::super::codec::Path,
872) -> Result<std::string::String, super::super::codec::DecodeError> {
873 if DATE_LEXICAL_FORM.is_match(text.as_bytes()) {
874 Ok(text)
875 } else {
876 Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
877 }
878}
879
880impl Date {
881 pub fn check(text: &str) -> Result<(), super::super::codec::LexicalError> {
887 if DATE_LEXICAL_FORM.is_match(text.as_bytes()) {
888 Ok(())
889 } else {
890 Err(super::super::codec::LexicalError { type_code: "date" })
891 }
892 }
893}
894
895impl super::super::codec::Primitive for Date {
896 fn value_json(
897 &self,
898 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
899 Ok(self
900 .value
901 .as_ref()
902 .map(|v| super::super::codec::Value::String(v.clone())))
903 }
904
905 fn element_json(
906 &self,
907 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
908 if self.id.is_none() && self.extension.is_empty() {
909 return Ok(None);
910 }
911 let mut object = super::super::codec::Object::new();
912 if let Some(id) = &self.id {
913 object.insert(
914 std::string::String::from("id"),
915 super::super::codec::Value::String(id.clone()),
916 );
917 }
918 if !self.extension.is_empty() {
919 let mut items = Vec::with_capacity(self.extension.len());
920 for item in &self.extension {
921 items.push(super::super::codec::Value::Object(
922 super::super::codec::Json::to_json(item)?,
923 ));
924 }
925 object.insert(
926 std::string::String::from("extension"),
927 super::super::codec::Value::Array(items),
928 );
929 }
930 Ok(Some(super::super::codec::Value::Object(object)))
931 }
932
933 fn has_value(&self) -> bool {
934 self.value.is_some()
935 }
936
937 fn has_element(&self) -> bool {
938 self.id.is_some() || !self.extension.is_empty()
939 }
940
941 fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
942 match self.value.as_ref() {
943 Some(v) => serde::Serializer::serialize_str(serializer, v),
944 None => serde::Serializer::serialize_none(serializer),
945 }
946 }
947
948 fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
949 if self.id.is_none() && self.extension.is_empty() {
950 return serde::Serializer::serialize_none(serializer);
951 }
952 let mut map = serde::Serializer::serialize_map(serializer, None)?;
953 if !self.extension.is_empty() {
954 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
955 }
956 if let Some(id) = &self.id {
957 serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
958 }
959 serde::ser::SerializeMap::end(map)
960 }
961
962 fn from_json_parts(
963 value: Option<&super::super::codec::Value>,
964 element: Option<&super::super::codec::Value>,
965 path: &mut super::super::codec::Path,
966 ) -> Result<Self, super::super::codec::DecodeError> {
967 if value.is_none() && element.is_none() && !path.is_lenient() {
968 return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
969 }
970 let value = match value {
971 Some(value) => {
972 let value = super::super::codec::expect_single(value, path)?;
973 Some(checked_date(
974 super::super::codec::expect_string(value, path)?,
975 path,
976 )?)
977 }
978 None => None,
979 };
980 let mut id = None;
981 let mut extension = Vec::new();
982 if let Some(element) = element {
983 let object = super::super::codec::expect_object(element, path)?;
984 for (key, item) in object {
985 match key.as_str() {
986 "id" => {
987 id = Some(path.with("id", |path| {
988 super::primitives::checked_string(
989 super::super::codec::expect_string(item, path)?,
990 path,
991 )
992 })?);
993 }
994 "extension" => {
995 for (index, entry) in super::super::codec::expect_array(item, path)?
996 .iter()
997 .enumerate()
998 {
999 extension.push(path.with_index("extension", index, |path| {
1000 super::super::codec::Json::from_json(
1001 super::super::codec::expect_object(entry, path)?,
1002 path,
1003 )
1004 })?);
1005 }
1006 }
1007 _ => {
1008 return path.with(key, |path| {
1009 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
1010 });
1011 }
1012 }
1013 }
1014 }
1015 Ok(Self {
1016 id,
1017 extension,
1018 value,
1019 })
1020 }
1021}
1022
1023#[derive(Debug, Clone, Default, PartialEq, Eq)]
1031pub struct DateTime {
1032 pub id: Option<std::string::String>,
1036 pub extension: Vec<super::extension::Extension>,
1045 pub value: Option<std::string::String>,
1049}
1050impl From<std::string::String> for DateTime {
1051 fn from(value: std::string::String) -> Self {
1052 Self {
1053 value: Some(value),
1054 ..Self::default()
1055 }
1056 }
1057}
1058impl From<&str> for DateTime {
1059 fn from(value: &str) -> Self {
1060 Self::from(std::string::String::from(value))
1061 }
1062}
1063
1064#[expect(
1070 clippy::expect_used,
1071 reason = "the emitter compiles every lexical form it writes, so the pattern holds"
1072)]
1073static DATE_TIME_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> = std::sync::LazyLock::new(
1074 || {
1075 regex::bytes::RegexBuilder::new(r"^(?:([0-9]([0-9]([0-9][1-9]|[1-9]0)|[1-9]00)|[1-9]000)(-(0[1-9]|1[0-2])(-(0[1-9]|[1-2][0-9]|3[0-1])(T([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\.[0-9]{1,9})?)?)?(Z|(\+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00)?)?)?)$")
1076 .unicode(false)
1077 .build()
1078 .expect("the lexical form of `dateTime` should compile")
1079 },
1080);
1081
1082pub(crate) fn checked_date_time(
1088 text: std::string::String,
1089 path: &super::super::codec::Path,
1090) -> Result<std::string::String, super::super::codec::DecodeError> {
1091 if DATE_TIME_LEXICAL_FORM.is_match(text.as_bytes()) {
1092 Ok(text)
1093 } else {
1094 Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
1095 }
1096}
1097
1098impl DateTime {
1099 pub fn check(text: &str) -> Result<(), super::super::codec::LexicalError> {
1105 if DATE_TIME_LEXICAL_FORM.is_match(text.as_bytes()) {
1106 Ok(())
1107 } else {
1108 Err(super::super::codec::LexicalError {
1109 type_code: "dateTime",
1110 })
1111 }
1112 }
1113}
1114
1115impl super::super::codec::Primitive for DateTime {
1116 fn value_json(
1117 &self,
1118 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
1119 Ok(self
1120 .value
1121 .as_ref()
1122 .map(|v| super::super::codec::Value::String(v.clone())))
1123 }
1124
1125 fn element_json(
1126 &self,
1127 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
1128 if self.id.is_none() && self.extension.is_empty() {
1129 return Ok(None);
1130 }
1131 let mut object = super::super::codec::Object::new();
1132 if let Some(id) = &self.id {
1133 object.insert(
1134 std::string::String::from("id"),
1135 super::super::codec::Value::String(id.clone()),
1136 );
1137 }
1138 if !self.extension.is_empty() {
1139 let mut items = Vec::with_capacity(self.extension.len());
1140 for item in &self.extension {
1141 items.push(super::super::codec::Value::Object(
1142 super::super::codec::Json::to_json(item)?,
1143 ));
1144 }
1145 object.insert(
1146 std::string::String::from("extension"),
1147 super::super::codec::Value::Array(items),
1148 );
1149 }
1150 Ok(Some(super::super::codec::Value::Object(object)))
1151 }
1152
1153 fn has_value(&self) -> bool {
1154 self.value.is_some()
1155 }
1156
1157 fn has_element(&self) -> bool {
1158 self.id.is_some() || !self.extension.is_empty()
1159 }
1160
1161 fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
1162 match self.value.as_ref() {
1163 Some(v) => serde::Serializer::serialize_str(serializer, v),
1164 None => serde::Serializer::serialize_none(serializer),
1165 }
1166 }
1167
1168 fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
1169 if self.id.is_none() && self.extension.is_empty() {
1170 return serde::Serializer::serialize_none(serializer);
1171 }
1172 let mut map = serde::Serializer::serialize_map(serializer, None)?;
1173 if !self.extension.is_empty() {
1174 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
1175 }
1176 if let Some(id) = &self.id {
1177 serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
1178 }
1179 serde::ser::SerializeMap::end(map)
1180 }
1181
1182 fn from_json_parts(
1183 value: Option<&super::super::codec::Value>,
1184 element: Option<&super::super::codec::Value>,
1185 path: &mut super::super::codec::Path,
1186 ) -> Result<Self, super::super::codec::DecodeError> {
1187 if value.is_none() && element.is_none() && !path.is_lenient() {
1188 return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
1189 }
1190 let value = match value {
1191 Some(value) => {
1192 let value = super::super::codec::expect_single(value, path)?;
1193 Some(checked_date_time(
1194 super::super::codec::expect_string(value, path)?,
1195 path,
1196 )?)
1197 }
1198 None => None,
1199 };
1200 let mut id = None;
1201 let mut extension = Vec::new();
1202 if let Some(element) = element {
1203 let object = super::super::codec::expect_object(element, path)?;
1204 for (key, item) in object {
1205 match key.as_str() {
1206 "id" => {
1207 id = Some(path.with("id", |path| {
1208 super::primitives::checked_string(
1209 super::super::codec::expect_string(item, path)?,
1210 path,
1211 )
1212 })?);
1213 }
1214 "extension" => {
1215 for (index, entry) in super::super::codec::expect_array(item, path)?
1216 .iter()
1217 .enumerate()
1218 {
1219 extension.push(path.with_index("extension", index, |path| {
1220 super::super::codec::Json::from_json(
1221 super::super::codec::expect_object(entry, path)?,
1222 path,
1223 )
1224 })?);
1225 }
1226 }
1227 _ => {
1228 return path.with(key, |path| {
1229 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
1230 });
1231 }
1232 }
1233 }
1234 }
1235 Ok(Self {
1236 id,
1237 extension,
1238 value,
1239 })
1240 }
1241}
1242
1243#[derive(Debug, Clone, Default, PartialEq, Eq)]
1247pub struct Decimal {
1248 pub id: Option<std::string::String>,
1252 pub extension: Vec<super::extension::Extension>,
1261 pub value: Option<std::string::String>,
1265}
1266impl From<std::string::String> for Decimal {
1267 fn from(value: std::string::String) -> Self {
1268 Self {
1269 value: Some(value),
1270 ..Self::default()
1271 }
1272 }
1273}
1274impl From<&str> for Decimal {
1275 fn from(value: &str) -> Self {
1276 Self::from(std::string::String::from(value))
1277 }
1278}
1279
1280impl super::super::codec::Primitive for Decimal {
1281 fn value_json(
1282 &self,
1283 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
1284 self.value
1285 .as_ref()
1286 .map(|text| {
1287 text.parse::<super::super::codec::Number>()
1288 .map(super::super::codec::Value::Number)
1289 .map_err(|_| super::super::codec::EncodeError::BadDecimal {
1290 text: text.clone(),
1291 })
1292 })
1293 .transpose()
1294 }
1295
1296 fn element_json(
1297 &self,
1298 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
1299 if self.id.is_none() && self.extension.is_empty() {
1300 return Ok(None);
1301 }
1302 let mut object = super::super::codec::Object::new();
1303 if let Some(id) = &self.id {
1304 object.insert(
1305 std::string::String::from("id"),
1306 super::super::codec::Value::String(id.clone()),
1307 );
1308 }
1309 if !self.extension.is_empty() {
1310 let mut items = Vec::with_capacity(self.extension.len());
1311 for item in &self.extension {
1312 items.push(super::super::codec::Value::Object(
1313 super::super::codec::Json::to_json(item)?,
1314 ));
1315 }
1316 object.insert(
1317 std::string::String::from("extension"),
1318 super::super::codec::Value::Array(items),
1319 );
1320 }
1321 Ok(Some(super::super::codec::Value::Object(object)))
1322 }
1323
1324 fn has_value(&self) -> bool {
1325 self.value.is_some()
1326 }
1327
1328 fn has_element(&self) -> bool {
1329 self.id.is_some() || !self.extension.is_empty()
1330 }
1331
1332 fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
1333 match self.value.as_ref() {
1334 Some(text) => serde::Serialize::serialize(
1335 &text.parse::<super::super::codec::Number>().map_err(|_| {
1336 <S::Error as serde::ser::Error>::custom(
1337 super::super::codec::EncodeError::BadDecimal { text: text.clone() },
1338 )
1339 })?,
1340 serializer,
1341 ),
1342 None => serde::Serializer::serialize_none(serializer),
1343 }
1344 }
1345
1346 fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
1347 if self.id.is_none() && self.extension.is_empty() {
1348 return serde::Serializer::serialize_none(serializer);
1349 }
1350 let mut map = serde::Serializer::serialize_map(serializer, None)?;
1351 if !self.extension.is_empty() {
1352 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
1353 }
1354 if let Some(id) = &self.id {
1355 serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
1356 }
1357 serde::ser::SerializeMap::end(map)
1358 }
1359
1360 fn from_json_parts(
1361 value: Option<&super::super::codec::Value>,
1362 element: Option<&super::super::codec::Value>,
1363 path: &mut super::super::codec::Path,
1364 ) -> Result<Self, super::super::codec::DecodeError> {
1365 if value.is_none() && element.is_none() && !path.is_lenient() {
1366 return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
1367 }
1368 let value = match value {
1369 Some(value) => {
1370 let value = super::super::codec::expect_single(value, path)?;
1371 Some(super::super::codec::expect_decimal(value, path)?)
1372 }
1373 None => None,
1374 };
1375 let mut id = None;
1376 let mut extension = Vec::new();
1377 if let Some(element) = element {
1378 let object = super::super::codec::expect_object(element, path)?;
1379 for (key, item) in object {
1380 match key.as_str() {
1381 "id" => {
1382 id = Some(path.with("id", |path| {
1383 super::primitives::checked_string(
1384 super::super::codec::expect_string(item, path)?,
1385 path,
1386 )
1387 })?);
1388 }
1389 "extension" => {
1390 for (index, entry) in super::super::codec::expect_array(item, path)?
1391 .iter()
1392 .enumerate()
1393 {
1394 extension.push(path.with_index("extension", index, |path| {
1395 super::super::codec::Json::from_json(
1396 super::super::codec::expect_object(entry, path)?,
1397 path,
1398 )
1399 })?);
1400 }
1401 }
1402 _ => {
1403 return path.with(key, |path| {
1404 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
1405 });
1406 }
1407 }
1408 }
1409 }
1410 Ok(Self {
1411 id,
1412 extension,
1413 value,
1414 })
1415 }
1416}
1417
1418#[derive(Debug, Clone, Default, PartialEq, Eq)]
1424pub struct Id {
1425 pub id: Option<std::string::String>,
1429 pub extension: Vec<super::extension::Extension>,
1438 pub value: Option<std::string::String>,
1440}
1441impl From<std::string::String> for Id {
1442 fn from(value: std::string::String) -> Self {
1443 Self {
1444 value: Some(value),
1445 ..Self::default()
1446 }
1447 }
1448}
1449impl From<&str> for Id {
1450 fn from(value: &str) -> Self {
1451 Self::from(std::string::String::from(value))
1452 }
1453}
1454
1455#[expect(
1461 clippy::expect_used,
1462 reason = "the emitter compiles every lexical form it writes, so the pattern holds"
1463)]
1464static ID_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> = std::sync::LazyLock::new(|| {
1465 regex::bytes::RegexBuilder::new(r"^(?:[A-Za-z0-9\-\.]{1,64})$")
1466 .unicode(false)
1467 .build()
1468 .expect("the lexical form of `id` should compile")
1469});
1470
1471pub(crate) fn checked_id(
1477 text: std::string::String,
1478 path: &super::super::codec::Path,
1479) -> Result<std::string::String, super::super::codec::DecodeError> {
1480 if ID_LEXICAL_FORM.is_match(text.as_bytes()) {
1481 Ok(text)
1482 } else {
1483 Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
1484 }
1485}
1486
1487impl Id {
1488 pub fn check(text: &str) -> Result<(), super::super::codec::LexicalError> {
1494 if ID_LEXICAL_FORM.is_match(text.as_bytes()) {
1495 Ok(())
1496 } else {
1497 Err(super::super::codec::LexicalError { type_code: "id" })
1498 }
1499 }
1500}
1501
1502impl super::super::codec::Primitive for Id {
1503 fn value_json(
1504 &self,
1505 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
1506 Ok(self
1507 .value
1508 .as_ref()
1509 .map(|v| super::super::codec::Value::String(v.clone())))
1510 }
1511
1512 fn element_json(
1513 &self,
1514 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
1515 if self.id.is_none() && self.extension.is_empty() {
1516 return Ok(None);
1517 }
1518 let mut object = super::super::codec::Object::new();
1519 if let Some(id) = &self.id {
1520 object.insert(
1521 std::string::String::from("id"),
1522 super::super::codec::Value::String(id.clone()),
1523 );
1524 }
1525 if !self.extension.is_empty() {
1526 let mut items = Vec::with_capacity(self.extension.len());
1527 for item in &self.extension {
1528 items.push(super::super::codec::Value::Object(
1529 super::super::codec::Json::to_json(item)?,
1530 ));
1531 }
1532 object.insert(
1533 std::string::String::from("extension"),
1534 super::super::codec::Value::Array(items),
1535 );
1536 }
1537 Ok(Some(super::super::codec::Value::Object(object)))
1538 }
1539
1540 fn has_value(&self) -> bool {
1541 self.value.is_some()
1542 }
1543
1544 fn has_element(&self) -> bool {
1545 self.id.is_some() || !self.extension.is_empty()
1546 }
1547
1548 fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
1549 match self.value.as_ref() {
1550 Some(v) => serde::Serializer::serialize_str(serializer, v),
1551 None => serde::Serializer::serialize_none(serializer),
1552 }
1553 }
1554
1555 fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
1556 if self.id.is_none() && self.extension.is_empty() {
1557 return serde::Serializer::serialize_none(serializer);
1558 }
1559 let mut map = serde::Serializer::serialize_map(serializer, None)?;
1560 if !self.extension.is_empty() {
1561 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
1562 }
1563 if let Some(id) = &self.id {
1564 serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
1565 }
1566 serde::ser::SerializeMap::end(map)
1567 }
1568
1569 fn from_json_parts(
1570 value: Option<&super::super::codec::Value>,
1571 element: Option<&super::super::codec::Value>,
1572 path: &mut super::super::codec::Path,
1573 ) -> Result<Self, super::super::codec::DecodeError> {
1574 if value.is_none() && element.is_none() && !path.is_lenient() {
1575 return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
1576 }
1577 let value = match value {
1578 Some(value) => {
1579 let value = super::super::codec::expect_single(value, path)?;
1580 Some(checked_id(
1581 super::super::codec::expect_string(value, path)?,
1582 path,
1583 )?)
1584 }
1585 None => None,
1586 };
1587 let mut id = None;
1588 let mut extension = Vec::new();
1589 if let Some(element) = element {
1590 let object = super::super::codec::expect_object(element, path)?;
1591 for (key, item) in object {
1592 match key.as_str() {
1593 "id" => {
1594 id = Some(path.with("id", |path| {
1595 super::primitives::checked_string(
1596 super::super::codec::expect_string(item, path)?,
1597 path,
1598 )
1599 })?);
1600 }
1601 "extension" => {
1602 for (index, entry) in super::super::codec::expect_array(item, path)?
1603 .iter()
1604 .enumerate()
1605 {
1606 extension.push(path.with_index("extension", index, |path| {
1607 super::super::codec::Json::from_json(
1608 super::super::codec::expect_object(entry, path)?,
1609 path,
1610 )
1611 })?);
1612 }
1613 }
1614 _ => {
1615 return path.with(key, |path| {
1616 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
1617 });
1618 }
1619 }
1620 }
1621 }
1622 Ok(Self {
1623 id,
1624 extension,
1625 value,
1626 })
1627 }
1628}
1629
1630#[derive(Debug, Clone, Default, PartialEq, Eq)]
1634pub struct Instant {
1635 pub id: Option<std::string::String>,
1639 pub extension: Vec<super::extension::Extension>,
1648 pub value: Option<std::string::String>,
1652}
1653impl From<std::string::String> for Instant {
1654 fn from(value: std::string::String) -> Self {
1655 Self {
1656 value: Some(value),
1657 ..Self::default()
1658 }
1659 }
1660}
1661impl From<&str> for Instant {
1662 fn from(value: &str) -> Self {
1663 Self::from(std::string::String::from(value))
1664 }
1665}
1666
1667#[expect(
1673 clippy::expect_used,
1674 reason = "the emitter compiles every lexical form it writes, so the pattern holds"
1675)]
1676static INSTANT_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> = std::sync::LazyLock::new(
1677 || {
1678 regex::bytes::RegexBuilder::new(r"^(?:([0-9]([0-9]([0-9][1-9]|[1-9]0)|[1-9]00)|[1-9]000)-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])T([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\.[0-9]{1,9})?(Z|(\+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00)))$")
1679 .unicode(false)
1680 .build()
1681 .expect("the lexical form of `instant` should compile")
1682 },
1683);
1684
1685pub(crate) fn checked_instant(
1691 text: std::string::String,
1692 path: &super::super::codec::Path,
1693) -> Result<std::string::String, super::super::codec::DecodeError> {
1694 if INSTANT_LEXICAL_FORM.is_match(text.as_bytes()) {
1695 Ok(text)
1696 } else {
1697 Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
1698 }
1699}
1700
1701impl Instant {
1702 pub fn check(text: &str) -> Result<(), super::super::codec::LexicalError> {
1708 if INSTANT_LEXICAL_FORM.is_match(text.as_bytes()) {
1709 Ok(())
1710 } else {
1711 Err(super::super::codec::LexicalError {
1712 type_code: "instant",
1713 })
1714 }
1715 }
1716}
1717
1718impl super::super::codec::Primitive for Instant {
1719 fn value_json(
1720 &self,
1721 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
1722 Ok(self
1723 .value
1724 .as_ref()
1725 .map(|v| super::super::codec::Value::String(v.clone())))
1726 }
1727
1728 fn element_json(
1729 &self,
1730 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
1731 if self.id.is_none() && self.extension.is_empty() {
1732 return Ok(None);
1733 }
1734 let mut object = super::super::codec::Object::new();
1735 if let Some(id) = &self.id {
1736 object.insert(
1737 std::string::String::from("id"),
1738 super::super::codec::Value::String(id.clone()),
1739 );
1740 }
1741 if !self.extension.is_empty() {
1742 let mut items = Vec::with_capacity(self.extension.len());
1743 for item in &self.extension {
1744 items.push(super::super::codec::Value::Object(
1745 super::super::codec::Json::to_json(item)?,
1746 ));
1747 }
1748 object.insert(
1749 std::string::String::from("extension"),
1750 super::super::codec::Value::Array(items),
1751 );
1752 }
1753 Ok(Some(super::super::codec::Value::Object(object)))
1754 }
1755
1756 fn has_value(&self) -> bool {
1757 self.value.is_some()
1758 }
1759
1760 fn has_element(&self) -> bool {
1761 self.id.is_some() || !self.extension.is_empty()
1762 }
1763
1764 fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
1765 match self.value.as_ref() {
1766 Some(v) => serde::Serializer::serialize_str(serializer, v),
1767 None => serde::Serializer::serialize_none(serializer),
1768 }
1769 }
1770
1771 fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
1772 if self.id.is_none() && self.extension.is_empty() {
1773 return serde::Serializer::serialize_none(serializer);
1774 }
1775 let mut map = serde::Serializer::serialize_map(serializer, None)?;
1776 if !self.extension.is_empty() {
1777 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
1778 }
1779 if let Some(id) = &self.id {
1780 serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
1781 }
1782 serde::ser::SerializeMap::end(map)
1783 }
1784
1785 fn from_json_parts(
1786 value: Option<&super::super::codec::Value>,
1787 element: Option<&super::super::codec::Value>,
1788 path: &mut super::super::codec::Path,
1789 ) -> Result<Self, super::super::codec::DecodeError> {
1790 if value.is_none() && element.is_none() && !path.is_lenient() {
1791 return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
1792 }
1793 let value = match value {
1794 Some(value) => {
1795 let value = super::super::codec::expect_single(value, path)?;
1796 Some(checked_instant(
1797 super::super::codec::expect_string(value, path)?,
1798 path,
1799 )?)
1800 }
1801 None => None,
1802 };
1803 let mut id = None;
1804 let mut extension = Vec::new();
1805 if let Some(element) = element {
1806 let object = super::super::codec::expect_object(element, path)?;
1807 for (key, item) in object {
1808 match key.as_str() {
1809 "id" => {
1810 id = Some(path.with("id", |path| {
1811 super::primitives::checked_string(
1812 super::super::codec::expect_string(item, path)?,
1813 path,
1814 )
1815 })?);
1816 }
1817 "extension" => {
1818 for (index, entry) in super::super::codec::expect_array(item, path)?
1819 .iter()
1820 .enumerate()
1821 {
1822 extension.push(path.with_index("extension", index, |path| {
1823 super::super::codec::Json::from_json(
1824 super::super::codec::expect_object(entry, path)?,
1825 path,
1826 )
1827 })?);
1828 }
1829 }
1830 _ => {
1831 return path.with(key, |path| {
1832 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
1833 });
1834 }
1835 }
1836 }
1837 }
1838 Ok(Self {
1839 id,
1840 extension,
1841 value,
1842 })
1843 }
1844}
1845
1846#[derive(Debug, Clone, Default, PartialEq, Eq)]
1850pub struct Integer {
1851 pub id: Option<std::string::String>,
1855 pub extension: Vec<super::extension::Extension>,
1864 pub value: Option<i32>,
1868}
1869impl From<i32> for Integer {
1870 fn from(value: i32) -> Self {
1871 Self {
1872 value: Some(value),
1873 ..Self::default()
1874 }
1875 }
1876}
1877
1878impl super::super::codec::Primitive for Integer {
1879 fn value_json(
1880 &self,
1881 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
1882 Ok(self
1883 .value
1884 .as_ref()
1885 .map(|v| super::super::codec::Value::from(*v)))
1886 }
1887
1888 fn element_json(
1889 &self,
1890 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
1891 if self.id.is_none() && self.extension.is_empty() {
1892 return Ok(None);
1893 }
1894 let mut object = super::super::codec::Object::new();
1895 if let Some(id) = &self.id {
1896 object.insert(
1897 std::string::String::from("id"),
1898 super::super::codec::Value::String(id.clone()),
1899 );
1900 }
1901 if !self.extension.is_empty() {
1902 let mut items = Vec::with_capacity(self.extension.len());
1903 for item in &self.extension {
1904 items.push(super::super::codec::Value::Object(
1905 super::super::codec::Json::to_json(item)?,
1906 ));
1907 }
1908 object.insert(
1909 std::string::String::from("extension"),
1910 super::super::codec::Value::Array(items),
1911 );
1912 }
1913 Ok(Some(super::super::codec::Value::Object(object)))
1914 }
1915
1916 fn has_value(&self) -> bool {
1917 self.value.is_some()
1918 }
1919
1920 fn has_element(&self) -> bool {
1921 self.id.is_some() || !self.extension.is_empty()
1922 }
1923
1924 fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
1925 match self.value.as_ref() {
1926 Some(v) => serde::Serializer::serialize_i32(serializer, *v),
1927 None => serde::Serializer::serialize_none(serializer),
1928 }
1929 }
1930
1931 fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
1932 if self.id.is_none() && self.extension.is_empty() {
1933 return serde::Serializer::serialize_none(serializer);
1934 }
1935 let mut map = serde::Serializer::serialize_map(serializer, None)?;
1936 if !self.extension.is_empty() {
1937 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
1938 }
1939 if let Some(id) = &self.id {
1940 serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
1941 }
1942 serde::ser::SerializeMap::end(map)
1943 }
1944
1945 fn from_json_parts(
1946 value: Option<&super::super::codec::Value>,
1947 element: Option<&super::super::codec::Value>,
1948 path: &mut super::super::codec::Path,
1949 ) -> Result<Self, super::super::codec::DecodeError> {
1950 if value.is_none() && element.is_none() && !path.is_lenient() {
1951 return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
1952 }
1953 let value = match value {
1954 Some(value) => {
1955 let value = super::super::codec::expect_single(value, path)?;
1956 Some(super::super::codec::expect_i32(value, path)?)
1957 }
1958 None => None,
1959 };
1960 let mut id = None;
1961 let mut extension = Vec::new();
1962 if let Some(element) = element {
1963 let object = super::super::codec::expect_object(element, path)?;
1964 for (key, item) in object {
1965 match key.as_str() {
1966 "id" => {
1967 id = Some(path.with("id", |path| {
1968 super::primitives::checked_string(
1969 super::super::codec::expect_string(item, path)?,
1970 path,
1971 )
1972 })?);
1973 }
1974 "extension" => {
1975 for (index, entry) in super::super::codec::expect_array(item, path)?
1976 .iter()
1977 .enumerate()
1978 {
1979 extension.push(path.with_index("extension", index, |path| {
1980 super::super::codec::Json::from_json(
1981 super::super::codec::expect_object(entry, path)?,
1982 path,
1983 )
1984 })?);
1985 }
1986 }
1987 _ => {
1988 return path.with(key, |path| {
1989 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
1990 });
1991 }
1992 }
1993 }
1994 }
1995 Ok(Self {
1996 id,
1997 extension,
1998 value,
1999 })
2000 }
2001}
2002
2003#[derive(Debug, Clone, Default, PartialEq, Eq)]
2007pub struct Integer64 {
2008 pub id: Option<std::string::String>,
2012 pub extension: Vec<super::extension::Extension>,
2021 pub value: Option<i64>,
2025}
2026impl From<i64> for Integer64 {
2027 fn from(value: i64) -> Self {
2028 Self {
2029 value: Some(value),
2030 ..Self::default()
2031 }
2032 }
2033}
2034
2035impl super::super::codec::Primitive for Integer64 {
2036 fn value_json(
2037 &self,
2038 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
2039 Ok(self
2040 .value
2041 .as_ref()
2042 .map(|v| super::super::codec::Value::String(v.to_string())))
2043 }
2044
2045 fn element_json(
2046 &self,
2047 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
2048 if self.id.is_none() && self.extension.is_empty() {
2049 return Ok(None);
2050 }
2051 let mut object = super::super::codec::Object::new();
2052 if let Some(id) = &self.id {
2053 object.insert(
2054 std::string::String::from("id"),
2055 super::super::codec::Value::String(id.clone()),
2056 );
2057 }
2058 if !self.extension.is_empty() {
2059 let mut items = Vec::with_capacity(self.extension.len());
2060 for item in &self.extension {
2061 items.push(super::super::codec::Value::Object(
2062 super::super::codec::Json::to_json(item)?,
2063 ));
2064 }
2065 object.insert(
2066 std::string::String::from("extension"),
2067 super::super::codec::Value::Array(items),
2068 );
2069 }
2070 Ok(Some(super::super::codec::Value::Object(object)))
2071 }
2072
2073 fn has_value(&self) -> bool {
2074 self.value.is_some()
2075 }
2076
2077 fn has_element(&self) -> bool {
2078 self.id.is_some() || !self.extension.is_empty()
2079 }
2080
2081 fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
2082 match self.value.as_ref() {
2083 Some(v) => serde::Serializer::serialize_str(serializer, &v.to_string()),
2084 None => serde::Serializer::serialize_none(serializer),
2085 }
2086 }
2087
2088 fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
2089 if self.id.is_none() && self.extension.is_empty() {
2090 return serde::Serializer::serialize_none(serializer);
2091 }
2092 let mut map = serde::Serializer::serialize_map(serializer, None)?;
2093 if !self.extension.is_empty() {
2094 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
2095 }
2096 if let Some(id) = &self.id {
2097 serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
2098 }
2099 serde::ser::SerializeMap::end(map)
2100 }
2101
2102 fn from_json_parts(
2103 value: Option<&super::super::codec::Value>,
2104 element: Option<&super::super::codec::Value>,
2105 path: &mut super::super::codec::Path,
2106 ) -> Result<Self, super::super::codec::DecodeError> {
2107 if value.is_none() && element.is_none() && !path.is_lenient() {
2108 return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
2109 }
2110 let value = match value {
2111 Some(value) => {
2112 let value = super::super::codec::expect_single(value, path)?;
2113 Some(super::super::codec::expect_i64_string(value, path)?)
2114 }
2115 None => None,
2116 };
2117 let mut id = None;
2118 let mut extension = Vec::new();
2119 if let Some(element) = element {
2120 let object = super::super::codec::expect_object(element, path)?;
2121 for (key, item) in object {
2122 match key.as_str() {
2123 "id" => {
2124 id = Some(path.with("id", |path| {
2125 super::primitives::checked_string(
2126 super::super::codec::expect_string(item, path)?,
2127 path,
2128 )
2129 })?);
2130 }
2131 "extension" => {
2132 for (index, entry) in super::super::codec::expect_array(item, path)?
2133 .iter()
2134 .enumerate()
2135 {
2136 extension.push(path.with_index("extension", index, |path| {
2137 super::super::codec::Json::from_json(
2138 super::super::codec::expect_object(entry, path)?,
2139 path,
2140 )
2141 })?);
2142 }
2143 }
2144 _ => {
2145 return path.with(key, |path| {
2146 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
2147 });
2148 }
2149 }
2150 }
2151 }
2152 Ok(Self {
2153 id,
2154 extension,
2155 value,
2156 })
2157 }
2158}
2159
2160#[derive(Debug, Clone, Default, PartialEq, Eq)]
2165pub struct Markdown {
2166 pub id: Option<std::string::String>,
2170 pub extension: Vec<super::extension::Extension>,
2179 pub value: Option<std::string::String>,
2181}
2182impl From<std::string::String> for Markdown {
2183 fn from(value: std::string::String) -> Self {
2184 Self {
2185 value: Some(value),
2186 ..Self::default()
2187 }
2188 }
2189}
2190impl From<&str> for Markdown {
2191 fn from(value: &str) -> Self {
2192 Self::from(std::string::String::from(value))
2193 }
2194}
2195
2196#[expect(
2202 clippy::expect_used,
2203 reason = "the emitter compiles every lexical form it writes, so the pattern holds"
2204)]
2205static MARKDOWN_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> =
2206 std::sync::LazyLock::new(|| {
2207 regex::bytes::RegexBuilder::new(r"^(?:^[\s\S]+$)$")
2208 .unicode(false)
2209 .build()
2210 .expect("the lexical form of `markdown` should compile")
2211 });
2212
2213pub(crate) fn checked_markdown(
2219 text: std::string::String,
2220 path: &super::super::codec::Path,
2221) -> Result<std::string::String, super::super::codec::DecodeError> {
2222 if MARKDOWN_LEXICAL_FORM.is_match(text.as_bytes()) {
2223 Ok(text)
2224 } else {
2225 Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
2226 }
2227}
2228
2229impl Markdown {
2230 pub fn check(text: &str) -> Result<(), super::super::codec::LexicalError> {
2236 if MARKDOWN_LEXICAL_FORM.is_match(text.as_bytes()) {
2237 Ok(())
2238 } else {
2239 Err(super::super::codec::LexicalError {
2240 type_code: "markdown",
2241 })
2242 }
2243 }
2244}
2245
2246impl super::super::codec::Primitive for Markdown {
2247 fn value_json(
2248 &self,
2249 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
2250 Ok(self
2251 .value
2252 .as_ref()
2253 .map(|v| super::super::codec::Value::String(v.clone())))
2254 }
2255
2256 fn element_json(
2257 &self,
2258 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
2259 if self.id.is_none() && self.extension.is_empty() {
2260 return Ok(None);
2261 }
2262 let mut object = super::super::codec::Object::new();
2263 if let Some(id) = &self.id {
2264 object.insert(
2265 std::string::String::from("id"),
2266 super::super::codec::Value::String(id.clone()),
2267 );
2268 }
2269 if !self.extension.is_empty() {
2270 let mut items = Vec::with_capacity(self.extension.len());
2271 for item in &self.extension {
2272 items.push(super::super::codec::Value::Object(
2273 super::super::codec::Json::to_json(item)?,
2274 ));
2275 }
2276 object.insert(
2277 std::string::String::from("extension"),
2278 super::super::codec::Value::Array(items),
2279 );
2280 }
2281 Ok(Some(super::super::codec::Value::Object(object)))
2282 }
2283
2284 fn has_value(&self) -> bool {
2285 self.value.is_some()
2286 }
2287
2288 fn has_element(&self) -> bool {
2289 self.id.is_some() || !self.extension.is_empty()
2290 }
2291
2292 fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
2293 match self.value.as_ref() {
2294 Some(v) => serde::Serializer::serialize_str(serializer, v),
2295 None => serde::Serializer::serialize_none(serializer),
2296 }
2297 }
2298
2299 fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
2300 if self.id.is_none() && self.extension.is_empty() {
2301 return serde::Serializer::serialize_none(serializer);
2302 }
2303 let mut map = serde::Serializer::serialize_map(serializer, None)?;
2304 if !self.extension.is_empty() {
2305 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
2306 }
2307 if let Some(id) = &self.id {
2308 serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
2309 }
2310 serde::ser::SerializeMap::end(map)
2311 }
2312
2313 fn from_json_parts(
2314 value: Option<&super::super::codec::Value>,
2315 element: Option<&super::super::codec::Value>,
2316 path: &mut super::super::codec::Path,
2317 ) -> Result<Self, super::super::codec::DecodeError> {
2318 if value.is_none() && element.is_none() && !path.is_lenient() {
2319 return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
2320 }
2321 let value = match value {
2322 Some(value) => {
2323 let value = super::super::codec::expect_single(value, path)?;
2324 Some(checked_markdown(
2325 super::super::codec::expect_string(value, path)?,
2326 path,
2327 )?)
2328 }
2329 None => None,
2330 };
2331 let mut id = None;
2332 let mut extension = Vec::new();
2333 if let Some(element) = element {
2334 let object = super::super::codec::expect_object(element, path)?;
2335 for (key, item) in object {
2336 match key.as_str() {
2337 "id" => {
2338 id = Some(path.with("id", |path| {
2339 super::primitives::checked_string(
2340 super::super::codec::expect_string(item, path)?,
2341 path,
2342 )
2343 })?);
2344 }
2345 "extension" => {
2346 for (index, entry) in super::super::codec::expect_array(item, path)?
2347 .iter()
2348 .enumerate()
2349 {
2350 extension.push(path.with_index("extension", index, |path| {
2351 super::super::codec::Json::from_json(
2352 super::super::codec::expect_object(entry, path)?,
2353 path,
2354 )
2355 })?);
2356 }
2357 }
2358 _ => {
2359 return path.with(key, |path| {
2360 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
2361 });
2362 }
2363 }
2364 }
2365 }
2366 Ok(Self {
2367 id,
2368 extension,
2369 value,
2370 })
2371 }
2372}
2373
2374#[derive(Debug, Clone, Default, PartialEq, Eq)]
2378pub struct Oid {
2379 pub id: Option<std::string::String>,
2383 pub extension: Vec<super::extension::Extension>,
2392 pub value: Option<std::string::String>,
2394}
2395impl From<std::string::String> for Oid {
2396 fn from(value: std::string::String) -> Self {
2397 Self {
2398 value: Some(value),
2399 ..Self::default()
2400 }
2401 }
2402}
2403impl From<&str> for Oid {
2404 fn from(value: &str) -> Self {
2405 Self::from(std::string::String::from(value))
2406 }
2407}
2408
2409#[expect(
2415 clippy::expect_used,
2416 reason = "the emitter compiles every lexical form it writes, so the pattern holds"
2417)]
2418static OID_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> =
2419 std::sync::LazyLock::new(|| {
2420 regex::bytes::RegexBuilder::new(r"^(?:urn:oid:[0-2](\.(0|[1-9][0-9]*))+)$")
2421 .unicode(false)
2422 .build()
2423 .expect("the lexical form of `oid` should compile")
2424 });
2425
2426pub(crate) fn checked_oid(
2432 text: std::string::String,
2433 path: &super::super::codec::Path,
2434) -> Result<std::string::String, super::super::codec::DecodeError> {
2435 if OID_LEXICAL_FORM.is_match(text.as_bytes()) {
2436 Ok(text)
2437 } else {
2438 Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
2439 }
2440}
2441
2442impl Oid {
2443 pub fn check(text: &str) -> Result<(), super::super::codec::LexicalError> {
2449 if OID_LEXICAL_FORM.is_match(text.as_bytes()) {
2450 Ok(())
2451 } else {
2452 Err(super::super::codec::LexicalError { type_code: "oid" })
2453 }
2454 }
2455}
2456
2457impl super::super::codec::Primitive for Oid {
2458 fn value_json(
2459 &self,
2460 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
2461 Ok(self
2462 .value
2463 .as_ref()
2464 .map(|v| super::super::codec::Value::String(v.clone())))
2465 }
2466
2467 fn element_json(
2468 &self,
2469 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
2470 if self.id.is_none() && self.extension.is_empty() {
2471 return Ok(None);
2472 }
2473 let mut object = super::super::codec::Object::new();
2474 if let Some(id) = &self.id {
2475 object.insert(
2476 std::string::String::from("id"),
2477 super::super::codec::Value::String(id.clone()),
2478 );
2479 }
2480 if !self.extension.is_empty() {
2481 let mut items = Vec::with_capacity(self.extension.len());
2482 for item in &self.extension {
2483 items.push(super::super::codec::Value::Object(
2484 super::super::codec::Json::to_json(item)?,
2485 ));
2486 }
2487 object.insert(
2488 std::string::String::from("extension"),
2489 super::super::codec::Value::Array(items),
2490 );
2491 }
2492 Ok(Some(super::super::codec::Value::Object(object)))
2493 }
2494
2495 fn has_value(&self) -> bool {
2496 self.value.is_some()
2497 }
2498
2499 fn has_element(&self) -> bool {
2500 self.id.is_some() || !self.extension.is_empty()
2501 }
2502
2503 fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
2504 match self.value.as_ref() {
2505 Some(v) => serde::Serializer::serialize_str(serializer, v),
2506 None => serde::Serializer::serialize_none(serializer),
2507 }
2508 }
2509
2510 fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
2511 if self.id.is_none() && self.extension.is_empty() {
2512 return serde::Serializer::serialize_none(serializer);
2513 }
2514 let mut map = serde::Serializer::serialize_map(serializer, None)?;
2515 if !self.extension.is_empty() {
2516 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
2517 }
2518 if let Some(id) = &self.id {
2519 serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
2520 }
2521 serde::ser::SerializeMap::end(map)
2522 }
2523
2524 fn from_json_parts(
2525 value: Option<&super::super::codec::Value>,
2526 element: Option<&super::super::codec::Value>,
2527 path: &mut super::super::codec::Path,
2528 ) -> Result<Self, super::super::codec::DecodeError> {
2529 if value.is_none() && element.is_none() && !path.is_lenient() {
2530 return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
2531 }
2532 let value = match value {
2533 Some(value) => {
2534 let value = super::super::codec::expect_single(value, path)?;
2535 Some(checked_oid(
2536 super::super::codec::expect_string(value, path)?,
2537 path,
2538 )?)
2539 }
2540 None => None,
2541 };
2542 let mut id = None;
2543 let mut extension = Vec::new();
2544 if let Some(element) = element {
2545 let object = super::super::codec::expect_object(element, path)?;
2546 for (key, item) in object {
2547 match key.as_str() {
2548 "id" => {
2549 id = Some(path.with("id", |path| {
2550 super::primitives::checked_string(
2551 super::super::codec::expect_string(item, path)?,
2552 path,
2553 )
2554 })?);
2555 }
2556 "extension" => {
2557 for (index, entry) in super::super::codec::expect_array(item, path)?
2558 .iter()
2559 .enumerate()
2560 {
2561 extension.push(path.with_index("extension", index, |path| {
2562 super::super::codec::Json::from_json(
2563 super::super::codec::expect_object(entry, path)?,
2564 path,
2565 )
2566 })?);
2567 }
2568 }
2569 _ => {
2570 return path.with(key, |path| {
2571 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
2572 });
2573 }
2574 }
2575 }
2576 }
2577 Ok(Self {
2578 id,
2579 extension,
2580 value,
2581 })
2582 }
2583}
2584
2585#[derive(Debug, Clone, Default, PartialEq, Eq)]
2589pub struct PositiveInt {
2590 pub id: Option<std::string::String>,
2594 pub extension: Vec<super::extension::Extension>,
2603 pub value: Option<u32>,
2605}
2606impl From<u32> for PositiveInt {
2607 fn from(value: u32) -> Self {
2608 Self {
2609 value: Some(value),
2610 ..Self::default()
2611 }
2612 }
2613}
2614
2615impl super::super::codec::Primitive for PositiveInt {
2616 fn value_json(
2617 &self,
2618 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
2619 Ok(self
2620 .value
2621 .as_ref()
2622 .map(|v| super::super::codec::Value::from(*v)))
2623 }
2624
2625 fn element_json(
2626 &self,
2627 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
2628 if self.id.is_none() && self.extension.is_empty() {
2629 return Ok(None);
2630 }
2631 let mut object = super::super::codec::Object::new();
2632 if let Some(id) = &self.id {
2633 object.insert(
2634 std::string::String::from("id"),
2635 super::super::codec::Value::String(id.clone()),
2636 );
2637 }
2638 if !self.extension.is_empty() {
2639 let mut items = Vec::with_capacity(self.extension.len());
2640 for item in &self.extension {
2641 items.push(super::super::codec::Value::Object(
2642 super::super::codec::Json::to_json(item)?,
2643 ));
2644 }
2645 object.insert(
2646 std::string::String::from("extension"),
2647 super::super::codec::Value::Array(items),
2648 );
2649 }
2650 Ok(Some(super::super::codec::Value::Object(object)))
2651 }
2652
2653 fn has_value(&self) -> bool {
2654 self.value.is_some()
2655 }
2656
2657 fn has_element(&self) -> bool {
2658 self.id.is_some() || !self.extension.is_empty()
2659 }
2660
2661 fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
2662 match self.value.as_ref() {
2663 Some(v) => serde::Serializer::serialize_u32(serializer, *v),
2664 None => serde::Serializer::serialize_none(serializer),
2665 }
2666 }
2667
2668 fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
2669 if self.id.is_none() && self.extension.is_empty() {
2670 return serde::Serializer::serialize_none(serializer);
2671 }
2672 let mut map = serde::Serializer::serialize_map(serializer, None)?;
2673 if !self.extension.is_empty() {
2674 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
2675 }
2676 if let Some(id) = &self.id {
2677 serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
2678 }
2679 serde::ser::SerializeMap::end(map)
2680 }
2681
2682 fn from_json_parts(
2683 value: Option<&super::super::codec::Value>,
2684 element: Option<&super::super::codec::Value>,
2685 path: &mut super::super::codec::Path,
2686 ) -> Result<Self, super::super::codec::DecodeError> {
2687 if value.is_none() && element.is_none() && !path.is_lenient() {
2688 return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
2689 }
2690 let value = match value {
2691 Some(value) => {
2692 let value = super::super::codec::expect_single(value, path)?;
2693 Some(super::super::codec::expect_u32(value, 1, path)?)
2694 }
2695 None => None,
2696 };
2697 let mut id = None;
2698 let mut extension = Vec::new();
2699 if let Some(element) = element {
2700 let object = super::super::codec::expect_object(element, path)?;
2701 for (key, item) in object {
2702 match key.as_str() {
2703 "id" => {
2704 id = Some(path.with("id", |path| {
2705 super::primitives::checked_string(
2706 super::super::codec::expect_string(item, path)?,
2707 path,
2708 )
2709 })?);
2710 }
2711 "extension" => {
2712 for (index, entry) in super::super::codec::expect_array(item, path)?
2713 .iter()
2714 .enumerate()
2715 {
2716 extension.push(path.with_index("extension", index, |path| {
2717 super::super::codec::Json::from_json(
2718 super::super::codec::expect_object(entry, path)?,
2719 path,
2720 )
2721 })?);
2722 }
2723 }
2724 _ => {
2725 return path.with(key, |path| {
2726 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
2727 });
2728 }
2729 }
2730 }
2731 }
2732 Ok(Self {
2733 id,
2734 extension,
2735 value,
2736 })
2737 }
2738}
2739
2740#[derive(Debug, Clone, Default, PartialEq, Eq)]
2744pub struct String {
2745 pub id: Option<std::string::String>,
2749 pub extension: Vec<super::extension::Extension>,
2758 pub value: Option<std::string::String>,
2762}
2763impl From<std::string::String> for String {
2764 fn from(value: std::string::String) -> Self {
2765 Self {
2766 value: Some(value),
2767 ..Self::default()
2768 }
2769 }
2770}
2771impl From<&str> for String {
2772 fn from(value: &str) -> Self {
2773 Self::from(std::string::String::from(value))
2774 }
2775}
2776
2777#[expect(
2783 clippy::expect_used,
2784 reason = "the emitter compiles every lexical form it writes, so the pattern holds"
2785)]
2786static STRING_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> =
2787 std::sync::LazyLock::new(|| {
2788 regex::bytes::RegexBuilder::new(r"^(?:^[\s\S]+$)$")
2789 .unicode(false)
2790 .build()
2791 .expect("the lexical form of `string` should compile")
2792 });
2793
2794pub(crate) fn checked_string(
2800 text: std::string::String,
2801 path: &super::super::codec::Path,
2802) -> Result<std::string::String, super::super::codec::DecodeError> {
2803 if STRING_LEXICAL_FORM.is_match(text.as_bytes()) {
2804 Ok(text)
2805 } else {
2806 Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
2807 }
2808}
2809
2810impl String {
2811 pub fn check(text: &str) -> Result<(), super::super::codec::LexicalError> {
2817 if STRING_LEXICAL_FORM.is_match(text.as_bytes()) {
2818 Ok(())
2819 } else {
2820 Err(super::super::codec::LexicalError {
2821 type_code: "string",
2822 })
2823 }
2824 }
2825}
2826
2827impl super::super::codec::Primitive for String {
2828 fn value_json(
2829 &self,
2830 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
2831 Ok(self
2832 .value
2833 .as_ref()
2834 .map(|v| super::super::codec::Value::String(v.clone())))
2835 }
2836
2837 fn element_json(
2838 &self,
2839 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
2840 if self.id.is_none() && self.extension.is_empty() {
2841 return Ok(None);
2842 }
2843 let mut object = super::super::codec::Object::new();
2844 if let Some(id) = &self.id {
2845 object.insert(
2846 std::string::String::from("id"),
2847 super::super::codec::Value::String(id.clone()),
2848 );
2849 }
2850 if !self.extension.is_empty() {
2851 let mut items = Vec::with_capacity(self.extension.len());
2852 for item in &self.extension {
2853 items.push(super::super::codec::Value::Object(
2854 super::super::codec::Json::to_json(item)?,
2855 ));
2856 }
2857 object.insert(
2858 std::string::String::from("extension"),
2859 super::super::codec::Value::Array(items),
2860 );
2861 }
2862 Ok(Some(super::super::codec::Value::Object(object)))
2863 }
2864
2865 fn has_value(&self) -> bool {
2866 self.value.is_some()
2867 }
2868
2869 fn has_element(&self) -> bool {
2870 self.id.is_some() || !self.extension.is_empty()
2871 }
2872
2873 fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
2874 match self.value.as_ref() {
2875 Some(v) => serde::Serializer::serialize_str(serializer, v),
2876 None => serde::Serializer::serialize_none(serializer),
2877 }
2878 }
2879
2880 fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
2881 if self.id.is_none() && self.extension.is_empty() {
2882 return serde::Serializer::serialize_none(serializer);
2883 }
2884 let mut map = serde::Serializer::serialize_map(serializer, None)?;
2885 if !self.extension.is_empty() {
2886 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
2887 }
2888 if let Some(id) = &self.id {
2889 serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
2890 }
2891 serde::ser::SerializeMap::end(map)
2892 }
2893
2894 fn from_json_parts(
2895 value: Option<&super::super::codec::Value>,
2896 element: Option<&super::super::codec::Value>,
2897 path: &mut super::super::codec::Path,
2898 ) -> Result<Self, super::super::codec::DecodeError> {
2899 if value.is_none() && element.is_none() && !path.is_lenient() {
2900 return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
2901 }
2902 let value = match value {
2903 Some(value) => {
2904 let value = super::super::codec::expect_single(value, path)?;
2905 Some(checked_string(
2906 super::super::codec::expect_string(value, path)?,
2907 path,
2908 )?)
2909 }
2910 None => None,
2911 };
2912 let mut id = None;
2913 let mut extension = Vec::new();
2914 if let Some(element) = element {
2915 let object = super::super::codec::expect_object(element, path)?;
2916 for (key, item) in object {
2917 match key.as_str() {
2918 "id" => {
2919 id = Some(path.with("id", |path| {
2920 super::primitives::checked_string(
2921 super::super::codec::expect_string(item, path)?,
2922 path,
2923 )
2924 })?);
2925 }
2926 "extension" => {
2927 for (index, entry) in super::super::codec::expect_array(item, path)?
2928 .iter()
2929 .enumerate()
2930 {
2931 extension.push(path.with_index("extension", index, |path| {
2932 super::super::codec::Json::from_json(
2933 super::super::codec::expect_object(entry, path)?,
2934 path,
2935 )
2936 })?);
2937 }
2938 }
2939 _ => {
2940 return path.with(key, |path| {
2941 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
2942 });
2943 }
2944 }
2945 }
2946 }
2947 Ok(Self {
2948 id,
2949 extension,
2950 value,
2951 })
2952 }
2953}
2954
2955#[derive(Debug, Clone, Default, PartialEq, Eq)]
2959pub struct Time {
2960 pub id: Option<std::string::String>,
2964 pub extension: Vec<super::extension::Extension>,
2973 pub value: Option<std::string::String>,
2977}
2978impl From<std::string::String> for Time {
2979 fn from(value: std::string::String) -> Self {
2980 Self {
2981 value: Some(value),
2982 ..Self::default()
2983 }
2984 }
2985}
2986impl From<&str> for Time {
2987 fn from(value: &str) -> Self {
2988 Self::from(std::string::String::from(value))
2989 }
2990}
2991
2992#[expect(
2998 clippy::expect_used,
2999 reason = "the emitter compiles every lexical form it writes, so the pattern holds"
3000)]
3001static TIME_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> =
3002 std::sync::LazyLock::new(|| {
3003 regex::bytes::RegexBuilder::new(
3004 r"^(?:([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\.[0-9]{1,9})?)$",
3005 )
3006 .unicode(false)
3007 .build()
3008 .expect("the lexical form of `time` should compile")
3009 });
3010
3011pub(crate) fn checked_time(
3017 text: std::string::String,
3018 path: &super::super::codec::Path,
3019) -> Result<std::string::String, super::super::codec::DecodeError> {
3020 if TIME_LEXICAL_FORM.is_match(text.as_bytes()) {
3021 Ok(text)
3022 } else {
3023 Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
3024 }
3025}
3026
3027impl Time {
3028 pub fn check(text: &str) -> Result<(), super::super::codec::LexicalError> {
3034 if TIME_LEXICAL_FORM.is_match(text.as_bytes()) {
3035 Ok(())
3036 } else {
3037 Err(super::super::codec::LexicalError { type_code: "time" })
3038 }
3039 }
3040}
3041
3042impl super::super::codec::Primitive for Time {
3043 fn value_json(
3044 &self,
3045 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
3046 Ok(self
3047 .value
3048 .as_ref()
3049 .map(|v| super::super::codec::Value::String(v.clone())))
3050 }
3051
3052 fn element_json(
3053 &self,
3054 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
3055 if self.id.is_none() && self.extension.is_empty() {
3056 return Ok(None);
3057 }
3058 let mut object = super::super::codec::Object::new();
3059 if let Some(id) = &self.id {
3060 object.insert(
3061 std::string::String::from("id"),
3062 super::super::codec::Value::String(id.clone()),
3063 );
3064 }
3065 if !self.extension.is_empty() {
3066 let mut items = Vec::with_capacity(self.extension.len());
3067 for item in &self.extension {
3068 items.push(super::super::codec::Value::Object(
3069 super::super::codec::Json::to_json(item)?,
3070 ));
3071 }
3072 object.insert(
3073 std::string::String::from("extension"),
3074 super::super::codec::Value::Array(items),
3075 );
3076 }
3077 Ok(Some(super::super::codec::Value::Object(object)))
3078 }
3079
3080 fn has_value(&self) -> bool {
3081 self.value.is_some()
3082 }
3083
3084 fn has_element(&self) -> bool {
3085 self.id.is_some() || !self.extension.is_empty()
3086 }
3087
3088 fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
3089 match self.value.as_ref() {
3090 Some(v) => serde::Serializer::serialize_str(serializer, v),
3091 None => serde::Serializer::serialize_none(serializer),
3092 }
3093 }
3094
3095 fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
3096 if self.id.is_none() && self.extension.is_empty() {
3097 return serde::Serializer::serialize_none(serializer);
3098 }
3099 let mut map = serde::Serializer::serialize_map(serializer, None)?;
3100 if !self.extension.is_empty() {
3101 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
3102 }
3103 if let Some(id) = &self.id {
3104 serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
3105 }
3106 serde::ser::SerializeMap::end(map)
3107 }
3108
3109 fn from_json_parts(
3110 value: Option<&super::super::codec::Value>,
3111 element: Option<&super::super::codec::Value>,
3112 path: &mut super::super::codec::Path,
3113 ) -> Result<Self, super::super::codec::DecodeError> {
3114 if value.is_none() && element.is_none() && !path.is_lenient() {
3115 return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
3116 }
3117 let value = match value {
3118 Some(value) => {
3119 let value = super::super::codec::expect_single(value, path)?;
3120 Some(checked_time(
3121 super::super::codec::expect_string(value, path)?,
3122 path,
3123 )?)
3124 }
3125 None => None,
3126 };
3127 let mut id = None;
3128 let mut extension = Vec::new();
3129 if let Some(element) = element {
3130 let object = super::super::codec::expect_object(element, path)?;
3131 for (key, item) in object {
3132 match key.as_str() {
3133 "id" => {
3134 id = Some(path.with("id", |path| {
3135 super::primitives::checked_string(
3136 super::super::codec::expect_string(item, path)?,
3137 path,
3138 )
3139 })?);
3140 }
3141 "extension" => {
3142 for (index, entry) in super::super::codec::expect_array(item, path)?
3143 .iter()
3144 .enumerate()
3145 {
3146 extension.push(path.with_index("extension", index, |path| {
3147 super::super::codec::Json::from_json(
3148 super::super::codec::expect_object(entry, path)?,
3149 path,
3150 )
3151 })?);
3152 }
3153 }
3154 _ => {
3155 return path.with(key, |path| {
3156 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
3157 });
3158 }
3159 }
3160 }
3161 }
3162 Ok(Self {
3163 id,
3164 extension,
3165 value,
3166 })
3167 }
3168}
3169
3170#[derive(Debug, Clone, Default, PartialEq, Eq)]
3174pub struct UnsignedInt {
3175 pub id: Option<std::string::String>,
3179 pub extension: Vec<super::extension::Extension>,
3188 pub value: Option<u32>,
3190}
3191impl From<u32> for UnsignedInt {
3192 fn from(value: u32) -> Self {
3193 Self {
3194 value: Some(value),
3195 ..Self::default()
3196 }
3197 }
3198}
3199
3200impl super::super::codec::Primitive for UnsignedInt {
3201 fn value_json(
3202 &self,
3203 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
3204 Ok(self
3205 .value
3206 .as_ref()
3207 .map(|v| super::super::codec::Value::from(*v)))
3208 }
3209
3210 fn element_json(
3211 &self,
3212 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
3213 if self.id.is_none() && self.extension.is_empty() {
3214 return Ok(None);
3215 }
3216 let mut object = super::super::codec::Object::new();
3217 if let Some(id) = &self.id {
3218 object.insert(
3219 std::string::String::from("id"),
3220 super::super::codec::Value::String(id.clone()),
3221 );
3222 }
3223 if !self.extension.is_empty() {
3224 let mut items = Vec::with_capacity(self.extension.len());
3225 for item in &self.extension {
3226 items.push(super::super::codec::Value::Object(
3227 super::super::codec::Json::to_json(item)?,
3228 ));
3229 }
3230 object.insert(
3231 std::string::String::from("extension"),
3232 super::super::codec::Value::Array(items),
3233 );
3234 }
3235 Ok(Some(super::super::codec::Value::Object(object)))
3236 }
3237
3238 fn has_value(&self) -> bool {
3239 self.value.is_some()
3240 }
3241
3242 fn has_element(&self) -> bool {
3243 self.id.is_some() || !self.extension.is_empty()
3244 }
3245
3246 fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
3247 match self.value.as_ref() {
3248 Some(v) => serde::Serializer::serialize_u32(serializer, *v),
3249 None => serde::Serializer::serialize_none(serializer),
3250 }
3251 }
3252
3253 fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
3254 if self.id.is_none() && self.extension.is_empty() {
3255 return serde::Serializer::serialize_none(serializer);
3256 }
3257 let mut map = serde::Serializer::serialize_map(serializer, None)?;
3258 if !self.extension.is_empty() {
3259 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
3260 }
3261 if let Some(id) = &self.id {
3262 serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
3263 }
3264 serde::ser::SerializeMap::end(map)
3265 }
3266
3267 fn from_json_parts(
3268 value: Option<&super::super::codec::Value>,
3269 element: Option<&super::super::codec::Value>,
3270 path: &mut super::super::codec::Path,
3271 ) -> Result<Self, super::super::codec::DecodeError> {
3272 if value.is_none() && element.is_none() && !path.is_lenient() {
3273 return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
3274 }
3275 let value = match value {
3276 Some(value) => {
3277 let value = super::super::codec::expect_single(value, path)?;
3278 Some(super::super::codec::expect_u32(value, 0, path)?)
3279 }
3280 None => None,
3281 };
3282 let mut id = None;
3283 let mut extension = Vec::new();
3284 if let Some(element) = element {
3285 let object = super::super::codec::expect_object(element, path)?;
3286 for (key, item) in object {
3287 match key.as_str() {
3288 "id" => {
3289 id = Some(path.with("id", |path| {
3290 super::primitives::checked_string(
3291 super::super::codec::expect_string(item, path)?,
3292 path,
3293 )
3294 })?);
3295 }
3296 "extension" => {
3297 for (index, entry) in super::super::codec::expect_array(item, path)?
3298 .iter()
3299 .enumerate()
3300 {
3301 extension.push(path.with_index("extension", index, |path| {
3302 super::super::codec::Json::from_json(
3303 super::super::codec::expect_object(entry, path)?,
3304 path,
3305 )
3306 })?);
3307 }
3308 }
3309 _ => {
3310 return path.with(key, |path| {
3311 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
3312 });
3313 }
3314 }
3315 }
3316 }
3317 Ok(Self {
3318 id,
3319 extension,
3320 value,
3321 })
3322 }
3323}
3324
3325#[derive(Debug, Clone, Default, PartialEq, Eq)]
3329pub struct Uri {
3330 pub id: Option<std::string::String>,
3334 pub extension: Vec<super::extension::Extension>,
3343 pub value: Option<std::string::String>,
3347}
3348impl From<std::string::String> for Uri {
3349 fn from(value: std::string::String) -> Self {
3350 Self {
3351 value: Some(value),
3352 ..Self::default()
3353 }
3354 }
3355}
3356impl From<&str> for Uri {
3357 fn from(value: &str) -> Self {
3358 Self::from(std::string::String::from(value))
3359 }
3360}
3361
3362#[expect(
3368 clippy::expect_used,
3369 reason = "the emitter compiles every lexical form it writes, so the pattern holds"
3370)]
3371static URI_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> =
3372 std::sync::LazyLock::new(|| {
3373 regex::bytes::RegexBuilder::new(r"^(?:\S*)$")
3374 .unicode(false)
3375 .build()
3376 .expect("the lexical form of `uri` should compile")
3377 });
3378
3379pub(crate) fn checked_uri(
3385 text: std::string::String,
3386 path: &super::super::codec::Path,
3387) -> Result<std::string::String, super::super::codec::DecodeError> {
3388 if URI_LEXICAL_FORM.is_match(text.as_bytes()) {
3389 Ok(text)
3390 } else {
3391 Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
3392 }
3393}
3394
3395impl Uri {
3396 pub fn check(text: &str) -> Result<(), super::super::codec::LexicalError> {
3402 if URI_LEXICAL_FORM.is_match(text.as_bytes()) {
3403 Ok(())
3404 } else {
3405 Err(super::super::codec::LexicalError { type_code: "uri" })
3406 }
3407 }
3408}
3409
3410impl super::super::codec::Primitive for Uri {
3411 fn value_json(
3412 &self,
3413 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
3414 Ok(self
3415 .value
3416 .as_ref()
3417 .map(|v| super::super::codec::Value::String(v.clone())))
3418 }
3419
3420 fn element_json(
3421 &self,
3422 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
3423 if self.id.is_none() && self.extension.is_empty() {
3424 return Ok(None);
3425 }
3426 let mut object = super::super::codec::Object::new();
3427 if let Some(id) = &self.id {
3428 object.insert(
3429 std::string::String::from("id"),
3430 super::super::codec::Value::String(id.clone()),
3431 );
3432 }
3433 if !self.extension.is_empty() {
3434 let mut items = Vec::with_capacity(self.extension.len());
3435 for item in &self.extension {
3436 items.push(super::super::codec::Value::Object(
3437 super::super::codec::Json::to_json(item)?,
3438 ));
3439 }
3440 object.insert(
3441 std::string::String::from("extension"),
3442 super::super::codec::Value::Array(items),
3443 );
3444 }
3445 Ok(Some(super::super::codec::Value::Object(object)))
3446 }
3447
3448 fn has_value(&self) -> bool {
3449 self.value.is_some()
3450 }
3451
3452 fn has_element(&self) -> bool {
3453 self.id.is_some() || !self.extension.is_empty()
3454 }
3455
3456 fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
3457 match self.value.as_ref() {
3458 Some(v) => serde::Serializer::serialize_str(serializer, v),
3459 None => serde::Serializer::serialize_none(serializer),
3460 }
3461 }
3462
3463 fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
3464 if self.id.is_none() && self.extension.is_empty() {
3465 return serde::Serializer::serialize_none(serializer);
3466 }
3467 let mut map = serde::Serializer::serialize_map(serializer, None)?;
3468 if !self.extension.is_empty() {
3469 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
3470 }
3471 if let Some(id) = &self.id {
3472 serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
3473 }
3474 serde::ser::SerializeMap::end(map)
3475 }
3476
3477 fn from_json_parts(
3478 value: Option<&super::super::codec::Value>,
3479 element: Option<&super::super::codec::Value>,
3480 path: &mut super::super::codec::Path,
3481 ) -> Result<Self, super::super::codec::DecodeError> {
3482 if value.is_none() && element.is_none() && !path.is_lenient() {
3483 return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
3484 }
3485 let value = match value {
3486 Some(value) => {
3487 let value = super::super::codec::expect_single(value, path)?;
3488 Some(checked_uri(
3489 super::super::codec::expect_string(value, path)?,
3490 path,
3491 )?)
3492 }
3493 None => None,
3494 };
3495 let mut id = None;
3496 let mut extension = Vec::new();
3497 if let Some(element) = element {
3498 let object = super::super::codec::expect_object(element, path)?;
3499 for (key, item) in object {
3500 match key.as_str() {
3501 "id" => {
3502 id = Some(path.with("id", |path| {
3503 super::primitives::checked_string(
3504 super::super::codec::expect_string(item, path)?,
3505 path,
3506 )
3507 })?);
3508 }
3509 "extension" => {
3510 for (index, entry) in super::super::codec::expect_array(item, path)?
3511 .iter()
3512 .enumerate()
3513 {
3514 extension.push(path.with_index("extension", index, |path| {
3515 super::super::codec::Json::from_json(
3516 super::super::codec::expect_object(entry, path)?,
3517 path,
3518 )
3519 })?);
3520 }
3521 }
3522 _ => {
3523 return path.with(key, |path| {
3524 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
3525 });
3526 }
3527 }
3528 }
3529 }
3530 Ok(Self {
3531 id,
3532 extension,
3533 value,
3534 })
3535 }
3536}
3537
3538#[derive(Debug, Clone, Default, PartialEq, Eq)]
3542pub struct Url {
3543 pub id: Option<std::string::String>,
3547 pub extension: Vec<super::extension::Extension>,
3556 pub value: Option<std::string::String>,
3558}
3559impl From<std::string::String> for Url {
3560 fn from(value: std::string::String) -> Self {
3561 Self {
3562 value: Some(value),
3563 ..Self::default()
3564 }
3565 }
3566}
3567impl From<&str> for Url {
3568 fn from(value: &str) -> Self {
3569 Self::from(std::string::String::from(value))
3570 }
3571}
3572
3573#[expect(
3579 clippy::expect_used,
3580 reason = "the emitter compiles every lexical form it writes, so the pattern holds"
3581)]
3582static URL_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> =
3583 std::sync::LazyLock::new(|| {
3584 regex::bytes::RegexBuilder::new(r"^(?:\S*)$")
3585 .unicode(false)
3586 .build()
3587 .expect("the lexical form of `url` should compile")
3588 });
3589
3590pub(crate) fn checked_url(
3596 text: std::string::String,
3597 path: &super::super::codec::Path,
3598) -> Result<std::string::String, super::super::codec::DecodeError> {
3599 if URL_LEXICAL_FORM.is_match(text.as_bytes()) {
3600 Ok(text)
3601 } else {
3602 Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
3603 }
3604}
3605
3606impl Url {
3607 pub fn check(text: &str) -> Result<(), super::super::codec::LexicalError> {
3613 if URL_LEXICAL_FORM.is_match(text.as_bytes()) {
3614 Ok(())
3615 } else {
3616 Err(super::super::codec::LexicalError { type_code: "url" })
3617 }
3618 }
3619}
3620
3621impl super::super::codec::Primitive for Url {
3622 fn value_json(
3623 &self,
3624 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
3625 Ok(self
3626 .value
3627 .as_ref()
3628 .map(|v| super::super::codec::Value::String(v.clone())))
3629 }
3630
3631 fn element_json(
3632 &self,
3633 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
3634 if self.id.is_none() && self.extension.is_empty() {
3635 return Ok(None);
3636 }
3637 let mut object = super::super::codec::Object::new();
3638 if let Some(id) = &self.id {
3639 object.insert(
3640 std::string::String::from("id"),
3641 super::super::codec::Value::String(id.clone()),
3642 );
3643 }
3644 if !self.extension.is_empty() {
3645 let mut items = Vec::with_capacity(self.extension.len());
3646 for item in &self.extension {
3647 items.push(super::super::codec::Value::Object(
3648 super::super::codec::Json::to_json(item)?,
3649 ));
3650 }
3651 object.insert(
3652 std::string::String::from("extension"),
3653 super::super::codec::Value::Array(items),
3654 );
3655 }
3656 Ok(Some(super::super::codec::Value::Object(object)))
3657 }
3658
3659 fn has_value(&self) -> bool {
3660 self.value.is_some()
3661 }
3662
3663 fn has_element(&self) -> bool {
3664 self.id.is_some() || !self.extension.is_empty()
3665 }
3666
3667 fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
3668 match self.value.as_ref() {
3669 Some(v) => serde::Serializer::serialize_str(serializer, v),
3670 None => serde::Serializer::serialize_none(serializer),
3671 }
3672 }
3673
3674 fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
3675 if self.id.is_none() && self.extension.is_empty() {
3676 return serde::Serializer::serialize_none(serializer);
3677 }
3678 let mut map = serde::Serializer::serialize_map(serializer, None)?;
3679 if !self.extension.is_empty() {
3680 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
3681 }
3682 if let Some(id) = &self.id {
3683 serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
3684 }
3685 serde::ser::SerializeMap::end(map)
3686 }
3687
3688 fn from_json_parts(
3689 value: Option<&super::super::codec::Value>,
3690 element: Option<&super::super::codec::Value>,
3691 path: &mut super::super::codec::Path,
3692 ) -> Result<Self, super::super::codec::DecodeError> {
3693 if value.is_none() && element.is_none() && !path.is_lenient() {
3694 return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
3695 }
3696 let value = match value {
3697 Some(value) => {
3698 let value = super::super::codec::expect_single(value, path)?;
3699 Some(checked_url(
3700 super::super::codec::expect_string(value, path)?,
3701 path,
3702 )?)
3703 }
3704 None => None,
3705 };
3706 let mut id = None;
3707 let mut extension = Vec::new();
3708 if let Some(element) = element {
3709 let object = super::super::codec::expect_object(element, path)?;
3710 for (key, item) in object {
3711 match key.as_str() {
3712 "id" => {
3713 id = Some(path.with("id", |path| {
3714 super::primitives::checked_string(
3715 super::super::codec::expect_string(item, path)?,
3716 path,
3717 )
3718 })?);
3719 }
3720 "extension" => {
3721 for (index, entry) in super::super::codec::expect_array(item, path)?
3722 .iter()
3723 .enumerate()
3724 {
3725 extension.push(path.with_index("extension", index, |path| {
3726 super::super::codec::Json::from_json(
3727 super::super::codec::expect_object(entry, path)?,
3728 path,
3729 )
3730 })?);
3731 }
3732 }
3733 _ => {
3734 return path.with(key, |path| {
3735 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
3736 });
3737 }
3738 }
3739 }
3740 }
3741 Ok(Self {
3742 id,
3743 extension,
3744 value,
3745 })
3746 }
3747}
3748
3749#[derive(Debug, Clone, Default, PartialEq, Eq)]
3753pub struct Uuid {
3754 pub id: Option<std::string::String>,
3758 pub extension: Vec<super::extension::Extension>,
3767 pub value: Option<std::string::String>,
3769}
3770impl From<std::string::String> for Uuid {
3771 fn from(value: std::string::String) -> Self {
3772 Self {
3773 value: Some(value),
3774 ..Self::default()
3775 }
3776 }
3777}
3778impl From<&str> for Uuid {
3779 fn from(value: &str) -> Self {
3780 Self::from(std::string::String::from(value))
3781 }
3782}
3783
3784#[expect(
3790 clippy::expect_used,
3791 reason = "the emitter compiles every lexical form it writes, so the pattern holds"
3792)]
3793static UUID_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> =
3794 std::sync::LazyLock::new(|| {
3795 regex::bytes::RegexBuilder::new(
3796 r"^(?:urn:uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$",
3797 )
3798 .unicode(false)
3799 .build()
3800 .expect("the lexical form of `uuid` should compile")
3801 });
3802
3803pub(crate) fn checked_uuid(
3809 text: std::string::String,
3810 path: &super::super::codec::Path,
3811) -> Result<std::string::String, super::super::codec::DecodeError> {
3812 if UUID_LEXICAL_FORM.is_match(text.as_bytes()) {
3813 Ok(text)
3814 } else {
3815 Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
3816 }
3817}
3818
3819impl Uuid {
3820 pub fn check(text: &str) -> Result<(), super::super::codec::LexicalError> {
3826 if UUID_LEXICAL_FORM.is_match(text.as_bytes()) {
3827 Ok(())
3828 } else {
3829 Err(super::super::codec::LexicalError { type_code: "uuid" })
3830 }
3831 }
3832}
3833
3834impl super::super::codec::Primitive for Uuid {
3835 fn value_json(
3836 &self,
3837 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
3838 Ok(self
3839 .value
3840 .as_ref()
3841 .map(|v| super::super::codec::Value::String(v.clone())))
3842 }
3843
3844 fn element_json(
3845 &self,
3846 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
3847 if self.id.is_none() && self.extension.is_empty() {
3848 return Ok(None);
3849 }
3850 let mut object = super::super::codec::Object::new();
3851 if let Some(id) = &self.id {
3852 object.insert(
3853 std::string::String::from("id"),
3854 super::super::codec::Value::String(id.clone()),
3855 );
3856 }
3857 if !self.extension.is_empty() {
3858 let mut items = Vec::with_capacity(self.extension.len());
3859 for item in &self.extension {
3860 items.push(super::super::codec::Value::Object(
3861 super::super::codec::Json::to_json(item)?,
3862 ));
3863 }
3864 object.insert(
3865 std::string::String::from("extension"),
3866 super::super::codec::Value::Array(items),
3867 );
3868 }
3869 Ok(Some(super::super::codec::Value::Object(object)))
3870 }
3871
3872 fn has_value(&self) -> bool {
3873 self.value.is_some()
3874 }
3875
3876 fn has_element(&self) -> bool {
3877 self.id.is_some() || !self.extension.is_empty()
3878 }
3879
3880 fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
3881 match self.value.as_ref() {
3882 Some(v) => serde::Serializer::serialize_str(serializer, v),
3883 None => serde::Serializer::serialize_none(serializer),
3884 }
3885 }
3886
3887 fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
3888 if self.id.is_none() && self.extension.is_empty() {
3889 return serde::Serializer::serialize_none(serializer);
3890 }
3891 let mut map = serde::Serializer::serialize_map(serializer, None)?;
3892 if !self.extension.is_empty() {
3893 serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
3894 }
3895 if let Some(id) = &self.id {
3896 serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
3897 }
3898 serde::ser::SerializeMap::end(map)
3899 }
3900
3901 fn from_json_parts(
3902 value: Option<&super::super::codec::Value>,
3903 element: Option<&super::super::codec::Value>,
3904 path: &mut super::super::codec::Path,
3905 ) -> Result<Self, super::super::codec::DecodeError> {
3906 if value.is_none() && element.is_none() && !path.is_lenient() {
3907 return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
3908 }
3909 let value = match value {
3910 Some(value) => {
3911 let value = super::super::codec::expect_single(value, path)?;
3912 Some(checked_uuid(
3913 super::super::codec::expect_string(value, path)?,
3914 path,
3915 )?)
3916 }
3917 None => None,
3918 };
3919 let mut id = None;
3920 let mut extension = Vec::new();
3921 if let Some(element) = element {
3922 let object = super::super::codec::expect_object(element, path)?;
3923 for (key, item) in object {
3924 match key.as_str() {
3925 "id" => {
3926 id = Some(path.with("id", |path| {
3927 super::primitives::checked_string(
3928 super::super::codec::expect_string(item, path)?,
3929 path,
3930 )
3931 })?);
3932 }
3933 "extension" => {
3934 for (index, entry) in super::super::codec::expect_array(item, path)?
3935 .iter()
3936 .enumerate()
3937 {
3938 extension.push(path.with_index("extension", index, |path| {
3939 super::super::codec::Json::from_json(
3940 super::super::codec::expect_object(entry, path)?,
3941 path,
3942 )
3943 })?);
3944 }
3945 }
3946 _ => {
3947 return path.with(key, |path| {
3948 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
3949 });
3950 }
3951 }
3952 }
3953 }
3954 Ok(Self {
3955 id,
3956 extension,
3957 value,
3958 })
3959 }
3960}
3961
3962#[derive(Debug, Clone, Default, PartialEq, Eq)]
3966pub struct Xhtml {
3967 pub id: Option<std::string::String>,
3971 pub value: std::string::String,
3973}
3974impl From<std::string::String> for Xhtml {
3975 fn from(value: std::string::String) -> Self {
3976 Self {
3977 value,
3978 ..Self::default()
3979 }
3980 }
3981}
3982impl From<&str> for Xhtml {
3983 fn from(value: &str) -> Self {
3984 Self::from(std::string::String::from(value))
3985 }
3986}
3987
3988impl super::super::codec::Primitive for Xhtml {
3989 fn value_json(
3990 &self,
3991 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
3992 Ok(Some(&self.value).map(|v| super::super::codec::Value::String(v.clone())))
3993 }
3994
3995 fn element_json(
3996 &self,
3997 ) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
3998 if self.id.is_none() {
3999 return Ok(None);
4000 }
4001 let mut object = super::super::codec::Object::new();
4002 if let Some(id) = &self.id {
4003 object.insert(
4004 std::string::String::from("id"),
4005 super::super::codec::Value::String(id.clone()),
4006 );
4007 }
4008 Ok(Some(super::super::codec::Value::Object(object)))
4009 }
4010
4011 fn has_value(&self) -> bool {
4012 true
4013 }
4014
4015 fn has_element(&self) -> bool {
4016 self.id.is_some()
4017 }
4018
4019 fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
4020 serde::Serializer::serialize_str(serializer, &self.value)
4021 }
4022
4023 fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
4024 if self.id.is_none() {
4025 return serde::Serializer::serialize_none(serializer);
4026 }
4027 let mut map = serde::Serializer::serialize_map(serializer, None)?;
4028 if let Some(id) = &self.id {
4029 serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
4030 }
4031 serde::ser::SerializeMap::end(map)
4032 }
4033
4034 fn from_json_parts(
4035 value: Option<&super::super::codec::Value>,
4036 element: Option<&super::super::codec::Value>,
4037 path: &mut super::super::codec::Path,
4038 ) -> Result<Self, super::super::codec::DecodeError> {
4039 if value.is_none() && element.is_none() && !path.is_lenient() {
4040 return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
4041 }
4042 let value = match value {
4043 Some(value) => {
4044 let value = super::super::codec::expect_single(value, path)?;
4045 Some(super::super::codec::expect_string(value, path)?)
4046 }
4047 None => None,
4048 };
4049 let value = value
4050 .ok_or_else(|| path.error(super::super::codec::DecodeErrorKind::MissingProperty))?;
4051 let mut id = None;
4052 if let Some(element) = element {
4053 let object = super::super::codec::expect_object(element, path)?;
4054 for (key, item) in object {
4055 match key.as_str() {
4056 "id" => {
4057 id = Some(path.with("id", |path| {
4058 super::primitives::checked_string(
4059 super::super::codec::expect_string(item, path)?,
4060 path,
4061 )
4062 })?);
4063 }
4064 _ => {
4065 return path.with(key, |path| {
4066 Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
4067 });
4068 }
4069 }
4070 }
4071 }
4072 Ok(Self { id, value })
4073 }
4074}
4075
4076#[must_use]
4087pub fn check_value(
4088 code: &str,
4089 value: &super::super::codec::Value,
4090) -> Option<Result<(), super::super::codec::DecodeError>> {
4091 let mut path = super::super::codec::Path::root(code);
4092 let checked = match code {
4093 #[cfg(feature = "terminology")]
4094 "base64Binary" => <Base64Binary as super::super::codec::Primitive>::from_json_parts(
4095 Some(value),
4096 None,
4097 &mut path,
4098 )
4099 .map(|_| ()),
4100 #[cfg(feature = "terminology")]
4101 "boolean" => <Boolean as super::super::codec::Primitive>::from_json_parts(
4102 Some(value),
4103 None,
4104 &mut path,
4105 )
4106 .map(|_| ()),
4107 #[cfg(feature = "terminology")]
4108 "canonical" => <Canonical as super::super::codec::Primitive>::from_json_parts(
4109 Some(value),
4110 None,
4111 &mut path,
4112 )
4113 .map(|_| ()),
4114 #[cfg(feature = "terminology")]
4115 "code" => {
4116 <Code as super::super::codec::Primitive>::from_json_parts(Some(value), None, &mut path)
4117 .map(|_| ())
4118 }
4119 #[cfg(feature = "terminology")]
4120 "date" => {
4121 <Date as super::super::codec::Primitive>::from_json_parts(Some(value), None, &mut path)
4122 .map(|_| ())
4123 }
4124 #[cfg(feature = "terminology")]
4125 "dateTime" => <DateTime as super::super::codec::Primitive>::from_json_parts(
4126 Some(value),
4127 None,
4128 &mut path,
4129 )
4130 .map(|_| ()),
4131 #[cfg(feature = "terminology")]
4132 "decimal" => <Decimal as super::super::codec::Primitive>::from_json_parts(
4133 Some(value),
4134 None,
4135 &mut path,
4136 )
4137 .map(|_| ()),
4138 #[cfg(feature = "terminology")]
4139 "id" => {
4140 <Id as super::super::codec::Primitive>::from_json_parts(Some(value), None, &mut path)
4141 .map(|_| ())
4142 }
4143 #[cfg(feature = "terminology")]
4144 "instant" => <Instant as super::super::codec::Primitive>::from_json_parts(
4145 Some(value),
4146 None,
4147 &mut path,
4148 )
4149 .map(|_| ()),
4150 #[cfg(feature = "terminology")]
4151 "integer" => <Integer as super::super::codec::Primitive>::from_json_parts(
4152 Some(value),
4153 None,
4154 &mut path,
4155 )
4156 .map(|_| ()),
4157 #[cfg(feature = "terminology")]
4158 "integer64" => <Integer64 as super::super::codec::Primitive>::from_json_parts(
4159 Some(value),
4160 None,
4161 &mut path,
4162 )
4163 .map(|_| ()),
4164 #[cfg(feature = "terminology")]
4165 "markdown" => <Markdown as super::super::codec::Primitive>::from_json_parts(
4166 Some(value),
4167 None,
4168 &mut path,
4169 )
4170 .map(|_| ()),
4171 #[cfg(feature = "terminology")]
4172 "oid" => {
4173 <Oid as super::super::codec::Primitive>::from_json_parts(Some(value), None, &mut path)
4174 .map(|_| ())
4175 }
4176 #[cfg(feature = "terminology")]
4177 "positiveInt" => <PositiveInt as super::super::codec::Primitive>::from_json_parts(
4178 Some(value),
4179 None,
4180 &mut path,
4181 )
4182 .map(|_| ()),
4183 #[cfg(feature = "terminology")]
4184 "string" => <String as super::super::codec::Primitive>::from_json_parts(
4185 Some(value),
4186 None,
4187 &mut path,
4188 )
4189 .map(|_| ()),
4190 #[cfg(feature = "terminology")]
4191 "time" => {
4192 <Time as super::super::codec::Primitive>::from_json_parts(Some(value), None, &mut path)
4193 .map(|_| ())
4194 }
4195 #[cfg(feature = "terminology")]
4196 "unsignedInt" => <UnsignedInt as super::super::codec::Primitive>::from_json_parts(
4197 Some(value),
4198 None,
4199 &mut path,
4200 )
4201 .map(|_| ()),
4202 #[cfg(feature = "terminology")]
4203 "uri" => {
4204 <Uri as super::super::codec::Primitive>::from_json_parts(Some(value), None, &mut path)
4205 .map(|_| ())
4206 }
4207 #[cfg(feature = "terminology")]
4208 "url" => {
4209 <Url as super::super::codec::Primitive>::from_json_parts(Some(value), None, &mut path)
4210 .map(|_| ())
4211 }
4212 #[cfg(feature = "terminology")]
4213 "uuid" => {
4214 <Uuid as super::super::codec::Primitive>::from_json_parts(Some(value), None, &mut path)
4215 .map(|_| ())
4216 }
4217 #[cfg(feature = "terminology")]
4218 "xhtml" => {
4219 <Xhtml as super::super::codec::Primitive>::from_json_parts(Some(value), None, &mut path)
4220 .map(|_| ())
4221 }
4222 _ => return None,
4223 };
4224 Some(checked)
4225}