use core::fmt;
use core::str::FromStr;
use openehr_mapping_core::diagnostic::render_admitted;
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("`{value}` is not one of {}", render_admitted(admitted))]
pub struct KeywordError {
pub value: String,
pub admitted: &'static [&'static str],
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Direction {
OpenehrToFhir,
FhirToOpenehr,
}
impl Direction {
pub const ADMITTED: &[&str] = &["openehr->fhir", "fhir->openehr"];
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::OpenehrToFhir => "openehr->fhir",
Self::FhirToOpenehr => "fhir->openehr",
}
}
}
impl fmt::Display for Direction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for Direction {
type Err = KeywordError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
if value.eq_ignore_ascii_case(Self::OpenehrToFhir.as_str()) {
Ok(Self::OpenehrToFhir)
} else if value.eq_ignore_ascii_case(Self::FhirToOpenehr.as_str()) {
Ok(Self::FhirToOpenehr)
} else {
Err(KeywordError {
value: value.to_owned(),
admitted: Self::ADMITTED,
})
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ExtensionMethod {
Add,
Append,
Overwrite,
}
impl ExtensionMethod {
pub const ADMITTED: &[&str] = &["add", "append", "overwrite"];
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Add => "add",
Self::Append => "append",
Self::Overwrite => "overwrite",
}
}
}
impl fmt::Display for ExtensionMethod {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for ExtensionMethod {
type Err = KeywordError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"add" => Ok(Self::Add),
"append" => Ok(Self::Append),
"overwrite" => Ok(Self::Overwrite),
_ => Err(KeywordError {
value: value.to_owned(),
admitted: Self::ADMITTED,
}),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum ContextScope {
#[default]
Root,
Sub,
}
impl ContextScope {
pub const ADMITTED: &[&str] = &["root", "sub"];
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Root => "root",
Self::Sub => "sub",
}
}
}
impl fmt::Display for ContextScope {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for ContextScope {
type Err = KeywordError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"root" => Ok(Self::Root),
"sub" => Ok(Self::Sub),
_ => Err(KeywordError {
value: value.to_owned(),
admitted: Self::ADMITTED,
}),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum DataType {
None,
Quantity,
DateTime,
CodeableConcept,
Coding,
String,
Dosage,
Id,
Identifier,
Proportion,
}
impl DataType {
pub const ADMITTED: &[&str] = &[
"NONE",
"QUANTITY",
"DATETIME",
"CODEABLECONCEPT",
"CODING",
"STRING",
"DOSAGE",
"ID",
"IDENTIFIER",
"PROPORTION",
];
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::None => "NONE",
Self::Quantity => "QUANTITY",
Self::DateTime => "DATETIME",
Self::CodeableConcept => "CODEABLECONCEPT",
Self::Coding => "CODING",
Self::String => "STRING",
Self::Dosage => "DOSAGE",
Self::Id => "ID",
Self::Identifier => "IDENTIFIER",
Self::Proportion => "PROPORTION",
}
}
}
impl fmt::Display for DataType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for DataType {
type Err = KeywordError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"NONE" => Ok(Self::None),
"QUANTITY" => Ok(Self::Quantity),
"DATETIME" => Ok(Self::DateTime),
"CODEABLECONCEPT" => Ok(Self::CodeableConcept),
"CODING" => Ok(Self::Coding),
"STRING" => Ok(Self::String),
"DOSAGE" => Ok(Self::Dosage),
"ID" => Ok(Self::Id),
"IDENTIFIER" => Ok(Self::Identifier),
"PROPORTION" => Ok(Self::Proportion),
_ => Err(KeywordError {
value: value.to_owned(),
admitted: Self::ADMITTED,
}),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ConditionOperator {
OneOf,
NotOf,
Empty,
NotEmpty,
Type,
}
impl ConditionOperator {
pub const ADMITTED: &[&str] = &["one of", "not of", "empty", "not empty", "type"];
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::OneOf => "one of",
Self::NotOf => "not of",
Self::Empty => "empty",
Self::NotEmpty => "not empty",
Self::Type => "type",
}
}
#[must_use]
pub const fn takes_criteria(self) -> bool {
match self {
Self::OneOf | Self::NotOf | Self::Type => true,
Self::Empty | Self::NotEmpty => false,
}
}
}
impl fmt::Display for ConditionOperator {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for ConditionOperator {
type Err = KeywordError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"one of" => Ok(Self::OneOf),
"not of" => Ok(Self::NotOf),
"empty" => Ok(Self::Empty),
"not empty" => Ok(Self::NotEmpty),
"type" => Ok(Self::Type),
_ => Err(KeywordError {
value: value.to_owned(),
admitted: Self::ADMITTED,
}),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Variable {
Resource,
FhirRoot,
Archetype,
OpenehrRoot,
Composition,
Reference,
Context,
}
impl Variable {
pub const ADMITTED: &[&str] = &[
"resource",
"fhirRoot",
"archetype",
"openehrRoot",
"composition",
"reference",
"context",
];
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Resource => "resource",
Self::FhirRoot => "fhirRoot",
Self::Archetype => "archetype",
Self::OpenehrRoot => "openehrRoot",
Self::Composition => "composition",
Self::Reference => "reference",
Self::Context => "context",
}
}
}
impl fmt::Display for Variable {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "${}", self.as_str())
}
}
impl FromStr for Variable {
type Err = KeywordError;
fn from_str(value: &str) -> Result<Self, Self::Err> {
let name = value.strip_prefix('$').unwrap_or(value);
let admitted = [
Self::Resource,
Self::FhirRoot,
Self::Archetype,
Self::OpenehrRoot,
Self::Composition,
Self::Reference,
Self::Context,
];
admitted
.into_iter()
.find(|variable| name.eq_ignore_ascii_case(variable.as_str()))
.ok_or_else(|| KeywordError {
value: value.to_owned(),
admitted: Self::ADMITTED,
})
}
}