use fhir_types::codec::Value;
pub const UNKNOWN_SOURCE: &str = "unknown";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Origin {
system_id: String,
originating: Option<String>,
source: Option<SourceItem>,
originating_items: Option<Vec<ItemId>>,
feeder_items: Vec<ItemId>,
location: Option<Party>,
provider: Option<Party>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Party {
pub name: Option<String>,
pub identifiers: Vec<ItemId>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ItemId {
pub id: String,
pub kind: String,
pub issuer: Option<String>,
}
impl Origin {
#[must_use]
pub fn new(system_id: impl Into<String>) -> Self {
Self {
system_id: system_id.into(),
originating: None,
source: None,
originating_items: None,
feeder_items: Vec::new(),
location: None,
provider: None,
}
}
#[must_use]
pub fn with_source(mut self, source: SourceItem) -> Self {
self.source = Some(source);
self
}
#[must_use]
pub fn with_originating_system(mut self, system_id: impl Into<String>) -> Self {
self.originating = Some(system_id.into());
self
}
#[must_use]
pub fn originating_system(&self) -> Option<&str> {
self.originating.as_deref()
}
#[must_use]
pub fn with_originating_items(mut self, items: Vec<ItemId>) -> Self {
self.originating_items = Some(items);
self
}
#[must_use]
pub fn with_feeder_items(mut self, items: Vec<ItemId>) -> Self {
self.feeder_items = items;
self
}
#[must_use]
pub fn with_location(mut self, location: Party) -> Self {
self.location = Some(location);
self
}
#[must_use]
pub fn with_provider(mut self, provider: Party) -> Self {
self.provider = Some(provider);
self
}
#[must_use]
pub const fn location(&self) -> Option<&Party> {
self.location.as_ref()
}
#[must_use]
pub const fn provider(&self) -> Option<&Party> {
self.provider.as_ref()
}
#[must_use]
pub fn originating_items(&self) -> Option<&[ItemId]> {
self.originating_items.as_deref()
}
#[must_use]
pub fn feeder_items(&self) -> &[ItemId] {
&self.feeder_items
}
#[must_use]
pub fn system_id(&self) -> &str {
&self.system_id
}
#[must_use]
pub const fn source(&self) -> Option<&SourceItem> {
self.source.as_ref()
}
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum MessageIdError {
#[error("a {kind} cannot be empty")]
Empty {
kind: &'static str,
},
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MessageControlId(String);
impl MessageControlId {
pub fn new(text: impl Into<String>) -> Result<Self, MessageIdError> {
let text = text.into();
if text.is_empty() {
return Err(MessageIdError::Empty {
kind: "message control id",
});
}
Ok(Self(text))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MessageType(String);
impl MessageType {
pub fn new(text: impl Into<String>) -> Result<Self, MessageIdError> {
let text = text.into();
if text.is_empty() {
return Err(MessageIdError::Empty {
kind: "message type",
});
}
Ok(Self(text))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SourceItem {
resource_type: String,
id: Option<String>,
version_id: Option<String>,
}
impl SourceItem {
#[must_use]
pub fn new(resource_type: impl Into<String>) -> Self {
Self {
resource_type: resource_type.into(),
id: None,
version_id: None,
}
}
#[must_use]
pub fn message(control_id: MessageControlId, message_type: MessageType) -> Self {
Self::new(message_type.0).with_id(control_id.0)
}
#[must_use]
pub fn of(document: &Value) -> Option<Self> {
let resource_type = document.get("resourceType").and_then(Value::as_str)?;
let mut source = Self::new(resource_type);
if let Some(id) = document.get("id").and_then(Value::as_str) {
source = source.with_id(id);
}
if let Some(version) = document
.get("meta")
.and_then(|meta| meta.get("versionId"))
.and_then(Value::as_str)
{
source = source.with_version_id(version);
}
Some(source)
}
#[must_use]
pub fn with_id(mut self, id: impl Into<String>) -> Self {
self.id = Some(id.into());
self
}
#[must_use]
pub fn with_version_id(mut self, version_id: impl Into<String>) -> Self {
self.version_id = Some(version_id.into());
self
}
#[must_use]
pub fn resource_type(&self) -> &str {
&self.resource_type
}
#[must_use]
pub fn id(&self) -> Option<&str> {
self.id.as_deref()
}
#[must_use]
pub fn version_id(&self) -> Option<&str> {
self.version_id.as_deref()
}
}
#[cfg(test)]
mod tests {
use super::{MessageControlId, MessageIdError, MessageType, SourceItem};
use fhir_types::codec::Value;
#[test]
fn a_source_item_reads_the_type_the_id_and_the_version() {
let document = Value::from_serde_json(serde_json::json!({
"resourceType": "Condition",
"id": "sender-1",
"meta": {"versionId": "3"}
}));
let source = SourceItem::of(&document).expect("a resource names its type");
assert_eq!(source.resource_type(), "Condition");
assert_eq!(source.id(), Some("sender-1"));
assert_eq!(source.version_id(), Some("3"));
}
#[test]
fn a_message_source_names_its_control_id_and_its_type() {
let source = SourceItem::message(
MessageControlId::new("MSG-0001").expect("a non-empty control id"),
MessageType::new("ORU^R01").expect("a non-empty message type"),
);
assert_eq!(source.id(), Some("MSG-0001"));
assert_eq!(source.resource_type(), "ORU^R01");
assert_eq!(source.version_id(), None);
}
#[test]
fn an_empty_control_id_or_message_type_is_refused() {
assert_eq!(
Err(MessageIdError::Empty {
kind: "message control id"
}),
MessageControlId::new("")
);
assert_eq!(
Err(MessageIdError::Empty {
kind: "message type"
}),
MessageType::new("")
);
}
#[test]
fn a_document_with_no_resource_type_is_no_source() {
let document = Value::from_serde_json(serde_json::json!({"id": "x"}));
assert_eq!(SourceItem::of(&document), None);
}
}