Use a single validator function; update tests

This commit is contained in:
Paul van Tilburg 2022-10-01 11:48:35 +02:00
parent 8ad3d5dea7
commit 76ff1c95da
Signed by: paul
GPG Key ID: C6DE073EDA9EEC4D
1 changed files with 58 additions and 11 deletions

View File

@ -316,22 +316,17 @@ impl GeoUri {
Some(_) | None => (CoordRefSystem::default(), None), Some(_) | None => (CoordRefSystem::default(), None),
}; };
// Validate the parsed values. // Validate the geo URI before returning it.
crs.validate(latitude, longitude)?; let geo_uri = GeoUri {
// FIXME: Move this into the validator? This code is duplicate now.
if let Some(unc) = uncertainty {
if unc < 0.0 {
return Err(Error::OutOfRangeUncertainty);
}
}
Ok(GeoUri {
crs, crs,
latitude, latitude,
longitude, longitude,
altitude, altitude,
uncertainty, uncertainty,
}) };
geo_uri.validate()?;
Ok(geo_uri)
} }
/// Returns the latitude coordinate. /// Returns the latitude coordinate.
@ -400,6 +395,29 @@ impl GeoUri {
Ok(()) Ok(())
} }
/// Validates the coordinates.
///
/// This is only meant for internal use to prevent returning [`GeoUri`] objects that are
/// actually invalid.
///
/// # Errors
///
/// Returns an error if the current latitude/longitude is invalid with respect to the current
/// coordinate reference system, or if the uncertainy, if set, is not zero or positive.
fn validate(&self) -> Result<(), Error> {
// Validate the latitude/longitude against the coordinate refrence system.
self.crs.validate(self.latitude, self.longitude)?;
// Ensure that the uncertainty is not negatify, if set.
if let Some(unc) = self.uncertainty {
if unc < 0.0 {
return Err(Error::OutOfRangeUncertainty);
}
}
Ok(())
}
} }
impl fmt::Display for GeoUri { impl fmt::Display for GeoUri {
@ -533,6 +551,12 @@ mod tests {
Err(GeoUriBuilderError::ValidationError(_)) Err(GeoUriBuilderError::ValidationError(_))
)); ));
builder.longitude(5.134).uncertainty(-200.0);
assert!(matches!(
builder.build(),
Err(GeoUriBuilderError::ValidationError(_))
));
Ok(()) Ok(())
} }
@ -645,6 +669,29 @@ mod tests {
Ok(()) Ok(())
} }
#[test]
fn geo_uri_validate() {
let mut geo_uri = GeoUri {
crs: CoordRefSystem::Wgs84,
latitude: 52.107,
longitude: 5.134,
altitude: None,
uncertainty: None,
};
assert_eq!(geo_uri.validate(), Ok(()));
geo_uri.latitude = 100.0;
assert_eq!(geo_uri.validate(), Err(Error::OutOfRangeLatitude));
geo_uri.latitude = 52.107;
geo_uri.longitude = -200.0;
assert_eq!(geo_uri.validate(), Err(Error::OutOfRangeLongitude));
geo_uri.longitude = 5.134;
geo_uri.uncertainty = Some(-2000.0);
assert_eq!(geo_uri.validate(), Err(Error::OutOfRangeUncertainty));
}
#[test] #[test]
fn geo_uri_get_set() { fn geo_uri_get_set() {
let mut geo_uri = GeoUri { let mut geo_uri = GeoUri {