Skip to content

Commit

Permalink
feat: add case convention validation
Browse files Browse the repository at this point in the history
  • Loading branch information
johnstonskj committed Feb 20, 2024
1 parent 9841074 commit c0defe5
Show file tree
Hide file tree
Showing 15 changed files with 208 additions and 68 deletions.
4 changes: 2 additions & 2 deletions sdml-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sdml-core"
description = "Core Model for Simple Domain Modeling Language (SDML)"
version = "0.2.12"
version = "0.2.13"
authors = ["Simon Johnston <johnstonskj@gmail.com>"]
repository = "https://github.com/johnstonskj/rust-sdml.git"
license-file = "../LICENSE"
Expand All @@ -26,7 +26,7 @@ lazy_static = "1.4.0"
ordered-float = { version = "4.2.0", features = ["serde"] }
regex = "1.10.2"
rust_decimal = { version = "1.34.2", features = ["serde"] }
sdml_error = { version = "0.1.3", path = "../sdml_error" }
sdml_error = { version = "0.1.5", path = "../sdml_error" }
serde = { version = "1.0.195", features = ["derive"], optional = true }
serde_json = { version = "1.0.113", optional = true }
serde_regex = { version = "1.1.0", optional = true }
Expand Down
4 changes: 4 additions & 0 deletions sdml-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Note that other tools can use the `sdml_core` API to create or manipulate models

## Changes

**Version 0.2.13**

* Feature: add new validation for `IdentifierNotPreferredCase`, to enforce case conventions.

**Version 0.2.12**

* Feature: more term validation, mainly to reduce the number of `todo!()` panics.
Expand Down
9 changes: 6 additions & 3 deletions sdml-core/src/model/definitions/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ use crate::model::annotations::Annotation;
use crate::model::check::Validate;
use crate::model::constraints::{ConstraintSentence, FunctionCardinality, FunctionSignature};
use crate::model::identifiers::{Identifier, IdentifierReference};
use crate::model::{References, Span};
use crate::model::{HasName, References, Span};

