• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Open-S2 / open-vector-tile / #49

17 Feb 2025 08:39PM UTC coverage: 40.419% (-58.3%) from 98.718%
#49

push

Mr Martian
setup a wasm build for experimentation; main rust didnt have no_std (bug)

18 of 18 new or added lines in 3 files covered. (100.0%)

2956 existing lines in 15 files now uncovered.

2082 of 5151 relevant lines covered (40.42%)

107.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/rust/mapbox/vector_layer.rs
1
use crate::{
2
    base::BaseVectorLayer,
3
    mapbox::{write_feature, MapboxVectorFeature, Value},
4
    VectorFeatureMethods, VectorLayerMethods,
5
};
6

7
use pbf::{ProtoRead, Protobuf};
8

9
use core::cell::RefCell;
10

11
use alloc::collections::btree_map::Entry;
12
use alloc::collections::BTreeMap;
13
use alloc::rc::Rc;
14
use alloc::string::String;
15
use alloc::vec::Vec;
16

17
/// Mapbox specification for a Layer
18
#[derive(Debug)]
19
pub struct MapboxVectorLayer {
20
    /// the version of the vector tile layer.
21
    pub version: u16,
22
    /// the name of the layer
23
    pub name: String,
24
    /// the extent of the vector layer
25
    pub extent: usize,
26
    /// the features in the layer
27
    pub features: BTreeMap<usize, MapboxVectorFeature>,
28
    /// track the positions of the features
29
    pub feature_positions: Vec<usize>,
30
    /// whether or not the layer is an s2 layer. This is an extension to the Mapbox spec and not used
31
    /// in production by most tools
32
    is_s2: bool,
33
    /// a reference to the pbf
34
    pbf: Rc<RefCell<Protobuf>>,
35
    /// key store used by features
36
    keys: Rc<RefCell<Vec<String>>>,
37
    /// value store used by features
38
    values: Rc<RefCell<Vec<Value>>>,
39
}
40
impl MapboxVectorLayer {
41
    /// Create a new MapboxVectorLayer
UNCOV
42
    pub fn new(pbf: Rc<RefCell<Protobuf>>, is_s2: bool) -> MapboxVectorLayer {
×
UNCOV
43
        MapboxVectorLayer {
×
UNCOV
44
            version: 5,
×
UNCOV
45
            name: String::new(),
×
UNCOV
46
            extent: 4_096,
×
UNCOV
47
            is_s2,
×
UNCOV
48
            pbf: pbf.clone(),
×
UNCOV
49
            keys: Rc::new(RefCell::new(Vec::new())),
×
UNCOV
50
            values: Rc::new(RefCell::new(Vec::new())),
×
UNCOV
51
            features: BTreeMap::new(),
×
UNCOV
52
            feature_positions: Vec::new(),
×
UNCOV
53
        }
×
UNCOV
54
    }
×
55
}
56
impl VectorLayerMethods for MapboxVectorLayer {
UNCOV
57
    fn version(&self) -> u16 {
×
UNCOV
58
        self.version
×
UNCOV
59
    }
×
60

UNCOV
61
    fn name(&self) -> String {
×
UNCOV
62
        self.name.clone()
×
UNCOV
63
    }
×
64

UNCOV
65
    fn extent(&self) -> usize {
×
UNCOV
66
        self.extent
×
UNCOV
67
    }
×
68

69
    /// the number of features in the layer
UNCOV
70
    fn len(&self) -> usize {
×
UNCOV
71
        self.feature_positions.len()
×
UNCOV
72
    }
×
73

74
    /// Check if the layer is empty
UNCOV
75
    fn is_empty(&self) -> bool {
×
UNCOV
76
        self.feature_positions.is_empty()
×
UNCOV
77
    }
×
78

UNCOV
79
    fn feature(&mut self, i: usize) -> Option<&mut dyn VectorFeatureMethods> {
×
80
        // First check if self.features already has the feature
UNCOV
81
        if let Entry::Vacant(e) = self.features.entry(i) {
×
82
            // Read the feature
UNCOV
83
            let mut feature = MapboxVectorFeature::new(
×
UNCOV
84
                self.pbf.clone(),
×
UNCOV
85
                self.is_s2,
×
UNCOV
86
                self.extent,
×
UNCOV
87
                self.version,
×
UNCOV
88
                self.keys.clone(),
×
UNCOV
89
                self.values.clone(),
×
UNCOV
90
            );
×
UNCOV
91
            let mut pbf = self.pbf.borrow_mut();
×
UNCOV
92
            pbf.set_pos(self.feature_positions[i]);
×
UNCOV
93
            pbf.read_message(&mut feature);
×
UNCOV
94
            e.insert(feature);
×
UNCOV
95

×
UNCOV
96
            // Now safely retrieve the inserted feature
×
UNCOV
97
            Some(self.features.get_mut(&i).unwrap() as &mut dyn VectorFeatureMethods)
×
98
        } else {
99
            // Safe to unwrap since we just checked the key exists
100
            Some(self.features.get_mut(&i).unwrap() as &mut dyn VectorFeatureMethods)
×
101
        }
UNCOV
102
    }
×
103
}
104
impl ProtoRead for MapboxVectorLayer {
UNCOV
105
    fn read(&mut self, tag: u64, pb: &mut Protobuf) {
×
UNCOV
106
        match tag {
×
UNCOV
107
            15 => self.version = pb.read_varint::<u16>(),
×
UNCOV
108
            1 => self.name = pb.read_string(),
×
UNCOV
109
            2 => self.feature_positions.push(pb.get_pos()),
×
UNCOV
110
            3 => {
×
UNCOV
111
                self.keys.borrow_mut().push(pb.read_string());
×
UNCOV
112
            }
×
UNCOV
113
            4 => {
×
UNCOV
114
                let mut value = Value::Null;
×
UNCOV
115
                pb.read_message(&mut value);
×
UNCOV
116
                self.values.borrow_mut().push(value);
×
UNCOV
117
            }
×
UNCOV
118
            5 => self.extent = pb.read_varint::<usize>(),
×
119
            #[tarpaulin::skip]
120
            _ => panic!("Unknown layer type"),
×
121
        }
UNCOV
122
    }
×
123
}
124

