pub mod walk;
use fhir_types::schema::FieldSchema;
use fhir_types::schema::Kind;
use fhir_types::schema::Schemas;
use fhir_types::schema::TypeSchema;
use fhir_types::schema::ValueKind;
use crate::tree::error::ResolveError;
use crate::tree::path::FhirPath;
use crate::tree::path::Head;
use crate::tree::path::Ordinal;
use crate::tree::path::Step;
pub trait Table {
fn type_named(&self, name: &str) -> Option<&'static TypeSchema>;
fn is_resource(&self, name: &str) -> bool;
}
impl Table for Schemas {
fn type_named(&self, name: &str) -> Option<&'static TypeSchema> {
Self::type_named(self, name)
}
fn is_resource(&self, name: &str) -> bool {
Self::is_resource(self, name)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Field {
path: String,
key: String,
kind: Kind,
min: u32,
max: Option<u32>,
types: &'static [&'static str],
target_profiles: &'static [&'static str],
rivals: Vec<String>,
}
impl Field {
#[must_use]
pub fn path(&self) -> &str {
&self.path
}
#[must_use]
pub fn key(&self) -> &str {
&self.key
}
#[must_use]
pub const fn kind(&self) -> Kind {
self.kind
}
#[must_use]
pub const fn min(&self) -> u32 {
self.min
}
#[must_use]
pub const fn max(&self) -> Option<u32> {
self.max
}
#[must_use]
pub const fn types(&self) -> &'static [&'static str] {
self.types
}
#[must_use]
pub const fn target_profiles(&self) -> &'static [&'static str] {
self.target_profiles
}
#[must_use]
pub fn rivals(&self) -> &[String] {
&self.rivals
}
#[must_use]
pub fn repeats(&self) -> bool {
self.max != Some(1)
}
fn under(&self, key: String) -> Self {
Self {
path: self.path.clone(),
key,
kind: self.kind,
min: self.min,
max: self.max,
types: self.types,
target_profiles: self.target_profiles,
rivals: self.rivals.clone(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Move {
Member(Field),
Choice {
field: Field,
schema: &'static FieldSchema,
},
Extension {
field: Field,
url: String,
},
Ordinal(Ordinal),
Index(usize),
Predicate(String),
Resolve {
expected: Option<String>,
continuation: Vec<Step>,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Location {
Complex(&'static TypeSchema),
Primitive(ValueKind),
Attribute,
PrimitiveElement,
Choice(String),
Resource,
Deferred,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Resolved {
resource: String,
moves: Vec<Move>,
location: Location,
}
impl Resolved {
#[must_use]
pub fn resource(&self) -> &str {
&self.resource
}
#[must_use]
pub fn moves(&self) -> &[Move] {
&self.moves
}
#[must_use]
pub const fn location(&self) -> &Location {
&self.location
}
#[must_use]
pub fn type_code(&self) -> Option<&'static str> {
match self.location {
Location::Complex(schema) => Some(schema.name),
Location::Primitive(_) | Location::Attribute => match self.moves.last() {
Some(Move::Member(field) | Move::Extension { field, .. }) => match field.types {
[code] => Some(*code),
_ => None,
},
_ => None,
},
Location::PrimitiveElement
| Location::Choice(_)
| Location::Resource
| Location::Deferred => None,
}
}
#[must_use]
pub fn target_profiles(&self) -> &'static [&'static str] {
self.moves
.iter()
.rev()
.find_map(|step| match step {
Move::Member(field)
| Move::Choice { field, .. }
| Move::Extension { field, .. } => Some(field.target_profiles()),
Move::Ordinal(_) | Move::Index(_) | Move::Predicate(_) | Move::Resolve { .. } => {
None
}
})
.unwrap_or_default()
}
#[must_use]
pub fn leaf(&self) -> &str {
self.moves
.iter()
.rev()
.find_map(|step| match step {
Move::Member(field)
| Move::Choice { field, .. }
| Move::Extension { field, .. } => Some(field.path()),
Move::Ordinal(_) | Move::Index(_) | Move::Predicate(_) | Move::Resolve { .. } => {
None
}
})
.unwrap_or(&self.resource)
}
}
pub fn resolve<T: Table + ?Sized>(
table: &T,
resource: &str,
path: &FhirPath,
) -> Result<Resolved, ResolveError> {
if path.head() != Head::Resource {
return Err(ResolveError::NotAnchored {
expression: String::from(path.as_str()),
});
}
let root = table
.type_named(resource)
.filter(|schema| schema.path == resource && table.is_resource(resource))
.ok_or_else(|| ResolveError::UnknownResource {
name: String::from(resource),
})?;
let mut walk = Walk {
table,
location: Location::Complex(root),
owner: String::from(resource),
moves: Vec::new(),
};
let steps = path.steps();
let mut index = 0usize;
while let Some(step) = steps.get(index) {
index = index.saturating_add(1);
if walk.take(step, steps.get(index..).unwrap_or_default())? {
break;
}
}
Ok(Resolved {
resource: String::from(resource),
moves: walk.moves,
location: walk.location,
})
}
struct Walk<'table, T: Table + ?Sized> {
table: &'table T,
location: Location,
owner: String,
moves: Vec<Move>,
}