use core::fmt;
use openehr_mapping_core::diagnostic::LanguageCodes;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ModelCode {
UnknownKey,
UnexpectedNodeKind,
MissingKey,
WrongMappingFileType,
InvalidDirection,
InvalidExtensionMethod,
InvalidDataType,
ConflictingDataType,
InvalidOperator,
UnknownPathVariable,
RootUnidirectional,
NullMappings,
SchemaViolation,
SchemaCompilation,
ConditionTargetRootMismatch,
CriteriaNotAllowed,
CriteriaMissing,
UnknownMappingReference,
ExtensionMethodOutsideExtensionFile,
ReferenceWithoutReferenceVariable,
UnknownMappingCode,
InvalidContextScope,
CyclicContextReference,
UnreferencedSubContext,
SubContextReferenceVariable,
}
impl ModelCode {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::UnknownKey => "fc-unknown-key",
Self::UnexpectedNodeKind => "fc-unexpected-node-kind",
Self::MissingKey => "fc-missing-key",
Self::WrongMappingFileType => "fc-wrong-mapping-file-type",
Self::InvalidDirection => "fc-invalid-direction",
Self::InvalidExtensionMethod => "fc-invalid-extension-method",
Self::InvalidDataType => "fc-invalid-data-type",
Self::ConflictingDataType => "fc-conflicting-data-type",
Self::InvalidOperator => "fc-invalid-operator",
Self::UnknownPathVariable => "fc-unknown-path-variable",
Self::RootUnidirectional => "fc-root-unidirectional",
Self::NullMappings => "fc-null-mappings",
Self::SchemaViolation => "fc-schema-violation",
Self::SchemaCompilation => "fc-schema-compilation",
Self::ConditionTargetRootMismatch => "fc-condition-target-root-mismatch",
Self::CriteriaNotAllowed => "fc-criteria-not-allowed",
Self::CriteriaMissing => "fc-criteria-missing",
Self::UnknownMappingReference => "fc-unknown-mapping-reference",
Self::ExtensionMethodOutsideExtensionFile => "fc-extension-method-outside-model",
Self::ReferenceWithoutReferenceVariable => "fc-reference-without-reference-variable",
Self::UnknownMappingCode => "fc-unknown-mapping-code",
Self::InvalidContextScope => "fc-invalid-context-scope",
Self::CyclicContextReference => "fc-cyclic-context-reference",
Self::UnreferencedSubContext => "fc-unreferenced-sub-context",
Self::SubContextReferenceVariable => "fc-sub-context-reference-variable",
}
}
#[must_use]
pub const fn all() -> &'static [Self] {
&[
Self::UnknownKey,
Self::UnexpectedNodeKind,
Self::MissingKey,
Self::WrongMappingFileType,
Self::InvalidDirection,
Self::InvalidExtensionMethod,
Self::InvalidDataType,
Self::ConflictingDataType,
Self::InvalidOperator,
Self::UnknownPathVariable,
Self::RootUnidirectional,
Self::NullMappings,
Self::SchemaViolation,
Self::SchemaCompilation,
Self::ConditionTargetRootMismatch,
Self::CriteriaNotAllowed,
Self::CriteriaMissing,
Self::UnknownMappingReference,
Self::ExtensionMethodOutsideExtensionFile,
Self::ReferenceWithoutReferenceVariable,
Self::UnknownMappingCode,
Self::InvalidContextScope,
Self::CyclicContextReference,
Self::UnreferencedSubContext,
Self::SubContextReferenceVariable,
]
}
}
impl LanguageCodes for ModelCode {
fn spelling(self) -> &'static str {
self.as_str()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SchemaKind {
Model,
Context,
}
impl SchemaKind {
#[must_use]
pub const fn file_name(self) -> &'static str {
match self {
Self::Model => "model-mapping.schema.json",
Self::Context => "contextual-mapping.schema.json",
}
}
}
impl fmt::Display for SchemaKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.file_name())
}
}
#[cfg(test)]
mod tests {
use openehr_mapping_core::diagnostic::DiagnosticCode;
use openehr_mapping_core::diagnostic::LanguageCode;
use super::ModelCode;
use super::SchemaKind;
#[test]
fn every_code_is_a_language_code() {
for code in ModelCode::all() {
assert!(
LanguageCode::new(code.as_str()).is_ok(),
"the code `{}` is outside the language-code alphabet",
code.as_str()
);
}
}
#[test]
fn every_code_has_a_distinct_spelling() {
let mut spellings: Vec<&str> = ModelCode::all().iter().map(|c| c.as_str()).collect();
let total = spellings.len();
spellings.sort_unstable();
spellings.dedup();
assert_eq!(spellings.len(), total, "two model codes share a spelling");
}
#[test]
fn a_code_renders_as_its_diagnostic_code() {
assert_eq!(
DiagnosticCode::from(ModelCode::NullMappings).to_string(),
"fc-null-mappings"
);
}
#[test]
fn a_schema_kind_names_its_published_file() {
assert_eq!(SchemaKind::Model.to_string(), "model-mapping.schema.json");
assert_eq!(
SchemaKind::Context.to_string(),
"contextual-mapping.schema.json"
);
}
}