125
/// Write a layer to a protobuffer using the S2 Specification
UNCOV
126
pub fn write_layer(layer: &BaseVectorLayer, mapbox_support: bool) -> Vec<u8> {
×
UNCOV
127
    let mut pbf = Protobuf::new();
×
UNCOV
128
    let mut keys: BTreeMap<String, usize> = BTreeMap::new();
×
UNCOV
129
    let mut values: BTreeMap<Value, usize> = BTreeMap::new();
×
UNCOV
130

×
UNCOV
131
    pbf.write_varint_field(15, if mapbox_support { 1 } else { 5 });
×
UNCOV
132
    pbf.write_string_field(1, &layer.name);
×
UNCOV
133
    for feature in layer.features.iter() {
×
UNCOV
134
        pbf.write_bytes_field(2, &write_feature(feature, &mut keys, &mut values, mapbox_support));
×
UNCOV
135
    }
×
UNCOV
136
    let mut keys: Vec<(String, usize)> = keys.into_iter().collect();
×
UNCOV
137
    keys.sort_by(|a, b| a.1.cmp(&b.1));
×
138
    // keys and values
UNCOV
139
    for (key, _) in keys.iter() {
×
UNCOV
140
        pbf.write_string_field(3, key);
×
UNCOV
141
    }
×
UNCOV
142
    let mut values: Vec<(Value, usize)> = values.into_iter().collect();
×
UNCOV
143
    values.sort_by(|a, b| a.1.cmp(&b.1));
×
UNCOV
144
    for (value, _) in values.iter() {
×
UNCOV
145
        pbf.write_message(4, value);
×
UNCOV
146
    }
×
UNCOV
147
    pbf.write_varint_field(5, layer.extent as usize);
×
UNCOV
148

×
UNCOV
149
    pbf.take()
×
UNCOV
150
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc