pbfhogg 0.5.0

Fast OpenStreetMap PBF reader and writer for Rust. Read, write, and merge .osm.pbf files with pipelined parallel decoding.
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
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
//! OSM PrimitiveBlock wire-format message parsers.
//!
//! Uses [`protohoggr`] for the generic protobuf primitives (Cursor, packed
//! iterators, wire constants). This module adds the OSM-specific message
//! parsers: PrimitiveBlock, PrimitiveGroup, Node, Way, Relation, DenseNodes,
//! DenseInfo, Info, and StringTable.

use crate::error::Result;
pub(crate) use protohoggr::{
    Cursor, PackedBoolIter, PackedInt32Iter, PackedSint32Iter, PackedSint64Iter, PackedUint32Iter,
    WIRE_LEN, WIRE_VARINT,
};

// ---------------------------------------------------------------------------
// WireStringTable - zero-copy indexed string table
// ---------------------------------------------------------------------------

/// String table storing buffer-relative offsets as inline LE bytes in the
/// decompressed buffer. Zero separate heap allocations.
///
/// Offsets are relative to the decompressed buffer, not the StringTable message,
/// because `PrimitiveBlock` in `block.rs` transmutes `WireBlock<'a>` to
/// `WireBlock<'static>` for self-referential ownership.
///
/// The (u32, u32) entry pairs (offset, length) are appended to the buffer
/// after the protobuf data by `WireBlock::parse_and_inline`. This eliminates
/// the cross-thread Box alloc/free retention that caused 25+ GB OOM at
/// Europe/planet scale with the pipelined reader.
#[derive(Clone, Debug)]
pub(crate) struct WireStringTable<'a> {
    buffer: &'a [u8],
    /// Byte offset into buffer where inline (u32, u32) LE entries start.
    entries_offset: u32,
    /// Number of inline entries.
    entries_count: u32,
}

impl<'a> WireStringTable<'a> {
    /// Scan string table entries from protobuf data, collecting into caller's Vec.
    /// Caller appends the Vec contents to the buffer afterward.
    fn scan_entries(data: &[u8], buffer: &[u8], out: &mut Vec<(u32, u32)>) -> Result<()> {
        let mut cursor = Cursor::new(data);
        out.clear();
        while let Some((field, wire_type)) = cursor.read_tag()? {
            if field == 1 && wire_type == WIRE_LEN {
                let bytes = cursor.read_len_delimited()?;
                let offset = bytes.as_ptr() as usize - buffer.as_ptr() as usize;
                #[allow(clippy::cast_possible_truncation)]
                out.push((offset as u32, bytes.len() as u32));
            } else {
                cursor.skip_field(wire_type)?;
            }
        }
        Ok(())
    }

    /// Create a WireStringTable that reads entries from the buffer itself.
    fn new(buffer: &'a [u8], entries_offset: u32, entries_count: u32) -> Self {
        Self {
            buffer,
            entries_offset,
            entries_count,
        }
    }

    #[inline]
    pub fn len(&self) -> usize {
        self.entries_count as usize
    }

    #[inline]
    pub fn get(&self, index: usize) -> Option<&'a [u8]> {
        if index >= self.entries_count as usize {
            return None;
        }
        let base = self.entries_offset as usize + index * 8;
        if base + 8 > self.buffer.len() {
            return None;
        }
        let off = u32::from_le_bytes([
            self.buffer[base],
            self.buffer[base + 1],
            self.buffer[base + 2],
            self.buffer[base + 3],
        ]);
        let len = u32::from_le_bytes([
            self.buffer[base + 4],
            self.buffer[base + 5],
            self.buffer[base + 6],
            self.buffer[base + 7],
        ]);
        Some(&self.buffer[off as usize..off as usize + len as usize])
    }
}

// ---------------------------------------------------------------------------
// WireBlock - parsed PrimitiveBlock
// ---------------------------------------------------------------------------

