diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..6bcb5da --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,9 @@ +# 0.1.3 + +- Update dependency versions +- Correct definition of the error struct (add `impl Error`) +- Add unit tests for the error struct + +# 0.1.2 + +Initial release diff --git a/Cargo.toml b/Cargo.toml index 788092f..0e2e21b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "latlon" -version = "0.1.2" +version = "0.1.3" authors = ["Ondřej Hruška "] description = "Parse latitude/longitude from many common formats" edition = "2018" @@ -11,6 +11,6 @@ categories = ["parser-implementations", "science"] license = "MIT" [dependencies] -geo-types = "0.5.0" -regex = "1.3.7" -lazy_static = "1.4.0" +geo-types = "0.7" +regex = "1" +lazy_static = "1.4" diff --git a/src/errors.rs b/src/errors.rs index 241ad92..e7ae664 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,5 +1,5 @@ use std::fmt; -use std::fmt::{Display, Formatter}; +use std::fmt::{Display, Formatter, Debug}; use std::num::ParseFloatError; #[derive(Debug)] @@ -17,11 +17,20 @@ impl Display for ParseErrorInternal { } } -#[derive(Debug)] -pub struct GeoParseError + Display>(pub T); +pub struct GeoParseError>(pub T); + +impl> Display for GeoParseError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "Error parsing coordinates from {}", self.0.as_ref()) + } +} -impl + Display> Display for GeoParseError { +impl> Debug for GeoParseError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "Error parsing coordinates from {}", self.0) + f.debug_tuple("GeoParseError") + .field(&self.0.as_ref()) + .finish() } } + +impl> std::error::Error for GeoParseError {} diff --git a/src/tests.rs b/src/tests.rs index c17ee35..3f52f28 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,4 +1,4 @@ -use crate::{parse, parse_lat, parse_lng}; +use crate::{parse, parse_lat, parse_lng, GeoParseError}; use geo_types::Point; #[test] @@ -472,3 +472,18 @@ fn d() { parse_lng(r#"-180°"#).unwrap(); parse_lng(r#"1°"#).unwrap(); } + +#[test] +fn error() { + let e = GeoParseError("Hello"); + assert_eq!("GeoParseError(\"Hello\")", format!("{:?}", e)); + assert_eq!("GeoParseError(\n \"Hello\",\n)", format!("{:#?}", e)); + + let e = GeoParseError("Hello2".to_string()); + assert_eq!("GeoParseError(\"Hello2\")", format!("{:?}", e)); + assert_eq!("GeoParseError(\n \"Hello2\",\n)", format!("{:#?}", e)); + + let e = GeoParseError(std::borrow::Cow::Owned("Foo".to_string())); + assert_eq!("GeoParseError(\"Foo\")", format!("{:?}", e)); + assert_eq!("GeoParseError(\n \"Foo\",\n)", format!("{:#?}", e)); +}