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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
//! # Flexible Polyline encoding
//!
//! The flexible polyline encoding is a lossy compressed representation of a list of coordinate
//! pairs or coordinate triples. It achieves that by:
//!
//! 1. Reducing the decimal digits of each value.
//! 2. Encoding only the offset from the previous point.
//! 3. Using variable length for each coordinate delta.
//! 4. Using 64 URL-safe characters to display the result.
//!
//! The encoding is a variant of [Encoded Polyline Algorithm Format]. The advantage of this encoding
//! over the original are the following:
//!
//! * Output string is composed by only URL-safe characters, i.e. may be used without URL encoding
//!   as query parameters.
//! * Floating point precision is configurable: This allows to represent coordinates with precision
//!   up to microns (5 decimal places allow meter precision only).
//! * It allows to encode a 3rd dimension with a given precision, which may be a level, altitude,
//!   elevation or some other custom value.
//!
//! ## Specification
//!
//! See [Specification].
//!
//! [Encoded Polyline Algorithm Format]: https://developers.google.com/maps/documentation/utilities/polylinealgorithm
//! [Specification]: https://github.com/heremaps/flexible-polyline#specifications
//!
//! ## Example
//!
//! ```rust
//! use flexpolyline::{Polyline, Precision};
//!
//! // encode
//! let coordinates = vec![
//!     (50.1022829, 8.6982122),
//!     (50.1020076, 8.6956695),
//!     (50.1006313, 8.6914960),
//!     (50.0987800, 8.6875156),
//! ];
//!
//! let polyline = Polyline::Data2d {
//!     coordinates,
//!     precision2d: Precision::Digits5,
//! };
//!
//! let encoded = polyline.encode().unwrap();
//! assert_eq!(encoded, "BFoz5xJ67i1B1B7PzIhaxL7Y");
//!
//! // decode
//! let decoded = Polyline::decode("BFoz5xJ67i1B1B7PzIhaxL7Y").unwrap();
//! assert_eq!(
//!     decoded,
//!     Polyline::Data2d {
//!         coordinates: vec![
//!             (50.10228, 8.69821),
//!             (50.10201, 8.69567),
//!             (50.10063, 8.69150),
//!             (50.09878, 8.68752)
//!         ],
//!         precision2d: Precision::Digits5
//!     }
//! );
//! ```

#![doc(html_playground_url = "https://play.rust-lang.org/")]
#![deny(warnings, missing_docs)]
#![allow(clippy::unreadable_literal)]

/// Coordinate precision in the polyline
///
/// Represents how many digits are to be encoded after the decimal point, e.g.
/// precision 3 would encode 4.456787 as 4.457.
///
/// Supported values: `[0,16)`
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Precision {
    /// 0 decimal digits
    Digits0 = 0,
    /// 1 decimal digits
    Digits1 = 1,
    /// 2 decimal digits
    Digits2 = 2,
    /// 3 decimal digits
    Digits3 = 3,
    /// 4 decimal digits
    Digits4 = 4,
    /// 5 decimal digits
    Digits5 = 5,
    /// 6 decimal digits
    Digits6 = 6,
    /// 7 decimal digits
    Digits7 = 7,
    /// 8 decimal digits
    Digits8 = 8,
    /// 9 decimal digits
    Digits9 = 9,
    /// 10 decimal digits
    Digits10 = 10,
    /// 11 decimal digits
    Digits11 = 11,
    /// 12 decimal digits
    Digits12 = 12,
    /// 13 decimal digits
    Digits13 = 13,
    /// 14 decimal digits
    Digits14 = 14,
    /// 15 decimal digits
    Digits15 = 15,
}

impl Precision {
    /// Converts `u32` to precision.
    pub fn from_u32(digits: u32) -> Option<Precision> {
        match digits {
            0 => Some(Precision::Digits0),
            1 => Some(Precision::Digits1),
            2 => Some(Precision::Digits2),
            3 => Some(Precision::Digits3),
            4 => Some(Precision::Digits4),
            5 => Some(Precision::Digits5),
            6 => Some(Precision::Digits6),
            7 => Some(Precision::Digits7),
            8 => Some(Precision::Digits8),
            9 => Some(Precision::Digits9),
            10 => Some(Precision::Digits10),
            11 => Some(Precision::Digits11),
            12 => Some(Precision::Digits12),
            13 => Some(Precision::Digits13),
            14 => Some(Precision::Digits14),
            15 => Some(Precision::Digits15),
            _ => None,
        }
    }

