utiles-core 0.7.3

Map tile utilities aka utiles
Documentation
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//! Parsing util(e)ities
use crate::bbox::BBox;
use crate::errors::UtilesCoreResult;
use crate::UtilesCoreError;
use serde_json::Value;

/// Parse a string into a `BBox`
///
/// # Errors
///
/// Returns error if unable to parse JSON string into a `BBox`
pub fn parse_bbox_json(string: &str) -> UtilesCoreResult<BBox> {
    // strip leading/trailing  whitespace
    let s = string.trim();
    // if the first char is "{" assume it is geojson-like
    if s.starts_with('{') {
        // parse to serde_json::Value
        let v: Value = serde_json::from_str(s)?;
        // if it has a "bbox" key, use that
        if v["bbox"].is_array() {
            let bbox: (f64, f64, f64, f64) = serde_json::from_value(v["bbox"].clone())?;
            return Ok(BBox::from(bbox));
        }
        // return Ok(geojson_bounds(s));

        return Err(UtilesCoreError::InvalidBbox(
            "Invalid bbox: ".to_string() + s,
        ));
    }

    let v: Value = serde_json::from_str(s)?;

    // Assume a single pair of coordinates represents a CoordTuple
    // and a four-element array represents a BBoxTuple
    let bbox = match v.as_array().map(Vec::len) {
        // match len 0, 1, 3
        Some(0 | 1 | 3) => Err(UtilesCoreError::InvalidBbox(
            "Invalid bbox: ".to_string() + s,
        )),
        Some(2) => {
            let coord: (f64, f64) = serde_json::from_value::<(f64, f64)>(v)?;
            Ok(BBox::new(coord.0, coord.1, coord.0, coord.1))
        }
        Some(4) => {
            let bbox: (f64, f64, f64, f64) = serde_json::from_value(v)?;
            Ok(BBox::from(bbox))
        }
        _ => {
            // take first four elements
            let bbox_vec_as_arr = v.as_array();
            match bbox_vec_as_arr {
                None => Err(UtilesCoreError::InvalidBbox(
                    "Invalid bbox: ".to_string() + s,
                )),
                Some(bbox_vec) => {
                    let bbox_vec =
                        bbox_vec.iter().take(4).cloned().collect::<Vec<Value>>();
                    Ok(BBox::from(serde_json::from_value::<(f64, f64, f64, f64)>(
                        Value::Array(bbox_vec),
                    )?))
                }
            }
        }
    };
    bbox
}

/// Parse a string into a `BBox`
///
/// # Errors
///
/// Returns error on bbox parsing failure
///
/// # Examples
///
/// ```
/// use utiles_core::parsing::parse_bbox;
/// let bbox = parse_bbox("-180,-85,180,85").unwrap();
/// assert_eq!(bbox, utiles_core::bbox::BBox::new(-180.0, -85.0, 180.0, 85.0));
/// ```
///
/// ```
/// use utiles_core::parsing::parse_bbox;
/// let bbox = parse_bbox("-180.0, -85.0, 180.0, 85.0").unwrap();
/// assert_eq!(bbox, utiles_core::bbox::BBox::new(-180.0, -85.0, 180.0, 85.0));
/// ```
///
/// ```
/// use utiles_core::parsing::parse_bbox;
/// let bbox = parse_bbox("-180.0 -85.0 180.0 85.0").unwrap();
/// assert_eq!(bbox, utiles_core::bbox::BBox::new(-180.0, -85.0, 180.0, 85.0));
/// ```
///
/// ```
/// use utiles_core::parsing::parse_bbox;
/// let bbox = parse_bbox("[-180.0, -85.0, 180.0, 85.0]").unwrap();
/// assert_eq!(bbox, utiles_core::bbox::BBox::new(-180.0, -85.0, 180.0, 85.0));
/// ```
pub fn parse_bbox(string: &str) -> UtilesCoreResult<BBox> {
    // strip leading/trailing  whitespace
    let s = string.trim();
    // if the first char is "{" assume it is geojson-like
    if s.starts_with('{') || s.starts_with('[') {
        let bbox = parse_bbox_json(s);
        return bbox;
    }
    let parts: Vec<f64> = if s.contains(',') {
        s.split(',')
            .map(str::trim)
            .filter_map(|p| p.parse::<f64>().ok())
            .collect()
    } else if s.contains(' ') {
        s.split(' ')
            .map(str::trim)
            .filter_map(|p| p.parse::<f64>().ok())
            .collect()
    } else {
        vec![]
    };
    if parts.len() == 4 {
        // if north < south err out
        if parts[3] < parts[1] {
            Err(UtilesCoreError::InvalidBbox(
                "Invalid bbox: ".to_string() + s + " (north < south)",
            ))
        } else {
            Ok(BBox::new(parts[0], parts[1], parts[2], parts[3]))
        }
    } else {
        Err(UtilesCoreError::InvalidBbox(
            "Invalid bbox: ".to_string() + s,
        ))
    }
}

