Skip to content

Commit

Permalink
expose sub_elements_spec for ElementType
Browse files Browse the repository at this point in the history
  • Loading branch information
EdVanDance authored and DanielT committed Dec 28, 2024
1 parent bcd8f69 commit a546d05
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
11 changes: 11 additions & 0 deletions autosar_data.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ class ElementType:
def reference_dest_value(self, target: ElementType) -> EnumItem:
"""helper to determine the correct value for the DEST attribute when setting a reference"""
...
sub_elements_spec: List[SubElementSpec]
"""a list of the specifications of all sub elements allowed on elements of this type"""
def find_sub_element(self, target_name: ElementName, version: VersionSpecification) -> ElementType:
"""find the ElementType of the named sub element in the specification of this ElementType"""
...
Expand Down Expand Up @@ -424,6 +426,15 @@ class AttributeSpec:
required: bool
"""is the attribute required or optional"""

class SubElementSpec:
"""The specification of a sub element"""
element_name: str
"""name of the sub element"""
element_type: ElementType
"""element type of the sub element"""
allowed_versions: List[AutosarVersion]
"""list of versions in which this sub element is compatible"""

class CharacterDataTypeEnum:
"""Character data type: enum"""
values: List[str]
Expand Down
19 changes: 18 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ struct IncompatibleAttributeValueError {
target_version: AutosarVersion,
}

#[pyclass(frozen)]
#[pyclass(eq, frozen)]
#[derive(Debug, Clone, PartialEq, Eq)]
/// Type of an Element in the specification
struct ElementType(autosar_data_specification::ElementType);

Expand Down Expand Up @@ -142,6 +143,21 @@ struct AttributeSpec {
required: bool,
}

#[pyclass(frozen)]
#[derive(Debug)]
/// Specification of a sub element
struct SubElementSpec {
#[pyo3(get)]
/// name of the sub element
element_name: String,
#[pyo3(get)]
/// element type of the sub element
element_type: ElementType,
#[pyo3(get)]
/// list of versions in which this sub element is compatible
allowed_versions: Vec<AutosarVersion>,
}

#[pyclass(eq, eq_int)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// The content mode of an element type
Expand Down Expand Up @@ -457,6 +473,7 @@ fn autosar_data(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<AttributeIterator>()?;
m.add_class::<Attribute>()?;
m.add_class::<AttributeSpec>()?;
m.add_class::<SubElementSpec>()?;
m.add_class::<ContentMode>()?;
m.add_class::<ValidSubElementInfo>()?;
m.add_class::<CharacterDataTypeEnum>()?;
Expand Down
25 changes: 25 additions & 0 deletions src/specification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ impl ElementType {
.map(|enumitem| enumitem.to_string())
}

#[getter]
fn sub_elements_spec(&self) -> Vec<SubElementSpec> {
self.0
.sub_element_spec_iter()
.map(|(element_name, element_type, version_mask, _)| {
let versions = expand_version_mask(version_mask)
.iter()
.map(|&ver| AutosarVersion::from(ver))
.collect();
SubElementSpec {
element_name: element_name.to_string(),
element_type: ElementType(element_type),
allowed_versions: versions,
}
})
.collect()
}

fn find_sub_element(
&self,
target_name: &str,
Expand Down Expand Up @@ -131,6 +149,13 @@ impl ContentMode {
}
}

#[pymethods]
impl SubElementSpec {
fn __repr__(&self) -> String {
format!("{self:?}")
}
}

#[pymethods]
impl CharacterDataTypeEnum {
fn __repr__(&self) -> String {
Expand Down
10 changes: 10 additions & 0 deletions test/specification_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,13 @@ def test_specification_uint() -> None:
assert not cse_spec.__repr__() is None


def test_specification_sub_elements_spec() -> None:
model = AutosarModel()
model.create_file("file")
el_ar_packages = model.root_element.create_sub_element("AR-PACKAGES")
el_pkg = el_ar_packages.create_named_sub_element("AR-PACKAGE", "pkg")
sub_element_spec = el_ar_packages.element_type.sub_elements_spec[0]

assert sub_element_spec.element_name == "AR-PACKAGE"
assert sub_element_spec.element_type == el_pkg.element_type
assert AutosarVersion.AUTOSAR_00050 in sub_element_spec.allowed_versions

0 comments on commit a546d05

Please sign in to comment.