galileo 0.2.1

Cross-platform general purpose map rendering engine
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
use std::ops::{Index, IndexMut, RangeBounds};

use crate::layer::Layer;

/// Collection of layers with some meta-information.
///
/// When a map is rendered, it draws all visible layers in the order they are stored in the
/// collection. Any layer can be temporary hidden with the [`LayerCollection::hide`] or
/// [`LayerCollection::show_by`] methods. These layers will be ignored by the renderer, but
/// retain their place in the collection.
///
/// Since a map should be able to render anything implementing the [`Layer`] trait, this
/// collection stores layers as trait objects. You can use downcasting through `Any` trait
/// to obtain a concrete layer type you work with.
///
/// ```no_run
/// use galileo::layer::{RasterTileLayer, VectorTileLayer};
/// use galileo::layer::raster_tile_layer::RasterTileLayerBuilder;
/// use galileo::layer::vector_tile_layer::VectorTileLayerBuilder;
/// use galileo::LayerCollection;
/// use galileo::tile_schema::TileIndex;
///
/// let raster_tiles = RasterTileLayerBuilder::new_osm().build()?;
/// let vector_tiles = VectorTileLayerBuilder::new_rest(|_| unimplemented!()).build()?;
///
/// let mut collection = LayerCollection::default();
/// collection.push(raster_tiles);
/// collection.push(vector_tiles);
///
/// assert!(collection.get_typed::<VectorTileLayer>(1).is_some());
/// # Ok::<(), galileo::error::GalileoError>(())
/// ```
#[derive(Default)]
pub struct LayerCollection(Vec<LayerEntry>);

struct LayerEntry {
    layer: Box<dyn Layer>,
    is_hidden: bool,
}

impl LayerCollection {
    /// Shortens the collection, keeping the first `length` layers and dropping the rest. If
    /// the length of the collection is less than `length` does nothing.
    ///
    /// # Examples
    ///
    /// ```
    /// use galileo::LayerCollection;
    /// use galileo::layer::TestLayer;
    ///
    /// let mut collection = LayerCollection::from(vec![
    ///     TestLayer("Layer A"),
    ///     TestLayer("Layer B"),
    /// ]);
    ///
    /// collection.truncate(3);
    /// assert_eq!(collection.len(), 2);
    /// collection.truncate(1);
    /// assert_eq!(collection.len(), 1);
    /// assert_eq!(collection[0].as_any().downcast_ref(), Some(&TestLayer("Layer A")));
    /// ```
    pub fn truncate(&mut self, length: usize) {
        self.0.truncate(length)
    }

    /// Removes all layers from the collection.
    ///
    /// # Examples
    ///
    /// ```
    /// use galileo::LayerCollection;
    /// use galileo::layer::TestLayer;
    ///
    /// let mut collection = LayerCollection::from(vec![
    ///     TestLayer("Layer A"),
    ///     TestLayer("Layer B"),
    /// ]);
    ///
    /// collection.clear();
    /// assert_eq!(collection.len(), 0);
    /// ```
    pub fn clear(&mut self) {
        self.0.clear()
    }

    /// Removes a layer from the collection and returns it. The removed element is replaced by the
    /// last layer in the collection.
    ///
    /// # Panics
    ///
    /// Panics if `index` equals or greater then collection length.
    ///
    /// # Examples
    ///
    /// ```
    /// use galileo::LayerCollection;
    /// use galileo::layer::TestLayer;
    ///
    /// let mut collection = LayerCollection::from(vec![
    ///     TestLayer("Layer A"),
    ///     TestLayer("Layer B"),
    ///     TestLayer("Layer C"),
    /// ]);
    ///
    /// let removed = collection.swap_remove(0);
    /// assert_eq!(removed.as_any().downcast_ref(), Some(&TestLayer("Layer A")));
    /// assert_eq!(collection[0].as_any().downcast_ref(), Some(&TestLayer("Layer C")));
    /// ```
    pub fn swap_remove(&mut self, index: usize) -> Box<dyn Layer> {
        self.0.swap_remove(index).layer
    }

