#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Dosage {
pub id: Option<std::string::String>,
pub extension: Vec<super::extension::Extension>,
pub sequence: Option<super::primitives::Integer>,
pub text: Option<super::primitives::String>,
pub additional_instruction: Vec<super::codeable_concept::CodeableConcept>,
pub patient_instruction: Option<super::primitives::String>,
pub timing: Option<super::timing::Timing>,
pub as_needed: Option<DosageAsNeeded>,
pub site: Option<super::codeable_concept::CodeableConcept>,
pub route: Option<super::codeable_concept::CodeableConcept>,
pub method: Option<super::codeable_concept::CodeableConcept>,
pub dose: Option<DosageDose>,
pub max_dose_per_period: Option<super::ratio::Ratio>,
pub max_dose_per_administration: Option<super::quantity::Quantity>,
pub max_dose_per_lifetime: Option<super::quantity::Quantity>,
pub rate: Option<DosageRate>,
}
impl super::super::codec::Json for Dosage {
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"),
super::super::codec::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(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),
);
}
if let Some(item) = &self.sequence {
if let Some(v) = super::super::codec::Primitive::value_json(item)? {
object.insert(std::string::String::from("sequence"), v);
}
if let Some(e) = super::super::codec::Primitive::element_json(item)? {
object.insert(std::string::String::from("_sequence"), e);
}
}
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);
}
}
if !self.additional_instruction.is_empty() {
let mut items = Vec::with_capacity(self.additional_instruction.len());
for item in &self.additional_instruction {
items.push(super::super::codec::Value::Object(
super::super::codec::Json::to_json(item)?,
));
}
object.insert(
std::string::String::from("additionalInstruction"),
super::super::codec::Value::Array(items),
);
}
if let Some(item) = &self.patient_instruction {
if let Some(v) = super::super::codec::Primitive::value_json(item)? {
object.insert(std::string::String::from("patientInstruction"), v);
}
if let Some(e) = super::super::codec::Primitive::element_json(item)? {
object.insert(std::string::String::from("_patientInstruction"), e);
}
}
if let Some(item) = &self.timing {
object.insert(
std::string::String::from("timing"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.as_needed {
let (suffix, value, element) = item.to_json_parts()?;
if let Some(value) = value {
object.insert(format!("asNeeded{suffix}"), value);
}
if let Some(element) = element {
object.insert(format!("_asNeeded{suffix}"), element);
}
}
if let Some(item) = &self.site {
object.insert(
std::string::String::from("site"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.route {
object.insert(
std::string::String::from("route"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.method {
object.insert(
std::string::String::from("method"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.dose {
let (suffix, value, element) = item.to_json_parts()?;
if let Some(value) = value {
object.insert(format!("dose{suffix}"), value);
}
if let Some(element) = element {
object.insert(format!("_dose{suffix}"), element);
}
}
if let Some(item) = &self.max_dose_per_period {
object.insert(
std::string::String::from("maxDosePerPeriod"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.max_dose_per_administration {
object.insert(
std::string::String::from("maxDosePerAdministration"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.max_dose_per_lifetime {
object.insert(
std::string::String::from("maxDosePerLifetime"),
super::super::codec::Value::Object(super::super::codec::Json::to_json(item)?),
);
}
if let Some(item) = &self.rate {
let (suffix, value, element) = item.to_json_parts()?;
if let Some(value) = value {
object.insert(format!("rate{suffix}"), value);
}
if let Some(element) = element {
object.insert(format!("_rate{suffix}"), element);
}
}
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<&super::super::codec::Value> = None;
let mut raw_extension: Option<&super::super::codec::Value> = None;
let mut raw_sequence: Option<&super::super::codec::Value> = None;
let mut raw_sequence_element: Option<&super::super::codec::Value> = None;
let mut raw_text: Option<&super::super::codec::Value> = None;
let mut raw_text_element: Option<&super::super::codec::Value> = None;
let mut raw_additional_instruction: Option<&super::super::codec::Value> = None;
let mut raw_patient_instruction: Option<&super::super::codec::Value> = None;
let mut raw_patient_instruction_element: Option<&super::super::codec::Value> = None;
let mut raw_timing: Option<&super::super::codec::Value> = None;
let mut raw_as_needed = super::super::codec::ChoiceSlot::default();
let mut raw_site: Option<&super::super::codec::Value> = None;
let mut raw_route: Option<&super::super::codec::Value> = None;
let mut raw_method: Option<&super::super::codec::Value> = None;
let mut raw_dose = super::super::codec::ChoiceSlot::default();
let mut raw_max_dose_per_period: Option<&super::super::codec::Value> = None;
let mut raw_max_dose_per_administration: Option<&super::super::codec::Value> = None;
let mut raw_max_dose_per_lifetime: Option<&super::super::codec::Value> = None;
let mut raw_rate = super::super::codec::ChoiceSlot::default();
for (key, value) in object {
match key.as_str() {
"id" => {
raw_id = Some(value);
}
"extension" => {
raw_extension = Some(value);
}
"sequence" => {
raw_sequence = Some(value);
}
"_sequence" => {
raw_sequence_element = Some(value);
}
"text" => {
raw_text = Some(value);
}
"_text" => {
raw_text_element = Some(value);
}
"additionalInstruction" => {
raw_additional_instruction = Some(value);
}
"patientInstruction" => {
raw_patient_instruction = Some(value);
}
"_patientInstruction" => {
raw_patient_instruction_element = Some(value);
}
"timing" => {
raw_timing = Some(value);
}
"asNeededBoolean" => {
raw_as_needed.value("Boolean", value, path)?;
}
"_asNeededBoolean" => {
raw_as_needed.element("Boolean", value, path)?;
}
"asNeededCodeableConcept" => {
raw_as_needed.value("CodeableConcept", value, path)?;
}
"_asNeededCodeableConcept" => {
raw_as_needed.element("CodeableConcept", value, path)?;
}
"site" => {
raw_site = Some(value);
}
"route" => {
raw_route = Some(value);
}
"method" => {
raw_method = Some(value);
}
"doseRange" => {
raw_dose.value("Range", value, path)?;
}
"_doseRange" => {
raw_dose.element("Range", value, path)?;
}
"doseQuantity" => {
raw_dose.value("Quantity", value, path)?;
}
"_doseQuantity" => {
raw_dose.element("Quantity", value, path)?;
}
"maxDosePerPeriod" => {
raw_max_dose_per_period = Some(value);
}
"maxDosePerAdministration" => {
raw_max_dose_per_administration = Some(value);
}
"maxDosePerLifetime" => {
raw_max_dose_per_lifetime = Some(value);
}
"rateRatio" => {
raw_rate.value("Ratio", value, path)?;
}
"_rateRatio" => {
raw_rate.element("Ratio", value, path)?;
}
"rateRange" => {
raw_rate.value("Range", value, path)?;
}
"_rateRange" => {
raw_rate.element("Range", value, path)?;
}
"rateQuantity" => {
raw_rate.value("Quantity", value, path)?;
}
"_rateQuantity" => {
raw_rate.element("Quantity", value, path)?;
}
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_sequence = match (raw_sequence, raw_sequence_element) {
(None, None) => None,
(value, element) => Some(path.with("sequence", |path| {
super::super::codec::Primitive::from_json_parts(value, element, path)
})?),
};
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)
})?),
};
let mut field_additional_instruction = Vec::new();
if let Some(raw) = raw_additional_instruction {
for (index, value) in super::super::codec::expect_array(raw, path)?
.iter()
.enumerate()
{
field_additional_instruction.push(path.with_index(
"additionalInstruction",
index,
|path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(value, path)?,
path,
)
},
)?);
}
}
let field_patient_instruction =
match (raw_patient_instruction, raw_patient_instruction_element) {
(None, None) => None,
(value, element) => Some(path.with("patientInstruction", |path| {
super::super::codec::Primitive::from_json_parts(value, element, path)
})?),
};
let field_timing = raw_timing
.map(|value| {
path.with("timing", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_as_needed = match raw_as_needed.suffix {
None => None,
Some(suffix) => Some(path.with("asNeeded", |path| {
DosageAsNeeded::from_json_parts(
suffix,
raw_as_needed.value,
raw_as_needed.element,
path,
)
})?),
};
let field_site = raw_site
.map(|value| {
path.with("site", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_route = raw_route
.map(|value| {
path.with("route", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_method = raw_method
.map(|value| {
path.with("method", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_dose = match raw_dose.suffix {
None => None,
Some(suffix) => Some(path.with("dose", |path| {
DosageDose::from_json_parts(suffix, raw_dose.value, raw_dose.element, path)
})?),
};
let field_max_dose_per_period = raw_max_dose_per_period
.map(|value| {
path.with("maxDosePerPeriod", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_max_dose_per_administration = raw_max_dose_per_administration
.map(|value| {
path.with("maxDosePerAdministration", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_max_dose_per_lifetime = raw_max_dose_per_lifetime
.map(|value| {
path.with("maxDosePerLifetime", |path| {
super::super::codec::Json::from_json(
super::super::codec::expect_object(
super::super::codec::expect_single(value, path)?,
path,
)?,
path,
)
})
})
.transpose()?;
let field_rate = match raw_rate.suffix {
None => None,
Some(suffix) => Some(path.with("rate", |path| {
DosageRate::from_json_parts(suffix, raw_rate.value, raw_rate.element, path)
})?),
};
Ok(Self {
id: field_id,
extension: field_extension,
sequence: field_sequence,
text: field_text,
additional_instruction: field_additional_instruction,
patient_instruction: field_patient_instruction,
timing: field_timing,
as_needed: field_as_needed,
site: field_site,
route: field_route,
method: field_method,
dose: field_dose,
max_dose_per_period: field_max_dose_per_period,
max_dose_per_administration: field_max_dose_per_administration,
max_dose_per_lifetime: field_max_dose_per_lifetime,
rate: field_rate,
})
}
}
impl serde::Serialize for Dosage {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serde::Serializer::serialize_map(serializer, None)?;
if let Some(DosageAsNeeded::Boolean(inner)) = &self.as_needed {
super::super::codec::element_entry(&mut map, "_asNeededBoolean", inner)?;
}
if let Some(item) = &self.patient_instruction {
super::super::codec::element_entry(&mut map, "_patientInstruction", item)?;
}
if let Some(item) = &self.sequence {
super::super::codec::element_entry(&mut map, "_sequence", item)?;
}
if let Some(item) = &self.text {
super::super::codec::element_entry(&mut map, "_text", item)?;
}
if !self.additional_instruction.is_empty() {
serde::ser::SerializeMap::serialize_entry(
&mut map,
"additionalInstruction",
&self.additional_instruction,
)?;
}
match &self.as_needed {
Some(DosageAsNeeded::Boolean(inner)) => {
super::super::codec::value_entry(&mut map, "asNeededBoolean", inner)?;
}
Some(DosageAsNeeded::CodeableConcept(inner)) => {
serde::ser::SerializeMap::serialize_entry(
&mut map,
"asNeededCodeableConcept",
inner.as_ref(),
)?;
}
None => {}
}
match &self.dose {
Some(DosageDose::Quantity(inner)) => {
serde::ser::SerializeMap::serialize_entry(
&mut map,
"doseQuantity",
inner.as_ref(),
)?;
}
Some(DosageDose::Range(inner)) => {
serde::ser::SerializeMap::serialize_entry(&mut map, "doseRange", inner.as_ref())?;
}
None => {}
}
if !self.extension.is_empty() {
serde::ser::SerializeMap::serialize_entry(&mut map, "extension", &self.extension)?;
}
if let Some(v) = &self.id {
serde::ser::SerializeMap::serialize_entry(&mut map, "id", v)?;
}
if let Some(item) = &self.max_dose_per_administration {
serde::ser::SerializeMap::serialize_entry(&mut map, "maxDosePerAdministration", item)?;
}
if let Some(item) = &self.max_dose_per_lifetime {
serde::ser::SerializeMap::serialize_entry(&mut map, "maxDosePerLifetime", item)?;
}
if let Some(item) = &self.max_dose_per_period {
serde::ser::SerializeMap::serialize_entry(&mut map, "maxDosePerPeriod", item)?;
}
if let Some(item) = &self.method {
serde::ser::SerializeMap::serialize_entry(&mut map, "method", item)?;
}
if let Some(item) = &self.patient_instruction {
super::super::codec::value_entry(&mut map, "patientInstruction", item)?;
}
match &self.rate {
Some(DosageRate::Quantity(inner)) => {
serde::ser::SerializeMap::serialize_entry(
&mut map,
"rateQuantity",
inner.as_ref(),
)?;
}
Some(DosageRate::Range(inner)) => {
serde::ser::SerializeMap::serialize_entry(&mut map, "rateRange", inner.as_ref())?;
}
Some(DosageRate::Ratio(inner)) => {
serde::ser::SerializeMap::serialize_entry(&mut map, "rateRatio", inner.as_ref())?;
}
None => {}
}
if let Some(item) = &self.route {
serde::ser::SerializeMap::serialize_entry(&mut map, "route", item)?;
}
if let Some(item) = &self.sequence {
super::super::codec::value_entry(&mut map, "sequence", item)?;
}
if let Some(item) = &self.site {
serde::ser::SerializeMap::serialize_entry(&mut map, "site", item)?;
}
if let Some(item) = &self.text {
super::super::codec::value_entry(&mut map, "text", item)?;
}
if let Some(item) = &self.timing {
serde::ser::SerializeMap::serialize_entry(&mut map, "timing", item)?;
}
serde::ser::SerializeMap::end(map)
}
}
impl<'de> serde::Deserialize<'de> for Dosage {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let value = <super::super::codec::Value as serde::Deserialize>::deserialize(deserializer)?;
let mut path = super::super::codec::Path::root("Dosage");
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 DosageAsNeeded {
Boolean(super::primitives::Boolean),
CodeableConcept(Box<super::codeable_concept::CodeableConcept>),
}
impl DosageAsNeeded {
pub fn to_json_parts(
&self,
) -> Result<
(
&'static str,
Option<super::super::codec::Value>,
Option<super::super::codec::Value>,
),
super::super::codec::EncodeError,
> {
match self {
Self::Boolean(inner) => Ok((
"Boolean",
super::super::codec::Primitive::value_json(inner)?,
super::super::codec::Primitive::element_json(inner)?,
)),
Self::CodeableConcept(inner) => Ok((
"CodeableConcept",
Some(super::super::codec::Value::Object(
super::super::codec::Json::to_json(inner.as_ref())?,
)),
None,
)),
}
}
pub fn from_json_parts(
suffix: &str,
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
match suffix {
"Boolean" => Ok(Self::Boolean(
super::super::codec::Primitive::from_json_parts(value, element, path)?,
)),
"CodeableConcept" => {
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::CodeableConcept(Box::new(inner)))
}
_ => Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty)),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DosageDose {
Range(Box<super::range::Range>),
Quantity(Box<super::quantity::Quantity>),
}
impl DosageDose {
pub fn to_json_parts(
&self,
) -> Result<
(
&'static str,
Option<super::super::codec::Value>,
Option<super::super::codec::Value>,
),
super::super::codec::EncodeError,
> {
match self {
Self::Range(inner) => Ok((
"Range",
Some(super::super::codec::Value::Object(
super::super::codec::Json::to_json(inner.as_ref())?,
)),
None,
)),
Self::Quantity(inner) => Ok((
"Quantity",
Some(super::super::codec::Value::Object(
super::super::codec::Json::to_json(inner.as_ref())?,
)),
None,
)),
}
}
pub fn from_json_parts(
suffix: &str,
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
match suffix {
"Range" => {
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::Range(Box::new(inner)))
}
"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(Box::new(inner)))
}
_ => Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty)),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DosageRate {
Ratio(Box<super::ratio::Ratio>),
Range(Box<super::range::Range>),
Quantity(Box<super::quantity::Quantity>),
}
impl DosageRate {
pub fn to_json_parts(
&self,
) -> Result<
(
&'static str,
Option<super::super::codec::Value>,
Option<super::super::codec::Value>,
),
super::super::codec::EncodeError,
> {
match self {
Self::Ratio(inner) => Ok((
"Ratio",
Some(super::super::codec::Value::Object(
super::super::codec::Json::to_json(inner.as_ref())?,
)),
None,
)),
Self::Range(inner) => Ok((
"Range",
Some(super::super::codec::Value::Object(
super::super::codec::Json::to_json(inner.as_ref())?,
)),
None,
)),
Self::Quantity(inner) => Ok((
"Quantity",
Some(super::super::codec::Value::Object(
super::super::codec::Json::to_json(inner.as_ref())?,
)),
None,
)),
}
}
pub fn from_json_parts(
suffix: &str,
value: Option<&super::super::codec::Value>,
element: Option<&super::super::codec::Value>,
path: &mut super::super::codec::Path,
) -> Result<Self, super::super::codec::DecodeError> {
match suffix {
"Ratio" => {
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::Ratio(Box::new(inner)))
}
"Range" => {
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::Range(Box::new(inner)))
}
"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(Box::new(inner)))
}
_ => Err(path.error(super::super::codec::DecodeErrorKind::UnknownProperty)),
}
}
}