Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix limits #47

Merged
merged 1 commit into from
May 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ where
let mut previous = Coord { x: 0, y: 0 };

for (i, next) in coordinates.into_iter().enumerate() {
if !(MIN_LATITUDE..MAX_LATITUDE).contains(&next.y) {
if !(MIN_LATITUDE..=MAX_LATITUDE).contains(&next.y) {
return Err(format!(
"Invalid latitude: {lat} at position {i}",
lat = next.y
));
}
if !(MIN_LONGITUDE..MAX_LONGITUDE).contains(&next.x) {
if !(MIN_LONGITUDE..=MAX_LONGITUDE).contains(&next.x) {
return Err(format!(
"Invalid longitude: {lon} at position {i}",
lon = next.x
Expand Down Expand Up @@ -122,7 +122,7 @@ pub fn decode_polyline(polyline: &str, precision: u32) -> Result<LineString<f64>
let latitude_change = decode_next(&mut chars)?;
scaled_lat += latitude_change;
let lat = scaled_lat as f64 / factor as f64;
if !(MIN_LATITUDE..MAX_LATITUDE).contains(&lat) {
if !(MIN_LATITUDE..=MAX_LATITUDE).contains(&lat) {
return Err(format!("Invalid latitude: {lat} at index: {lat_start}"));
}

Expand All @@ -134,7 +134,7 @@ pub fn decode_polyline(polyline: &str, precision: u32) -> Result<LineString<f64>
let longitude_change = decode_next(&mut chars)?;
scaled_lon += longitude_change;
let lon = scaled_lon as f64 / factor as f64;
if !(MIN_LONGITUDE..MAX_LONGITUDE).contains(&lon) {
if !(MIN_LONGITUDE..=MAX_LONGITUDE).contains(&lon) {
return Err(format!("Invalid longitude: {lon} at index: {lon_start}"));
}

Expand Down Expand Up @@ -279,4 +279,30 @@ mod tests {
)
.unwrap();
}

#[test]
fn limits() {
let res: LineString<f64> = vec![[-180.0, -90.0], [180.0, 90.0], [0.0, 0.0]].into();
let polyline = "~fdtjD~niivI_oiivI__tsmT~fdtjD~niivI";
assert_eq!(
encode_coordinates(res.coords().copied(), 6).unwrap(),
polyline
);
assert_eq!(decode_polyline(polyline, 6).unwrap(), res);
}

#[test]
fn truncated() {
let input = LineString::from(vec![[2.0, 1.0], [4.0, 3.0]]);
let polyline = "_ibE_seK_seK_seK";
assert_eq!(
encode_coordinates(input.coords().copied(), 5).unwrap(),
polyline
);
assert_eq!(decode_polyline(polyline, 5).unwrap(), input);

let truncated_polyline = "_ibE_seK_seK";
let err = decode_polyline(truncated_polyline, 5).unwrap_err();
assert_eq!(err, "No longitude to go with latitude at index: 8");
}
}