Skip to content

Commit

Permalink
refactor: split into multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielT committed Aug 22, 2023
1 parent a7967d6 commit 501c27e
Show file tree
Hide file tree
Showing 4 changed files with 672 additions and 654 deletions.
146 changes: 146 additions & 0 deletions src/arxmlfile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
use std::collections::hash_map::DefaultHasher;
use std::hash::Hash;
use std::hash::Hasher;

use crate::*;
use ::autosar_data as autosar_data_rs;
use autosar_data_rs::CompatibilityError;

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

fn __str__(&self) -> PyResult<String> {
self.serialize()
}

fn __richcmp__(&self, other: &ArxmlFile, op: pyo3::basic::CompareOp) -> bool {
match op {
pyo3::pyclass::CompareOp::Eq => self.0 == other.0,
pyo3::pyclass::CompareOp::Ne => self.0 != other.0,
pyo3::pyclass::CompareOp::Lt
| pyo3::pyclass::CompareOp::Le
| pyo3::pyclass::CompareOp::Gt
| pyo3::pyclass::CompareOp::Ge => false,
}
}

fn __hash__(&self) -> isize {
let mut hasher = DefaultHasher::new();
self.0.hash(&mut hasher);
hasher.finish() as isize
}

#[getter]
fn filename(&self) -> String {
self.0.filename().to_string_lossy().into_owned()
}

#[setter]
fn set_filename(&self, filename: &str) -> PyResult<()> {
self.0
.set_filename(filename)
.map_err(|error| AutosarDataError::new_err(error.to_string()))
}

#[getter]
fn version(&self) -> autosar_data_rs::AutosarVersion {
self.0.version()
}

#[setter]
fn set_version(&self, version: autosar_data_rs::AutosarVersion) -> PyResult<()> {
self.0
.set_version(version)
.map_err(|error| AutosarDataError::new_err(error.to_string()))
}

fn check_version_compatibility(
&self,
target_version: autosar_data_rs::AutosarVersion,
) -> Vec<PyObject> {
Python::with_gil(|py| {
self.0
.check_version_compatibility(target_version)
.0
.iter()
.map(|cerr| -> PyObject {
match cerr {
CompatibilityError::IncompatibleAttribute {
element,
attribute,
version_mask,
} => Py::new(
py,
IncompatibleAttributeError {
element: Element(element.to_owned()),
attribute: *attribute,
version_mask: *version_mask,
target_version,
},
)
.unwrap()
.into_py(py),
CompatibilityError::IncompatibleAttributeValue {
element,
attribute,
attribute_value,
version_mask,
} => Py::new(
py,
IncompatibleAttributeValueError {
element: Element(element.to_owned()),
attribute: *attribute,
attribute_value: attribute_value.to_owned(),
version_mask: *version_mask,
target_version,
},
)
.unwrap()
.into_py(py),
CompatibilityError::IncompatibleElement {
element,
version_mask,
} => Py::new(
py,
IncompatibleElementError {
element: Element(element.to_owned()),
version_mask: *version_mask,
target_version,
},
)
.unwrap()
.into_py(py),
}
})
.collect()
})
}

#[getter]
fn model(&self) -> PyResult<AutosarModel> {
match self.0.model() {
Ok(model) => Ok(AutosarModel(model)),
Err(error) => PyResult::Err(AutosarDataError::new_err(error.to_string())),
}
}

#[getter]
fn elements_dfs(&self) -> ArxmlFileElementsDfsIterator {
ArxmlFileElementsDfsIterator(self.0.elements_dfs())
}

fn serialize(&self) -> PyResult<String> {
match self.0.serialize() {
Ok(text) => Ok(text),
Err(error) => Err(AutosarDataError::new_err(error.to_string())),
}
}

#[getter]
fn xml_standalone(&self) -> Option<bool> {
self.0.xml_standalone()
}
}
Loading

0 comments on commit 501c27e

Please sign in to comment.