    /// Inserts a layer at position `index`, shifting all layers after it to the right.
    ///
    /// # Panics
    ///
    /// Panics if `index > len`
    ///
    /// # Examples
    ///
    /// ```
    /// use galileo::LayerCollection;
    /// use galileo::layer::TestLayer;
    ///
    /// let mut collection = LayerCollection::from(vec![
    ///     TestLayer("Layer A"),
    ///     TestLayer("Layer B"),
    /// ]);
    ///
    /// collection.insert(1, TestLayer("Layer C"));
    /// assert_eq!(collection.len(), 3);
    /// assert_eq!(collection[1].as_any().downcast_ref(), Some(&TestLayer("Layer C")));
    /// assert_eq!(collection[2].as_any().downcast_ref(), Some(&TestLayer("Layer B")));
    pub fn insert(&mut self, index: usize, layer: impl Layer + 'static) {
        self.0.insert(index, layer.into());
    }

    /// Removes a layer at `index`, shifting all layers after it to the left and returning the
    /// removed layer.
    ///
    /// # Panics
    ///
    /// Panics if `index` is out of bounds.
    ///
    /// # Examples
    ///
    /// ```
    /// use galileo::LayerCollection;
    /// use galileo::layer::TestLayer;
    ///
    /// let mut collection = LayerCollection::from(vec![
    ///     TestLayer("Layer A"),
    ///     TestLayer("Layer B"),
    ///     TestLayer("Layer C"),
    /// ]);
    ///
    /// let removed = collection.remove(1);
    /// assert_eq!(removed.as_any().downcast_ref(), Some(&TestLayer("Layer B")));
    /// assert_eq!(collection.len(), 2);
    /// assert_eq!(collection[1].as_any().downcast_ref(), Some(&TestLayer("Layer C")));
    /// ```
    pub fn remove(&mut self, index: usize) -> Box<dyn Layer> {
        self.0.remove(index).layer
    }

    /// Retains only the layers specified by the predicate. In other words, remove all layers `l`
    /// for which f(&l) returns false.
    ///
    /// # Examples
    ///
    /// ```
    /// use galileo::LayerCollection;
    /// use galileo::layer::TestLayer;
    ///
    /// let mut collection = LayerCollection::from(vec![
    ///     TestLayer("Layer A"),
    ///     TestLayer("Layer B"),
    ///     TestLayer("Layer C"),
    /// ]);
    ///
    /// collection.retain(|layer| !layer.as_any().downcast_ref::<TestLayer>().is_some_and(|l| l.0.ends_with("A")));
    ///
    /// assert_eq!(collection.len(), 2);
    /// assert_eq!(collection[0].as_any().downcast_ref(), Some(&TestLayer("Layer B")));
    /// assert_eq!(collection[1].as_any().downcast_ref(), Some(&TestLayer("Layer C")));
    /// ```
    pub fn retain<F>(&mut self, mut f: F)
    where
        F: FnMut(&dyn Layer) -> bool,
    {
        self.0.retain(|entry| f(&*entry.layer))
    }

    /// Adds the layer to the end of the collection.
    ///
    /// # Examples
    ///
    /// ```
    /// use galileo::LayerCollection;
    /// use galileo::layer::TestLayer;
    ///
    /// let mut collection = LayerCollection::from(vec![
    ///     TestLayer("Layer A"),
    ///     TestLayer("Layer B"),
    /// ]);
    ///
    /// collection.push(TestLayer("Layer C"));
    ///
    /// assert_eq!(collection.len(), 3);
    /// assert_eq!(collection[2].as_any().downcast_ref(), Some(&TestLayer("Layer C")));
    /// ```
    pub fn push(&mut self, layer: impl Layer + 'static) {
        self.0.push(layer.into())
    }

    /// Removes the last layer from the collection and returns it. Returns `None` if the collection
    /// is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use galileo::LayerCollection;
    /// use galileo::layer::TestLayer;
    ///
    /// let mut collection = LayerCollection::from(vec![
    ///     TestLayer("Layer A"),
    ///     TestLayer("Layer B"),
    ///     TestLayer("Layer C"),
    /// ]);
    ///
    /// let removed = collection.pop();
    ///
    /// assert_eq!(collection.len(), 2);
    /// assert_eq!(removed.unwrap().as_any().downcast_ref(), Some(&TestLayer("Layer C")));
    /// ```
    pub fn pop(&mut self) -> Option<Box<dyn Layer>> {
        self.0.pop().map(|entry| entry.layer)
    }

