|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+use coord::TileCoord;
|
|
|
2
|
+use regex::Regex;
|
|
|
3
|
+
|
|
|
4
|
+
|
|
|
5
|
+/// Kinds of placeholders for a `UrlTemplate`
|
|
|
6
|
+#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
|
|
|
7
|
+enum Placeholder {
|
|
|
8
|
+ /// Tile x coordinate
|
|
|
9
|
+ X,
|
|
|
10
|
+ /// Tile y coordinate
|
|
|
11
|
+ Y,
|
|
|
12
|
+ /// Tile zoom
|
|
|
13
|
+ Z,
|
|
|
14
|
+ /// Quadkey encoded coord
|
|
|
15
|
+ Quadkey
|
|
|
16
|
+}
|
|
|
17
|
+
|
|
|
18
|
+impl Placeholder {
|
|
|
19
|
+ /// Returns maximum number of bytes that the value for a placeholder with occupy.
|
|
|
20
|
+ fn max_size(&self) -> usize {
|
|
|
21
|
+ match *self {
|
|
|
22
|
+ Placeholder::X | Placeholder::Y | Placeholder::Z => 11,
|
|
|
23
|
+ Placeholder::Quadkey => 30,
|
|
|
24
|
+ }
|
|
|
25
|
+ }
|
|
|
26
|
+}
|
|
|
27
|
+
|
|
|
28
|
+#[derive(Debug)]
|
|
|
29
|
+pub struct UrlTemplate {
|
|
|
30
|
+ /// The template string that includes placeholders between static parts
|
|
|
31
|
+ template_string: String,
|
|
|
32
|
+ /// Ranges into `template_string` for static parts
|
|
|
33
|
+ static_parts: Vec<::std::ops::Range<usize>>,
|
|
|
34
|
+ /// Kinds of placeholders between the static parts
|
|
|
35
|
+ placeholders: Vec<Placeholder>,
|
|
|
36
|
+ /// Maximum length in bytes of a filled template
|
|
|
37
|
+ max_size: usize,
|
|
|
38
|
+}
|
|
|
39
|
+
|
|
|
40
|
+impl UrlTemplate {
|
|
|
41
|
+ pub fn new<S: Into<String>>(template_str: S) -> Result<UrlTemplate, String> {
|
|
|
42
|
+ let template_string = template_str.into();
|
|
|
43
|
+ let mut static_parts = vec![];
|
|
|
44
|
+ let mut placeholders = vec![];
|
|
|
45
|
+ let mut max_size = 0;
|
|
|
46
|
+
|
|
|
47
|
+ lazy_static! {
|
|
|
48
|
+ static ref RE: Regex = Regex::new(r"\{([a-z]+)\}").unwrap();
|
|
|
49
|
+ }
|
|
|
50
|
+
|
|
|
51
|
+ let mut offset = 0;
|
|
|
52
|
+ for cap in RE.captures_iter(&template_string) {
|
|
|
53
|
+ let cap0 = cap.get(0).unwrap();
|
|
|
54
|
+ static_parts.push(offset..cap0.start());
|
|
|
55
|
+ max_size += cap0.start() - offset;
|
|
|
56
|
+
|
|
|
57
|
+ {
|
|
|
58
|
+ let ph = match cap.get(1).unwrap().as_str() {
|
|
|
59
|
+ "x" => Placeholder::X,
|
|
|
60
|
+ "y" => Placeholder::Y,
|
|
|
61
|
+ "z" => Placeholder::Z,
|
|
|
62
|
+ "quadkey" => Placeholder::Quadkey,
|
|
|
63
|
+ s => return Err(format!("Invalid placeholder in url template: {:?}", s)),
|
|
|
64
|
+ };
|
|
|
65
|
+ max_size += ph.max_size();
|
|
|
66
|
+ placeholders.push(ph);
|
|
|
67
|
+ }
|
|
|
68
|
+
|
|
|
69
|
+ offset = cap0.end();
|
|
|
70
|
+ }
|
|
|
71
|
+
|
|
|
72
|
+ static_parts.push(offset..template_string.len());
|
|
|
73
|
+ max_size += template_string.len() - offset;
|
|
|
74
|
+
|
|
|
75
|
+ let template_valid =
|
|
|
76
|
+ placeholders.contains(&Placeholder::Quadkey) ||
|
|
|
77
|
+ (placeholders.contains(&Placeholder::X) &&
|
|
|
78
|
+ placeholders.contains(&Placeholder::Y) &&
|
|
|
79
|
+ placeholders.contains(&Placeholder::Z));
|
|
|
80
|
+
|
|
|
81
|
+ if !template_valid {
|
|
|
82
|
+ return Err(format!(
|
|
|
83
|
+ "template is not valid because one or multiple placeholders are missing: {:?}",
|
|
|
84
|
+ template_string)
|
|
|
85
|
+ );
|
|
|
86
|
+ }
|
|
|
87
|
+
|
|
|
88
|
+ Ok(UrlTemplate {
|
|
|
89
|
+ template_string,
|
|
|
90
|
+ static_parts,
|
|
|
91
|
+ placeholders,
|
|
|
92
|
+ max_size,
|
|
|
93
|
+ })
|
|
|
94
|
+ }
|
|
|
95
|
+
|
|
|
96
|
+ pub fn fill(&self, tile_coord: TileCoord) -> Option<String> {
|
|
|
97
|
+ let mut ret = String::with_capacity(self.max_size);
|
|
|
98
|
+
|
|
|
99
|
+ if let Some(prefix) = self.static_parts.first() {
|
|
|
100
|
+ ret += &self.template_string[prefix.start..prefix.end];
|
|
|
101
|
+ }
|
|
|
102
|
+
|
|
|
103
|
+ for (i, static_part) in self.static_parts.iter().skip(1).enumerate() {
|
|
|
104
|
+ let dyn_part = match self.placeholders[i] {
|
|
|
105
|
+ Placeholder::X => tile_coord.x.to_string(),
|
|
|
106
|
+ Placeholder::Y => tile_coord.y.to_string(),
|
|
|
107
|
+ Placeholder::Z => tile_coord.zoom.to_string(),
|
|
|
108
|
+ Placeholder::Quadkey => {
|
|
|
109
|
+ match tile_coord.to_quadkey() {
|
|
|
110
|
+ Some(q) => q,
|
|
|
111
|
+ None => return None,
|
|
|
112
|
+ }
|
|
|
113
|
+ }
|
|
|
114
|
+ };
|
|
|
115
|
+ ret += &dyn_part;
|
|
|
116
|
+ ret += &self.template_string[static_part.start..static_part.end];;
|
|
|
117
|
+ }
|
|
|
118
|
+ Some(ret)
|
|
|
119
|
+ }
|
|
|
120
|
+}
|
|
|
121
|
+
|
|
|
122
|
+#[cfg(test)]
|
|
|
123
|
+mod tests {
|
|
|
124
|
+ use url_template::*;
|
|
|
125
|
+
|
|
|
126
|
+ fn check_templ(templ_str: &str, coord: TileCoord, result: &str) {
|
|
|
127
|
+ let t = UrlTemplate::new(templ_str).unwrap();
|
|
|
128
|
+ assert_eq!(t.fill(coord), Some(result.to_string()));
|
|
|
129
|
+ }
|
|
|
130
|
+
|
|
|
131
|
+ #[test]
|
|
|
132
|
+ fn check_new() {
|
|
|
133
|
+ assert!(UrlTemplate::new("").is_err());
|
|
|
134
|
+ assert!(UrlTemplate::new("abc").is_err());
|
|
|
135
|
+ assert!(UrlTemplate::new("{x}").is_err());
|
|
|
136
|
+ assert!(UrlTemplate::new("{z}{y}").is_err());
|
|
|
137
|
+ assert!(UrlTemplate::new("{x}{z}{y}").is_ok());
|
|
|
138
|
+ assert!(UrlTemplate::new("{quadkey}").is_ok());
|
|
|
139
|
+ assert!(UrlTemplate::new("{x}{quadkey}").is_ok());
|
|
|
140
|
+ }
|
|
|
141
|
+
|
|
|
142
|
+ #[test]
|
|
|
143
|
+ fn check_fill() {
|
|
|
144
|
+ check_templ("https://tiles.example.com/{z}/{x}/{y}.png",
|
|
|
145
|
+ TileCoord::new(2, 1, 0),
|
|
|
146
|
+ "https://tiles.example.com/2/1/0.png");
|
|
|
147
|
+ check_templ("{z}{x}{y}",
|
|
|
148
|
+ TileCoord::new(2, 1, 0),
|
|
|
149
|
+ "210");
|
|
|
150
|
+ check_templ("{quadkey}",
|
|
|
151
|
+ TileCoord::new(3, 1, 0),
|
|
|
152
|
+ "001");
|
|
|
153
|
+ check_templ("{x}{x}{y}{z}",
|
|
|
154
|
+ TileCoord::new(2, 1, 0),
|
|
|
155
|
+ "1102");
|
|
|
156
|
+ check_templ("a{quadkey}b{z}c",
|
|
|
157
|
+ TileCoord::new(1, 0, 0),
|
|
|
158
|
+ "a0b1c");
|
|
|
159
|
+ }
|
|
|
160
|
+}
|