/// Parse a string into a `BBox` with special handling of 'world' and 'planet'
///
/// # Errors
///
/// Returns error on bbox parsing failure
///
/// # Examples
///
/// ```
/// use utiles_core::parsing::parse_bbox_ext;
/// let bbox = parse_bbox_ext("world").unwrap();
/// assert_eq!(bbox, utiles_core::bbox::BBox::new(-180.0, -90.0, 180.0, 90.0));
/// ```
///
/// ```
/// use utiles_core::parsing::parse_bbox_ext;
/// let bbox = parse_bbox_ext("planet").unwrap();
/// assert_eq!(bbox, utiles_core::bbox::BBox::new(-180.0, -90.0, 180.0, 90.0));
/// ```
///
/// ```
/// use utiles_core::parsing::parse_bbox_ext;
/// let bbox = parse_bbox_ext("-180,-85,180,85").unwrap();
/// assert_eq!(bbox, utiles_core::bbox::BBox::new(-180.0, -85.0, 180.0, 85.0));
/// ```
pub fn parse_bbox_ext(string: &str) -> UtilesCoreResult<BBox> {
    // match 'world' or 'planet'
    // match string/lower
    let str_lower = string
        .trim()
        .trim_matches(|c| c == '\'' || c == '"')
        .to_lowercase();
    let r = match str_lower.as_str() {
        "world" | "planet" | "all" | "*" => Ok(BBox::new(-180.0, -90.0, 180.0, 90.0)),
        "n" | "north" => Ok(BBox::new(-180.0, 0.0, 180.0, 90.0)),
        "s" | "south" => Ok(BBox::new(-180.0, -90.0, 180.0, 0.0)),
        "e" | "east" => Ok(BBox::new(0.0, -90.0, 180.0, 90.0)),
        "w" | "west" => Ok(BBox::new(-180.0, -90.0, 0.0, 90.0)),
        "ne" | "northeast" => Ok(BBox::new(0.0, 0.0, 180.0, 90.0)),
        "nw" | "northwest" => Ok(BBox::new(-180.0, 0.0, 0.0, 90.0)),
        "se" | "southeast" => Ok(BBox::new(0.0, -90.0, 180.0, 0.0)),
        "sw" | "southwest" => Ok(BBox::new(-180.0, -90.0, 0.0, 0.0)),
        _ => parse_bbox(&str_lower),
    };
    r
}

/// Parse a string into vector of integer strings
///
/// # Examples
/// ```
/// use utiles_core::parsing::parse_uint_strings;
/// let ints = parse_uint_strings("1,2,3,4,5");
/// assert_eq!(ints, vec!["1", "2", "3", "4", "5"]);
/// ```
///
/// ```
/// use utiles_core::parsing::parse_uint_strings;
/// let ints = parse_uint_strings("x1y2z3");
/// assert_eq!(ints, vec!["1", "2", "3"]);
/// ```
///
/// ```
/// use utiles_core::parsing::parse_uint_strings;
/// let ints = parse_uint_strings("as;ldfkjas;ldfkj");
/// assert_eq!(ints, Vec::<String>::new());
/// ```
///
/// ```
/// use utiles_core::parsing::parse_uint_strings;
/// let ints = parse_uint_strings("http://example.com/tiles/3/2/1.png");
/// assert_eq!(ints, vec!["3", "2", "1"]);
/// ```
#[must_use]
pub fn parse_uint_strings(input: &str) -> Vec<&str> {
    let mut blocks = Vec::new();
    let mut start = None;
    for (i, c) in input.char_indices() {
        if c.is_ascii_digit() {
            if start.is_none() {
                start = Some(i);
            }
        } else if let Some(s) = start {
            blocks.push(&input[s..i]);
            start = None;
        }
    }
    if let Some(s) = start {
        blocks.push(&input[s..]);
    }
    blocks
}