/// Parsed PrimitiveBlock - the root message of each data blob.
///
/// All fields borrow from `buffer` (the decompressed blob bytes). Group data
/// and string table entries are stored as buffer-relative `(offset, length)`
/// pairs rather than slices, because `PrimitiveBlock` in `block.rs` transmutes
/// `WireBlock<'a>` to `WireBlock<'static>` for self-referential ownership.
/// The `buffer` reference is reconstituted at access time via `group()` and
/// `WireStringTable::get()`.
#[derive(Debug)]
pub(crate) struct WireBlock<'a> {
    buffer: &'a [u8],
    pub stringtable: WireStringTable<'a>,
    /// Raw StringTable protobuf bytes location (for per-group raw passthrough).
    #[allow(dead_code)]
    st_raw_offset: u32,
    #[allow(dead_code)]
    st_raw_len: u32,
    group_ranges_offset: u32,
    group_ranges_count: u32,
    pub granularity: i32,
    pub lat_offset: i64,
    pub lon_offset: i64,
    pub date_granularity: i32,
    /// Length of the original protobuf data (before inline entries were appended).
    pub proto_len: u32,
}

impl<'a> WireBlock<'a> {
    /// Parse and inline: scan protobuf, then append string table entries and group
    /// ranges as raw LE bytes to the buffer. Zero separate heap allocations.
    ///
    /// The buffer is extended in-place. After this call, the buffer layout is:
    /// `[protobuf data] [st entries: (u32,u32) × N as LE bytes] [group ranges: (u32,u32) × M as LE bytes]`
    ///
    /// Temp Vecs used during scanning are allocated and freed on the calling thread
    /// (no cross-thread retention). Only the buffer itself crosses thread boundaries.
    #[hotpath::measure]
    #[allow(clippy::cast_possible_truncation)]
    pub fn parse_and_inline(buf: &mut Vec<u8>) -> Result<WireBlockMeta> {
        let mut st_scratch = Vec::new();
        let mut gr_scratch = Vec::new();
        Self::parse_and_inline_with_scratch(buf, &mut st_scratch, &mut gr_scratch)
    }

