Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skip serializing if Option is None #6

Merged
merged 1 commit into from
Oct 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/tilejson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ pub struct TileJSON {
pub tilejson: &'static str,

/// The tileset id.
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,

/// A name describing the tileset. The name can
/// contain any legal character. Implementations SHOULD NOT interpret the
/// name as HTML.
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,

/// A text description of the tileset. The
/// description can contain any legal character. Implementations SHOULD NOT
/// interpret the description as HTML.
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,

/// A semver.org style version number. When
Expand All @@ -30,29 +33,34 @@ pub struct TileJSON {
/// level MUST only have changes to tiles that are contained within one tile.
/// When tiles change significantly, the major version MUST be increased.
/// Implementations MUST NOT use tiles with different major versions.
#[serde(skip_serializing_if = "Option::is_none")]
pub version: Option<String>,

/// Contains an attribution to be displayed
/// when the map is shown to a user. Implementations MAY decide to treat this
/// as HTML or literal text. For security reasons, make absolutely sure that
/// this field can't be abused as a vector for XSS or beacon tracking.
#[serde(skip_serializing_if = "Option::is_none")]
pub attribution: Option<String>,

/// Contains a mustache template to be used to
/// format data from grids for interaction.
/// See https://github.com/mapbox/utfgrid-spec/tree/master/1.2
/// for the interactivity specification.
#[serde(skip_serializing_if = "Option::is_none")]
pub template: Option<String>,

/// Contains a legend to be displayed with the map.
/// Implementations MAY decide to treat this as HTML or literal text.
/// For security reasons, make absolutely sure that this field can't be
/// abused as a vector for XSS or beacon tracking.
#[serde(skip_serializing_if = "Option::is_none")]
pub legend: Option<String>,

/// Either "xyz" or "tms". Influences the y
/// direction of the tile coordinates.
/// The global-mercator (aka Spherical Mercator) profile is assumed.
#[serde(skip_serializing_if = "Option::is_none")]
pub scheme: Option<String>,

/// An array of tile endpoints. {z}, {x} and {y}, if present,
Expand All @@ -69,6 +77,7 @@ pub struct TileJSON {
/// for this tileset.
/// See https://github.com/mapbox/utfgrid-spec/tree/master/1.2
/// for the interactivity specification.
#[serde(skip_serializing_if = "Option::is_none")]
pub grids: Option<Vec<String>>,

/// An array of data files in GeoJSON format.
Expand All @@ -78,18 +87,22 @@ pub struct TileJSON {
/// All endpoints MUST return the same content for the same URL.
/// If the array doesn't contain any entries, then no data is present in
/// the map.
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Vec<String>>,

/// An integer specifying the minimum zoom level.
#[serde(skip_serializing_if = "Option::is_none")]
pub minzoom: Option<u8>,

/// An integer specifying the maximum zoom level. MUST be >= minzoom.
#[serde(skip_serializing_if = "Option::is_none")]
pub maxzoom: Option<u8>,

/// The maximum extent of available map tiles. Bounds MUST define an area
/// covered by all zoom levels. The bounds are represented in WGS:84
/// latitude and longitude values, in the order left, bottom, right, top.
/// Values may be integers or floating point numbers.
#[serde(skip_serializing_if = "Option::is_none")]
pub bounds: Option<Vec<f32>>,

/// The first value is the longitude, the second is latitude (both in
Expand All @@ -99,6 +112,7 @@ pub struct TileJSON {
/// Implementations can use this value to set the default location. If the
/// value is null, implementations may use their own algorithm for
/// determining a default location.
#[serde(skip_serializing_if = "Option::is_none")]
pub center: Option<Vec<i32>>,
}

Expand Down Expand Up @@ -130,7 +144,7 @@ impl TileJSONBuilder {
name: None,
description: None,
version: Some("1.0.0".to_owned()),
attribution: Some("".to_string()),
attribution: None,
template: None,
legend: None,
scheme: Some("xyz".to_owned()),
Expand Down Expand Up @@ -299,7 +313,7 @@ mod tests {

assert_eq!(
serialized_tilejson,
r#"{"tilejson":"2.2.0","id":null,"name":"compositing","description":null,"version":"1.0.0","attribution":"","template":null,"legend":null,"scheme":"tms","tiles":["http://localhost:8888/admin/1.0.0/world-light,broadband/{z}/{x}/{y}.png"],"grids":null,"data":null,"minzoom":0,"maxzoom":30,"bounds":[-180.0,-90.0,180.0,90.0],"center":null}"#
r#"{"tilejson":"2.2.0","name":"compositing","version":"1.0.0","scheme":"tms","tiles":["http://localhost:8888/admin/1.0.0/world-light,broadband/{z}/{x}/{y}.png"],"minzoom":0,"maxzoom":30,"bounds":[-180.0,-90.0,180.0,90.0]}"#
)
}
}