Skip to content

Commit

Permalink
Merge pull request #3 from DanielT/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
DanielT authored Aug 23, 2023
2 parents b54b05c + 140bcc3 commit 859d3c0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 84 deletions.
5 changes: 1 addition & 4 deletions src/arxmlfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ impl ArxmlFile {
.map_err(|error| AutosarDataError::new_err(error.to_string()))
}

fn check_version_compatibility(
&self,
target_version: AutosarVersion,
) -> Vec<PyObject> {
fn check_version_compatibility(&self, target_version: AutosarVersion) -> Vec<PyObject> {
Python::with_gil(|py| {
self.0
.check_version_compatibility(target_version.into())
Expand Down
84 changes: 31 additions & 53 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,35 @@ struct AttributeIterator(autosar_data_rs::AttributeIterator);
#[pyclass(frozen)]
#[derive(Debug, Clone)]
struct IncompatibleElementError {
#[pyo3(get)]
element: Element,
#[pyo3(get)]
version_mask: u32,
target_version: AutosarVersion,
}

#[pyclass(frozen)]
#[derive(Debug, Clone)]
struct IncompatibleAttributeError {
#[pyo3(get)]
element: Element,
#[pyo3(get)]
attribute: String,
#[pyo3(get)]
version_mask: u32,
target_version: AutosarVersion,
}

#[pyclass(frozen)]
#[derive(Debug, Clone)]
struct IncompatibleAttributeValueError {
#[pyo3(get)]
element: Element,
#[pyo3(get)]
attribute: String,
#[pyo3(get)]
attribute_value: String,
#[pyo3(get)]
version_mask: u32,
target_version: AutosarVersion,
}
Expand Down Expand Up @@ -115,21 +124,6 @@ impl IncompatibleAttributeError {
self.target_version
)
}

#[getter]
fn get_attribute(&self) -> String {
self.attribute.to_string()
}

#[getter]
fn get_version_mask(&self) -> u32 {
self.version_mask
}

#[getter]
fn get_element(&self) -> Element {
self.element.clone()
}
}

#[pymethods]
Expand All @@ -147,26 +141,6 @@ impl IncompatibleAttributeValueError {
self.target_version
)
}

#[getter]
fn get_attribute(&self) -> String {
self.attribute.to_string()
}

#[getter]
fn get_attribute_value(&self) -> String {
self.attribute_value.clone()
}

#[getter]
fn get_version_mask(&self) -> u32 {
self.version_mask
}

#[getter]
fn get_element(&self) -> Element {
self.element.clone()
}
}

#[pymethods]
Expand All @@ -182,16 +156,6 @@ impl IncompatibleElementError {
self.target_version
)
}

#[getter]
fn get_version_mask(&self) -> u32 {
self.version_mask
}

#[getter]
fn get_element(&self) -> Element {
self.element.clone()
}
}

#[pymethods]
Expand Down Expand Up @@ -225,7 +189,9 @@ impl ElementType {
}

fn reference_dest_value(&self, target: &ElementType) -> Option<String> {
self.0.reference_dest_value(&target.0).map(|enumitem| enumitem.to_string())
self.0
.reference_dest_value(&target.0)
.map(|enumitem| enumitem.to_string())
}

