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

Improve structured errors #50

Merged
merged 2 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
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
22 changes: 18 additions & 4 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Errors that can occur during encoding / decoding of Polylines

use geo_types::Coord;

#[derive(Debug, PartialEq)]
#[non_exhaustive]
pub enum PolylineError {
Expand All @@ -23,26 +25,38 @@ pub enum PolylineError {
/// The string index of the character that caused the decoding error
idx: usize,
},
DecodeCharError,
EncodeToCharError,
CoordEncodingError {
coord: Coord<f64>,
/// The array index of the coordinate error
idx: usize,
},
}

impl std::error::Error for PolylineError {}
impl std::fmt::Display for PolylineError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
PolylineError::LongitudeCoordError { coord, idx } => {
write!(f, "invalid longitude: {} at position {}", coord, idx)
write!(f, "longitude out of bounds: {} at position {}", coord, idx)
}
PolylineError::LatitudeCoordError { coord, idx } => {
write!(f, "invalid latitude: {} at position {}", coord, idx)
write!(f, "latitude out of bounds: {} at position {}", coord, idx)
}
PolylineError::DecodeError { idx } => {
write!(f, "cannot decode character at index {}", idx)
}
PolylineError::NoLongError { idx } => {
write!(f, "no longitude to go with latitude at index: {}", idx)
}
PolylineError::DecodeCharError => write!(f, "couldn't decode character"),
PolylineError::EncodeToCharError => write!(f, "couldn't encode character"),
PolylineError::CoordEncodingError { coord, idx } => {
write!(
f,
"the coordinate {:?} at index: {} could not be encoded",
coord, idx
)
}
}
}
}
18 changes: 14 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ fn encode(delta: i64, output: &mut String) -> Result<(), PolylineError> {
}
while value >= 0x20 {
let from_char = char::from_u32(((0x20 | (value & 0x1f)) + 63) as u32)
.ok_or(PolylineError::DecodeCharError)?;
.ok_or(PolylineError::EncodeToCharError)?;
output.push(from_char);
value >>= 5;
}
let from_char = char::from_u32((value + 63) as u32).ok_or(PolylineError::DecodeCharError)?;
let from_char = char::from_u32((value + 63) as u32).ok_or(PolylineError::EncodeToCharError)?;
output.push(from_char);
Ok(())
}
Expand Down Expand Up @@ -95,8 +95,18 @@ where
x: scale(next.x, factor),
y: scale(next.y, factor),
};
encode(scaled_next.y - previous.y, &mut output)?;
encode(scaled_next.x - previous.x, &mut output)?;
encode(scaled_next.y - previous.y, &mut output).map_err(|_| {
PolylineError::CoordEncodingError {
coord: next,
idx: i,
}
})?;
encode(scaled_next.x - previous.x, &mut output).map_err(|_| {
PolylineError::CoordEncodingError {
coord: next,
idx: i,
}
})?;
previous = scaled_next;
}
Ok(output)
Expand Down