    /// Like [`parse_and_inline`] but reuses caller-provided scratch buffers
    /// to avoid per-block allocation. The scratch Vecs are cleared at the start
    /// and may grow to accommodate the block's string table and group entries.
    /// Between blocks, the capacity is retained - no malloc/free after the
    /// first few blocks.
    #[hotpath::measure]
    #[allow(clippy::cast_possible_truncation)]
    pub fn parse_and_inline_with_scratch(
        buf: &mut Vec<u8>,
        st_entries: &mut Vec<(u32, u32)>,
        group_entries: &mut Vec<(u32, u32)>,
    ) -> Result<WireBlockMeta> {
        let proto_len = buf.len() as u32;

        // Phase 1: scan protobuf, collect into reusable scratch Vecs.
        st_entries.clear();
        group_entries.clear();
        let mut granularity: i32 = 100;
        let mut lat_offset: i64 = 0;
        let mut lon_offset: i64 = 0;
        let mut date_granularity: i32 = 1000;
        let mut stringtable_offset: usize = 0;
        let mut stringtable_len: usize = 0;

        {
            let buffer: &[u8] = buf;
            let mut cursor = Cursor::new(buffer);

            while let Some((field, wire_type)) = cursor.read_tag()? {
                match (field, wire_type) {
                    (1, WIRE_LEN) => {
                        let data = cursor.read_len_delimited()?;
                        stringtable_offset = data.as_ptr() as usize - buffer.as_ptr() as usize;
                        stringtable_len = data.len();
                    }
                    (2, WIRE_LEN) => {
                        let data = cursor.read_len_delimited()?;
                        let offset = data.as_ptr() as usize - buffer.as_ptr() as usize;
                        group_entries.push((offset as u32, data.len() as u32));
                    }
                    (17, WIRE_VARINT) => {
                        #[allow(clippy::cast_possible_wrap)]
                        {
                            granularity = cursor.read_varint()? as i32;
                        }
                    }
                    (19, WIRE_VARINT) => {
                        lat_offset = cursor.read_varint_i64()?;
                    }
                    (20, WIRE_VARINT) => {
                        lon_offset = cursor.read_varint_i64()?;
                    }
                    (18, WIRE_VARINT) => {
                        #[allow(clippy::cast_possible_wrap)]
                        {
                            date_granularity = cursor.read_varint()? as i32;
                        }
                    }
                    _ => cursor.skip_field(wire_type)?,
                }
            }

            // Scan string table entries from the stringtable submessage.
            let st_data = if stringtable_len > 0 {
                &buffer[stringtable_offset..stringtable_offset + stringtable_len]
            } else {
                &[]
            };
            WireStringTable::scan_entries(st_data, buffer, st_entries)?;
        }
        // Phase 1 temp Vecs are still alive but immutable borrow of buf is released.

        // Phase 2: append entries as raw LE bytes to the buffer.
        // Pre-reserve exact capacity to avoid reallocation during extend.
        let append_size = (st_entries.len() + group_entries.len()) * 8;
        buf.reserve(append_size);

        let st_inline_offset = buf.len() as u32;
        let st_inline_count = st_entries.len() as u32;
        for &(off, len) in st_entries.iter() {
            buf.extend_from_slice(&off.to_le_bytes());
            buf.extend_from_slice(&len.to_le_bytes());
        }

        let gr_inline_offset = buf.len() as u32;
        let gr_inline_count = group_entries.len() as u32;
        for &(off, len) in group_entries.iter() {
            buf.extend_from_slice(&off.to_le_bytes());
            buf.extend_from_slice(&len.to_le_bytes());
        }

        // Scratch Vecs retain capacity for the next block (caller reuses them).
        #[allow(clippy::cast_possible_truncation)]
        Ok(WireBlockMeta {
            proto_len,
            st_raw_offset: stringtable_offset as u32,
            st_raw_len: stringtable_len as u32,
            st_inline_offset,
            st_inline_count,
            gr_inline_offset,
            gr_inline_count,
            granularity,
            lat_offset,
            lon_offset,
            date_granularity,
        })
    }

    /// Construct a WireBlock from inline metadata + the extended buffer.
    pub fn from_inline(buffer: &'a [u8], meta: &WireBlockMeta) -> Self {
        Self {
            buffer,
            stringtable: WireStringTable::new(buffer, meta.st_inline_offset, meta.st_inline_count),
            st_raw_offset: meta.st_raw_offset,
            st_raw_len: meta.st_raw_len,
            group_ranges_offset: meta.gr_inline_offset,
            group_ranges_count: meta.gr_inline_count,
            granularity: meta.granularity,
            lat_offset: meta.lat_offset,
            lon_offset: meta.lon_offset,
            date_granularity: meta.date_granularity,
            proto_len: meta.proto_len,
        }
    }

    /// Number of primitive groups in this block.
    #[inline]
    pub fn group_count(&self) -> usize {
        self.group_ranges_count as usize
    }

    /// Raw StringTable protobuf bytes (field 1 of PrimitiveBlock).
    /// Scaffolding for future per-group raw passthrough.
    #[inline]
    #[allow(dead_code)]
    pub fn raw_stringtable(&self) -> &'a [u8] {
        &self.buffer
            [self.st_raw_offset as usize..self.st_raw_offset as usize + self.st_raw_len as usize]
    }

    /// Raw protobuf bytes for the entire PrimitiveBlock (before inline entries).
    /// The first `proto_len` bytes of the buffer.
    #[inline]
    #[allow(dead_code)]
    pub fn raw_proto(&self) -> &'a [u8] {
        &self.buffer[..self.proto_len as usize]
    }

    #[inline]
    pub fn group(&self, index: usize) -> &'a [u8] {
        let base = self.group_ranges_offset as usize + index * 8;
        let off = u32::from_le_bytes([
            self.buffer[base],
            self.buffer[base + 1],
            self.buffer[base + 2],
            self.buffer[base + 3],
        ]);
        let len = u32::from_le_bytes([
            self.buffer[base + 4],
            self.buffer[base + 5],
            self.buffer[base + 6],
            self.buffer[base + 7],
        ]);
        &self.buffer[off as usize..off as usize + len as usize]
    }
}