fn find_sub_element(&self, target_name: String, version: u32) -> PyResult<Option<ElementType>> {
Expand Down Expand Up @@ -389,12 +355,17 @@ fn autosar_data(py: Python, m: &PyModule) -> PyResult<()> {
Ok(())
}

fn extract_character_data(spec: &CharacterDataSpec, any: PyObject) -> PyResult<autosar_data_rs::CharacterData> {
fn extract_character_data(
spec: &CharacterDataSpec,
any: PyObject,
) -> PyResult<autosar_data_rs::CharacterData> {
Python::with_gil(|py| {
if let Ok(text) = any.extract::<String>(py) {
parse_cdata_string(spec, text).map_err(|_| AutosarDataError::new_err(
autosar_data_rs::AutosarDataError::IncorrectContentType.to_string(),
))
parse_cdata_string(spec, text).map_err(|_| {
AutosarDataError::new_err(
autosar_data_rs::AutosarDataError::IncorrectContentType.to_string(),
)
})
} else if let Ok(val) = any.extract::<u64>(py) {
Ok(autosar_data_rs::CharacterData::UnsignedInteger(val))
} else if let Ok(val) = any.extract::<f64>(py) {
Expand All @@ -407,7 +378,10 @@ fn extract_character_data(spec: &CharacterDataSpec, any: PyObject) -> PyResult<a
})
}

fn parse_cdata_string(spec: &CharacterDataSpec, text: String) -> Result<autosar_data_rs::CharacterData, ()> {
fn parse_cdata_string(
spec: &CharacterDataSpec,
text: String,
) -> Result<autosar_data_rs::CharacterData, ()> {
match spec {
CharacterDataSpec::Enum { items } => {
let enumitem = autosar_data_rs::EnumItem::from_str(&text).map_err(|_| ())?;
Expand All @@ -417,7 +391,11 @@ fn parse_cdata_string(spec: &CharacterDataSpec, text: String) -> Result<autosar_
Err(())
}
}
CharacterDataSpec::Pattern { check_fn, max_length, .. } => {
CharacterDataSpec::Pattern {
check_fn,
max_length,
..
} => {
if text.len() < max_length.unwrap_or(usize::MAX) && check_fn(text.as_bytes()) {
Ok(autosar_data_rs::CharacterData::String(text))
} else {
Expand All @@ -438,7 +416,7 @@ fn parse_cdata_string(spec: &CharacterDataSpec, text: String) -> Result<autosar_
Err(())
}
}
CharacterDataSpec::Double => {
CharacterDataSpec::Double => {
if let Ok(val) = text.parse() {
Ok(autosar_data_rs::CharacterData::Double(val))
} else {
Expand Down
6 changes: 1 addition & 5 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ impl AutosarModel {
hasher.finish() as isize
}

fn create_file(
&self,
filename: &str,
version: AutosarVersion,
) -> PyResult<ArxmlFile> {
fn create_file(&self, filename: &str, version: AutosarVersion) -> PyResult<ArxmlFile> {
match self.0.create_file(filename, version.into()) {
Ok(file) => Ok(ArxmlFile(file)),
Err(error) => PyResult::Err(AutosarDataError::new_err(error.to_string())),
Expand Down
44 changes: 22 additions & 22 deletions src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,28 @@ impl AutosarVersion {
}
}

impl Into<autosar_data_specification::AutosarVersion> for AutosarVersion {
fn into(self) -> autosar_data_specification::AutosarVersion {
match self {
Self::Autosar_4_0_1 => autosar_data_specification::AutosarVersion::Autosar_4_0_1,
Self::Autosar_4_0_2 => autosar_data_specification::AutosarVersion::Autosar_4_0_2,
Self::Autosar_4_0_3 => autosar_data_specification::AutosarVersion::Autosar_4_0_3,
Self::Autosar_4_1_1 => autosar_data_specification::AutosarVersion::Autosar_4_1_1,
Self::Autosar_4_1_2 => autosar_data_specification::AutosarVersion::Autosar_4_1_2,
Self::Autosar_4_1_3 => autosar_data_specification::AutosarVersion::Autosar_4_1_3,
Self::Autosar_4_2_1 => autosar_data_specification::AutosarVersion::Autosar_4_2_1,
Self::Autosar_4_2_2 => autosar_data_specification::AutosarVersion::Autosar_4_2_2,
Self::Autosar_4_3_0 => autosar_data_specification::AutosarVersion::Autosar_4_3_0,
Self::Autosar_00042 => autosar_data_specification::AutosarVersion::Autosar_00042,
Self::Autosar_00043 => autosar_data_specification::AutosarVersion::Autosar_00043,
Self::Autosar_00044 => autosar_data_specification::AutosarVersion::Autosar_00044,
Self::Autosar_00045 => autosar_data_specification::AutosarVersion::Autosar_00045,
Self::Autosar_00046 => autosar_data_specification::AutosarVersion::Autosar_00046,
Self::Autosar_00047 => autosar_data_specification::AutosarVersion::Autosar_00047,
Self::Autosar_00048 => autosar_data_specification::AutosarVersion::Autosar_00048,
Self::Autosar_00049 => autosar_data_specification::AutosarVersion::Autosar_00049,
Self::Autosar_00050 => autosar_data_specification::AutosarVersion::Autosar_00050,
Self::Autosar_00051 => autosar_data_specification::AutosarVersion::Autosar_00051,
impl From<AutosarVersion> for autosar_data_specification::AutosarVersion {
fn from(val: AutosarVersion) -> Self {
match val {
AutosarVersion::Autosar_4_0_1 => Self::Autosar_4_0_1,
AutosarVersion::Autosar_4_0_2 => Self::Autosar_4_0_2,
AutosarVersion::Autosar_4_0_3 => Self::Autosar_4_0_3,
AutosarVersion::Autosar_4_1_1 => Self::Autosar_4_1_1,
AutosarVersion::Autosar_4_1_2 => Self::Autosar_4_1_2,
AutosarVersion::Autosar_4_1_3 => Self::Autosar_4_1_3,
AutosarVersion::Autosar_4_2_1 => Self::Autosar_4_2_1,
AutosarVersion::Autosar_4_2_2 => Self::Autosar_4_2_2,
AutosarVersion::Autosar_4_3_0 => Self::Autosar_4_3_0,
AutosarVersion::Autosar_00042 => Self::Autosar_00042,
AutosarVersion::Autosar_00043 => Self::Autosar_00043,
AutosarVersion::Autosar_00044 => Self::Autosar_00044,
AutosarVersion::Autosar_00045 => Self::Autosar_00045,
AutosarVersion::Autosar_00046 => Self::Autosar_00046,
AutosarVersion::Autosar_00047 => Self::Autosar_00047,
AutosarVersion::Autosar_00048 => Self::Autosar_00048,
AutosarVersion::Autosar_00049 => Self::Autosar_00049,
AutosarVersion::Autosar_00050 => Self::Autosar_00050,
AutosarVersion::Autosar_00051 => Self::Autosar_00051,
}
}
}
Expand Down

0 comments on commit 859d3c0

Please sign in to comment.