use openehr_rm::v1_2::model;
use openehr_rm::v1_2::paths::RmPath;
use openehr_rm::v1_2::typing::PathStep;
use openehr_rm::v1_2::typing::RmType;
use openehr_rm::v1_2::typing::TypingError;
use openehr_rm::v1_2::typing::path_type;
pub(crate) fn steps(rm_type: &str, tail: &RmPath) -> Result<Option<Vec<PathStep>>, TypingError> {
let Ok(class) = rm_type.parse::<RmType>() else {
return Ok(None);
};
if model::class(class.name()).is_none() {
return Ok(None);
}
match path_type(&class, tail) {
Ok(steps) => Ok(Some(steps)),
Err(TypingError::UnknownClass(_)) => Ok(None),
Err(error) => Err(error),
}
}
pub(crate) fn concrete_forms(rm_type: &str) -> Vec<&'static str> {
let Some(class) = model::class(rm_type) else {
return Vec::new();
};
let mut forms: Vec<&'static str> = vec![class.name];
for &descendant in class.descendants {
if !forms.contains(&descendant) {
forms.push(descendant);
}
}
forms
}
pub(crate) fn concrete_leaf(declared: &str) -> String {
let Some(class) = model::class(declared) else {
return String::from(declared);
};
if !class.is_abstract {
return String::from(declared);
}
let concrete: Vec<&str> = class
.descendants
.iter()
.copied()
.filter(|name| model::class(name).is_some_and(|found| !found.is_abstract))
.collect();
match *concrete.as_slice() {
[only] => String::from(only),
_ => String::from(declared),
}
}