Skip to content

Commit 501c27e

Browse files
committed
refactor: split into multiple files
1 parent a7967d6 commit 501c27e

File tree

4 files changed

+672
-654
lines changed

4 files changed

+672
-654
lines changed

src/arxmlfile.rs

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
use std::collections::hash_map::DefaultHasher;
2+
use std::hash::Hash;
3+
use std::hash::Hasher;
4+
5+
use crate::*;
6+
use ::autosar_data as autosar_data_rs;
7+
use autosar_data_rs::CompatibilityError;
8+
9+
#[pymethods]
10+
impl ArxmlFile {
11+
fn __repr__(&self) -> String {
12+
format!("{:#?}", self.0)
13+
}
14+
15+
fn __str__(&self) -> PyResult<String> {
16+
self.serialize()
17+
}
18+
19+
fn __richcmp__(&self, other: &ArxmlFile, op: pyo3::basic::CompareOp) -> bool {
20+
match op {
21+
pyo3::pyclass::CompareOp::Eq => self.0 == other.0,
22+
pyo3::pyclass::CompareOp::Ne => self.0 != other.0,
23+
pyo3::pyclass::CompareOp::Lt
24+
| pyo3::pyclass::CompareOp::Le
25+
| pyo3::pyclass::CompareOp::Gt
26+
| pyo3::pyclass::CompareOp::Ge => false,
27+
}
28+
}
29+
30+
fn __hash__(&self) -> isize {
31+
let mut hasher = DefaultHasher::new();
32+
self.0.hash(&mut hasher);
33+
hasher.finish() as isize
34+
}
35+
36+
#[getter]
37+
fn filename(&self) -> String {
38+
self.0.filename().to_string_lossy().into_owned()
39+
}
40+
41+
#[setter]
42+
fn set_filename(&self, filename: &str) -> PyResult<()> {
43+
self.0
44+
.set_filename(filename)
45+
.map_err(|error| AutosarDataError::new_err(error.to_string()))
46+
}
47+
48+
#[getter]
49+
fn version(&self) -> autosar_data_rs::AutosarVersion {
50+
self.0.version()
51+
}
52+
53+
#[setter]
54+
fn set_version(&self, version: autosar_data_rs::AutosarVersion) -> PyResult<()> {
55+
self.0
56+
.set_version(version)
57+
.map_err(|error| AutosarDataError::new_err(error.to_string()))
58+
}
59+
60+
fn check_version_compatibility(
61+
&self,
62+
target_version: autosar_data_rs::AutosarVersion,
63+
) -> Vec<PyObject> {
64+
Python::with_gil(|py| {
65+
self.0
66+
.check_version_compatibility(target_version)
67+
.0
68+
.iter()
69+
.map(|cerr| -> PyObject {
70+
match cerr {
71+
CompatibilityError::IncompatibleAttribute {
72+
element,
73+
attribute,
74+
version_mask,
75+
} => Py::new(
76+
py,
77+
IncompatibleAttributeError {
78+
element: Element(element.to_owned()),
79+
attribute: *attribute,
80+
version_mask: *version_mask,
81+
target_version,
82+
},
83+
)
84+
.unwrap()
85+
.into_py(py),
86+
CompatibilityError::IncompatibleAttributeValue {
87+
element,
88+
attribute,
89+
attribute_value,
90+
version_mask,
91+
} => Py::new(
92+
py,
93+
IncompatibleAttributeValueError {
94+
element: Element(element.to_owned()),
95+
attribute: *attribute,
96+
attribute_value: attribute_value.to_owned(),
97+
version_mask: *version_mask,
98+
target_version,
99+
},
100+
)
101+
.unwrap()
102+
.into_py(py),
103+
CompatibilityError::IncompatibleElement {
104+
element,
105+
version_mask,
106+
} => Py::new(
107+
py,
108+
IncompatibleElementError {
109+
element: Element(element.to_owned()),
110+
version_mask: *version_mask,
111+
target_version,
112+
},
113+
)
114+
.unwrap()
115+
.into_py(py),
116+
}
117+
})
118+
.collect()
119+
})
120+
}
121+
122+
#[getter]
123+
fn model(&self) -> PyResult<AutosarModel> {
124+
match self.0.model() {
125+
Ok(model) => Ok(AutosarModel(model)),
126+
Err(error) => PyResult::Err(AutosarDataError::new_err(error.to_string())),
127+
}
128+
}
129+
130+
#[getter]
131+
fn elements_dfs(&self) -> ArxmlFileElementsDfsIterator {
132+
ArxmlFileElementsDfsIterator(self.0.elements_dfs())
133+
}
134+
135+
fn serialize(&self) -> PyResult<String> {
136+
match self.0.serialize() {
137+
Ok(text) => Ok(text),
138+
Err(error) => Err(AutosarDataError::new_err(error.to_string())),
139+
}
140+
}
141+
142+
#[getter]
143+
fn xml_standalone(&self) -> Option<bool> {
144+
self.0.xml_standalone()
145+
}
146+
}

0 commit comments

Comments
 (0)