    /// Removes the specified range of layers from the collection in bulk, returning all removed
    /// layers in an iterator. If the iterator is dropped before being fully consumed, it drops
    /// the remaining removed layers.
    ///
    /// # Panics
    ///
    /// Panics if the starting point is greater than the end point and if the end point is
    /// greater that the length of the collection.
    ///
    /// # Examples
    ///
    /// ```
    /// use galileo::LayerCollection;
    /// use galileo::layer::TestLayer;
    ///
    /// let mut collection = LayerCollection::from(vec![
    ///     TestLayer("Layer A"),
    ///     TestLayer("Layer B"),
    ///     TestLayer("Layer C"),
    /// ]);
    ///
    /// let drained: Vec<_> = collection.drain(0..2).collect();
    /// assert_eq!(drained.len(), 2);
    /// assert_eq!(drained[1].as_any().downcast_ref(), Some(&TestLayer("Layer B")));
    ///
    /// assert_eq!(collection.len(), 1);
    /// assert_eq!(collection[0].as_any().downcast_ref(), Some(&TestLayer("Layer C")));
    /// ```
    pub fn drain<R>(&mut self, range: R) -> impl Iterator<Item = Box<dyn Layer>> + '_
    where
        R: RangeBounds<usize>,
    {
        self.0.drain(range).map(|entry| entry.layer)
    }

    /// Returns the count of layers in the collection.
    ///
    /// # Examples
    ///
    /// ```
    /// use galileo::LayerCollection;
    /// use galileo::layer::TestLayer;
    ///
    /// let collection = LayerCollection::from(vec![
    ///     TestLayer("Layer A"),
    ///     TestLayer("Layer B"),
    /// ]);
    ///
    /// assert_eq!(collection.len(), 2);
    /// ```
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Returns `true` if the collection contains zero layers.
    ///
    /// # Examples
    ///
    /// ```
    /// use galileo::LayerCollection;
    /// use galileo::layer::TestLayer;
    ///
    /// let mut collection = LayerCollection::default();
    /// assert!(collection.is_empty());
    ///
    /// collection.push(TestLayer("Layer A"));
    /// assert!(!collection.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Returns a layer at `index`, or `None` if index is out of bounds.
    ///
    /// # Examples
    ///
    /// ```
    /// use galileo::LayerCollection;
    /// use galileo::layer::TestLayer;
    ///
    /// let collection = LayerCollection::from(vec![
    ///     TestLayer("Layer A"),
    ///     TestLayer("Layer B"),
    /// ]);
    ///
    /// assert_eq!(collection.get(1).and_then(|layer| layer.as_any().downcast_ref()), Some(&TestLayer("Layer B")));
    /// assert!(collection.get(2).is_none());
    /// ```
    pub fn get(&self, index: usize) -> Option<&dyn Layer> {
        self.0.get(index).map(|entry| &*entry.layer)
    }

    /// Returns a mutable reference to a layer at `index`, or `None` if index is out of bounds.
    ///
    /// # Examples
    ///
    /// ```
    /// use galileo::LayerCollection;
    /// use galileo::layer::TestLayer;
    ///
    /// let mut collection = LayerCollection::from(vec![
    ///     TestLayer("Layer A"),
    ///     TestLayer("Layer B"),
    /// ]);
    ///
    /// assert_eq!(collection.get_mut(1).and_then(|layer| layer.as_any_mut().downcast_ref()), Some(&TestLayer("Layer B")));
    /// assert!(collection.get(2).is_none());
    /// ```
    pub fn get_mut(&mut self, index: usize) -> Option<&mut Box<dyn Layer>> {
        self.0.get_mut(index).map(|entry| &mut entry.layer)
    }