/// Parse a string into vector of integers
///
/// # Panics
///
/// Panics if the string contains a number that cannot be parsed as u64.
///
/// # Examples
/// ```
/// use utiles_core::parsing::parse_uints;
/// let ints = parse_uints("1,2,3,4,5");
/// assert_eq!(ints, vec![1, 2, 3, 4, 5]);
/// ```
///
/// ```
/// use utiles_core::parsing::parse_uints;
/// let ints = parse_uints("x1y2z3");
/// assert_eq!(ints, vec![1, 2, 3]);
/// ```
///
/// ```
/// use utiles_core::parsing::parse_uints;
/// let ints = parse_uints("as;ldfkjas;ldfkj");
/// assert_eq!(ints, Vec::<u64>::new());
/// ```
#[must_use]
pub fn parse_uints(input: &str) -> Vec<u64> {
    parse_uint_strings(input)
        .iter()
        .flat_map(|s| s.parse::<u64>())
        .collect()
}

/// Parse a string into a vector of signed integer strings
///
/// # Examples
/// ```
/// use utiles_core::parsing::parse_int_strings;
/// let ints = parse_int_strings("-1,2,---3,4,-5");
/// assert_eq!(ints, vec!["-1", "2", "-3", "4", "-5"]);
/// ```
///
/// ```
/// use utiles_core::parsing::parse_int_strings;
/// let ints = parse_int_strings("x-1y2z-3");
/// assert_eq!(ints, vec!["-1", "2", "-3"]);
/// ```
///
/// ```
/// use utiles_core::parsing::parse_int_strings;
/// let ints = parse_int_strings("as;ldfkjas;ldfkj");
/// assert_eq!(ints, Vec::<&str>::new());
/// ```
///
/// ```
/// use utiles_core::parsing::parse_int_strings;
/// let ints = parse_int_strings("http://example.com/tiles/-3/2/1.png");
/// assert_eq!(ints, vec!["-3", "2", "1"]);
/// ```
#[must_use]
pub fn parse_int_strings(input: &str) -> Vec<&str> {
    let mut blocks = Vec::new();
    let mut start = None;
    let mut is_negative = false; // flag to track if the current number is negative

    for (i, c) in input.char_indices() {
        match c {
            '-' => {
                // If we encounter a '-' and no number has started, note the start and set negative flag
                if start.is_none() {
                    start = Some(i);
                    is_negative = true; // Expecting a number after this
                } else if let Some(s) = start {
                    // If '-' follows digits or another '-', end the previous block (if valid) and start a new one
                    if !is_negative || s < i - 1 {
                        // Check if previous char was also '-' or if it's a valid number
                        blocks.push(&input[s..i]);
                    }
                    start = Some(i);
                    is_negative = true;
                }
            }
            '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' => {
                if start.is_none() || is_negative {
                    // Start of a new number block (potentially negative)
                    start = Some(i - usize::from(is_negative)); // Include '-' in block if negative
                }
                is_negative = false; // Once we have digits, it's no longer just a '-'
            }
            _ => {
                // For any other character, end the current number block if it exists and is valid
                if let Some(s) = start {
                    if !is_negative || s < i - 1 {
                        // Ensure it's not just a '-' without digits
                        blocks.push(&input[s..i]);
                    }
                }
                // Reset for the next number
                start = None;
                is_negative = false;
            }
        }
    }
    // Capture the last number block if there's one and it's valid
    if let Some(s) = start {
        if !is_negative || s < input.len() - 1 {
            // Ensure it's not just a '-' without digits
            blocks.push(&input[s..]);
        }
    }
    blocks
}

/// Parse a string into a vector of signed integers
///
/// # Examples
/// ```
/// use utiles_core::parsing::parse_ints;
/// let ints = parse_ints("-1,2,---3,4,-5");
/// assert_eq!(ints, vec![-1, 2, -3, 4, -5]);
/// ```
#[must_use]
pub fn parse_ints(input: &str) -> Vec<i64> {
    parse_int_strings(input)
        .iter()
        .flat_map(|s| s.parse::<i64>())
        .collect()
}