/// Metadata from `WireBlock::parse_and_inline`. Stored on the stack - no heap.
#[derive(Clone, Copy, Debug)]
pub(crate) struct WireBlockMeta {
    pub proto_len: u32,
    /// Raw StringTable protobuf bytes: offset and length within the proto region.
    pub st_raw_offset: u32,
    pub st_raw_len: u32,
    pub st_inline_offset: u32,
    pub st_inline_count: u32,
    pub gr_inline_offset: u32,
    pub gr_inline_count: u32,
    pub granularity: i32,
    pub lat_offset: i64,
    pub lon_offset: i64,
    pub date_granularity: i32,
}

// ---------------------------------------------------------------------------
// WireGroup - lazy PrimitiveGroup scanner
// ---------------------------------------------------------------------------

/// Lazy PrimitiveGroup scanner - yields raw sub-message bytes by element type.
///
/// Does not parse eagerly; each accessor (`nodes()`, `dense()`, `ways()`,
/// `relations()`) scans the group's wire format for the target field number.
/// A PrimitiveGroup contains exactly one element type per the OSM PBF spec.
pub(crate) struct WireGroup<'a> {
    data: &'a [u8],
}

impl<'a> WireGroup<'a> {
    #[inline]
    pub fn new(data: &'a [u8]) -> Self {
        Self { data }
    }

    #[inline]
    pub fn nodes(&self) -> WireMessageIter<'a> {
        WireMessageIter::new(self.data, 1)
    }

    pub fn dense(&self) -> Result<Option<&'a [u8]>> {
        let mut cursor = Cursor::new(self.data);
        while let Some((field, wire_type)) = cursor.read_tag()? {
            if field == 2 && wire_type == WIRE_LEN {
                return Ok(Some(cursor.read_len_delimited()?));
            }
            cursor.skip_field(wire_type)?;
        }
        Ok(None)
    }

    #[inline]
    pub fn ways(&self) -> WireMessageIter<'a> {
        WireMessageIter::new(self.data, 3)
    }

    #[inline]
    pub fn relations(&self) -> WireMessageIter<'a> {
        WireMessageIter::new(self.data, 4)
    }
}

// ---------------------------------------------------------------------------
// WireMessageIter - yields sub-message byte slices for a given field number
// ---------------------------------------------------------------------------

/// Iterator over length-delimited sub-messages matching a specific field number.
///
/// Used by `WireGroup` to yield raw bytes for each Node, Way, or Relation
/// message within a PrimitiveGroup. Skips non-matching fields.
pub(crate) struct WireMessageIter<'a> {
    cursor: Cursor<'a>,
    target_field: u32,
}

impl<'a> WireMessageIter<'a> {
    fn new(data: &'a [u8], target_field: u32) -> Self {
        Self {
            cursor: Cursor::new(data),
            target_field,
        }
    }

    pub fn empty() -> Self {
        Self {
            cursor: Cursor::new(&[]),
            target_field: 0,
        }
    }
}

impl<'a> Iterator for WireMessageIter<'a> {
    type Item = &'a [u8];