    /// Swaps two layers in the collection.
    ///
    /// # Panics
    ///
    /// Panics if `a` or `b` are out of bounds.
    ///
    /// # Examples
    ///
    /// ```
    /// use galileo::LayerCollection;
    /// use galileo::layer::TestLayer;
    ///
    /// let mut collection = LayerCollection::from(vec![
    ///     TestLayer("Layer A"),
    ///     TestLayer("Layer B"),
    ///     TestLayer("Layer C"),
    /// ]);
    ///
    /// collection.swap(1, 2);
    ///
    /// assert_eq!(collection[1].as_any().downcast_ref(), Some(&TestLayer("Layer C")));
    /// assert_eq!(collection[2].as_any().downcast_ref(), Some(&TestLayer("Layer B")));
    /// ```
    pub fn swap(&mut self, a: usize, b: usize) {
        self.0.swap(a, b)
    }

    /// Iterates over all layers in the collection.
    ///
    /// ```
    /// use galileo::LayerCollection;
    /// use galileo::layer::TestLayer;
    ///
    /// let collection = LayerCollection::from(vec![
    ///     TestLayer("Layer A"),
    ///     TestLayer("Layer B"),
    /// ]);
    ///
    /// let mut iterator = collection.iter();
    /// assert_eq!(iterator.next().and_then(|layer| layer.as_any().downcast_ref()), Some(&TestLayer("Layer A")));
    /// assert_eq!(iterator.next().and_then(|layer| layer.as_any().downcast_ref()), Some(&TestLayer("Layer B")));
    /// assert!(iterator.next().is_none());
    /// ```
    pub fn iter(&self) -> impl Iterator<Item = &dyn Layer> + '_ {
        self.0.iter().map(|entry| &*entry.layer)
    }

    /// Iterates over mutable references to all layers in the collection.
    ///
    /// ```
    /// use galileo::LayerCollection;
    /// use galileo::layer::TestLayer;
    ///
    /// let mut collection = LayerCollection::from(vec![
    ///     TestLayer("Layer A"),
    ///     TestLayer("Layer B"),
    /// ]);
    ///
    /// let mut iterator = collection.iter_mut();
    /// assert_eq!(iterator.next().and_then(|layer| layer.as_any_mut().downcast_ref()), Some(&TestLayer("Layer A")));
    /// assert_eq!(iterator.next().and_then(|layer| layer.as_any_mut().downcast_ref()), Some(&TestLayer("Layer B")));
    /// assert!(iterator.next().is_none());
    /// ```
    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Box<dyn Layer>> + '_ {
        self.0.iter_mut().map(|entry| &mut entry.layer)
    }

    /// Sets the layer at `index` as invisible. The hidden layer can be later shown with
    /// [`LayerCollection::show`].
    ///
    /// Hidden layers are stored in the layer collection, but are not rendered to a map.
    ///
    /// # Panics
    ///
    /// Panics if `index` is out of bounds.
    ///
    /// # Examples
    ///
    /// ```
    /// use galileo::LayerCollection;
    /// use galileo::layer::TestLayer;
    ///
    /// let mut collection = LayerCollection::from(vec![
    ///     TestLayer("Layer A"),
    ///     TestLayer("Layer B"),
    /// ]);
    ///
    /// collection.hide(1);
    /// assert!(!collection.is_visible(1));
    /// ```
    pub fn hide(&mut self, index: usize) {
        self.0[index].is_hidden = true;
    }

    /// Sets the layer at `index` as visible.
    ///
    /// Hidden layers are stored in the layer collection, but are not rendered to a map.
    ///
    /// # Panics
    ///
    /// Panics if `index` is out of bounds.
    ///
    /// # Examples
    ///
    /// ```
    /// use galileo::LayerCollection;
    /// use galileo::layer::TestLayer;
    ///
    /// let mut collection = LayerCollection::from(vec![
    ///     TestLayer("Layer A"),
    ///     TestLayer("Layer B"),
    /// ]);
    ///
    /// collection.hide(1);
    /// collection.show(1);
    /// assert!(collection.is_visible(1));
    /// ```
    pub fn show(&mut self, index: usize) {
        self.0[index].is_hidden = false;
    }

    /// Sets all layers for which the predicate returns true as visible. The rest of layers are set
    /// as hidden.
    ///
    /// # Examples
    ///
    /// ```
    /// use galileo::LayerCollection;
    /// use galileo::layer::TestLayer;
    ///
    /// let mut collection = LayerCollection::from(vec![
    ///     TestLayer("Layer A"),
    ///     TestLayer("Layer B"),
    ///     TestLayer("Layer C"),
    /// ]);
    ///
    /// collection.show_by(|layer| layer.as_any().downcast_ref::<TestLayer>().unwrap().0.ends_with("B"));
    ///
    /// assert!(!collection.is_visible(0));
    /// assert!(collection.is_visible(1));
    /// assert!(!collection.is_visible(2));
    pub fn show_by<F>(&mut self, mut f: F)
    where
        F: FnMut(&dyn Layer) -> bool,
    {
        for entry in &mut self.0 {
            entry.is_hidden = !f(&*entry.layer);
        }
    }

    /// Returns true, if the layer at `index` is not hidden.
    ///
    /// Hidden layers are stored in the layer collection, but are not rendered to a map.
    ///
    /// # Panics
    ///
    /// Panics if `index` is out of bounds.
    ///
    /// # Examples
    ///
    /// ```
    /// use galileo::LayerCollection;
    /// use galileo::layer::TestLayer;
    ///
    /// let mut collection = LayerCollection::from(vec![
    ///     TestLayer("Layer A"),
    ///     TestLayer("Layer B"),
    /// ]);
    ///
    /// assert!(collection.is_visible(1));
    /// collection.hide(1);
    /// assert!(!collection.is_visible(1));
    /// collection.show(1);
    /// assert!(collection.is_visible(1));
    /// ```
    pub fn is_visible(&self, index: usize) -> bool {
        !self.0[index].is_hidden
    }

    /// Iterates over all visible layers in the collection.
    ///
    /// # Examples
    ///
    /// ```
    /// use galileo::LayerCollection;
    /// use galileo::layer::TestLayer;
    ///
    /// let mut collection = LayerCollection::from(vec![
    ///     TestLayer("Layer A"),
    ///     TestLayer("Layer B"),
    ///     TestLayer("Layer C"),
    /// ]);
    ///
    /// collection.hide(1);
    ///
    /// let mut iterator = collection.iter_visible();
    /// assert_eq!(iterator.next().and_then(|layer| layer.as_any().downcast_ref()), Some(&TestLayer("Layer A")));
    /// assert_eq!(iterator.next().and_then(|layer| layer.as_any().downcast_ref()), Some(&TestLayer("Layer C")));
    /// assert!(iterator.next().is_none());
    /// ```
    pub fn iter_visible(&self) -> impl Iterator<Item = &dyn Layer> + '_ {
        self.0
            .iter()
            .filter(|entry| !entry.is_hidden)
            .map(|entry| &*entry.layer)
    }

    /// Returns the layer converted to its original type if it was `T`.
    pub fn get_typed<T: Layer + 'static>(&self, index: usize) -> Option<&T> {
        self.0
            .get(index)
            .and_then(|layer| layer.layer.as_any().downcast_ref::<T>())
    }
}

impl Index<usize> for LayerCollection {
    type Output = dyn Layer;

    fn index(&self, index: usize) -> &Self::Output {
        &*self.0[index].layer
    }
}

impl IndexMut<usize> for LayerCollection {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut *self.0[index].layer
    }
}

impl<L: Into<LayerEntry>, T: IntoIterator<Item = L>> From<T> for LayerCollection {
    fn from(value: T) -> Self {
        Self(value.into_iter().map(|layer| layer.into()).collect())
    }
}

impl<T: Layer + 'static> From<T> for LayerEntry {
    fn from(value: T) -> Self {
        Self {
            layer: Box::new(value),
            is_hidden: false,
        }
    }
}

impl From<Box<dyn Layer>> for LayerEntry {
    fn from(value: Box<dyn Layer>) -> Self {
        Self {
            layer: value,
            is_hidden: false,
        }
    }
}