    /// Converts precision to `u32`.
    pub fn to_u32(self) -> u32 {
        self as u32
    }
}

/// Informs about the type of the 3rd dimension of a 3D coordinate vector
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Type3d {
    /// E.g. floor of a building
    Level = 1,
    /// E.g. altitude (in the air) relative to ground level or mean sea level
    Altitude = 2,
    /// E.g. elevation above mean-sea-level
    Elevation = 3,
    /// Reserved for future types
    Reserved1 = 4,
    /// Reserved for future types
    Reserved2 = 5,
    /// Reserved for custom types
    Custom1 = 6,
    /// Reserved for custom types
    Custom2 = 7,
}

/// 2- or 3-dimensional polyline
#[derive(Debug, Clone, PartialEq)]
pub enum Polyline {
    /// 2-dimensional polyline
    Data2d {
        /// List of 2D coordinates making up this polyline
        coordinates: Vec<(f64, f64)>,
        /// Precision of the coordinates (e.g. used for encoding,
        /// or to report the precision supplied in encoded data)
        precision2d: Precision,
    },
    /// 3-dimensional polyline
    Data3d {
        /// List of 3D coordinates making up this polyline
        coordinates: Vec<(f64, f64, f64)>,
        /// Precision of the 2D part of the coordinates (e.g. used for encoding,
        /// or to report the precision supplied in encoded data)
        precision2d: Precision,
        /// Precision of the 3D part of the coordinates (e.g. used for encoding,
        /// or to report the precision supplied in encoded data)
        precision3d: Precision,
        /// Type of the 3D component
        type3d: Type3d,
    },
}

impl std::fmt::Display for Polyline {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let prec = f.precision().unwrap_or(6);
        match self {
            Polyline::Data2d {
                coordinates,
                precision2d,
            } => {
                write!(f, "{{({}); [", precision2d.to_u32())?;
                for coord in coordinates {
                    write!(
                        f,
                        "({:.*}, {:.*}), ",
                        prec as usize, coord.0, prec as usize, coord.1
                    )?;
                }
                write!(f, "]}}")?;
            }
            Polyline::Data3d {
                coordinates,
                precision2d,
                precision3d,
                type3d,
            } => {
                write!(
                    f,
                    "{{({}, {}, {}); [",
                    precision2d.to_u32(),
                    precision3d.to_u32(),
                    *type3d as usize
                )?;
                for coord in coordinates {
                    write!(
                        f,
                        "({:.*}, {:.*}, {:.*}), ",
                        prec as usize, coord.0, prec as usize, coord.1, prec as usize, coord.2
                    )?;
                }
                write!(f, "]}}")?;
            }
        }
        Ok(())
    }
}

/// Error reported when encoding or decoding polylines
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
    /// Data is encoded with unsupported version
    UnsupportedVersion,
    /// Precision is not supported by encoding
    InvalidPrecision,
    /// Encoding is corrupt
    InvalidEncoding,
    #[doc(hidden)]
    __Nonexhaustive,
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Error::UnsupportedVersion => write!(f, "UnsupportedVersion"),
            Error::InvalidPrecision => write!(f, "InvalidPrecision"),
            Error::InvalidEncoding => write!(f, "InvalidEncoding"),
            Error::__Nonexhaustive => panic!(),
        }
    }
}

impl std::error::Error for Error {}

impl Polyline {
    /// Encodes a polyline into a string.
    ///
    /// The precision of the polyline is used to round coordinates, so the transformation is lossy
    /// in nature.
    pub fn encode(&self) -> Result<String, Error> {
        match self {
            Polyline::Data2d {
                coordinates,
                precision2d,
            } => encode_2d(&coordinates, precision2d.to_u32()),
            Polyline::Data3d {
                coordinates,
                precision2d,
                precision3d,
                type3d,
            } => encode_3d(
                coordinates,
                precision2d.to_u32(),
                precision3d.to_u32(),
                *type3d as u32,
            ),
        }
    }

