use core::fmt;
use std::collections::BTreeMap;
use fhir_types::codec::DecodeError;
use fhir_types::codec::EncodeError;
use fhir_types::codec::Value;
use openehr_its::json::JsonParseError;
use crate::engine::lens::LensError;
use crate::engine::subject::SubjectIdentifier;
pub const SELF_PORT: &str = "";
#[derive(Debug, Clone, PartialEq)]
pub struct Datum {
class: String,
value: serde_json::Value,
}
impl Datum {
#[must_use]
pub fn new(class: impl Into<String>, value: serde_json::Value) -> Self {
Self {
class: class.into(),
value,
}
}
#[must_use]
pub fn class(&self) -> &str {
&self.class
}
#[must_use]
pub const fn value(&self) -> &serde_json::Value {
&self.value
}
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Ports(BTreeMap<String, Datum>);
impl Ports {
#[must_use]
pub const fn new() -> Self {
Self(BTreeMap::new())
}
#[must_use]
pub fn with(mut self, port: impl Into<String>, datum: Datum) -> Self {
self.0.insert(port.into(), datum);
self
}
#[must_use]
pub fn get(&self, port: &str) -> Option<&Datum> {
self.0.get(port)
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn iter(&self) -> impl Iterator<Item = (&str, &Datum)> {
self.0.iter().map(|(port, datum)| (port.as_str(), datum))
}
}
pub trait MappingFunctions: fmt::Debug + Send + Sync {
fn to_fhir(&self, code: &str, ports: &Ports) -> Result<Option<Value>, MappingCodeError>;
fn to_openehr(&self, code: &str, element: &Value) -> Result<Ports, MappingCodeError>;
fn writes_subject(&self, _code: &str) -> bool {
false
}
fn to_subject(
&self,
code: &str,
_element: &Value,
) -> Result<Vec<SubjectIdentifier>, MappingCodeError> {
Err(MappingCodeError::Unknown {
code: String::from(code),
})
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct NoMappingFunctions;
impl MappingFunctions for NoMappingFunctions {
fn to_fhir(&self, code: &str, _ports: &Ports) -> Result<Option<Value>, MappingCodeError> {
Err(MappingCodeError::Unknown {
code: String::from(code),
})
}
fn to_openehr(&self, code: &str, _element: &Value) -> Result<Ports, MappingCodeError> {
Err(MappingCodeError::Unknown {
code: String::from(code),
})
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum MappingCodeError {
#[error("no registered function is named {code}")]
Unknown {
code: String,
},
#[error("the function {code} refused its input: {reason}")]
Refused {
code: String,
reason: String,
},
#[error("the function {code} cannot read the FHIR element as {element}")]
FhirDecode {
code: String,
element: &'static str,
#[source]
source: Box<DecodeError>,
},
#[error("the function {code} built a FHIR element with no JSON form")]
FhirEncode {
code: String,
#[source]
source: Box<EncodeError>,
},
#[error("the function {code} cannot read the {class} at port `{port}`")]
RmDecode {
code: String,
port: String,
class: String,
#[source]
source: Box<JsonParseError>,
},
#[error("the function {code} could not convert a value")]
Cell {
code: String,
#[source]
source: Box<LensError>,
},
#[error("the function {code} wrote the port `{port}` its signature does not declare")]
UndeclaredPort {
code: String,
port: String,
},
#[error("the template admits no {class} at the port `{port}` of {code}")]
UnadmittedClass {
code: String,
port: String,
class: String,
},
}