Skip to content

Commit c7733fe

Browse files
committed
Return 'Result' for 'SpatialRef::get_axis_...' methods
1 parent 379b5bc commit c7733fe

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed

src/errors.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,9 @@ pub enum GdalError {
5757
to: String,
5858
msg: Option<String>,
5959
},
60+
#[error("Axis not found for key '{key}' in method '{method_name}'")]
61+
AxisNotFoundError {
62+
key: String,
63+
method_name: &'static str,
64+
},
6065
}

src/spatial_ref/srs.rs

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -385,32 +385,44 @@ impl SpatialRef {
385385
unsafe { gdal_sys::OSRIsVertical(self.0) == 1 }
386386
}
387387

388-
pub fn get_axis_orientation(&self, target_key: &str, axis: i32) -> AxisOrientationType {
389-
// We can almost safely assume that if we fail to build a CString then the input
390-
// is not a valide key.
388+
pub fn get_axis_orientation(&self, target_key: &str, axis: i32) -> Result<AxisOrientationType> {
391389
let mut orientation = gdal_sys::OGRAxisOrientation::OAO_Other;
392-
if let Ok(c_str) = CString::new(target_key) {
393-
unsafe {
394-
gdal_sys::OSRGetAxis(self.0, c_str.as_ptr(), axis as c_int, &mut orientation)
395-
};
390+
let c_ptr = unsafe {
391+
gdal_sys::OSRGetAxis(
392+
self.0,
393+
CString::new(target_key)?.as_ptr(),
394+
axis as c_int,
395+
&mut orientation,
396+
)
397+
};
398+
// null ptr indicate a failure (but no CPLError) see Gdal documentation.
399+
if c_ptr.is_null() {
400+
Err(GdalError::AxisNotFoundError {
401+
key: target_key.into(),
402+
method_name: "OSRGetAxis",
403+
})
404+
} else {
405+
Ok(orientation)
396406
}
397-
orientation
398407
}
399408

400-
pub fn get_axis_name(&self, target_key: &str, axis: i32) -> Option<String> {
409+
pub fn get_axis_name(&self, target_key: &str, axis: i32) -> Result<String> {
401410
// See get_axis_orientation
402-
if let Ok(c_str) = CString::new(target_key) {
403-
let c_ptr = unsafe {
404-
gdal_sys::OSRGetAxis(self.0, c_str.as_ptr(), axis as c_int, ptr::null_mut())
405-
};
406-
// null ptr indicate a failure (but no CPLError) see Gdal documentation.
407-
if c_ptr.is_null() {
408-
None
409-
} else {
410-
Some(_string(c_ptr))
411-
}
411+
let c_ptr = unsafe {
412+
gdal_sys::OSRGetAxis(
413+
self.0,
414+
CString::new(target_key)?.as_ptr(),
415+
axis as c_int,
416+
ptr::null_mut(),
417+
)
418+
};
419+
if c_ptr.is_null() {
420+
Err(GdalError::AxisNotFoundError {
421+
key: target_key.into(),
422+
method_name: "OSRGetAxis",
423+
})
412424
} else {
413-
None
425+
Ok(_string(c_ptr))
414426
}
415427
}
416428

src/spatial_ref/tests.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,9 @@ fn crs_axis() {
297297
#[cfg(all(major_ge_3, minor_ge_1))]
298298
assert_eq!(spatial_ref.get_axes_count(), 2);
299299

300-
let orientation = spatial_ref.get_axis_orientation("GEOGCS", 0);
300+
let orientation = spatial_ref.get_axis_orientation("GEOGCS", 0).unwrap();
301301
assert_eq!(orientation, gdal_sys::OGRAxisOrientation::OAO_North);
302-
303-
assert!(spatial_ref.get_axis_name("GEOGCS", 0).is_some());
304-
assert!(spatial_ref.get_axis_name("DO_NO_EXISTS", 0).is_none());
305-
306-
let orientation = spatial_ref.get_axis_orientation("DO_NO_EXISTS", 0);
307-
assert_eq!(orientation, gdal_sys::OGRAxisOrientation::OAO_Other);
302+
assert!(spatial_ref.get_axis_name("GEOGCS", 0).is_ok());
303+
assert!(spatial_ref.get_axis_name("DO_NO_EXISTS", 0).is_err());
304+
assert!(spatial_ref.get_axis_orientation("DO_NO_EXISTS", 0).is_err());
308305
}

0 commit comments

Comments
 (0)