diff --git a/CHANGELOG.md b/CHANGELOG.md index ebf8778..771915d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ + +### v0.3.4 (2022-11-15) +* Add proper `Error` implementation to `Bounds` and `Center` parsing errors + ### v0.3.3 (2022-11-07) * Add `Display` with precision support for `Bounds` and `Center` structs diff --git a/Cargo.toml b/Cargo.toml index dca8512..c888538 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 ", @@ -19,3 +19,4 @@ categories = ["science::geo"] serde = { version = "1", features = ["derive"] } serde_json = "1" serde_tuple = "0.5" +thiserror = "1" diff --git a/src/bounds.rs b/src/bounds.rs index c18b5d3..7086e41 100644 --- a/src/bounds.rs +++ b/src/bounds.rs @@ -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 { @@ -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::()` - 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 { @@ -333,8 +326,7 @@ impl FromStr for Bounds { .next() .ok_or(ParseBoundsError::BadLen)? .trim() - .parse() - .map_err(ParseBoundsError::ParseCoordError)?; + .parse()?; } values .next() @@ -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() { diff --git a/src/center.rs b/src/center.rs index 09f8020..223a5a4 100644 --- a/src/center.rs +++ b/src/center.rs @@ -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 { @@ -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::()` - ParseCoordError(ParseFloatError), + #[error(transparent)] + ParseCoordError(#[from] ParseFloatError), /// Wrapped error from the `parse::()` - 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 { @@ -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),