use sdml_error::diagnostics::functions::IdentifierCaseConvention;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -126,11 +127,13 @@ impl References for TypeClassDef {
impl Validate for TypeClassDef {
fn validate(
&self,
_top: &crate::model::modules::Module,
top: &crate::model::modules::Module,
_cache: &ModuleCache,
_loader: &impl ModuleLoader,
loader: &impl ModuleLoader,
_check_constraints: bool,
) {
self.name()
.validate(top, loader, Some(IdentifierCaseConvention::TypeDefinition));
todo!()
}
}
Expand Down
11 changes: 9 additions & 2 deletions sdml-core/src/model/definitions/datatypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ use crate::model::References;
use crate::model::{
annotations::AnnotationOnlyBody,
identifiers::{Identifier, IdentifierReference},
Span,
HasName, Span,
};
use sdml_error::diagnostics::functions::{
datatype_invalid_base_type, type_definition_not_found, IdentifierCaseConvention,
};
use sdml_error::diagnostics::functions::{datatype_invalid_base_type, type_definition_not_found};
use std::{collections::HashSet, fmt::Debug};

#[cfg(feature = "serde")]
Expand Down Expand Up @@ -56,6 +58,11 @@ impl Validate for DatatypeDef {
loader: &impl ModuleLoader,
_check_constraints: bool,
) {
self.name().validate(
top,
loader,
Some(IdentifierCaseConvention::DatatypeDefinition),
);
if let Some(defn) = find_definition(self.base_type(), top, cache) {
if let Definition::Datatype(_base) = defn {
// TODO: check restriction annotations.
Expand Down
45 changes: 38 additions & 7 deletions sdml-core/src/model/definitions/entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::model::check::MaybeIncomplete;
use crate::model::members::TypeReference;
use crate::model::References;
use crate::model::{
annotations::{Annotation, HasAnnotations},
annotations::Annotation,
check::Validate,
definitions::HasMembers,
identifiers::{Identifier, IdentifierReference},
Expand All @@ -15,6 +15,7 @@ use crate::model::{
};
use std::{collections::HashSet, fmt::Debug};

use sdml_error::diagnostics::functions::IdentifierCaseConvention;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use tracing::warn;
Expand Down Expand Up @@ -89,12 +90,26 @@ impl_has_source_span_for!(EntityDef);

impl_references_for!(EntityDef => delegate optional body);

impl_validate_for!(EntityDef => delegate optional body);

impl_annotation_builder!(EntityDef, optional body);

impl_maybe_invalid_for!(EntityDef);

impl Validate for EntityDef {
fn validate(
&self,
top: &Module,
cache: &ModuleCache,
loader: &impl ModuleLoader,
check_constraints: bool,
) {
self.name
.validate(top, loader, Some(IdentifierCaseConvention::TypeDefinition));
if let Some(body) = &self.body {
body.validate(top, cache, loader, check_constraints);
}
}
}

impl EntityDef {
// --------------------------------------------------------------------------------------------
// EntityDef :: Constructors
Expand All @@ -117,10 +132,27 @@ impl_has_members_for!(EntityBody);

impl_has_source_span_for!(EntityBody);

impl_validate_for_annotations_and_members!(EntityBody);

impl_maybe_invalid_for!(EntityBody; over members);

impl Validate for EntityBody {
fn validate(
&self,
top: &Module,
cache: &ModuleCache,
loader: &impl ModuleLoader,
check_constraints: bool,
) {
self.identity
.validate(top, cache, loader, check_constraints);
for annotation in &self.annotations {
annotation.validate(top, cache, loader, check_constraints);
}
for member in &self.members {
member.validate(top, cache, loader, check_constraints);
}
}
}

impl References for EntityBody {
fn referenced_annotations<'a>(&'a self, names: &mut HashSet<&'a IdentifierReference>) {
self.members().for_each(|m| m.referenced_annotations(names))
Expand Down Expand Up @@ -291,8 +323,7 @@ impl Validate for EntityIdentityDef {
_loader: &impl ModuleLoader,
_check_constraints: bool,
) {
warn!("");
// TODO: check type reference
warn!("EntityIdentityDef::Validate nope");
}
}

Expand Down
27 changes: 22 additions & 5 deletions sdml-core/src/model/definitions/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use crate::{
definitions::HasVariants,
identifiers::{Identifier, IdentifierReference},
modules::Module,
References, Span,
HasName, References, Span,
},
};
use sdml_error::diagnostics::functions::IdentifierCaseConvention;
use std::{collections::HashSet, fmt::Debug};
use tracing::warn;

Expand Down Expand Up @@ -70,8 +71,22 @@ impl_has_source_span_for!(EnumDef);

impl_maybe_invalid_for!(EnumDef; exists body);

// TODO check that any equivalent class is a datatype.
impl_validate_for!(EnumDef => delegate optional body);
impl Validate for EnumDef {
fn validate(
&self,
top: &Module,
cache: &ModuleCache,
loader: &impl ModuleLoader,
check_constraints: bool,
) {
// TODO check that any equivalent class is a datatype.
self.name()
.validate(top, loader, Some(IdentifierCaseConvention::TypeDefinition));
if let Some(body) = &self.body {
body.validate(top, cache, loader, check_constraints);
}
}
}

impl_annotation_builder!(EnumDef, optional body);

Expand Down Expand Up @@ -125,12 +140,14 @@ impl_annotation_builder!(ValueVariant, optional body);
impl Validate for ValueVariant {
fn validate(
&self,
_top: &Module,
top: &Module,
_cache: &ModuleCache,
_loader: &impl ModuleLoader,
loader: &impl ModuleLoader,
_check_constraints: bool,
) {
warn!("Missing validation for ValueVariant values.");
self.name()
.validate(top, loader, Some(IdentifierCaseConvention::ValueVariant));
}
}

Expand Down
22 changes: 19 additions & 3 deletions sdml-core/src/model/definitions/events.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::model::{
check::Validate,
definitions::StructureBody,
identifiers::{Identifier, IdentifierReference},
Span,
HasName, Span,
};
use sdml_error::diagnostics::functions::IdentifierCaseConvention;
use std::fmt::Debug;

#[cfg(feature = "serde")]
Expand Down Expand Up @@ -42,8 +44,22 @@ impl_has_source_span_for!(EventDef);

impl_maybe_invalid_for!(EventDef);

// TODO: need to include event_source in validation!!
impl_validate_for!(EventDef => delegate optional body);
impl Validate for EventDef {
fn validate(
&self,
top: &crate::model::modules::Module,
cache: &crate::cache::ModuleCache,
loader: &impl crate::load::ModuleLoader,
check_constraints: bool,
) {
// TODO: need to include event_source in validation!!
self.name()
.validate(top, loader, Some(IdentifierCaseConvention::TypeDefinition));
if let Some(body) = &self.body {
body.validate(top, cache, loader, check_constraints);
}
}
}

impl_annotation_builder!(EventDef, optional body);

Expand Down
21 changes: 18 additions & 3 deletions sdml-core/src/model/definitions/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ use crate::{
identifiers::{Identifier, IdentifierReference},
members::{Cardinality, HasCardinality, HasType, MemberDef, TypeReference},
modules::Module,
HasOptionalBody, References, Span,
HasName, HasOptionalBody, References, Span,
},
};
use sdml_error::diagnostics::functions::IdentifierCaseConvention;
use std::{collections::HashSet, fmt::Debug};
use tracing::warn;

Expand Down Expand Up @@ -77,10 +78,24 @@ impl_references_for!(PropertyDef => delegate optional body);

impl_maybe_invalid_for!(PropertyDef);

impl_validate_for!(PropertyDef => delegate optional body);

impl_annotation_builder!(PropertyDef, optional body);

impl Validate for PropertyDef {
fn validate(
&self,
top: &crate::model::modules::Module,
cache: &crate::cache::ModuleCache,
loader: &impl crate::load::ModuleLoader,
check_constraints: bool,
) {
self.name()
.validate(top, loader, Some(IdentifierCaseConvention::TypeDefinition));
if let Some(body) = &self.body {
body.validate(top, cache, loader, check_constraints);
}
}
}

impl PropertyDef {
// --------------------------------------------------------------------------------------------
// Constructors
Expand Down
14 changes: 8 additions & 6 deletions sdml-core/src/model/definitions/rdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
},
stdlib,
};
use tracing::info;
use sdml_error::diagnostics::functions::IdentifierCaseConvention;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -64,12 +64,14 @@ impl_maybe_invalid_for!(RdfDef; always false);
impl Validate for RdfDef {
fn validate(
&self,
_top: &Module,
_cache: &ModuleCache,
_loader: &impl ModuleLoader,
_check_constraints: bool,
top: &Module,
cache: &ModuleCache,
loader: &impl ModuleLoader,
check_constraints: bool,
) {
info!("RdfDef is always valid.");
self.name
.validate(top, loader, Some(IdentifierCaseConvention::RdfDefinition));
self.body.validate(top, cache, loader, check_constraints);
}
}

Expand Down
19 changes: 17 additions & 2 deletions sdml-core/src/model/definitions/structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::model::{
};
use std::{collections::HashSet, fmt::Debug};

use sdml_error::diagnostics::functions::IdentifierCaseConvention;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -50,12 +51,26 @@ impl_has_source_span_for!(StructureDef);

impl_references_for!(StructureDef => delegate optional body);

impl_validate_for!(StructureDef => delegate optional body);

impl_annotation_builder!(StructureDef, optional body);

impl_maybe_invalid_for!(StructureDef);

impl Validate for StructureDef {
fn validate(
&self,
top: &crate::model::modules::Module,
cache: &crate::cache::ModuleCache,
loader: &impl crate::load::ModuleLoader,
check_constraints: bool,
) {
self.name
.validate(top, loader, Some(IdentifierCaseConvention::TypeDefinition));
if let Some(body) = &self.body {
body.validate(top, cache, loader, check_constraints);
}
}
}

impl StructureDef {
// --------------------------------------------------------------------------------------------
// Constructors
Expand Down

0 comments on commit c0defe5

Please sign in to comment.