pub mod parameters;
use fhir_types::operation::ParametersError;
use fhir_types::r4::bundle::Bundle;
use fhir_types::r4::operation_outcome::OperationOutcome;
use fhir_types::r4::parameters::Parameters;
use fhir_types::r4::parameters::ParametersParameter;
use fhir_types::r4::parameters::ParametersParameterValue;
use fhir_types::r4::resource::Resource;
use crate::engine::context::CallContext;
use crate::operations::TO_FHIR;
use crate::operations::TO_OPENEHR;
use crate::operations::error::OperationError;
use crate::resolve::program::binding::TemplateId;
use crate::operations::contract::parameters::call_context;
use crate::operations::contract::parameters::code;
use crate::operations::contract::parameters::context_parameter;
use crate::operations::contract::parameters::named;
use crate::operations::contract::parameters::once;
use crate::operations::contract::parameters::string;
use crate::operations::contract::parameters::value_parameter;
const COMPOSITION: &str = "composition";
const TEMPLATE_ID: &str = "templateId";
const CONTEXT: &str = "context";
const BUNDLE: &str = "bundle";
const FORMAT: &str = "format";
const OUTCOME: &str = "outcome";
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CompositionPayload {
Canonical(serde_json::Map<String, serde_json::Value>),
Flat(serde_json::Map<String, serde_json::Value>),
}
impl CompositionPayload {
pub fn parse(text: &str) -> Result<Self, OperationError> {
let value: serde_json::Value =
serde_json::from_str(text).map_err(|source| OperationError::Payload { source })?;
let serde_json::Value::Object(object) = value else {
return Err(OperationError::NotAnObject {
what: "composition",
});
};
if object.contains_key("_type") {
Ok(Self::Canonical(object))
} else {
Ok(Self::Flat(object))
}
}
#[must_use]
pub const fn members(&self) -> &serde_json::Map<String, serde_json::Value> {
match *self {
Self::Canonical(ref object) | Self::Flat(ref object) => object,
}
}
#[must_use]
pub fn template_id(&self) -> Option<TemplateId> {
let Self::Canonical(ref object) = *self else {
return None;
};
object
.get("archetype_details")?
.get("template_id")?
.get("value")?
.as_str()
.map(TemplateId::new)
}
#[must_use]
pub fn to_json_string(&self) -> String {
serde_json::Value::Object(self.members().clone()).to_string()
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum Format {
#[default]
Canonical,
Flat,
}
impl Format {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Canonical => "canonical",
Self::Flat => "flat",
}
}
pub fn parse(code: &str) -> Result<Self, OperationError> {
match code {
"canonical" => Ok(Self::Canonical),
"flat" => Ok(Self::Flat),
_ => Err(OperationError::Parameters {
operation: TO_OPENEHR,
source: ParametersError::WrongType {
operation: "$toopenehr",
name: FORMAT,
expected: "code of `canonical` or `flat`",
},
}),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToFhirRequest {
composition: CompositionPayload,
template_id: Option<TemplateId>,
context: Option<CallContext>,
}
impl ToFhirRequest {
#[must_use]
pub const fn new(composition: CompositionPayload) -> Self {
Self {
composition,
template_id: None,
context: None,
}
}
#[must_use]
pub fn with_template_id(mut self, template: TemplateId) -> Self {
self.template_id = Some(template);
self
}
#[must_use]
pub fn with_context(mut self, context: CallContext) -> Self {
self.context = Some(context);
self
}
#[must_use]
pub const fn composition(&self) -> &CompositionPayload {
&self.composition
}
#[must_use]
pub const fn template_id(&self) -> Option<&TemplateId> {
self.template_id.as_ref()
}
#[must_use]
pub const fn context(&self) -> Option<&CallContext> {
self.context.as_ref()
}
pub fn from_parameters(parameters: &Parameters) -> Result<Self, OperationError> {
let mut composition: Option<CompositionPayload> = None;
let mut template_id: Option<TemplateId> = None;
let mut context: Option<CallContext> = None;
for parameter in ¶meters.parameter {
match named(TO_FHIR, "$tofhir", parameter)? {
COMPOSITION => {
once(TO_FHIR, "$tofhir", COMPOSITION, composition.is_some())?;
let text = string(TO_FHIR, "$tofhir", COMPOSITION, parameter)?;
composition = Some(CompositionPayload::parse(text)?);
}
TEMPLATE_ID => {
once(TO_FHIR, "$tofhir", TEMPLATE_ID, template_id.is_some())?;
template_id = Some(TemplateId::new(string(
TO_FHIR,
"$tofhir",
TEMPLATE_ID,
parameter,
)?));
}
CONTEXT => {
once(TO_FHIR, "$tofhir", CONTEXT, context.is_some())?;
context = Some(call_context(parameter)?);
}
other => {
return Err(OperationError::Parameters {
operation: TO_FHIR,
source: ParametersError::Undeclared {
operation: "$tofhir",
name: String::from(other),
},
});
}
}
}
let composition = composition.ok_or(OperationError::Parameters {
operation: TO_FHIR,
source: ParametersError::Missing {
operation: "$tofhir",
name: COMPOSITION,
},
})?;
if matches!(composition, CompositionPayload::Flat(_)) && template_id.is_none() {
return Err(OperationError::FlatTemplate);
}
Ok(Self {
composition,
template_id,
context,
})
}
#[must_use]
pub fn to_parameters(&self) -> Parameters {
let mut parameter = vec![value_parameter(
COMPOSITION,
ParametersParameterValue::String(fhir_types::r4::primitives::String::from(
self.composition.to_json_string(),
)),
)];
if let Some(ref template) = self.template_id {
parameter.push(value_parameter(
TEMPLATE_ID,
ParametersParameterValue::String(fhir_types::r4::primitives::String::from(
template.as_str(),
)),
));
}
if let Some(ref carried) = self.context {
parameter.push(context_parameter(carried));
}
Parameters {
parameter,
..Parameters::default()
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToFhirResponse {
bundle: Bundle,
}
impl ToFhirResponse {
#[must_use]
pub const fn new(bundle: Bundle) -> Self {
Self { bundle }
}
#[must_use]
pub const fn bundle(&self) -> &Bundle {
&self.bundle
}
#[must_use]
pub fn into_bundle(self) -> Bundle {
self.bundle
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToOpenehrRequest {
bundle: Bundle,
template_id: Option<TemplateId>,
format: Format,
}
impl ToOpenehrRequest {
#[must_use]
pub fn new(bundle: Bundle) -> Self {
Self {
bundle,
template_id: None,
format: Format::default(),
}
}
#[must_use]
pub fn with_template_id(mut self, template: TemplateId) -> Self {
self.template_id = Some(template);
self
}
#[must_use]
pub const fn with_format(mut self, format: Format) -> Self {
self.format = format;
self
}
#[must_use]
pub const fn bundle(&self) -> &Bundle {
&self.bundle
}
#[must_use]
pub const fn template_id(&self) -> Option<&TemplateId> {
self.template_id.as_ref()
}
#[must_use]
pub const fn format(&self) -> Format {
self.format
}
pub fn from_parameters(parameters: &Parameters) -> Result<Self, OperationError> {
let mut bundle: Option<Bundle> = None;
let mut template_id: Option<TemplateId> = None;
let mut format: Option<Format> = None;
for parameter in ¶meters.parameter {
match named(TO_OPENEHR, "$toopenehr", parameter)? {
BUNDLE => {
once(TO_OPENEHR, "$toopenehr", BUNDLE, bundle.is_some())?;
let Some(Resource::Bundle(ref carried)) = parameter.resource else {
return Err(OperationError::Parameters {
operation: TO_OPENEHR,
source: ParametersError::WrongType {
operation: "$toopenehr",
name: BUNDLE,
expected: "Bundle",
},
});
};
bundle = Some(carried.as_ref().clone());
}
TEMPLATE_ID => {
once(TO_OPENEHR, "$toopenehr", TEMPLATE_ID, template_id.is_some())?;
template_id = Some(TemplateId::new(string(
TO_OPENEHR,
"$toopenehr",
TEMPLATE_ID,
parameter,
)?));
}
FORMAT => {
once(TO_OPENEHR, "$toopenehr", FORMAT, format.is_some())?;
format = Some(Format::parse(code(
TO_OPENEHR,
"$toopenehr",
FORMAT,
parameter,
)?)?);
}
other => {
return Err(OperationError::Parameters {
operation: TO_OPENEHR,
source: ParametersError::Undeclared {
operation: "$toopenehr",
name: String::from(other),
},
});
}
}
}
let bundle = bundle.ok_or(OperationError::Parameters {
operation: TO_OPENEHR,
source: ParametersError::Missing {
operation: "$toopenehr",
name: BUNDLE,
},
})?;
Ok(Self {
bundle,
template_id,
format: format.unwrap_or_default(),
})
}
#[must_use]
pub fn to_parameters(&self) -> Parameters {
let mut parameter = vec![ParametersParameter {
name: fhir_types::r4::primitives::String::from(BUNDLE),
resource: Some(Resource::Bundle(Box::new(self.bundle.clone()))),
..ParametersParameter::default()
}];
if let Some(ref template) = self.template_id {
parameter.push(value_parameter(
TEMPLATE_ID,
ParametersParameterValue::String(fhir_types::r4::primitives::String::from(
template.as_str(),
)),
));
}
parameter.push(value_parameter(
FORMAT,
ParametersParameterValue::Code(fhir_types::r4::primitives::Code::from(
self.format.as_str(),
)),
));
Parameters {
parameter,
..Parameters::default()
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToOpenehrResponse {
composition: String,
outcome: Option<OperationOutcome>,
}
impl ToOpenehrResponse {
#[must_use]
pub const fn new(composition: String) -> Self {
Self {
composition,
outcome: None,
}
}
#[must_use]
pub fn with_outcome(mut self, outcome: OperationOutcome) -> Self {
self.outcome = Some(outcome);
self
}
#[must_use]
pub fn composition(&self) -> &str {
&self.composition
}
#[must_use]
pub const fn outcome(&self) -> Option<&OperationOutcome> {
self.outcome.as_ref()
}
pub fn from_parameters(parameters: &Parameters) -> Result<Self, OperationError> {
let mut composition: Option<String> = None;
let mut outcome: Option<OperationOutcome> = None;
for parameter in ¶meters.parameter {
match named(TO_OPENEHR, "$toopenehr", parameter)? {
COMPOSITION => {
once(TO_OPENEHR, "$toopenehr", COMPOSITION, composition.is_some())?;
composition = Some(String::from(string(
TO_OPENEHR,
"$toopenehr",
COMPOSITION,
parameter,
)?));
}
OUTCOME => {
once(TO_OPENEHR, "$toopenehr", OUTCOME, outcome.is_some())?;
let Some(Resource::OperationOutcome(ref carried)) = parameter.resource else {
return Err(OperationError::Parameters {
operation: TO_OPENEHR,
source: ParametersError::WrongType {
operation: "$toopenehr",
name: OUTCOME,
expected: "OperationOutcome",
},
});
};
outcome = Some(carried.as_ref().clone());
}
other => {
return Err(OperationError::Parameters {
operation: TO_OPENEHR,
source: ParametersError::Undeclared {
operation: "$toopenehr",
name: String::from(other),
},
});
}
}
}
let composition = composition.ok_or(OperationError::Parameters {
operation: TO_OPENEHR,
source: ParametersError::Missing {
operation: "$toopenehr",
name: COMPOSITION,
},
})?;
Ok(Self {
composition,
outcome,
})
}
#[must_use]
pub fn to_parameters(&self) -> Parameters {
let mut parameter = vec![value_parameter(
COMPOSITION,
ParametersParameterValue::String(fhir_types::r4::primitives::String::from(
self.composition.as_str(),
)),
)];
if let Some(ref outcome) = self.outcome {
parameter.push(ParametersParameter {
name: fhir_types::r4::primitives::String::from(OUTCOME),
resource: Some(Resource::OperationOutcome(Box::new(outcome.clone()))),
..ParametersParameter::default()
});
}
Parameters {
parameter,
..Parameters::default()
}
}
}