    /// Decodes an encoded polyline.
    pub fn decode<S: AsRef<str>>(encoded: S) -> Result<Self, Error> {
        let mut bytes = encoded.as_ref().bytes();

        let (precision2d, precision3d, type3d) = decode_header(&mut bytes)?;

        let type3d = match type3d {
            1 => Some(Type3d::Level),
            2 => Some(Type3d::Altitude),
            3 => Some(Type3d::Elevation),
            4 => Some(Type3d::Reserved1),
            5 => Some(Type3d::Reserved2),
            6 => Some(Type3d::Custom1),
            7 => Some(Type3d::Custom2),
            0 => None,
            _ => panic!(), // impossible, we only decoded 3 bits
        };

        if let Some(type3d) = type3d {
            let coordinates = decode3d(bytes, precision2d, precision3d)?;
            Ok(Polyline::Data3d {
                coordinates,
                precision2d: Precision::from_u32(precision2d).ok_or(Error::InvalidPrecision)?,
                precision3d: Precision::from_u32(precision3d).ok_or(Error::InvalidPrecision)?,
                type3d,
            })
        } else {
            let coordinates = decode2d(bytes, precision2d)?;
            Ok(Polyline::Data2d {
                coordinates,
                precision2d: Precision::from_u32(precision2d).ok_or(Error::InvalidPrecision)?,
            })
        }
    }
}

fn precision_to_scale(precision: u32) -> impl Fn(f64) -> i64 {
    let scale = 10_u64.pow(precision) as f64;
    move |value: f64| (value * scale).round() as i64
}

fn precision_to_inverse_scale(precision: u32) -> impl Fn(i64) -> f64 {
    let scale = 10_u64.pow(precision) as f64;
    move |value: i64| value as f64 / scale
}

fn encode_header(
    precision2d: u32,
    precision3d: u32,
    type3d: u32,
    result: &mut String,
) -> Result<(), Error> {
    if precision2d > 15 || precision3d > 15 {
        return Err(Error::InvalidPrecision);
    }
    var_encode_u64(1, result); // Version 1
    let header = (precision3d << 7) | (type3d << 4) | precision2d;
    var_encode_u64(u64::from(header), result);
    Ok(())
}

fn encode_2d(coords: &[(f64, f64)], precision2d: u32) -> Result<String, Error> {
    let mut result = String::with_capacity((coords.len() * 2) + 2);

    encode_header(precision2d, 0, 0, &mut result)?;
    let scale2d = precision_to_scale(precision2d);

    let mut last_coord = (0, 0);
    for coord in coords {
        let scaled_coord = (scale2d(coord.0), scale2d(coord.1));
        var_encode_i64(scaled_coord.0 - last_coord.0, &mut result);
        var_encode_i64(scaled_coord.1 - last_coord.1, &mut result);
        last_coord = scaled_coord;
    }

    Ok(result)
}

fn encode_3d(
    coords: &[(f64, f64, f64)],
    precision2d: u32,
    precision3d: u32,
    type3d: u32,
) -> Result<String, Error> {
    let mut result = String::with_capacity((coords.len() * 3) + 2);

    encode_header(precision2d, precision3d, type3d, &mut result)?;
    let scale2d = precision_to_scale(precision2d);
    let scale3d = precision_to_scale(precision3d);

    let mut last_coord = (0, 0, 0);
    for coord in coords {
        let scaled_coord = (scale2d(coord.0), scale2d(coord.1), scale3d(coord.2));
        var_encode_i64(scaled_coord.0 - last_coord.0, &mut result);
        var_encode_i64(scaled_coord.1 - last_coord.1, &mut result);
        var_encode_i64(scaled_coord.2 - last_coord.2, &mut result);
        last_coord = scaled_coord;
    }

    Ok(result)
}

fn var_encode_i64(value: i64, result: &mut String) {
    // make room on lowest bit
    let mut encoded = (value << 1) as u64;

    // invert bits if the value is negative
    if value < 0 {
        encoded = !encoded;
    }

    var_encode_u64(encoded, result);
}

fn var_encode_u64(mut value: u64, result: &mut String) {
    const ENCODING_TABLE: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";

    // var-length encode the number in chunks of 5 bits starting with the least significant
    // to the most significant
    while value > 0x1F {
        let pos = (value & 0x1F) | 0x20;
        let c = ENCODING_TABLE.as_bytes()[pos as usize] as char;
        result.push(c);
        value >>= 5;
    }
    let c = ENCODING_TABLE.as_bytes()[value as usize] as char;
    result.push(c);
}

