1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#[macro_use]
extern crate serde_derive;

extern crate serde;
extern crate serde_json;

#[derive(Serialize, Deserialize, Debug)]
pub struct TileJSON {
  tilejson: &'static str,
  name: Option<String>,
  description: Option<String>,
  version: Option<String>,
  attribution: Option<String>,
  template: Option<String>,
  legend: Option<String>,
  scheme: Option<String>,
  tiles: Vec<String>,
  grids: Option<Vec<String>>,
  data: Option<Vec<String>>,
  minzoom: Option<u8>,
  maxzoom: Option<u8>,
  bounds: Option<Vec<i32>>,
  center: Option<Vec<i32>>
}

#[derive(Serialize, Deserialize, Debug)]
pub struct TileJSONBuilder {
  tilejson: &'static str,
  name: Option<String>,
  description: Option<String>,
  version: Option<String>,
  attribution: Option<String>,
  template: Option<String>,
  legend: Option<String>,
  scheme: Option<String>,
  tiles: Vec<String>,
  grids: Option<Vec<String>>,
  data: Option<Vec<String>>,
  minzoom: Option<u8>,
  maxzoom: Option<u8>,
  bounds: Option<Vec<i32>>,
  center: Option<Vec<i32>>
}

impl TileJSONBuilder {
  pub fn new() -> TileJSONBuilder {
    TileJSONBuilder {
      tilejson: "2.2.0",
      name: None,
      description: None,
      version: Some("1.0.0".to_owned()),
      attribution: None,
      template: None,
      legend: None,
      scheme: Some("xyz".to_owned()),
      tiles: Vec::new(),
      grids: None,
      data: None,
      minzoom: Some(0),
      maxzoom: Some(30),
      bounds: Some(vec![-180, -90, 180, 90]),
      center: None
    }
  }

  pub fn name(&mut self, name: &str) -> &mut TileJSONBuilder {
    self.name = Some(name.to_string());
    self
  }

  pub fn description(&mut self, description: &str) -> &mut TileJSONBuilder {
    self.description = Some(description.to_string());
    self
  }

  pub fn version(&mut self, version: &str) -> &mut TileJSONBuilder {
    self.version = Some(version.to_string());
    self
  }

  pub fn attribution(&mut self, attribution: &str) -> &mut TileJSONBuilder {
    self.attribution = Some(attribution.to_string());
    self
  }

  pub fn template(&mut self, template: &str) -> &mut TileJSONBuilder {
    self.template = Some(template.to_string());
    self
  }

  pub fn legend(&mut self, legend: &str) -> &mut TileJSONBuilder {
    self.legend = Some(legend.to_string());
    self
  }

  pub fn scheme(&mut self, scheme: &str) -> &mut TileJSONBuilder {
    self.scheme = Some(scheme.to_string());
    self
  }

  pub fn tiles(&mut self, tiles: Vec<&str>) -> &mut TileJSONBuilder {
    self.tiles = tiles.into_iter().map(|url| url.to_owned()).collect();
    self
  }

  pub fn grids(&mut self, grids: Vec<&str>) -> &mut TileJSONBuilder {
    self.grids = Some(grids.into_iter().map(|url| url.to_owned()).collect());
    self
  }

  pub fn data(&mut self, data: Vec<&str>) -> &mut TileJSONBuilder {
    self.data = Some(data.into_iter().map(|url| url.to_owned()).collect());
    self
  }

  pub fn minzoom(&mut self, minzoom: u8) -> &mut TileJSONBuilder {
    self.minzoom = Some(minzoom);
    self
  }

  pub fn maxzoom(&mut self, maxzoom: u8) -> &mut TileJSONBuilder {
    self.maxzoom = Some(maxzoom);
    self
  }

  pub fn bounds(&mut self, bounds: Vec<i32>) -> &mut TileJSONBuilder {
    self.bounds = Some(bounds);
    self
  }

  pub fn center(&mut self, center: Vec<i32>) -> &mut TileJSONBuilder {
    self.center = Some(center);
    self
  }

  pub fn finalize(self) -> TileJSON {
    TileJSON {
      tilejson: self.tilejson,
      name: self.name,
      description: self.description,
      version: self.version,
      attribution: self.attribution,
      template: self.template,
      legend: self.legend,
      scheme: self.scheme,
      tiles: self.tiles,
      grids: self.grids,
      data: self.data,
      minzoom: self.minzoom,
      maxzoom: self.maxzoom,
      bounds: self.bounds,
      center: self.center,
    }
  }
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}