Skip to content

Commit

Permalink
Fix error implementation
Browse files Browse the repository at this point in the history
Use `thiserror` to correctly implement error handling.
* Implementats the missing `impl Error`
* Adds `From` for error conversion
  • Loading branch information
nyurik committed Nov 16, 2023
1 parent ce2bd3a commit 5fd5956
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 42 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<a name="v0.3.4"></a>
### v0.3.4 (2022-11-15)
* Add proper `Error` implementation to `Bounds` and `Center` parsing errors

<a name="v0.3.3"></a>
### v0.3.3 (2022-11-07)
* Add `Display` with precision support for `Bounds` and `Center` structs
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tilejson"
version = "0.3.3"
version = "0.3.4"
description = "Library for serializing the TileJSON file format"
authors = [
"Stepan Kuzmin <[email protected]>",
Expand All @@ -19,3 +19,4 @@ categories = ["science::geo"]
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_tuple = "0.5"
thiserror = "1"
24 changes: 8 additions & 16 deletions src/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use std::ops::{Add, AddAssign};
use std::str::FromStr;

use serde_tuple::{Deserialize_tuple, Serialize_tuple};
use thiserror::Error;

use crate::ParseBoundsError::{BadLen, ParseCoordError};
use crate::ParseBoundsError::BadLen;

#[derive(Serialize_tuple, Deserialize_tuple, PartialEq, Debug, Copy, Clone)]
pub struct Bounds {
Expand Down Expand Up @@ -163,21 +164,13 @@ impl AddAssign for Bounds {
}
}

#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Error, Debug, PartialEq, Eq, Clone)]
pub enum ParseBoundsError {
/// Incorrect number of values
#[error("Incorrect number of values. Bounds expects four f64 values.")]
BadLen,
/// Wrapped error from the `parse::<f64>()`
ParseCoordError(ParseFloatError),
}

impl Display for ParseBoundsError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
BadLen => f.write_str("Incorrect number of values. Bounds expects four f64 values."),
ParseCoordError(e) => e.fmt(f),
}
}
#[error(transparent)]
ParseCoordError(#[from] ParseFloatError),
}

impl From<[f64; 4]> for Bounds {
Expand Down Expand Up @@ -333,8 +326,7 @@ impl FromStr for Bounds {
.next()
.ok_or(ParseBoundsError::BadLen)?
.trim()
.parse()
.map_err(ParseBoundsError::ParseCoordError)?;
.parse()?;
}
values
.next()
Expand All @@ -345,7 +337,7 @@ impl FromStr for Bounds {
#[cfg(test)]
mod tests {
use super::*;
use crate::ParseBoundsError::BadLen;
use crate::ParseBoundsError::{BadLen, ParseCoordError};

#[test]
fn test_parse_err() {
Expand Down
35 changes: 10 additions & 25 deletions src/center.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::num::{ParseFloatError, ParseIntError};
use std::str::FromStr;

use serde_tuple::{Deserialize_tuple, Serialize_tuple};
use thiserror::Error;

#[derive(Serialize_tuple, Deserialize_tuple, PartialEq, Debug, Default, Copy, Clone)]
pub struct Center {
Expand Down Expand Up @@ -42,26 +43,16 @@ impl Display for Center {
}
}

#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Error, Debug, PartialEq, Eq, Clone)]
pub enum ParseCenterError {
/// Incorrect number of values
#[error("Incorrect number of values. Center expects two f64 and one u8 values.")]
BadLen,
/// Wrapped error from the `parse::<f64>()`
ParseCoordError(ParseFloatError),
#[error(transparent)]
ParseCoordError(#[from] ParseFloatError),
/// Wrapped error from the `parse::<u8>()`
ParseZoomError(ParseIntError),
}

impl Display for ParseCenterError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ParseCenterError::BadLen => {
f.write_str("Incorrect number of values. Center expects two f64 and one u8 values.")
}
ParseCenterError::ParseCoordError(e) => e.fmt(f),
ParseCenterError::ParseZoomError(e) => e.fmt(f),
}
}
#[error(transparent)]
ParseZoomError(#[from] ParseIntError),
}

impl FromStr for Center {
Expand All @@ -80,15 +71,9 @@ impl FromStr for Center {
let mut vals = s.split(',').map(|s| s.trim());
let mut next_val = || vals.next().ok_or(ParseCenterError::BadLen);
let center = Self {
longitude: next_val()?
.parse()
.map_err(ParseCenterError::ParseCoordError)?,
latitude: next_val()?
.parse()
.map_err(ParseCenterError::ParseCoordError)?,
zoom: next_val()?
.parse()
.map_err(ParseCenterError::ParseZoomError)?,
longitude: next_val()?.parse()?,
latitude: next_val()?.parse()?,
zoom: next_val()?.parse()?,
};
match vals.next() {
Some(_) => Err(ParseCenterError::BadLen),
Expand Down

0 comments on commit 5fd5956

Please sign in to comment.