fn decode_header<I: Iterator<Item = u8>>(bytes: &mut I) -> Result<(u32, u32, u32), Error> {
    let version = var_decode_u64(bytes)?;

    if version != 1 {
        return Err(Error::UnsupportedVersion);
    }

    let header = var_decode_u64(bytes)?;

    if header >= (1_u64 << 11) {
        return Err(Error::InvalidEncoding);
    }
    let precision2d = (header & 15) as u32;
    let type3d = ((header >> 4) & 7) as u32;
    let precision3d = ((header >> 7) & 15) as u32;

    Ok((precision2d, precision3d, type3d))
}

fn decode2d<I: ExactSizeIterator<Item = u8>>(
    mut bytes: I,
    precision2d: u32,
) -> Result<Vec<(f64, f64)>, Error> {
    let mut result = Vec::with_capacity(bytes.len() / 2);
    let scale2d = precision_to_inverse_scale(precision2d);
    let mut last_coord = (0, 0);
    while bytes.len() > 0 {
        let delta = (var_decode_i64(&mut bytes)?, var_decode_i64(&mut bytes)?);
        last_coord = (last_coord.0 + delta.0, last_coord.1 + delta.1);

        result.push((scale2d(last_coord.0), scale2d(last_coord.1)));
    }
    Ok(result)
}

fn decode3d<I: ExactSizeIterator<Item = u8>>(
    mut bytes: I,
    precision2d: u32,
    precision3d: u32,
) -> Result<Vec<(f64, f64, f64)>, Error> {
    let mut result = Vec::with_capacity(bytes.len() / 2);
    let scale2d = precision_to_inverse_scale(precision2d);
    let scale3d = precision_to_inverse_scale(precision3d);
    let mut last_coord = (0, 0, 0);
    while bytes.len() > 0 {
        let delta = (
            var_decode_i64(&mut bytes)?,
            var_decode_i64(&mut bytes)?,
            var_decode_i64(&mut bytes)?,
        );
        last_coord = (
            last_coord.0 + delta.0,
            last_coord.1 + delta.1,
            last_coord.2 + delta.2,
        );

        result.push((
            scale2d(last_coord.0),
            scale2d(last_coord.1),
            scale3d(last_coord.2),
        ));
    }
    Ok(result)
}

fn var_decode_i64<I: Iterator<Item = u8>>(bytes: &mut I) -> Result<i64, Error> {
    match var_decode_u64(bytes) {
        Ok(mut value) => {
            let negative = (value & 1) != 0;
            value >>= 1;
            if negative {
                value = !value;
            }
            Ok(value as i64)
        }
        Err(err) => Err(err),
    }
}

