Skip to content

Commit

Permalink
Improve structured errors (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
urschrei authored May 13, 2024
1 parent b3a3c56 commit 694ca02
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
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

0 comments on commit 694ca02

Please sign in to comment.