Skip to content

Commit

Permalink
Add Display for Bounds and Center (#22)
Browse files Browse the repository at this point in the history
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.
---
  • Loading branch information
nyurik authored Nov 7, 2023
1 parent 6621134 commit ff0dd67
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 10 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.3"></a>
### v0.3.3 (2022-11-07)
* Add `Display` with precision support for `Bounds` and `Center` structs

<a name="v0.3.2"></a>
### v0.3.2 (2022-10-30)
* Add `Bounds::from` for `[f64; 4]`, `[f32; 4]`, `[i32; 4]`
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
Expand Down
31 changes: 28 additions & 3 deletions src/bounds.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;

Expand Down
26 changes: 24 additions & 2 deletions src/center.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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(&center.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
Expand Down
8 changes: 5 additions & 3 deletions src/tilejson.rs
Original file line number Diff line number Diff line change
@@ -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
/// <https://github.com/mapbox/tilejson-spec> (version 3.0.0)
Expand Down
3 changes: 2 additions & 1 deletion src/vector_layer.rs
Original file line number Diff line number Diff line change
@@ -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.
///
Expand Down

0 comments on commit ff0dd67

Please sign in to comment.