fn var_decode_u64<I: Iterator<Item = u8>>(bytes: &mut I) -> Result<u64, Error> {
    #[rustfmt::skip]
    const DECODING_TABLE: &[i8] = &[
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, 62, -1, -1, 52, 53,
        54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
        -1, -1, -1, -1, -1,  0,  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, -1, -1, -1, -1, 63, -1, 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, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1,
    ];

    let mut result: u64 = 0;
    let mut shift = 0;

    for byte in bytes {
        let value = DECODING_TABLE[byte as usize];
        if value < 0 {
            return Err(Error::InvalidEncoding);
        }

        let value = value as u64;
        result |= (value & 0x1F) << shift;

        if (value & 0x20) == 0 {
            return Ok(result);
        }

        shift += 5;

        if shift >= 64 {
            return Err(Error::InvalidEncoding);
        }
    }

    Err(Error::InvalidEncoding)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_var_encode_i64() {
        let mut buf = String::new();
        var_encode_i64(-17998321, &mut buf);
        assert_eq!(buf, "h_wqiB");
    }

    #[test]
    fn test_encode_2d_example_1() {
        let coordinates = vec![
            (50.1022829, 8.6982122),
            (50.1020076, 8.6956695),
            (50.1006313, 8.6914960),
            (50.0987800, 8.6875156),
        ];

        let expected = "BFoz5xJ67i1B1B7PzIhaxL7Y";
        assert_eq!(
            &Polyline::Data2d {
                coordinates,
                precision2d: Precision::Digits5
            }
            .encode()
            .unwrap(),
            expected
        );
    }

    #[test]
    fn test_encode_2d_example_2() {
        let coordinates = vec![
            (52.5199356, 13.3866272),
            (52.5100899, 13.2816896),
            (52.4351807, 13.1935196),
            (52.4107285, 13.1964502),
            (52.3887100, 13.1557798),
            (52.3727798, 13.1491003),
            (52.3737488, 13.1154604),
            (52.3875198, 13.0872202),
            (52.4029388, 13.0706196),
            (52.4105797, 13.0755529),
        ];

        let expected = "BF05xgKuy2xCx9B7vUl0OhnR54EqSzpEl-HxjD3pBiGnyGi2CvwFsgD3nD4vB6e";
        assert_eq!(
            &Polyline::Data2d {
                coordinates,
                precision2d: Precision::Digits5
            }
            .encode()
            .unwrap(),
            expected
        );
    }

    #[test]
    fn test_encode_3d_example_1() {
        let coordinates = vec![
            (50.1022829, 8.6982122, 10.0),
            (50.1020076, 8.6956695, 20.0),
            (50.1006313, 8.6914960, 30.0),
            (50.0987800, 8.6875156, 40.0),
        ];

        let expected = "BVoz5xJ67i1BU1B7PUzIhaUxL7YU";
        assert_eq!(
            &Polyline::Data3d {
                coordinates,
                precision2d: Precision::Digits5,
                precision3d: Precision::Digits0,
                type3d: Type3d::Level
            }
            .encode()
            .unwrap(),
            expected
        );
    }

    #[test]
    fn test_var_decode_i64() -> Result<(), Error> {
        let mut bytes = "h_wqiB".bytes();
        let res = var_decode_i64(&mut bytes)?;
        assert_eq!(res, -17998321);
        let res = var_decode_i64(&mut bytes);
        assert!(res.is_err());

        let mut bytes = "hhhhhhhhhhhhhhhhhhh".bytes();
        let res = var_decode_i64(&mut bytes);
        assert!(res.is_err());
        Ok(())
    }

    #[test]
    fn test_decode_2d_example_1() -> Result<(), Error> {
        let polyline = Polyline::decode("BFoz5xJ67i1B1B7PzIhaxL7Y")?;
        let expected = "{(5); [\
                        (50.102280, 8.698210), \
                        (50.102010, 8.695670), \
                        (50.100630, 8.691500), \
                        (50.098780, 8.687520), \
                        ]}";
        let result = format!("{:.6}", polyline);
        assert_eq!(expected, result);
        Ok(())
    }

    #[test]
    fn test_decode_2d_example_2() -> Result<(), Error> {
        let polyline =
            Polyline::decode("BF05xgKuy2xCx9B7vUl0OhnR54EqSzpEl-HxjD3pBiGnyGi2CvwFsgD3nD4vB6e")?;
        let expected = "{(5); [\
                        (52.519940, 13.386630), \
                        (52.510090, 13.281690), \
                        (52.435180, 13.193520), \
                        (52.410730, 13.196450), \
                        (52.388710, 13.155780), \
                        (52.372780, 13.149100), \
                        (52.373750, 13.115460), \
                        (52.387520, 13.087220), \
                        (52.402940, 13.070620), \
                        (52.410580, 13.075550), \
                        ]}";

        let result = format!("{:.6}", polyline);
        assert_eq!(expected, result);
        Ok(())
    }

    #[test]
    fn test_decode_3d_example_1() -> Result<(), Error> {
        let polyline = Polyline::decode("BVoz5xJ67i1BU1B7PUzIhaUxL7YU")?;
        let expected = "{(5, 0, 1); [\
                        (50.102280, 8.698210, 10.000000), \
                        (50.102010, 8.695670, 20.000000), \
                        (50.100630, 8.691500, 30.000000), \
                        (50.098780, 8.687520, 40.000000), \
                        ]}";

        let result = format!("{:.6}", polyline);
        assert_eq!(expected, result);
        Ok(())
    }

    #[test]
    #[allow(clippy::zero_prefixed_literal)]
    fn test_encode_decode_2d() -> Result<(), Error> {
        let coordinate_values: Vec<(u64, u64)> = vec![
            (96821474666297905, 78334196549606266),
            (29405294060895017, 70361389340728572),
            (16173544634348013, 17673855782924183),
            (22448654820449524, 13005139703027850),
            (73351231936757857, 78298027377720633),
            (78008331957098324, 04847613123220218),
            (62755680515396509, 49165433608990700),
            (93297154866561429, 52373802822465027),
            (89973844644540399, 75975762025877533),
            (48555821719956867, 31591090068957813),
        ];

        for precision2d in 0..=15 {
            let to_f64 = |value: &(u64, u64)| {
                (
                    value.0 as f64 / 10_u64.pow(15) as f64,
                    value.1 as f64 / 10_u64.pow(15) as f64,
                )
            };

            let to_rounded_f64 = |value: &(u64, u64)| {
                let value = to_f64(value);
                let scale = 10_u64.pow(precision2d) as f64;
                (
                    (value.0 * scale).round() / scale,
                    (value.1 * scale).round() / scale,
                )
            };

            let expected = format!(
                "{:.*}",
                precision2d as usize + 1,
                Polyline::Data2d {
                    coordinates: coordinate_values.iter().map(to_rounded_f64).collect(),
                    precision2d: Precision::from_u32(precision2d).unwrap(),
                }
            );

            let encoded = &Polyline::Data2d {
                coordinates: coordinate_values.iter().map(to_f64).collect(),
                precision2d: Precision::from_u32(precision2d).unwrap(),
            }
            .encode()?;

            let polyline = Polyline::decode(&encoded)?;
            let result = format!("{:.*}", precision2d as usize + 1, polyline);
            assert_eq!(expected, result);
        }

        Ok(())
    }

    #[test]
    #[allow(clippy::zero_prefixed_literal)]
    fn test_encode_decode_3d() -> Result<(), Error> {
        let coordinate_values: Vec<(u64, u64, u64)> = vec![
            (96821474666297905, 78334196549606266, 23131023979661380),
            (29405294060895017, 70361389340728572, 81917934930416924),
            (16173544634348013, 17673855782924183, 86188502094968953),
            (22448654820449524, 13005139703027850, 68774670569614983),
            (73351231936757857, 78298027377720633, 52078352171243855),
            (78008331957098324, 04847613123220218, 06550838806837986),
            (62755680515396509, 49165433608990700, 39041897671300539),
            (93297154866561429, 52373802822465027, 67310807938230681),
            (89973844644540399, 75975762025877533, 66789448009436096),
            (48555821719956867, 31591090068957813, 49203621966471323),
        ];

        let precision2d = 5;
        for precision3d in 0..=15 {
            for type3d in &[
                Type3d::Level,
                Type3d::Altitude,
                Type3d::Elevation,
                Type3d::Reserved1,
                Type3d::Reserved2,
                Type3d::Custom1,
                Type3d::Custom2,
            ] {
                let to_f64 = |value: &(u64, u64, u64)| {
                    (
                        value.0 as f64 / 10_u64.pow(15) as f64,
                        value.1 as f64 / 10_u64.pow(15) as f64,
                        value.2 as f64 / 10_u64.pow(15) as f64,
                    )
                };

                let to_rounded_f64 = |value: &(u64, u64, u64)| {
                    let value = to_f64(value);
                    let scale2d = 10_u64.pow(precision2d) as f64;
                    let scale3d = 10_u64.pow(precision3d) as f64;
                    (
                        (value.0 * scale2d).round() / scale2d,
                        (value.1 * scale2d).round() / scale2d,
                        (value.2 * scale3d).round() / scale3d,
                    )
                };

                let expected = format!(
                    "{:.*}",
                    precision2d.max(precision3d) as usize + 1,
                    Polyline::Data3d {
                        coordinates: coordinate_values.iter().map(to_rounded_f64).collect(),
                        precision2d: Precision::from_u32(precision2d).unwrap(),
                        precision3d: Precision::from_u32(precision3d).unwrap(),
                        type3d: *type3d,
                    }
                );

                let encoded = Polyline::Data3d {
                    coordinates: coordinate_values.iter().map(to_f64).collect(),
                    precision2d: Precision::from_u32(precision2d).unwrap(),
                    precision3d: Precision::from_u32(precision3d).unwrap(),
                    type3d: *type3d,
                }
                .encode()?;

                let polyline = Polyline::decode(&encoded)?;
                let result = format!("{:.*}", precision2d.max(precision3d) as usize + 1, polyline);
                assert_eq!(expected, result);
            }
        }

        Ok(())
    }
}