    #[inline]
    fn next(&mut self) -> Option<&'a [u8]> {
        loop {
            let (field, wire_type) = self.cursor.read_tag().ok()??;
            if field == self.target_field && wire_type == WIRE_LEN {
                return self.cursor.read_len_delimited().ok();
            }
            if self.cursor.skip_field(wire_type).is_err() {
                return None;
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Per-element wire types
// ---------------------------------------------------------------------------

#[derive(Clone, Debug)]
pub(crate) struct WireNode<'a> {
    pub id: i64,
    pub lat: i64,
    pub lon: i64,
    pub keys_data: &'a [u8],
    pub vals_data: &'a [u8],
    pub info_data: Option<&'a [u8]>,
}

impl<'a> WireNode<'a> {
    pub fn parse(data: &'a [u8]) -> Result<Self> {
        let mut cursor = Cursor::new(data);
        let mut id: i64 = 0;
        let mut lat: i64 = 0;
        let mut lon: i64 = 0;
        let mut keys_data: &[u8] = &[];
        let mut vals_data: &[u8] = &[];
        let mut info_data: Option<&[u8]> = None;

        while let Some((field, wire_type)) = cursor.read_tag()? {
            match (field, wire_type) {
                (1, WIRE_VARINT) => id = cursor.read_sint64()?,
                (2, WIRE_LEN) => keys_data = cursor.read_len_delimited()?,
                (3, WIRE_LEN) => vals_data = cursor.read_len_delimited()?,
                (4, WIRE_LEN) => info_data = Some(cursor.read_len_delimited()?),
                (8, WIRE_VARINT) => lat = cursor.read_sint64()?,
                (9, WIRE_VARINT) => lon = cursor.read_sint64()?,
                _ => cursor.skip_field(wire_type)?,
            }
        }

        Ok(Self {
            id,
            lat,
            lon,
            keys_data,
            vals_data,
            info_data,
        })
    }
}

#[derive(Clone, Debug)]
pub(crate) struct WireWay<'a> {
    pub id: i64,
    pub keys_data: &'a [u8],
    pub vals_data: &'a [u8],
    pub refs_data: &'a [u8],
    pub lat_data: &'a [u8],
    pub lon_data: &'a [u8],
    pub info_data: Option<&'a [u8]>,
    pub pins_data: Option<&'a [u8]>,
}

impl<'a> WireWay<'a> {
    pub fn parse(data: &'a [u8]) -> Result<Self> {
        let mut cursor = Cursor::new(data);
        let mut id: i64 = 0;
        let mut keys_data: &[u8] = &[];
        let mut vals_data: &[u8] = &[];
        let mut refs_data: &[u8] = &[];
        let mut lat_data: &[u8] = &[];
        let mut lon_data: &[u8] = &[];
        let mut info_data: Option<&[u8]> = None;
        let mut pins_data: Option<&[u8]> = None;

        while let Some((field, wire_type)) = cursor.read_tag()? {
            match (field, wire_type) {
                (1, WIRE_VARINT) => id = cursor.read_varint_i64()?,
                (2, WIRE_LEN) => keys_data = cursor.read_len_delimited()?,
                (3, WIRE_LEN) => vals_data = cursor.read_len_delimited()?,
                (4, WIRE_LEN) => info_data = Some(cursor.read_len_delimited()?),
                (8, WIRE_LEN) => refs_data = cursor.read_len_delimited()?,
                (9, WIRE_LEN) => lat_data = cursor.read_len_delimited()?,
                (10, WIRE_LEN) => lon_data = cursor.read_len_delimited()?,
                (20, WIRE_LEN) => pins_data = Some(cursor.read_len_delimited()?),
                _ => cursor.skip_field(wire_type)?,
            }
        }

        Ok(Self {
            id,
            keys_data,
            vals_data,
            refs_data,
            lat_data,
            lon_data,
            info_data,
            pins_data,
        })
    }
}

#[derive(Clone, Debug)]
pub(crate) struct WireRelation<'a> {
    pub id: i64,
    pub keys_data: &'a [u8],
    pub vals_data: &'a [u8],
    pub roles_sid_data: &'a [u8],
    pub memids_data: &'a [u8],
    pub types_data: &'a [u8],
    pub info_data: Option<&'a [u8]>,
}