/// Parse float string blocks from a string
///
/// # Examples
/// ```
/// use utiles_core::parsing::parse_float_blocks;
/// let input = "-123.45..6abc--7.8.9";
/// let blocks = parse_float_blocks(input);
/// assert_eq!(blocks, vec!["-123.45", ".6", "-7.8", ".9"]);
/// ```
///
#[must_use]
pub fn parse_float_blocks(input: &str) -> Vec<&str> {
    let mut blocks = Vec::new();
    let mut start = None; // Start index of the current number block
    let mut has_decimal = false; // Track if the current block has a decimal point
    let mut has_digit = false; // Ensure there's at least one digit

    for (i, c) in input.char_indices() {
        match c {
            '0'..='9' => {
                if start.is_none() {
                    start = Some(i);
                }
                has_digit = true;
            }
            '-' => {
                if start.is_none() && !has_digit {
                    // Start of a new number
                    start = Some(i);
                } else if has_digit || has_decimal || start.is_some() {
                    // Malformed if in the middle of a number
                    if let Some(s) = start {
                        if has_digit {
                            // Ensure there's at least one digit
                            blocks.push(&input[s..i]);
                        }
                    }
                    start = Some(i); // Reset for a new potential number
                    has_decimal = false;
                    has_digit = false;
                }
            }
            '.' => {
                if !has_decimal && start.is_none() {
                    // First decimal in a new number
                    start = Some(i);
                    has_decimal = true;
                } else if has_decimal || start.is_none() {
                    // Malformed if another decimal or no start
                    if let Some(s) = start {
                        if has_digit {
                            // Ensure there's at least one digit
                            blocks.push(&input[s..i]);
                        }
                    }
                    start = Some(i); // Start a new potential number
                    has_decimal = true; // Current char is '.'
                    has_digit = false;
                } else {
                    // First decimal in an ongoing number
                    has_decimal = true;
                }
            }
            _ => {
                if let Some(s) = start {
                    if has_digit {
                        // Ensure there's at least one digit
                        blocks.push(&input[s..i]);
                    }
                }
                // Reset for the next number
                start = None;
                has_decimal = false;
                has_digit = false;
            }
        }
    }

    // Handle the last block if it's well-formed
    if let Some(s) = start {
        if has_digit {
            // Ensure there's at least one digit
            blocks.push(&input[s..]);
        }
    }

    blocks
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]

    use crate::bbox::*;
    use crate::parsing::parse_bbox;

    #[test]
    fn parse_bbox_simple() {
        let string = r"[-180.0, -85.0, 180.0, 85.0]";
        let bbox_result = parse_bbox(string);
        // assert!(bbox_result.is_ok());
        let bbox = bbox_result.unwrap();
        assert_eq!(bbox, BBox::new(-180.0, -85.0, 180.0, 85.0));
    }

    #[test]
    fn parse_bbox_simple_len_5() {
        let string = r#"[-180.0, -85.0, 180.0, 85.0, "uhhhhh"]"#;
        let bbox_result = parse_bbox(string);
        // assert!(bbox_result.is_ok());
        let bbox = bbox_result.unwrap();
        assert_eq!(bbox, BBox::new(-180.0, -85.0, 180.0, 85.0));
    }

    #[test]
    fn parse_bbox_simple_len_6() {
        let string = r"[-180.0, -85.0, 180.0, 85.0, 0, 10]";
        let bbox_result = parse_bbox(string);
        // assert!(bbox_result.is_ok());
        let bbox = bbox_result.unwrap();
        assert_eq!(bbox, BBox::new(-180.0, -85.0, 180.0, 85.0));
    }

    #[test]
    fn parse_bbox_str_commas() {
        let string = r"-180.0, -85.0, 180.0, 85.0";
        let bbox_result = parse_bbox(string);
        // assert!(bbox_result.is_ok());
        let bbox = bbox_result.unwrap();
        assert_eq!(bbox, BBox::new(-180.0, -85.0, 180.0, 85.0));
    }

    #[test]
    fn parse_bbox_str_spaces() {
        let string = r"-180.0 -85.0 180.0 85.0";
        let bbox_result = parse_bbox(string);
        // assert!(bbox_result.is_ok());
        let bbox = bbox_result.unwrap();
        assert_eq!(bbox, BBox::new(-180.0, -85.0, 180.0, 85.0));
    }

    #[test]
    fn parse_bbox_from_coords() {
        let string = "[-180.0, -85.0]";
        let bbox_result = parse_bbox(string);
        // assert!(bbox_result.is_ok());
        let bbox = bbox_result.unwrap();
        assert_eq!(bbox, BBox::new(-180.0, -85.0, -180.0, -85.0));
    }

    #[test]
    fn parse_bbox_bad() {
        let string = r"[-180.0,]";
        let bbox_result = parse_bbox(string);
        assert!(bbox_result.is_err());
    }

    #[test]
    fn parse_bbox_metadata_string() {
        let s = "-176.696694,-14.373776,145.830505,71.341324";
        let bbox = parse_bbox(s);
        assert!(bbox.is_ok());
        let bbox = bbox.unwrap();
        assert_eq!(
            bbox,
            BBox::new(-176.696_694, -14.373_776, 145.830_505, 71.341_324)
        );
    }
}