From ff0dd676b17ae27648a21608292066533c7ab260 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Tue, 7 Nov 2023 14:32:52 -0500 Subject: [PATCH] Add `Display` for `Bounds` and `Center` (#22) Additionally, ran new fmt to keep things a bit cleaner: ``` cargo +nightly fmt -- --config imports_granularity=Module,group_imports=StdExternalCrate ``` - [x] I agree to follow the project's [code of conduct](https://github.com/georust/geo/blob/master/CODE_OF_CONDUCT.md). - [x] I added an entry to `CHANGES.md` if knowledge of this change could be valuable to users. --- --- CHANGELOG.md | 4 ++++ Cargo.toml | 2 +- src/bounds.rs | 31 ++++++++++++++++++++++++++++--- src/center.rs | 26 ++++++++++++++++++++++++-- src/tilejson.rs | 8 +++++--- src/vector_layer.rs | 3 ++- 6 files changed, 64 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2cab45e..ebf8778 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ + +### v0.3.3 (2022-11-07) +* Add `Display` with precision support for `Bounds` and `Center` structs + ### v0.3.2 (2022-10-30) * Add `Bounds::from` for `[f64; 4]`, `[f32; 4]`, `[i32; 4]` diff --git a/Cargo.toml b/Cargo.toml index 7565541..1416e05 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tilejson" -version = "0.3.2" +version = "0.3.3" description = "Library for serializing the TileJSON file format" authors = [ "Stepan Kuzmin ", diff --git a/src/bounds.rs b/src/bounds.rs index fa7ea53..c18b5d3 100644 --- a/src/bounds.rs +++ b/src/bounds.rs @@ -1,10 +1,12 @@ -use crate::ParseBoundsError::{BadLen, ParseCoordError}; -use serde_tuple::{Deserialize_tuple, Serialize_tuple}; -use std::fmt::{Display, Formatter}; +use std::fmt::{Display, Formatter, Write as _}; use std::num::ParseFloatError; use std::ops::{Add, AddAssign}; use std::str::FromStr; +use serde_tuple::{Deserialize_tuple, Serialize_tuple}; + +use crate::ParseBoundsError::{BadLen, ParseCoordError}; + #[derive(Serialize_tuple, Deserialize_tuple, PartialEq, Debug, Copy, Clone)] pub struct Bounds { pub left: f64, @@ -75,6 +77,29 @@ impl Default for Bounds { } } +impl Display for Bounds { + /// Format Bounds struct as a comma-separated string. + /// Accepts all precision formatting parameters as for floats. + /// + /// ``` + /// # use tilejson::Bounds; + /// # use std::str::FromStr; + /// let bounds = Bounds::new(-1.5, -2.5, 3.5, 4.5); + /// assert_eq!(bounds.to_string(), "-1.5,-2.5,3.5,4.5"); + /// assert_eq!(format!("{:.2}", bounds), "-1.50,-2.50,3.50,4.50"); + /// assert_eq!(Bounds::from_str(&bounds.to_string()).unwrap(), bounds); + /// ``` + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + self.left.fmt(f)?; + f.write_char(',')?; + self.bottom.fmt(f)?; + f.write_char(',')?; + self.right.fmt(f)?; + f.write_char(',')?; + self.top.fmt(f) + } +} + impl Add for Bounds { type Output = Bounds; diff --git a/src/center.rs b/src/center.rs index 15378b7..09f8020 100644 --- a/src/center.rs +++ b/src/center.rs @@ -1,8 +1,9 @@ -use serde_tuple::{Deserialize_tuple, Serialize_tuple}; -use std::fmt::{Display, Formatter}; +use std::fmt::{Display, Formatter, Write as _}; use std::num::{ParseFloatError, ParseIntError}; use std::str::FromStr; +use serde_tuple::{Deserialize_tuple, Serialize_tuple}; + #[derive(Serialize_tuple, Deserialize_tuple, PartialEq, Debug, Default, Copy, Clone)] pub struct Center { pub longitude: f64, @@ -20,6 +21,27 @@ impl Center { } } +impl Display for Center { + /// Format center struct as a comma-separated string. + /// Longitude and latitude are formatted with specified precision parameters. + /// + /// ``` + /// # use tilejson::Center; + /// # use std::str::FromStr; + /// let center = Center::new(1.5, -2.5, 8); + /// assert_eq!(center.to_string(), "1.5,-2.5,8"); + /// assert_eq!(format!("{:.2}", center), "1.50,-2.50,8"); + /// assert_eq!(Center::from_str(¢er.to_string()).unwrap(), center); + /// ``` + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + self.longitude.fmt(f)?; + f.write_char(',')?; + self.latitude.fmt(f)?; + f.write_char(',')?; + write!(f, "{}", self.zoom) + } +} + #[derive(Debug, PartialEq, Eq, Clone)] pub enum ParseCenterError { /// Incorrect number of values diff --git a/src/tilejson.rs b/src/tilejson.rs index c751fa5..72f8d53 100644 --- a/src/tilejson.rs +++ b/src/tilejson.rs @@ -1,9 +1,11 @@ +use std::collections::HashMap; + +use serde::{Deserialize, Serialize}; +use serde_json::Value; + use crate::bounds::Bounds; use crate::center::Center; use crate::vector_layer::VectorLayer; -use serde::{Deserialize, Serialize}; -use serde_json::Value; -use std::collections::HashMap; /// TileJSON struct represents tilejson-spec metadata as specified by /// (version 3.0.0) diff --git a/src/vector_layer.rs b/src/vector_layer.rs index 150395b..c3be70a 100644 --- a/src/vector_layer.rs +++ b/src/vector_layer.rs @@ -1,6 +1,7 @@ +use std::collections::HashMap; + use serde::{Deserialize, Serialize}; use serde_json::Value; -use std::collections::HashMap; /// Each object describes one layer of vector tile data. ///