impl<'a> WireRelation<'a> {
    pub fn parse(data: &'a [u8]) -> Result<Self> {
        let mut cursor = Cursor::new(data);
        let mut id: i64 = 0;
        let mut keys_data: &[u8] = &[];
        let mut vals_data: &[u8] = &[];
        let mut roles_sid_data: &[u8] = &[];
        let mut memids_data: &[u8] = &[];
        let mut types_data: &[u8] = &[];
        let mut info_data: Option<&[u8]> = None;

        while let Some((field, wire_type)) = cursor.read_tag()? {
            match (field, wire_type) {
                (1, WIRE_VARINT) => id = cursor.read_varint_i64()?,
                (2, WIRE_LEN) => keys_data = cursor.read_len_delimited()?,
                (3, WIRE_LEN) => vals_data = cursor.read_len_delimited()?,
                (4, WIRE_LEN) => info_data = Some(cursor.read_len_delimited()?),
                (8, WIRE_LEN) => roles_sid_data = cursor.read_len_delimited()?,
                (9, WIRE_LEN) => memids_data = cursor.read_len_delimited()?,
                (10, WIRE_LEN) => types_data = cursor.read_len_delimited()?,
                _ => cursor.skip_field(wire_type)?,
            }
        }

        Ok(Self {
            id,
            keys_data,
            vals_data,
            roles_sid_data,
            memids_data,
            types_data,
            info_data,
        })
    }
}

#[derive(Clone, Copy)]
pub(crate) struct WireDenseNodes<'a> {
    pub id_data: &'a [u8],
    pub lat_data: &'a [u8],
    pub lon_data: &'a [u8],
    pub keys_vals_data: &'a [u8],
    pub info_data: Option<&'a [u8]>,
}

impl<'a> WireDenseNodes<'a> {
    pub fn parse(data: &'a [u8]) -> Result<Self> {
        let mut cursor = Cursor::new(data);
        let mut id_data: &[u8] = &[];
        let mut lat_data: &[u8] = &[];
        let mut lon_data: &[u8] = &[];
        let mut keys_vals_data: &[u8] = &[];
        let mut info_data: Option<&[u8]> = None;

        while let Some((field, wire_type)) = cursor.read_tag()? {
            match (field, wire_type) {
                (1, WIRE_LEN) => id_data = cursor.read_len_delimited()?,
                (5, WIRE_LEN) => info_data = Some(cursor.read_len_delimited()?),
                (8, WIRE_LEN) => lat_data = cursor.read_len_delimited()?,
                (9, WIRE_LEN) => lon_data = cursor.read_len_delimited()?,
                (10, WIRE_LEN) => keys_vals_data = cursor.read_len_delimited()?,
                _ => cursor.skip_field(wire_type)?,
            }
        }

        Ok(Self {
            id_data,
            lat_data,
            lon_data,
            keys_vals_data,
            info_data,
        })
    }
}

#[derive(Clone, Copy)]
pub(crate) struct WireDenseInfo<'a> {
    pub version_data: &'a [u8],
    pub timestamp_data: &'a [u8],
    pub changeset_data: &'a [u8],
    pub uid_data: &'a [u8],
    pub user_sid_data: &'a [u8],
    pub visible_data: &'a [u8],
}

impl<'a> WireDenseInfo<'a> {
    pub fn parse(data: &'a [u8]) -> Result<Self> {
        let mut cursor = Cursor::new(data);
        let mut version_data: &[u8] = &[];
        let mut timestamp_data: &[u8] = &[];
        let mut changeset_data: &[u8] = &[];
        let mut uid_data: &[u8] = &[];
        let mut user_sid_data: &[u8] = &[];
        let mut visible_data: &[u8] = &[];

        while let Some((field, wire_type)) = cursor.read_tag()? {
            match (field, wire_type) {
                (1, WIRE_LEN) => version_data = cursor.read_len_delimited()?,
                (2, WIRE_LEN) => timestamp_data = cursor.read_len_delimited()?,
                (3, WIRE_LEN) => changeset_data = cursor.read_len_delimited()?,
                (4, WIRE_LEN) => uid_data = cursor.read_len_delimited()?,
                (5, WIRE_LEN) => user_sid_data = cursor.read_len_delimited()?,
                (6, WIRE_LEN) => visible_data = cursor.read_len_delimited()?,
                _ => cursor.skip_field(wire_type)?,
            }
        }

        Ok(Self {
            version_data,
            timestamp_data,
            changeset_data,
            uid_data,
            user_sid_data,
            visible_data,
        })
    }
}

