#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Base64Binary {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub value: Option<std::string::String>,
}
impl From<std::string::String> for Base64Binary {
fn from(value: std::string::String) -> Self {
Self {
value: Some(value),
..Self::default()
}
}
}
impl From<&str> for Base64Binary {
fn from(value: &str) -> Self {
Self::from(std::string::String::from(value))
}
}
#[expect(
clippy::expect_used,
reason = "the emitter compiles every lexical form it writes, so the pattern holds"
)]
static BASE64_BINARY_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> =
std::sync::LazyLock::new(|| {
regex::bytes::RegexBuilder::new(
r"^(?:(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?)$",
)
.unicode(false)
.build()
.expect("the lexical form of `base64Binary` should compile")
});
pub(crate) fn checked_base64_binary(
text: std::string::String,
path: &super::super::codec::Path,
) -> Result<std::string::String, super::super::codec::DecodeError> {
if BASE64_BINARY_LEXICAL_FORM.is_match(text.as_bytes()) {
Ok(text)
} else {
Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
}
}
impl super::super::codec::Primitive for Base64Binary {
fn value_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
Ok(self
.value
.as_ref()
.map(|v| super::super::codec::Value::String(v.clone())))
}
fn element_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
if self.id.is_none() && self.extension.is_empty() {
return Ok(None);
}
let mut object = super::super::codec::Object::new();
if let Some(id) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(id.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
super::super::codec::Value::Array(items),
);
}
Ok(Some(super::super::codec::Value::Object(object)))
}
fn has_value(&self) -> bool {
self.value.is_some()
}
fn has_element(&self) -> bool {
self.id.is_some() || !self.extension.is_empty()
}
fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self.value.as_ref() {
Some(v) => serde::Serializer::serialize_str(serializer, v),
None => serde::Serializer::serialize_none(serializer),
}
}
fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.id.is_none() && self.extension.is_empty() {
return serde::Serializer::serialize_none(serializer);
}
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(id) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
}
serde::ser::SerializeMap::end(map)
}
fn from_json_parts(
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
if value.is_none() && element.is_none() && !path.is_lenient() {
return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
}
let value = match value {
Some(value) => {
let value = super::super::codec::expect_single(value, path)?;
Some(checked_base64_binary(
super::super::codec::expect_string(value, path)?,
path,
)?)
}
None => None,
};
let mut id = None;
let mut extension = Vec::new();
if let Some(element) = element {
let object = super::super::codec::expect_object(element, path)?;
for (key, item) in object {
match key.as_str() {
"id" => {
id = Some(path.with("id", |path| {
super::primitives::checked_string(
super::super::codec::expect_string(item, path)?,
path,
)
})?);
}
"extension" => {
for (index, entry) in super::super::codec::expect_array(item, path)?
.iter()
.enumerate()
{
extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(entry, path)?,
path,
)
})?);
}
}
_ => {
return path.with(key, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
}
Ok(Self {
id,
extension,
value,
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Boolean {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub value: Option<bool>,
}
impl From<bool> for Boolean {
fn from(value: bool) -> Self {
Self {
value: Some(value),
..Self::default()
}
}
}
impl super::super::codec::Primitive for Boolean {
fn value_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
Ok(self
.value
.as_ref()
.map(|v| super::super::codec::Value::Bool(*v)))
}
fn element_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
if self.id.is_none() && self.extension.is_empty() {
return Ok(None);
}
let mut object = super::super::codec::Object::new();
if let Some(id) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(id.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
super::super::codec::Value::Array(items),
);
}
Ok(Some(super::super::codec::Value::Object(object)))
}
fn has_value(&self) -> bool {
self.value.is_some()
}
fn has_element(&self) -> bool {
self.id.is_some() || !self.extension.is_empty()
}
fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self.value.as_ref() {
Some(v) => serde::Serializer::serialize_bool(serializer, *v),
None => serde::Serializer::serialize_none(serializer),
}
}
fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.id.is_none() && self.extension.is_empty() {
return serde::Serializer::serialize_none(serializer);
}
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(id) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
}
serde::ser::SerializeMap::end(map)
}
fn from_json_parts(
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
if value.is_none() && element.is_none() && !path.is_lenient() {
return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
}
let value = match value {
Some(value) => {
let value = super::super::codec::expect_single(value, path)?;
Some(super::super::codec::expect_bool(value, path)?)
}
None => None,
};
let mut id = None;
let mut extension = Vec::new();
if let Some(element) = element {
let object = super::super::codec::expect_object(element, path)?;
for (key, item) in object {
match key.as_str() {
"id" => {
id = Some(path.with("id", |path| {
super::primitives::checked_string(
super::super::codec::expect_string(item, path)?,
path,
)
})?);
}
"extension" => {
for (index, entry) in super::super::codec::expect_array(item, path)?
.iter()
.enumerate()
{
extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(entry, path)?,
path,
)
})?);
}
}
_ => {
return path.with(key, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
}
Ok(Self {
id,
extension,
value,
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Canonical {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub value: Option<std::string::String>,
}
impl From<std::string::String> for Canonical {
fn from(value: std::string::String) -> Self {
Self {
value: Some(value),
..Self::default()
}
}
}
impl From<&str> for Canonical {
fn from(value: &str) -> Self {
Self::from(std::string::String::from(value))
}
}
#[expect(
clippy::expect_used,
reason = "the emitter compiles every lexical form it writes, so the pattern holds"
)]
static CANONICAL_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> =
std::sync::LazyLock::new(|| {
regex::bytes::RegexBuilder::new(r"^(?:\S*)$")
.unicode(false)
.build()
.expect("the lexical form of `canonical` should compile")
});
pub(crate) fn checked_canonical(
text: std::string::String,
path: &super::super::codec::Path,
) -> Result<std::string::String, super::super::codec::DecodeError> {
if CANONICAL_LEXICAL_FORM.is_match(text.as_bytes()) {
Ok(text)
} else {
Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
}
}
impl super::super::codec::Primitive for Canonical {
fn value_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
Ok(self
.value
.as_ref()
.map(|v| super::super::codec::Value::String(v.clone())))
}
fn element_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
if self.id.is_none() && self.extension.is_empty() {
return Ok(None);
}
let mut object = super::super::codec::Object::new();
if let Some(id) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(id.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
super::super::codec::Value::Array(items),
);
}
Ok(Some(super::super::codec::Value::Object(object)))
}
fn has_value(&self) -> bool {
self.value.is_some()
}
fn has_element(&self) -> bool {
self.id.is_some() || !self.extension.is_empty()
}
fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self.value.as_ref() {
Some(v) => serde::Serializer::serialize_str(serializer, v),
None => serde::Serializer::serialize_none(serializer),
}
}
fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.id.is_none() && self.extension.is_empty() {
return serde::Serializer::serialize_none(serializer);
}
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(id) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
}
serde::ser::SerializeMap::end(map)
}
fn from_json_parts(
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
if value.is_none() && element.is_none() && !path.is_lenient() {
return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
}
let value = match value {
Some(value) => {
let value = super::super::codec::expect_single(value, path)?;
Some(checked_canonical(
super::super::codec::expect_string(value, path)?,
path,
)?)
}
None => None,
};
let mut id = None;
let mut extension = Vec::new();
if let Some(element) = element {
let object = super::super::codec::expect_object(element, path)?;
for (key, item) in object {
match key.as_str() {
"id" => {
id = Some(path.with("id", |path| {
super::primitives::checked_string(
super::super::codec::expect_string(item, path)?,
path,
)
})?);
}
"extension" => {
for (index, entry) in super::super::codec::expect_array(item, path)?
.iter()
.enumerate()
{
extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(entry, path)?,
path,
)
})?);
}
}
_ => {
return path.with(key, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
}
Ok(Self {
id,
extension,
value,
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Code {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub value: Option<std::string::String>,
}
impl From<std::string::String> for Code {
fn from(value: std::string::String) -> Self {
Self {
value: Some(value),
..Self::default()
}
}
}
impl From<&str> for Code {
fn from(value: &str) -> Self {
Self::from(std::string::String::from(value))
}
}
#[expect(
clippy::expect_used,
reason = "the emitter compiles every lexical form it writes, so the pattern holds"
)]
static CODE_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> =
std::sync::LazyLock::new(|| {
regex::bytes::RegexBuilder::new(r"^(?:[^\s]+( [^\s]+)*)$")
.unicode(false)
.build()
.expect("the lexical form of `code` should compile")
});
pub(crate) fn checked_code(
text: std::string::String,
path: &super::super::codec::Path,
) -> Result<std::string::String, super::super::codec::DecodeError> {
if CODE_LEXICAL_FORM.is_match(text.as_bytes()) {
Ok(text)
} else {
Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
}
}
impl super::super::codec::Primitive for Code {
fn value_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
Ok(self
.value
.as_ref()
.map(|v| super::super::codec::Value::String(v.clone())))
}
fn element_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
if self.id.is_none() && self.extension.is_empty() {
return Ok(None);
}
let mut object = super::super::codec::Object::new();
if let Some(id) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(id.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
super::super::codec::Value::Array(items),
);
}
Ok(Some(super::super::codec::Value::Object(object)))
}
fn has_value(&self) -> bool {
self.value.is_some()
}
fn has_element(&self) -> bool {
self.id.is_some() || !self.extension.is_empty()
}
fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self.value.as_ref() {
Some(v) => serde::Serializer::serialize_str(serializer, v),
None => serde::Serializer::serialize_none(serializer),
}
}
fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.id.is_none() && self.extension.is_empty() {
return serde::Serializer::serialize_none(serializer);
}
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(id) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
}
serde::ser::SerializeMap::end(map)
}
fn from_json_parts(
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
if value.is_none() && element.is_none() && !path.is_lenient() {
return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
}
let value = match value {
Some(value) => {
let value = super::super::codec::expect_single(value, path)?;
Some(checked_code(
super::super::codec::expect_string(value, path)?,
path,
)?)
}
None => None,
};
let mut id = None;
let mut extension = Vec::new();
if let Some(element) = element {
let object = super::super::codec::expect_object(element, path)?;
for (key, item) in object {
match key.as_str() {
"id" => {
id = Some(path.with("id", |path| {
super::primitives::checked_string(
super::super::codec::expect_string(item, path)?,
path,
)
})?);
}
"extension" => {
for (index, entry) in super::super::codec::expect_array(item, path)?
.iter()
.enumerate()
{
extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(entry, path)?,
path,
)
})?);
}
}
_ => {
return path.with(key, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
}
Ok(Self {
id,
extension,
value,
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Date {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub value: Option<std::string::String>,
}
impl From<std::string::String> for Date {
fn from(value: std::string::String) -> Self {
Self {
value: Some(value),
..Self::default()
}
}
}
impl From<&str> for Date {
fn from(value: &str) -> Self {
Self::from(std::string::String::from(value))
}
}
#[expect(
clippy::expect_used,
reason = "the emitter compiles every lexical form it writes, so the pattern holds"
)]
static DATE_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> = std::sync::LazyLock::new(
|| {
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]))?)?)$")
.unicode(false)
.build()
.expect("the lexical form of `date` should compile")
},
);
pub(crate) fn checked_date(
text: std::string::String,
path: &super::super::codec::Path,
) -> Result<std::string::String, super::super::codec::DecodeError> {
if DATE_LEXICAL_FORM.is_match(text.as_bytes()) {
Ok(text)
} else {
Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
}
}
impl super::super::codec::Primitive for Date {
fn value_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
Ok(self
.value
.as_ref()
.map(|v| super::super::codec::Value::String(v.clone())))
}
fn element_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
if self.id.is_none() && self.extension.is_empty() {
return Ok(None);
}
let mut object = super::super::codec::Object::new();
if let Some(id) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(id.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
super::super::codec::Value::Array(items),
);
}
Ok(Some(super::super::codec::Value::Object(object)))
}
fn has_value(&self) -> bool {
self.value.is_some()
}
fn has_element(&self) -> bool {
self.id.is_some() || !self.extension.is_empty()
}
fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self.value.as_ref() {
Some(v) => serde::Serializer::serialize_str(serializer, v),
None => serde::Serializer::serialize_none(serializer),
}
}
fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.id.is_none() && self.extension.is_empty() {
return serde::Serializer::serialize_none(serializer);
}
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(id) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
}
serde::ser::SerializeMap::end(map)
}
fn from_json_parts(
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
if value.is_none() && element.is_none() && !path.is_lenient() {
return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
}
let value = match value {
Some(value) => {
let value = super::super::codec::expect_single(value, path)?;
Some(checked_date(
super::super::codec::expect_string(value, path)?,
path,
)?)
}
None => None,
};
let mut id = None;
let mut extension = Vec::new();
if let Some(element) = element {
let object = super::super::codec::expect_object(element, path)?;
for (key, item) in object {
match key.as_str() {
"id" => {
id = Some(path.with("id", |path| {
super::primitives::checked_string(
super::super::codec::expect_string(item, path)?,
path,
)
})?);
}
"extension" => {
for (index, entry) in super::super::codec::expect_array(item, path)?
.iter()
.enumerate()
{
extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(entry, path)?,
path,
)
})?);
}
}
_ => {
return path.with(key, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
}
Ok(Self {
id,
extension,
value,
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct DateTime {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub value: Option<std::string::String>,
}
impl From<std::string::String> for DateTime {
fn from(value: std::string::String) -> Self {
Self {
value: Some(value),
..Self::default()
}
}
}
impl From<&str> for DateTime {
fn from(value: &str) -> Self {
Self::from(std::string::String::from(value))
}
}
#[expect(
clippy::expect_used,
reason = "the emitter compiles every lexical form it writes, so the pattern holds"
)]
static DATE_TIME_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> = std::sync::LazyLock::new(
|| {
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)?)?)?)$")
.unicode(false)
.build()
.expect("the lexical form of `dateTime` should compile")
},
);
pub(crate) fn checked_date_time(
text: std::string::String,
path: &super::super::codec::Path,
) -> Result<std::string::String, super::super::codec::DecodeError> {
if DATE_TIME_LEXICAL_FORM.is_match(text.as_bytes()) {
Ok(text)
} else {
Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
}
}
impl super::super::codec::Primitive for DateTime {
fn value_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
Ok(self
.value
.as_ref()
.map(|v| super::super::codec::Value::String(v.clone())))
}
fn element_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
if self.id.is_none() && self.extension.is_empty() {
return Ok(None);
}
let mut object = super::super::codec::Object::new();
if let Some(id) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(id.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
super::super::codec::Value::Array(items),
);
}
Ok(Some(super::super::codec::Value::Object(object)))
}
fn has_value(&self) -> bool {
self.value.is_some()
}
fn has_element(&self) -> bool {
self.id.is_some() || !self.extension.is_empty()
}
fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self.value.as_ref() {
Some(v) => serde::Serializer::serialize_str(serializer, v),
None => serde::Serializer::serialize_none(serializer),
}
}
fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.id.is_none() && self.extension.is_empty() {
return serde::Serializer::serialize_none(serializer);
}
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(id) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
}
serde::ser::SerializeMap::end(map)
}
fn from_json_parts(
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
if value.is_none() && element.is_none() && !path.is_lenient() {
return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
}
let value = match value {
Some(value) => {
let value = super::super::codec::expect_single(value, path)?;
Some(checked_date_time(
super::super::codec::expect_string(value, path)?,
path,
)?)
}
None => None,
};
let mut id = None;
let mut extension = Vec::new();
if let Some(element) = element {
let object = super::super::codec::expect_object(element, path)?;
for (key, item) in object {
match key.as_str() {
"id" => {
id = Some(path.with("id", |path| {
super::primitives::checked_string(
super::super::codec::expect_string(item, path)?,
path,
)
})?);
}
"extension" => {
for (index, entry) in super::super::codec::expect_array(item, path)?
.iter()
.enumerate()
{
extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(entry, path)?,
path,
)
})?);
}
}
_ => {
return path.with(key, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
}
Ok(Self {
id,
extension,
value,
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Decimal {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub value: Option<std::string::String>,
}
impl From<std::string::String> for Decimal {
fn from(value: std::string::String) -> Self {
Self {
value: Some(value),
..Self::default()
}
}
}
impl From<&str> for Decimal {
fn from(value: &str) -> Self {
Self::from(std::string::String::from(value))
}
}
impl super::super::codec::Primitive for Decimal {
fn value_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
self.value
.as_ref()
.map(|text| {
text.parse::<super::super::codec::Number>()
.map(super::super::codec::Value::Number)
.map_err(|_| super::super::codec::EncodeError::BadDecimal {
text: text.clone(),
})
})
.transpose()
}
fn element_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
if self.id.is_none() && self.extension.is_empty() {
return Ok(None);
}
let mut object = super::super::codec::Object::new();
if let Some(id) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(id.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
super::super::codec::Value::Array(items),
);
}
Ok(Some(super::super::codec::Value::Object(object)))
}
fn has_value(&self) -> bool {
self.value.is_some()
}
fn has_element(&self) -> bool {
self.id.is_some() || !self.extension.is_empty()
}
fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self.value.as_ref() {
Some(text) => serde::Serialize::serialize(
&text.parse::<super::super::codec::Number>().map_err(|_| {
<S::Error as serde::ser::Error>::custom(
super::super::codec::EncodeError::BadDecimal { text: text.clone() },
)
})?,
serializer,
),
None => serde::Serializer::serialize_none(serializer),
}
}
fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.id.is_none() && self.extension.is_empty() {
return serde::Serializer::serialize_none(serializer);
}
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(id) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
}
serde::ser::SerializeMap::end(map)
}
fn from_json_parts(
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
if value.is_none() && element.is_none() && !path.is_lenient() {
return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
}
let value = match value {
Some(value) => {
let value = super::super::codec::expect_single(value, path)?;
Some(super::super::codec::expect_decimal(value, path)?)
}
None => None,
};
let mut id = None;
let mut extension = Vec::new();
if let Some(element) = element {
let object = super::super::codec::expect_object(element, path)?;
for (key, item) in object {
match key.as_str() {
"id" => {
id = Some(path.with("id", |path| {
super::primitives::checked_string(
super::super::codec::expect_string(item, path)?,
path,
)
})?);
}
"extension" => {
for (index, entry) in super::super::codec::expect_array(item, path)?
.iter()
.enumerate()
{
extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(entry, path)?,
path,
)
})?);
}
}
_ => {
return path.with(key, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
}
Ok(Self {
id,
extension,
value,
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Id {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub value: Option<std::string::String>,
}
impl From<std::string::String> for Id {
fn from(value: std::string::String) -> Self {
Self {
value: Some(value),
..Self::default()
}
}
}
impl From<&str> for Id {
fn from(value: &str) -> Self {
Self::from(std::string::String::from(value))
}
}
#[expect(
clippy::expect_used,
reason = "the emitter compiles every lexical form it writes, so the pattern holds"
)]
static ID_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> = std::sync::LazyLock::new(|| {
regex::bytes::RegexBuilder::new(r"^(?:[A-Za-z0-9\-\.]{1,64})$")
.unicode(false)
.build()
.expect("the lexical form of `id` should compile")
});
pub(crate) fn checked_id(
text: std::string::String,
path: &super::super::codec::Path,
) -> Result<std::string::String, super::super::codec::DecodeError> {
if ID_LEXICAL_FORM.is_match(text.as_bytes()) {
Ok(text)
} else {
Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
}
}
impl super::super::codec::Primitive for Id {
fn value_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
Ok(self
.value
.as_ref()
.map(|v| super::super::codec::Value::String(v.clone())))
}
fn element_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
if self.id.is_none() && self.extension.is_empty() {
return Ok(None);
}
let mut object = super::super::codec::Object::new();
if let Some(id) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(id.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
super::super::codec::Value::Array(items),
);
}
Ok(Some(super::super::codec::Value::Object(object)))
}
fn has_value(&self) -> bool {
self.value.is_some()
}
fn has_element(&self) -> bool {
self.id.is_some() || !self.extension.is_empty()
}
fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self.value.as_ref() {
Some(v) => serde::Serializer::serialize_str(serializer, v),
None => serde::Serializer::serialize_none(serializer),
}
}
fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.id.is_none() && self.extension.is_empty() {
return serde::Serializer::serialize_none(serializer);
}
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(id) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
}
serde::ser::SerializeMap::end(map)
}
fn from_json_parts(
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
if value.is_none() && element.is_none() && !path.is_lenient() {
return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
}
let value = match value {
Some(value) => {
let value = super::super::codec::expect_single(value, path)?;
Some(checked_id(
super::super::codec::expect_string(value, path)?,
path,
)?)
}
None => None,
};
let mut id = None;
let mut extension = Vec::new();
if let Some(element) = element {
let object = super::super::codec::expect_object(element, path)?;
for (key, item) in object {
match key.as_str() {
"id" => {
id = Some(path.with("id", |path| {
super::primitives::checked_string(
super::super::codec::expect_string(item, path)?,
path,
)
})?);
}
"extension" => {
for (index, entry) in super::super::codec::expect_array(item, path)?
.iter()
.enumerate()
{
extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(entry, path)?,
path,
)
})?);
}
}
_ => {
return path.with(key, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
}
Ok(Self {
id,
extension,
value,
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Instant {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub value: Option<std::string::String>,
}
impl From<std::string::String> for Instant {
fn from(value: std::string::String) -> Self {
Self {
value: Some(value),
..Self::default()
}
}
}
impl From<&str> for Instant {
fn from(value: &str) -> Self {
Self::from(std::string::String::from(value))
}
}
#[expect(
clippy::expect_used,
reason = "the emitter compiles every lexical form it writes, so the pattern holds"
)]
static INSTANT_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> = std::sync::LazyLock::new(
|| {
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)))$")
.unicode(false)
.build()
.expect("the lexical form of `instant` should compile")
},
);
pub(crate) fn checked_instant(
text: std::string::String,
path: &super::super::codec::Path,
) -> Result<std::string::String, super::super::codec::DecodeError> {
if INSTANT_LEXICAL_FORM.is_match(text.as_bytes()) {
Ok(text)
} else {
Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
}
}
impl super::super::codec::Primitive for Instant {
fn value_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
Ok(self
.value
.as_ref()
.map(|v| super::super::codec::Value::String(v.clone())))
}
fn element_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
if self.id.is_none() && self.extension.is_empty() {
return Ok(None);
}
let mut object = super::super::codec::Object::new();
if let Some(id) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(id.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
super::super::codec::Value::Array(items),
);
}
Ok(Some(super::super::codec::Value::Object(object)))
}
fn has_value(&self) -> bool {
self.value.is_some()
}
fn has_element(&self) -> bool {
self.id.is_some() || !self.extension.is_empty()
}
fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self.value.as_ref() {
Some(v) => serde::Serializer::serialize_str(serializer, v),
None => serde::Serializer::serialize_none(serializer),
}
}
fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.id.is_none() && self.extension.is_empty() {
return serde::Serializer::serialize_none(serializer);
}
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(id) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
}
serde::ser::SerializeMap::end(map)
}
fn from_json_parts(
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
if value.is_none() && element.is_none() && !path.is_lenient() {
return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
}
let value = match value {
Some(value) => {
let value = super::super::codec::expect_single(value, path)?;
Some(checked_instant(
super::super::codec::expect_string(value, path)?,
path,
)?)
}
None => None,
};
let mut id = None;
let mut extension = Vec::new();
if let Some(element) = element {
let object = super::super::codec::expect_object(element, path)?;
for (key, item) in object {
match key.as_str() {
"id" => {
id = Some(path.with("id", |path| {
super::primitives::checked_string(
super::super::codec::expect_string(item, path)?,
path,
)
})?);
}
"extension" => {
for (index, entry) in super::super::codec::expect_array(item, path)?
.iter()
.enumerate()
{
extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(entry, path)?,
path,
)
})?);
}
}
_ => {
return path.with(key, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
}
Ok(Self {
id,
extension,
value,
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Integer {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub value: Option<i32>,
}
impl From<i32> for Integer {
fn from(value: i32) -> Self {
Self {
value: Some(value),
..Self::default()
}
}
}
impl super::super::codec::Primitive for Integer {
fn value_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
Ok(self
.value
.as_ref()
.map(|v| super::super::codec::Value::from(*v)))
}
fn element_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
if self.id.is_none() && self.extension.is_empty() {
return Ok(None);
}
let mut object = super::super::codec::Object::new();
if let Some(id) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(id.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
super::super::codec::Value::Array(items),
);
}
Ok(Some(super::super::codec::Value::Object(object)))
}
fn has_value(&self) -> bool {
self.value.is_some()
}
fn has_element(&self) -> bool {
self.id.is_some() || !self.extension.is_empty()
}
fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self.value.as_ref() {
Some(v) => serde::Serializer::serialize_i32(serializer, *v),
None => serde::Serializer::serialize_none(serializer),
}
}
fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.id.is_none() && self.extension.is_empty() {
return serde::Serializer::serialize_none(serializer);
}
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(id) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
}
serde::ser::SerializeMap::end(map)
}
fn from_json_parts(
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
if value.is_none() && element.is_none() && !path.is_lenient() {
return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
}
let value = match value {
Some(value) => {
let value = super::super::codec::expect_single(value, path)?;
Some(super::super::codec::expect_i32(value, path)?)
}
None => None,
};
let mut id = None;
let mut extension = Vec::new();
if let Some(element) = element {
let object = super::super::codec::expect_object(element, path)?;
for (key, item) in object {
match key.as_str() {
"id" => {
id = Some(path.with("id", |path| {
super::primitives::checked_string(
super::super::codec::expect_string(item, path)?,
path,
)
})?);
}
"extension" => {
for (index, entry) in super::super::codec::expect_array(item, path)?
.iter()
.enumerate()
{
extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(entry, path)?,
path,
)
})?);
}
}
_ => {
return path.with(key, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
}
Ok(Self {
id,
extension,
value,
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Integer64 {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub value: Option<i64>,
}
impl From<i64> for Integer64 {
fn from(value: i64) -> Self {
Self {
value: Some(value),
..Self::default()
}
}
}
impl super::super::codec::Primitive for Integer64 {
fn value_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
Ok(self
.value
.as_ref()
.map(|v| super::super::codec::Value::String(v.to_string())))
}
fn element_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
if self.id.is_none() && self.extension.is_empty() {
return Ok(None);
}
let mut object = super::super::codec::Object::new();
if let Some(id) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(id.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
super::super::codec::Value::Array(items),
);
}
Ok(Some(super::super::codec::Value::Object(object)))
}
fn has_value(&self) -> bool {
self.value.is_some()
}
fn has_element(&self) -> bool {
self.id.is_some() || !self.extension.is_empty()
}
fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self.value.as_ref() {
Some(v) => serde::Serializer::serialize_str(serializer, &v.to_string()),
None => serde::Serializer::serialize_none(serializer),
}
}
fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.id.is_none() && self.extension.is_empty() {
return serde::Serializer::serialize_none(serializer);
}
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(id) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
}
serde::ser::SerializeMap::end(map)
}
fn from_json_parts(
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
if value.is_none() && element.is_none() && !path.is_lenient() {
return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
}
let value = match value {
Some(value) => {
let value = super::super::codec::expect_single(value, path)?;
Some(super::super::codec::expect_i64_string(value, path)?)
}
None => None,
};
let mut id = None;
let mut extension = Vec::new();
if let Some(element) = element {
let object = super::super::codec::expect_object(element, path)?;
for (key, item) in object {
match key.as_str() {
"id" => {
id = Some(path.with("id", |path| {
super::primitives::checked_string(
super::super::codec::expect_string(item, path)?,
path,
)
})?);
}
"extension" => {
for (index, entry) in super::super::codec::expect_array(item, path)?
.iter()
.enumerate()
{
extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(entry, path)?,
path,
)
})?);
}
}
_ => {
return path.with(key, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
}
Ok(Self {
id,
extension,
value,
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Markdown {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub value: Option<std::string::String>,
}
impl From<std::string::String> for Markdown {
fn from(value: std::string::String) -> Self {
Self {
value: Some(value),
..Self::default()
}
}
}
impl From<&str> for Markdown {
fn from(value: &str) -> Self {
Self::from(std::string::String::from(value))
}
}
#[expect(
clippy::expect_used,
reason = "the emitter compiles every lexical form it writes, so the pattern holds"
)]
static MARKDOWN_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> =
std::sync::LazyLock::new(|| {
regex::bytes::RegexBuilder::new(r"^(?:^[\s\S]+$)$")
.unicode(false)
.build()
.expect("the lexical form of `markdown` should compile")
});
pub(crate) fn checked_markdown(
text: std::string::String,
path: &super::super::codec::Path,
) -> Result<std::string::String, super::super::codec::DecodeError> {
if MARKDOWN_LEXICAL_FORM.is_match(text.as_bytes()) {
Ok(text)
} else {
Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
}
}
impl super::super::codec::Primitive for Markdown {
fn value_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
Ok(self
.value
.as_ref()
.map(|v| super::super::codec::Value::String(v.clone())))
}
fn element_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
if self.id.is_none() && self.extension.is_empty() {
return Ok(None);
}
let mut object = super::super::codec::Object::new();
if let Some(id) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(id.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
super::super::codec::Value::Array(items),
);
}
Ok(Some(super::super::codec::Value::Object(object)))
}
fn has_value(&self) -> bool {
self.value.is_some()
}
fn has_element(&self) -> bool {
self.id.is_some() || !self.extension.is_empty()
}
fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self.value.as_ref() {
Some(v) => serde::Serializer::serialize_str(serializer, v),
None => serde::Serializer::serialize_none(serializer),
}
}
fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.id.is_none() && self.extension.is_empty() {
return serde::Serializer::serialize_none(serializer);
}
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(id) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
}
serde::ser::SerializeMap::end(map)
}
fn from_json_parts(
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
if value.is_none() && element.is_none() && !path.is_lenient() {
return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
}
let value = match value {
Some(value) => {
let value = super::super::codec::expect_single(value, path)?;
Some(checked_markdown(
super::super::codec::expect_string(value, path)?,
path,
)?)
}
None => None,
};
let mut id = None;
let mut extension = Vec::new();
if let Some(element) = element {
let object = super::super::codec::expect_object(element, path)?;
for (key, item) in object {
match key.as_str() {
"id" => {
id = Some(path.with("id", |path| {
super::primitives::checked_string(
super::super::codec::expect_string(item, path)?,
path,
)
})?);
}
"extension" => {
for (index, entry) in super::super::codec::expect_array(item, path)?
.iter()
.enumerate()
{
extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(entry, path)?,
path,
)
})?);
}
}
_ => {
return path.with(key, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
}
Ok(Self {
id,
extension,
value,
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Oid {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub value: Option<std::string::String>,
}
impl From<std::string::String> for Oid {
fn from(value: std::string::String) -> Self {
Self {
value: Some(value),
..Self::default()
}
}
}
impl From<&str> for Oid {
fn from(value: &str) -> Self {
Self::from(std::string::String::from(value))
}
}
#[expect(
clippy::expect_used,
reason = "the emitter compiles every lexical form it writes, so the pattern holds"
)]
static OID_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> =
std::sync::LazyLock::new(|| {
regex::bytes::RegexBuilder::new(r"^(?:urn:oid:[0-2](\.(0|[1-9][0-9]*))+)$")
.unicode(false)
.build()
.expect("the lexical form of `oid` should compile")
});
pub(crate) fn checked_oid(
text: std::string::String,
path: &super::super::codec::Path,
) -> Result<std::string::String, super::super::codec::DecodeError> {
if OID_LEXICAL_FORM.is_match(text.as_bytes()) {
Ok(text)
} else {
Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
}
}
impl super::super::codec::Primitive for Oid {
fn value_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
Ok(self
.value
.as_ref()
.map(|v| super::super::codec::Value::String(v.clone())))
}
fn element_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
if self.id.is_none() && self.extension.is_empty() {
return Ok(None);
}
let mut object = super::super::codec::Object::new();
if let Some(id) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(id.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
super::super::codec::Value::Array(items),
);
}
Ok(Some(super::super::codec::Value::Object(object)))
}
fn has_value(&self) -> bool {
self.value.is_some()
}
fn has_element(&self) -> bool {
self.id.is_some() || !self.extension.is_empty()
}
fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self.value.as_ref() {
Some(v) => serde::Serializer::serialize_str(serializer, v),
None => serde::Serializer::serialize_none(serializer),
}
}
fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.id.is_none() && self.extension.is_empty() {
return serde::Serializer::serialize_none(serializer);
}
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(id) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
}
serde::ser::SerializeMap::end(map)
}
fn from_json_parts(
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
if value.is_none() && element.is_none() && !path.is_lenient() {
return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
}
let value = match value {
Some(value) => {
let value = super::super::codec::expect_single(value, path)?;
Some(checked_oid(
super::super::codec::expect_string(value, path)?,
path,
)?)
}
None => None,
};
let mut id = None;
let mut extension = Vec::new();
if let Some(element) = element {
let object = super::super::codec::expect_object(element, path)?;
for (key, item) in object {
match key.as_str() {
"id" => {
id = Some(path.with("id", |path| {
super::primitives::checked_string(
super::super::codec::expect_string(item, path)?,
path,
)
})?);
}
"extension" => {
for (index, entry) in super::super::codec::expect_array(item, path)?
.iter()
.enumerate()
{
extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(entry, path)?,
path,
)
})?);
}
}
_ => {
return path.with(key, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
}
Ok(Self {
id,
extension,
value,
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PositiveInt {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub value: Option<u32>,
}
impl From<u32> for PositiveInt {
fn from(value: u32) -> Self {
Self {
value: Some(value),
..Self::default()
}
}
}
impl super::super::codec::Primitive for PositiveInt {
fn value_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
Ok(self
.value
.as_ref()
.map(|v| super::super::codec::Value::from(*v)))
}
fn element_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
if self.id.is_none() && self.extension.is_empty() {
return Ok(None);
}
let mut object = super::super::codec::Object::new();
if let Some(id) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(id.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
super::super::codec::Value::Array(items),
);
}
Ok(Some(super::super::codec::Value::Object(object)))
}
fn has_value(&self) -> bool {
self.value.is_some()
}
fn has_element(&self) -> bool {
self.id.is_some() || !self.extension.is_empty()
}
fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self.value.as_ref() {
Some(v) => serde::Serializer::serialize_u32(serializer, *v),
None => serde::Serializer::serialize_none(serializer),
}
}
fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.id.is_none() && self.extension.is_empty() {
return serde::Serializer::serialize_none(serializer);
}
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(id) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
}
serde::ser::SerializeMap::end(map)
}
fn from_json_parts(
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
if value.is_none() && element.is_none() && !path.is_lenient() {
return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
}
let value = match value {
Some(value) => {
let value = super::super::codec::expect_single(value, path)?;
Some(super::super::codec::expect_u32(value, 1, path)?)
}
None => None,
};
let mut id = None;
let mut extension = Vec::new();
if let Some(element) = element {
let object = super::super::codec::expect_object(element, path)?;
for (key, item) in object {
match key.as_str() {
"id" => {
id = Some(path.with("id", |path| {
super::primitives::checked_string(
super::super::codec::expect_string(item, path)?,
path,
)
})?);
}
"extension" => {
for (index, entry) in super::super::codec::expect_array(item, path)?
.iter()
.enumerate()
{
extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(entry, path)?,
path,
)
})?);
}
}
_ => {
return path.with(key, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
}
Ok(Self {
id,
extension,
value,
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct String {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub value: Option<std::string::String>,
}
impl From<std::string::String> for String {
fn from(value: std::string::String) -> Self {
Self {
value: Some(value),
..Self::default()
}
}
}
impl From<&str> for String {
fn from(value: &str) -> Self {
Self::from(std::string::String::from(value))
}
}
#[expect(
clippy::expect_used,
reason = "the emitter compiles every lexical form it writes, so the pattern holds"
)]
static STRING_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> =
std::sync::LazyLock::new(|| {
regex::bytes::RegexBuilder::new(r"^(?:^[\s\S]+$)$")
.unicode(false)
.build()
.expect("the lexical form of `string` should compile")
});
pub(crate) fn checked_string(
text: std::string::String,
path: &super::super::codec::Path,
) -> Result<std::string::String, super::super::codec::DecodeError> {
if STRING_LEXICAL_FORM.is_match(text.as_bytes()) {
Ok(text)
} else {
Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
}
}
impl super::super::codec::Primitive for String {
fn value_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
Ok(self
.value
.as_ref()
.map(|v| super::super::codec::Value::String(v.clone())))
}
fn element_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
if self.id.is_none() && self.extension.is_empty() {
return Ok(None);
}
let mut object = super::super::codec::Object::new();
if let Some(id) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(id.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
super::super::codec::Value::Array(items),
);
}
Ok(Some(super::super::codec::Value::Object(object)))
}
fn has_value(&self) -> bool {
self.value.is_some()
}
fn has_element(&self) -> bool {
self.id.is_some() || !self.extension.is_empty()
}
fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self.value.as_ref() {
Some(v) => serde::Serializer::serialize_str(serializer, v),
None => serde::Serializer::serialize_none(serializer),
}
}
fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.id.is_none() && self.extension.is_empty() {
return serde::Serializer::serialize_none(serializer);
}
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(id) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
}
serde::ser::SerializeMap::end(map)
}
fn from_json_parts(
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
if value.is_none() && element.is_none() && !path.is_lenient() {
return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
}
let value = match value {
Some(value) => {
let value = super::super::codec::expect_single(value, path)?;
Some(checked_string(
super::super::codec::expect_string(value, path)?,
path,
)?)
}
None => None,
};
let mut id = None;
let mut extension = Vec::new();
if let Some(element) = element {
let object = super::super::codec::expect_object(element, path)?;
for (key, item) in object {
match key.as_str() {
"id" => {
id = Some(path.with("id", |path| {
super::primitives::checked_string(
super::super::codec::expect_string(item, path)?,
path,
)
})?);
}
"extension" => {
for (index, entry) in super::super::codec::expect_array(item, path)?
.iter()
.enumerate()
{
extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(entry, path)?,
path,
)
})?);
}
}
_ => {
return path.with(key, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
}
Ok(Self {
id,
extension,
value,
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Time {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub value: Option<std::string::String>,
}
impl From<std::string::String> for Time {
fn from(value: std::string::String) -> Self {
Self {
value: Some(value),
..Self::default()
}
}
}
impl From<&str> for Time {
fn from(value: &str) -> Self {
Self::from(std::string::String::from(value))
}
}
#[expect(
clippy::expect_used,
reason = "the emitter compiles every lexical form it writes, so the pattern holds"
)]
static TIME_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> =
std::sync::LazyLock::new(|| {
regex::bytes::RegexBuilder::new(
r"^(?:([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\.[0-9]{1,9})?)$",
)
.unicode(false)
.build()
.expect("the lexical form of `time` should compile")
});
pub(crate) fn checked_time(
text: std::string::String,
path: &super::super::codec::Path,
) -> Result<std::string::String, super::super::codec::DecodeError> {
if TIME_LEXICAL_FORM.is_match(text.as_bytes()) {
Ok(text)
} else {
Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
}
}
impl super::super::codec::Primitive for Time {
fn value_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
Ok(self
.value
.as_ref()
.map(|v| super::super::codec::Value::String(v.clone())))
}
fn element_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
if self.id.is_none() && self.extension.is_empty() {
return Ok(None);
}
let mut object = super::super::codec::Object::new();
if let Some(id) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(id.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
super::super::codec::Value::Array(items),
);
}
Ok(Some(super::super::codec::Value::Object(object)))
}
fn has_value(&self) -> bool {
self.value.is_some()
}
fn has_element(&self) -> bool {
self.id.is_some() || !self.extension.is_empty()
}
fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self.value.as_ref() {
Some(v) => serde::Serializer::serialize_str(serializer, v),
None => serde::Serializer::serialize_none(serializer),
}
}
fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.id.is_none() && self.extension.is_empty() {
return serde::Serializer::serialize_none(serializer);
}
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(id) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
}
serde::ser::SerializeMap::end(map)
}
fn from_json_parts(
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
if value.is_none() && element.is_none() && !path.is_lenient() {
return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
}
let value = match value {
Some(value) => {
let value = super::super::codec::expect_single(value, path)?;
Some(checked_time(
super::super::codec::expect_string(value, path)?,
path,
)?)
}
None => None,
};
let mut id = None;
let mut extension = Vec::new();
if let Some(element) = element {
let object = super::super::codec::expect_object(element, path)?;
for (key, item) in object {
match key.as_str() {
"id" => {
id = Some(path.with("id", |path| {
super::primitives::checked_string(
super::super::codec::expect_string(item, path)?,
path,
)
})?);
}
"extension" => {
for (index, entry) in super::super::codec::expect_array(item, path)?
.iter()
.enumerate()
{
extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(entry, path)?,
path,
)
})?);
}
}
_ => {
return path.with(key, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
}
Ok(Self {
id,
extension,
value,
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct UnsignedInt {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub value: Option<u32>,
}
impl From<u32> for UnsignedInt {
fn from(value: u32) -> Self {
Self {
value: Some(value),
..Self::default()
}
}
}
impl super::super::codec::Primitive for UnsignedInt {
fn value_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
Ok(self
.value
.as_ref()
.map(|v| super::super::codec::Value::from(*v)))
}
fn element_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
if self.id.is_none() && self.extension.is_empty() {
return Ok(None);
}
let mut object = super::super::codec::Object::new();
if let Some(id) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(id.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
super::super::codec::Value::Array(items),
);
}
Ok(Some(super::super::codec::Value::Object(object)))
}
fn has_value(&self) -> bool {
self.value.is_some()
}
fn has_element(&self) -> bool {
self.id.is_some() || !self.extension.is_empty()
}
fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self.value.as_ref() {
Some(v) => serde::Serializer::serialize_u32(serializer, *v),
None => serde::Serializer::serialize_none(serializer),
}
}
fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.id.is_none() && self.extension.is_empty() {
return serde::Serializer::serialize_none(serializer);
}
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(id) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
}
serde::ser::SerializeMap::end(map)
}
fn from_json_parts(
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
if value.is_none() && element.is_none() && !path.is_lenient() {
return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
}
let value = match value {
Some(value) => {
let value = super::super::codec::expect_single(value, path)?;
Some(super::super::codec::expect_u32(value, 0, path)?)
}
None => None,
};
let mut id = None;
let mut extension = Vec::new();
if let Some(element) = element {
let object = super::super::codec::expect_object(element, path)?;
for (key, item) in object {
match key.as_str() {
"id" => {
id = Some(path.with("id", |path| {
super::primitives::checked_string(
super::super::codec::expect_string(item, path)?,
path,
)
})?);
}
"extension" => {
for (index, entry) in super::super::codec::expect_array(item, path)?
.iter()
.enumerate()
{
extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(entry, path)?,
path,
)
})?);
}
}
_ => {
return path.with(key, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
}
Ok(Self {
id,
extension,
value,
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Uri {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub value: Option<std::string::String>,
}
impl From<std::string::String> for Uri {
fn from(value: std::string::String) -> Self {
Self {
value: Some(value),
..Self::default()
}
}
}
impl From<&str> for Uri {
fn from(value: &str) -> Self {
Self::from(std::string::String::from(value))
}
}
#[expect(
clippy::expect_used,
reason = "the emitter compiles every lexical form it writes, so the pattern holds"
)]
static URI_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> =
std::sync::LazyLock::new(|| {
regex::bytes::RegexBuilder::new(r"^(?:\S*)$")
.unicode(false)
.build()
.expect("the lexical form of `uri` should compile")
});
pub(crate) fn checked_uri(
text: std::string::String,
path: &super::super::codec::Path,
) -> Result<std::string::String, super::super::codec::DecodeError> {
if URI_LEXICAL_FORM.is_match(text.as_bytes()) {
Ok(text)
} else {
Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
}
}
impl super::super::codec::Primitive for Uri {
fn value_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
Ok(self
.value
.as_ref()
.map(|v| super::super::codec::Value::String(v.clone())))
}
fn element_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
if self.id.is_none() && self.extension.is_empty() {
return Ok(None);
}
let mut object = super::super::codec::Object::new();
if let Some(id) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(id.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
super::super::codec::Value::Array(items),
);
}
Ok(Some(super::super::codec::Value::Object(object)))
}
fn has_value(&self) -> bool {
self.value.is_some()
}
fn has_element(&self) -> bool {
self.id.is_some() || !self.extension.is_empty()
}
fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self.value.as_ref() {
Some(v) => serde::Serializer::serialize_str(serializer, v),
None => serde::Serializer::serialize_none(serializer),
}
}
fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.id.is_none() && self.extension.is_empty() {
return serde::Serializer::serialize_none(serializer);
}
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(id) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
}
serde::ser::SerializeMap::end(map)
}
fn from_json_parts(
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
if value.is_none() && element.is_none() && !path.is_lenient() {
return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
}
let value = match value {
Some(value) => {
let value = super::super::codec::expect_single(value, path)?;
Some(checked_uri(
super::super::codec::expect_string(value, path)?,
path,
)?)
}
None => None,
};
let mut id = None;
let mut extension = Vec::new();
if let Some(element) = element {
let object = super::super::codec::expect_object(element, path)?;
for (key, item) in object {
match key.as_str() {
"id" => {
id = Some(path.with("id", |path| {
super::primitives::checked_string(
super::super::codec::expect_string(item, path)?,
path,
)
})?);
}
"extension" => {
for (index, entry) in super::super::codec::expect_array(item, path)?
.iter()
.enumerate()
{
extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(entry, path)?,
path,
)
})?);
}
}
_ => {
return path.with(key, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
}
Ok(Self {
id,
extension,
value,
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Url {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub value: Option<std::string::String>,
}
impl From<std::string::String> for Url {
fn from(value: std::string::String) -> Self {
Self {
value: Some(value),
..Self::default()
}
}
}
impl From<&str> for Url {
fn from(value: &str) -> Self {
Self::from(std::string::String::from(value))
}
}
#[expect(
clippy::expect_used,
reason = "the emitter compiles every lexical form it writes, so the pattern holds"
)]
static URL_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> =
std::sync::LazyLock::new(|| {
regex::bytes::RegexBuilder::new(r"^(?:\S*)$")
.unicode(false)
.build()
.expect("the lexical form of `url` should compile")
});
pub(crate) fn checked_url(
text: std::string::String,
path: &super::super::codec::Path,
) -> Result<std::string::String, super::super::codec::DecodeError> {
if URL_LEXICAL_FORM.is_match(text.as_bytes()) {
Ok(text)
} else {
Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
}
}
impl super::super::codec::Primitive for Url {
fn value_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
Ok(self
.value
.as_ref()
.map(|v| super::super::codec::Value::String(v.clone())))
}
fn element_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
if self.id.is_none() && self.extension.is_empty() {
return Ok(None);
}
let mut object = super::super::codec::Object::new();
if let Some(id) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(id.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
super::super::codec::Value::Array(items),
);
}
Ok(Some(super::super::codec::Value::Object(object)))
}
fn has_value(&self) -> bool {
self.value.is_some()
}
fn has_element(&self) -> bool {
self.id.is_some() || !self.extension.is_empty()
}
fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self.value.as_ref() {
Some(v) => serde::Serializer::serialize_str(serializer, v),
None => serde::Serializer::serialize_none(serializer),
}
}
fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.id.is_none() && self.extension.is_empty() {
return serde::Serializer::serialize_none(serializer);
}
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(id) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
}
serde::ser::SerializeMap::end(map)
}
fn from_json_parts(
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
if value.is_none() && element.is_none() && !path.is_lenient() {
return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
}
let value = match value {
Some(value) => {
let value = super::super::codec::expect_single(value, path)?;
Some(checked_url(
super::super::codec::expect_string(value, path)?,
path,
)?)
}
None => None,
};
let mut id = None;
let mut extension = Vec::new();
if let Some(element) = element {
let object = super::super::codec::expect_object(element, path)?;
for (key, item) in object {
match key.as_str() {
"id" => {
id = Some(path.with("id", |path| {
super::primitives::checked_string(
super::super::codec::expect_string(item, path)?,
path,
)
})?);
}
"extension" => {
for (index, entry) in super::super::codec::expect_array(item, path)?
.iter()
.enumerate()
{
extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(entry, path)?,
path,
)
})?);
}
}
_ => {
return path.with(key, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
}
Ok(Self {
id,
extension,
value,
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Uuid {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub value: Option<std::string::String>,
}
impl From<std::string::String> for Uuid {
fn from(value: std::string::String) -> Self {
Self {
value: Some(value),
..Self::default()
}
}
}
impl From<&str> for Uuid {
fn from(value: &str) -> Self {
Self::from(std::string::String::from(value))
}
}
#[expect(
clippy::expect_used,
reason = "the emitter compiles every lexical form it writes, so the pattern holds"
)]
static UUID_LEXICAL_FORM: std::sync::LazyLock<regex::bytes::Regex> =
std::sync::LazyLock::new(|| {
regex::bytes::RegexBuilder::new(
r"^(?:urn:uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$",
)
.unicode(false)
.build()
.expect("the lexical form of `uuid` should compile")
});
pub(crate) fn checked_uuid(
text: std::string::String,
path: &super::super::codec::Path,
) -> Result<std::string::String, super::super::codec::DecodeError> {
if UUID_LEXICAL_FORM.is_match(text.as_bytes()) {
Ok(text)
} else {
Err(path.error(super::super::codec::DecodeErrorKind::BadValue))
}
}
impl super::super::codec::Primitive for Uuid {
fn value_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
Ok(self
.value
.as_ref()
.map(|v| super::super::codec::Value::String(v.clone())))
}
fn element_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
if self.id.is_none() && self.extension.is_empty() {
return Ok(None);
}
let mut object = super::super::codec::Object::new();
if let Some(id) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(id.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
super::super::codec::Value::Array(items),
);
}
Ok(Some(super::super::codec::Value::Object(object)))
}
fn has_value(&self) -> bool {
self.value.is_some()
}
fn has_element(&self) -> bool {
self.id.is_some() || !self.extension.is_empty()
}
fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self.value.as_ref() {
Some(v) => serde::Serializer::serialize_str(serializer, v),
None => serde::Serializer::serialize_none(serializer),
}
}
fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.id.is_none() && self.extension.is_empty() {
return serde::Serializer::serialize_none(serializer);
}
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(id) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
}
serde::ser::SerializeMap::end(map)
}
fn from_json_parts(
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
if value.is_none() && element.is_none() && !path.is_lenient() {
return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
}
let value = match value {
Some(value) => {
let value = super::super::codec::expect_single(value, path)?;
Some(checked_uuid(
super::super::codec::expect_string(value, path)?,
path,
)?)
}
None => None,
};
let mut id = None;
let mut extension = Vec::new();
if let Some(element) = element {
let object = super::super::codec::expect_object(element, path)?;
for (key, item) in object {
match key.as_str() {
"id" => {
id = Some(path.with("id", |path| {
super::primitives::checked_string(
super::super::codec::expect_string(item, path)?,
path,
)
})?);
}
"extension" => {
for (index, entry) in super::super::codec::expect_array(item, path)?
.iter()
.enumerate()
{
extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(entry, path)?,
path,
)
})?);
}
}
_ => {
return path.with(key, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
}
Ok(Self {
id,
extension,
value,
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Xhtml {
pub id: Option<std::string::String>,
pub value: std::string::String,
}
impl From<std::string::String> for Xhtml {
fn from(value: std::string::String) -> Self {
Self {
value,
..Self::default()
}
}
}
impl From<&str> for Xhtml {
fn from(value: &str) -> Self {
Self::from(std::string::String::from(value))
}
}
impl super::super::codec::Primitive for Xhtml {
fn value_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
Ok(Some(&self.value).map(|v| super::super::codec::Value::String(v.clone())))
}
fn element_json(
&self,
) -> Result<Option<super::super::codec::Value>, super::super::codec::EncodeError> {
if self.id.is_none() {
return Ok(None);
}
let mut object = super::super::codec::Object::new();
if let Some(id) = &self.id {
object.insert(
std::string::String::from("id"),
super::super::codec::Value::String(id.clone()),
);
}
Ok(Some(super::super::codec::Value::Object(object)))
}
fn has_value(&self) -> bool {
true
}
fn has_element(&self) -> bool {
self.id.is_some()
}
fn serialize_value<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serde::Serializer::serialize_str(serializer, &self.value)
}
fn serialize_element<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if self.id.is_none() {
return serde::Serializer::serialize_none(serializer);
}
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if let Some(id) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", id)?;
}
serde::ser::SerializeMap::end(map)
}
fn from_json_parts(
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
if value.is_none() && element.is_none() && !path.is_lenient() {
return Err(path.error(super::super::codec::DecodeErrorKind::MissingProperty));
}
let value = match value {
Some(value) => {
let value = super::super::codec::expect_single(value, path)?;
Some(super::super::codec::expect_string(value, path)?)
}
None => None,
};
let value = value
.ok_or_else(|| path.error(super::super::codec::DecodeErrorKind::MissingProperty))?;
let mut id = None;
if let Some(element) = element {
let object = super::super::codec::expect_object(element, path)?;
for (key, item) in object {
match key.as_str() {
"id" => {
id = Some(path.with("id", |path| {
super::primitives::checked_string(
super::super::codec::expect_string(item, path)?,
path,
)
})?);
}
_ => {
return path.with(key, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
}
Ok(Self { id, value })
}
}