use fhir_types::r4::reference::Reference;
pub const EHR_ID: &str = "ehr_id";
pub const PATIENT: &str = "patient";
pub const WHO: &str = "who";
pub const ON_BEHALF_OF: &str = "onBehalfOf";
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CallContext {
ehr_id: Option<String>,
patient: Option<Reference>,
who: Option<Reference>,
on_behalf_of: Option<Reference>,
}
impl CallContext {
#[must_use]
pub const fn new() -> Self {
Self {
ehr_id: None,
patient: None,
who: None,
on_behalf_of: None,
}
}
#[must_use]
pub fn with_ehr_id(mut self, id: impl Into<String>) -> Self {
self.ehr_id = Some(id.into());
self
}
#[must_use]
pub fn with_patient(mut self, patient: Reference) -> Self {
self.patient = Some(patient);
self
}
#[must_use]
pub fn with_who(mut self, who: Reference) -> Self {
self.who = Some(who);
self
}
#[must_use]
pub fn with_on_behalf_of(mut self, organization: Reference) -> Self {
self.on_behalf_of = Some(organization);
self
}
#[must_use]
pub fn ehr_id(&self) -> Option<&str> {
self.ehr_id.as_deref()
}
#[must_use]
pub const fn patient(&self) -> Option<&Reference> {
self.patient.as_ref()
}
#[must_use]
pub const fn who(&self) -> Option<&Reference> {
self.who.as_ref()
}
#[must_use]
pub const fn on_behalf_of(&self) -> Option<&Reference> {
self.on_behalf_of.as_ref()
}
#[must_use]
pub fn member(&self, name: &str) -> Option<&str> {
match name {
EHR_ID => self.ehr_id(),
PATIENT => literal(self.patient.as_ref()),
WHO => literal(self.who.as_ref()),
ON_BEHALF_OF => literal(self.on_behalf_of.as_ref()),
_ => None,
}
}
}
fn literal(reference: Option<&Reference>) -> Option<&str> {
reference?.reference.as_ref()?.value.as_deref()
}
#[cfg(test)]
mod tests {
use super::CallContext;
use fhir_types::r4::reference::Reference;
fn reference(text: &str) -> Reference {
Reference {
reference: Some(fhir_types::r4::primitives::String::from(text)),
..Reference::default()
}
}
#[test]
fn an_empty_context_answers_no_member() {
let context = CallContext::new();
for name in ["ehr_id", "patient", "who", "onBehalfOf"] {
assert_eq!(context.member(name), None, "{name} answered a value");
}
}
#[test]
fn a_reference_member_answers_its_literal_reference() {
let context = CallContext::new()
.with_ehr_id("53d89df2-5501-4455-9a65-565a5d1ddb7c")
.with_who(reference("Practitioner/456"));
assert_eq!(
context.member("ehr_id"),
Some("53d89df2-5501-4455-9a65-565a5d1ddb7c")
);
assert_eq!(context.member("who"), Some("Practitioner/456"));
}
#[test]
fn a_member_the_chapter_does_not_declare_answers_none() {
let context = CallContext::new().with_ehr_id("an-ehr");
assert_eq!(context.member("composer"), None);
}
}