/// Parsed Info sub-message - all scalars, no byte references needed.
#[derive(Clone, Debug, Default)]
pub(crate) struct WireInfo {
    pub version: Option<i32>,
    pub timestamp: Option<i64>,
    pub changeset: Option<i64>,
    pub uid: Option<i32>,
    pub user_sid: Option<i32>,
    pub visible: Option<bool>,
}

impl WireInfo {
    pub fn parse(data: &[u8]) -> Result<Self> {
        let mut cursor = Cursor::new(data);
        let mut info = Self::default();

        while let Some((field, wire_type)) = cursor.read_tag()? {
            match (field, wire_type) {
                #[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
                (1, WIRE_VARINT) => info.version = Some(cursor.read_varint()? as i32),
                (2, WIRE_VARINT) => info.timestamp = Some(cursor.read_varint_i64()?),
                (3, WIRE_VARINT) => info.changeset = Some(cursor.read_varint_i64()?),
                #[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
                (4, WIRE_VARINT) => info.uid = Some(cursor.read_varint()? as i32),
                #[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
                (5, WIRE_VARINT) => info.user_sid = Some(cursor.read_varint()? as i32),
                (6, WIRE_VARINT) => info.visible = Some(cursor.read_varint()? != 0),
                _ => cursor.skip_field(wire_type)?,
            }
        }

        // Osmosis writes -1 for version and changeset when metadata is absent
        // (protobuf default is 0). Map these sentinels to None so downstream
        // code treats them as genuinely absent rather than real values.
        if info.version == Some(-1) {
            info.version = None;
        }
        if info.changeset == Some(-1) {
            info.changeset = None;
        }

        Ok(info)
    }
}

// ---------------------------------------------------------------------------
// Unit tests - OSM-specific parsers only. Primitive tests live in protohoggr.
// ---------------------------------------------------------------------------

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

    #[test]
    fn wire_info_parse() {
        let data = [
            0x08, 0x05, // field 1, varint, value=5
            0x20, 0x2A, // field 4, varint, value=42
            0x30, 0x01, // field 6, varint, value=1
        ];
        let info = WireInfo::parse(&data).unwrap();
        assert_eq!(info.version, Some(5));
        assert_eq!(info.uid, Some(42));
        assert_eq!(info.visible, Some(true));
        assert_eq!(info.timestamp, None);
        assert_eq!(info.changeset, None);
        assert_eq!(info.user_sid, None);
    }

    #[test]
    fn wire_way_parses_field_20_pins() {
        // field 1 (id) varint = 1; field 8 (refs) len=1 [0x00];
        // field 20 (pins) tag = (20<<3)|2 = 162 -> varint [0xA2, 0x01], len=1 [0xFF]
        let with_pins = [
            0x08, 0x01, // field 1: id = 1
            0x42, 0x01, 0x00, // field 8: refs = [0x00]
            0xA2, 0x01, 0x01, 0xFF, // field 20: pins = [0xFF]
        ];
        let way = WireWay::parse(&with_pins).unwrap();
        assert_eq!(way.pins_data, Some([0xFFu8].as_slice()));

        let without_pins = [0x08, 0x01, 0x42, 0x01, 0x00];
        let way = WireWay::parse(&without_pins).unwrap();
        assert_eq!(way.pins_data, None);
    }
}