Skip to content

Commit

Permalink
Cleanup: return PyResult intead of using unwrap()
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielT committed Dec 1, 2024
1 parent faf3f03 commit 54047f8
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 114 deletions.
114 changes: 58 additions & 56 deletions src/arxmlfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,64 +59,66 @@ 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,
) -> PyResult<Vec<PyObject>> {
let (error_list, _) = self.0.check_version_compatibility(target_version.into());
error_list
.iter()
.map(|cerr| {
match cerr {
CompatibilityError::IncompatibleAttribute {
element,
attribute,
version_mask,
} => {
let errobj = IncompatibleAttributeError {
element: Element(element.to_owned()),
attribute: attribute.to_string(),
allowed_versions: expand_version_mask(*version_mask)
.iter()
.map(|&v| v.into())
.collect(),
target_version,
};
Python::with_gil(|py| errobj.into_py_any(py))
}
CompatibilityError::IncompatibleAttributeValue {
element,
attribute,
attribute_value,
version_mask,
} => {
let errobj = IncompatibleAttributeValueError {
element: Element(element.to_owned()),
attribute: attribute.to_string(),
attribute_value: attribute_value.to_owned(),
allowed_versions: expand_version_mask(*version_mask)
.iter()
.map(|&v| v.into())
.collect(),
target_version,
};
Python::with_gil(|py| errobj.into_py_any(py))
}
CompatibilityError::IncompatibleElement {
element,
version_mask,
} => {
let errobj = IncompatibleElementError {
element: Element(element.to_owned()),
allowed_versions: expand_version_mask(*version_mask)
.iter()
.map(|&v| v.into())
.collect(),
target_version,
};
Python::with_gil(|py| errobj.into_py_any(py))
}
let mut out_list = Vec::with_capacity(error_list.len());
for compat_err in error_list {
let pyobj = match compat_err {
CompatibilityError::IncompatibleAttribute {
element,
attribute,
version_mask,
} => {
let errobj = IncompatibleAttributeError {
element: Element(element.to_owned()),
attribute: attribute.to_string(),
allowed_versions: expand_version_mask(version_mask)
.iter()
.map(|&v| v.into())
.collect(),
target_version,
};
Python::with_gil(|py| errobj.into_py_any(py))?
}
.unwrap()
})
.collect::<Vec<_>>()
CompatibilityError::IncompatibleAttributeValue {
element,
attribute,
attribute_value,
version_mask,
} => {
let errobj = IncompatibleAttributeValueError {
element: Element(element.to_owned()),
attribute: attribute.to_string(),
attribute_value: attribute_value.to_owned(),
allowed_versions: expand_version_mask(version_mask)
.iter()
.map(|&v| v.into())
.collect(),
target_version,
};
Python::with_gil(|py| errobj.into_py_any(py))?
}
CompatibilityError::IncompatibleElement {
element,
version_mask,
} => {
let errobj = IncompatibleElementError {
element: Element(element.to_owned()),
allowed_versions: expand_version_mask(version_mask)
.iter()
.map(|&v| v.into())
.collect(),
target_version,
};
Python::with_gil(|py| errobj.into_py_any(py))?
}
};
out_list.push(pyobj);
}
Ok(out_list)
}

#[getter]
Expand Down
18 changes: 9 additions & 9 deletions src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,11 @@ impl Element {
}

#[getter]
fn character_data(&self) -> Option<PyObject> {
fn character_data(&self) -> PyResult<Option<PyObject>> {
self.0
.character_data()
.map(|cdata| character_data_to_object(&cdata))
.transpose()
}

fn insert_character_content_item(&self, chardata: &str, position: usize) -> PyResult<()> {
Expand Down Expand Up @@ -344,10 +345,10 @@ impl Element {

pub(crate) fn attribute_value(&self, attrname_str: &str) -> PyResult<Option<PyObject>> {
let attrname = get_attribute_name(attrname_str)?;
Ok(self
.0
self.0
.attribute_value(attrname)
.map(|cdata| character_data_to_object(&cdata)))
.map(|cdata| character_data_to_object(&cdata))
.transpose()
}

pub(crate) fn set_attribute(&self, attrname_str: &str, value: PyObject) -> PyResult<()> {
Expand Down Expand Up @@ -397,11 +398,10 @@ impl Element {
let file_set_iter = weak_file_set
.iter()
.filter_map(|weak| weak.upgrade().map(ArxmlFile));
let pytuple = Python::with_gil(|py| {
let frozenset = PyFrozenSet::new(py, file_set_iter).unwrap();
(local, frozenset).into_py_any(py).unwrap()
});
Ok(pytuple)
Python::with_gil(|py| {
let frozenset = PyFrozenSet::new(py, file_set_iter)?;
(local, frozenset).into_py_any(py)
})
}
Err(error) => Err(AutosarDataError::new_err(error.to_string())),
}
Expand Down
78 changes: 40 additions & 38 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,11 @@ impl ElementsDfsIterator {
slf
}

fn __next__(&mut self) -> Option<PyObject> {
self.0.next().map(|(depth, elem)| {
Python::with_gil(|py| (depth, Element(elem)).into_py_any(py).unwrap())
})
fn __next__(&mut self) -> PyResult<Option<PyObject>> {
self.0
.next()
.map(|(depth, elem)| Python::with_gil(|py| (depth, Element(elem)).into_py_any(py)))
.transpose()
}
}

Expand All @@ -283,16 +284,15 @@ impl IdentifiablesIterator {
slf
}

fn __next__(&mut self) -> Option<PyObject> {
fn __next__(&mut self) -> PyResult<Option<PyObject>> {
for (path, weak) in &mut self.0 {
if let Some(elem) = weak.upgrade() {
return Some(Python::with_gil(|py| {
(path, Element(elem)).into_py_any(py).unwrap()
}));
let pyobj = Python::with_gil(|py| (path, Element(elem)).into_py_any(py))?;
return Ok(Some(pyobj));
}
}

None
Ok(None)
}
}

Expand All @@ -302,10 +302,11 @@ impl ArxmlFileElementsDfsIterator {
slf
}

fn __next__(&mut self) -> Option<PyObject> {
self.0.next().map(|(depth, elem)| {
Python::with_gil(|py| (depth, Element(elem)).into_py_any(py).unwrap())
})
fn __next__(&mut self) -> PyResult<Option<PyObject>> {
self.0
.next()
.map(|(depth, elem)| Python::with_gil(|py| (depth, Element(elem)).into_py_any(py)))
.transpose()
}
}

Expand All @@ -315,15 +316,17 @@ impl ElementContentIterator {
slf
}

fn __next__(&mut self) -> Option<PyObject> {
let ec = self.0.next()?;
fn __next__(&mut self) -> PyResult<Option<PyObject>> {
let ec = self.0.next();
match ec {
autosar_data_rs::ElementContent::Element(elem) => Some(Python::with_gil(|py| {
Element(elem).into_py_any(py).unwrap()
})),
autosar_data_rs::ElementContent::CharacterData(cdata) => {
Some(character_data_to_object(&cdata))
Some(autosar_data_rs::ElementContent::Element(elem)) => {
let pyobj = Python::with_gil(|py| Element(elem).into_py_any(py))?;
Ok(Some(pyobj))
}
Some(autosar_data_rs::ElementContent::CharacterData(cdata)) => {
Some(character_data_to_object(&cdata)).transpose()
}
None => Ok(None),
}
}
}
Expand All @@ -345,12 +348,14 @@ impl AttributeIterator {
slf
}

fn __next__(&mut self) -> Option<Attribute> {
let autosar_data_rs::Attribute { attrname, content } = self.0.next()?;
Some(Attribute {
fn __next__(&mut self) -> PyResult<Option<Attribute>> {
let Some(autosar_data_rs::Attribute { attrname, content }) = self.0.next() else {
return Ok(None);
};
Ok(Some(Attribute {
attrname: attrname.to_string(),
content: character_data_to_object(&content),
})
content: character_data_to_object(&content)?,
}))
}
}

Expand Down Expand Up @@ -528,21 +533,18 @@ fn extract_character_data(
})
}

fn character_data_to_object(cdata: &autosar_data_rs::CharacterData) -> PyObject {
Python::with_gil(|py| {
match cdata {
autosar_data_rs::CharacterData::Enum(enumitem) => enumitem.to_str().into_py_any(py),
autosar_data_rs::CharacterData::String(s) => {
if let Some(val) = cdata.parse_integer::<i64>() {
val.into_py_any(py)
} else {
s.into_py_any(py)
}
fn character_data_to_object(cdata: &autosar_data_rs::CharacterData) -> PyResult<PyObject> {
Python::with_gil(|py| match cdata {
autosar_data_rs::CharacterData::Enum(enumitem) => enumitem.to_str().into_py_any(py),
autosar_data_rs::CharacterData::String(s) => {
if let Some(val) = cdata.parse_integer::<i64>() {
val.into_py_any(py)
} else {
s.into_py_any(py)
}
autosar_data_rs::CharacterData::UnsignedInteger(val) => val.into_py_any(py),
autosar_data_rs::CharacterData::Float(val) => val.into_py_any(py),
}
.unwrap()
autosar_data_rs::CharacterData::UnsignedInteger(val) => val.into_py_any(py),
autosar_data_rs::CharacterData::Float(val) => val.into_py_any(py),
})
}

Expand Down
20 changes: 9 additions & 11 deletions src/specification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ impl ElementType {
}

#[getter]
fn chardata_spec(&self) -> Option<PyObject> {
self.0.chardata_spec().map(character_data_spec_to_object)
fn chardata_spec(&self) -> PyResult<Option<PyObject>> {
self.0
.chardata_spec()
.map(character_data_spec_to_object)
.transpose()
}

#[getter]
Expand Down Expand Up @@ -104,7 +107,7 @@ impl AttributeSpec {
}

#[getter]
fn value_spec(&self) -> PyObject {
fn value_spec(&self) -> PyResult<PyObject> {
character_data_spec_to_object(self.value_spec)
}
}
Expand Down Expand Up @@ -164,15 +167,14 @@ impl CharacterDataTypeUnsignedInt {
}
}

fn character_data_spec_to_object(spec: &CharacterDataSpec) -> PyObject {
fn character_data_spec_to_object(spec: &CharacterDataSpec) -> PyResult<PyObject> {
Python::with_gil(|py| match spec {
CharacterDataSpec::Enum { items } => {
//
CharacterDataTypeEnum {
values: items.iter().map(|(item, _)| item.to_string()).collect(),
}
.into_py_any(py)
.unwrap()
}
CharacterDataSpec::Pattern {
regex, max_length, ..
Expand All @@ -183,7 +185,6 @@ fn character_data_spec_to_object(spec: &CharacterDataSpec) -> PyObject {
max_length: *max_length,
}
.into_py_any(py)
.unwrap()
}
CharacterDataSpec::String {
preserve_whitespace,
Expand All @@ -195,11 +196,8 @@ fn character_data_spec_to_object(spec: &CharacterDataSpec) -> PyObject {
max_length: *max_length,
}
.into_py_any(py)
.unwrap()
}
CharacterDataSpec::UnsignedInteger => {
CharacterDataTypeUnsignedInt(()).into_py_any(py).unwrap()
}
CharacterDataSpec::Float => CharacterDataTypeFloat(()).into_py_any(py).unwrap(),
CharacterDataSpec::UnsignedInteger => CharacterDataTypeUnsignedInt(()).into_py_any(py),
CharacterDataSpec::Float => CharacterDataTypeFloat(()).into_py_any(py),
})
}

0 comments on commit 54047f8

Please sign in to comment.