#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct DosageSafety {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub modifier_extension: Vec<super::extension::Extension>,
pub dose_limit: Vec<DosageSafetyDoseLimit>,
pub if_exceeded: Option<super::primitives::String>,
}
impl super::super::codec::Json for DosageSafety {
fn to_json(&self) -> Result<super::super::codec::Object, super::super::codec::EncodeError> {
let mut object = super::super::codec::Object::new();
if let Some(v) = &self.id {
object.insert(
std::string::String::from("id"),
serde_json::Value::String(v.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(serde_json::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
serde_json::Value::Array(items),
);
}
if !self.modifier_extension.is_empty() {
let mut items = Vec::with_capacity(self.modifier_extension.len());
for item in &self.modifier_extension {
items.push(serde_json::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("modifierExtension"),
serde_json::Value::Array(items),
);
}
if !self.dose_limit.is_empty() {
let mut items = Vec::with_capacity(self.dose_limit.len());
for item in &self.dose_limit {
items.push(serde_json::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("doseLimit"),
serde_json::Value::Array(items),
);
}
if let Some(item) = &self.if_exceeded {
if let Some(v) = super::super::codec::Primitive::value_json(item)? {
object.insert(std::string::String::from("ifExceeded"), v);
}
if let Some(e) = super::super::codec::Primitive::element_json(item)? {
object.insert(std::string::String::from("_ifExceeded"), e);
}
}
Ok(object)
}
fn from_json(
object: &super::super::codec::Object,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
let mut raw_id: Option<&serde_json::Value> = None;
let mut raw_extension: Option<&serde_json::Value> = None;
let mut raw_modifier_extension: Option<&serde_json::Value> = None;
let mut raw_dose_limit: Option<&serde_json::Value> = None;
let mut raw_if_exceeded: Option<&serde_json::Value> = None;
let mut raw_if_exceeded_element: Option<&serde_json::Value> = None;
for (key, value) in object {
match key.as_str() {
"id" => raw_id = Some(value),
"extension" => raw_extension = Some(value),
"modifierExtension" => raw_modifier_extension = Some(value),
"doseLimit" => raw_dose_limit = Some(value),
"ifExceeded" => raw_if_exceeded = Some(value),
"_ifExceeded" => raw_if_exceeded_element = Some(value),
other => {
return path.with(other, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
let field_id = raw_id
.map(|value| {
path.with("id", |path| {
let value = super::super::codec::expect_single(value, path)?;
super::super::codec::expect_string(value, path)
})
})
.transpose()?;
let mut field_extension = Vec::new();
if let Some(raw) = raw_extension {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
let mut field_modifier_extension = Vec::new();
if let Some(raw) = raw_modifier_extension {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_modifier_extension.push(path.with_index(
"modifierExtension",
index,
|path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
},
)?);
}
}
let mut field_dose_limit = Vec::new();
if let Some(raw) = raw_dose_limit {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_dose_limit.push(path.with_index("doseLimit", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
let field_if_exceeded = match (raw_if_exceeded, raw_if_exceeded_element) {
(None, None) => None,
(value, element) => Some(path.with("ifExceeded", |path| {
super::super::codec::Primitive::from_json_parts(value, element, path)
})?),
};
Ok(Self {
id: field_id,
extension: field_extension,
modifier_extension: field_modifier_extension,
dose_limit: field_dose_limit,
if_exceeded: field_if_exceeded,
})
}
}
impl serde::Serialize for DosageSafety {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
super::super::codec::Json::to_json(self)
.map_err(serde::ser::Error::custom)?
.serialize(serializer)
}
}
impl<'de> serde::Deserialize<'de> for DosageSafety {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let value = serde_json::Value::deserialize(deserializer)?;
let mut path = super::super::codec::Path::root("DosageSafety");
let object =
super::super::codec::expect_object(&value, &path).map_err(serde::de::Error::custom)?;
super::super::codec::Json::from_json(object, &mut path).map_err(serde::de::Error::custom)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DosageSafetyDoseLimit {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub value: DosageSafetyDoseLimitValue,
pub scope: super::primitives::Code,
pub period: Option<super::duration::Duration>,
pub text: Option<super::primitives::String>,
}
impl super::super::codec::Json for DosageSafetyDoseLimit {
fn to_json(&self) -> Result<super::super::codec::Object, super::super::codec::EncodeError> {
let mut object = super::super::codec::Object::new();
if let Some(v) = &self.id {
object.insert(
std::string::String::from("id"),
serde_json::Value::String(v.clone()),
);
}
if !self.extension.is_empty() {
let mut items = Vec::with_capacity(self.extension.len());
for item in &self.extension {
items.push(serde_json::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("extension"),
serde_json::Value::Array(items),
);
}
let (suffix, value, element) = self.value.to_json_parts()?;
if let Some(value) = value {
object.insert(format!("value{suffix}"), value);
}
if let Some(element) = element {
object.insert(format!("_value{suffix}"), element);
}
if let Some(v) = super::super::codec::Primitive::value_json(&self.scope)? {
object.insert(std::string::String::from("scope"), v);
}
if let Some(e) = super::super::codec::Primitive::element_json(&self.scope)? {
object.insert(std::string::String::from("_scope"), e);
}
if let Some(item) = &self.period {
object.insert(
std::string::String::from("period"),
serde_json::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.text {
if let Some(v) = super::super::codec::Primitive::value_json(item)? {
object.insert(std::string::String::from("text"), v);
}
if let Some(e) = super::super::codec::Primitive::element_json(item)? {
object.insert(std::string::String::from("_text"), e);
}
}
Ok(object)
}
fn from_json(
object: &super::super::codec::Object,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
let mut raw_id: Option<&serde_json::Value> = None;
let mut raw_extension: Option<&serde_json::Value> = None;
let mut raw_value = super::super::codec::ChoiceSlot::default();
let mut raw_scope: Option<&serde_json::Value> = None;
let mut raw_scope_element: Option<&serde_json::Value> = None;
let mut raw_period: Option<&serde_json::Value> = None;
let mut raw_text: Option<&serde_json::Value> = None;
let mut raw_text_element: Option<&serde_json::Value> = None;
for (key, value) in object {
match key.as_str() {
"id" => raw_id = Some(value),
"extension" => raw_extension = Some(value),
"valueInteger" => {
raw_value.value("Integer", value, path)?;
}
"_valueInteger" => {
raw_value.element("Integer", value, path)?;
}
"valueQuantity" => {
raw_value.value("Quantity", value, path)?;
}
"_valueQuantity" => {
raw_value.element("Quantity", value, path)?;
}
"valueExpression" => {
raw_value.value("Expression", value, path)?;
}
"_valueExpression" => {
raw_value.element("Expression", value, path)?;
}
"scope" => raw_scope = Some(value),
"_scope" => raw_scope_element = Some(value),
"period" => raw_period = Some(value),
"text" => raw_text = Some(value),
"_text" => raw_text_element = Some(value),
other => {
return path.with(other, |path| {
Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty))
});
}
}
}
let field_id = raw_id
.map(|value| {
path.with("id", |path| {
let value = super::super::codec::expect_single(value, path)?;
super::super::codec::expect_string(value, path)
})
})
.transpose()?;
let mut field_extension = Vec::new();
if let Some(raw) = raw_extension {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_extension.push(path.with_index("extension", index, |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
})?);
}
}
let field_value = path.with("value", |path| {
let suffix = raw_value
.suffix
.ok_or_else(|| path.error(super::super::codec::DecodeErrorKind::MissingProperty))?;
DosageSafetyDoseLimitValue::from_json_parts(
suffix,
raw_value.value,
raw_value.element,
path,
)
})?;
let field_scope = path.with("scope", |path| {
super::super::codec::Primitive::from_json_parts(raw_scope, raw_scope_element, path)
})?;
let field_period = raw_period
.map(|value| {
path.with("period", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_text = match (raw_text, raw_text_element) {
(None, None) => None,
(value, element) => Some(path.with("text", |path| {
super::super::codec::Primitive::from_json_parts(value, element, path)
})?),
};
Ok(Self {
id: field_id,
extension: field_extension,
value: field_value,
scope: field_scope,
period: field_period,
text: field_text,
})
}
}
impl serde::Serialize for DosageSafetyDoseLimit {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
super::super::codec::Json::to_json(self)
.map_err(serde::ser::Error::custom)?
.serialize(serializer)
}
}
impl<'de> serde::Deserialize<'de> for DosageSafetyDoseLimit {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let value = serde_json::Value::deserialize(deserializer)?;
let mut path = super::super::codec::Path::root("DosageSafetyDoseLimit");
let object =
super::super::codec::expect_object(&value, &path).map_err(serde::de::Error::custom)?;
super::super::codec::Json::from_json(object, &mut path).map_err(serde::de::Error::custom)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DosageSafetyDoseLimitValue {
Integer(super::primitives::Integer),
Quantity(super::quantity::Quantity),
Expression(super::expression::Expression),
}
impl DosageSafetyDoseLimitValue {
pub fn to_json_parts(
&self,
) -> Result<
(
&'static str,
Option<serde_json::Value>,
Option<serde_json::Value>,
),
super::super::codec::EncodeError,
> {
match self {
Self::Integer(inner) => Ok((
"Integer",
super::super::codec::Primitive::value_json(inner)?,
super::super::codec::Primitive::element_json(inner)?,
)),
Self::Quantity(inner) => Ok((
"Quantity",
Some(serde_json::Value::Object(
super::super::codec::Json::to_json(inner)?,
)),
None,
)),
Self::Expression(inner) => Ok((
"Expression",
Some(serde_json::Value::Object(
super::super::codec::Json::to_json(inner)?,
)),
None,
)),
}
}
pub fn from_json_parts(
suffix: &str,
value: Option<&serde_json::Value>,
element: Option<&serde_json::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
match suffix {
"Integer" => Ok(Self::Integer(
super::super::codec::Primitive::from_json_parts(value, element, path)?,
)),
"Quantity" => {
if element.is_some() {
return Err(path.error(super::super::codec::DecodeErrorKind::WrongType {
expected: "no underscore form for a complex type",
}));
}
let value = value.ok_or_else(|| {
path.error(super::super::codec::DecodeErrorKind::MissingProperty)
})?;
let inner = super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)?;
Ok(Self::Quantity(inner))
}
"Expression" => {
if element.is_some() {
return Err(path.error(super::super::codec::DecodeErrorKind::WrongType {
expected: "no underscore form for a complex type",
}));
}
let value = value.ok_or_else(|| {
path.error(super::super::codec::DecodeErrorKind::MissingProperty)
})?;
let inner = super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)?;
Ok(Self::Expression(inner))
}
_ => Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty)),
}
}
}