Skip to main content

martin_core/tiles/
declared_grid.rs

1//! A source served on a tile grid the config declares for it, since stored archives cannot say so themselves.
2
3use std::sync::Arc;
4
5use martin_tile_utils::{TileCoord, TileData, TileGrid, TileInfo};
6use tilejson::TileJSON;
7
8use crate::CacheZoomRange;
9use crate::tiles::{AnySource, BackendSource, BoxedSource, MartinCoreResult, Source, Tile, UrlQuery};
10
11/// A source whose `z/x/y` addresses are declared to be on `grid`.
12///
13/// Every request passes straight through to the wrapped source.
14/// The `TileJSON` gains the `tileGrid` key and the catalog entry names the grid.
15#[derive(Debug)]
16pub struct DeclaredGridSource {
17    inner: BackendSource,
18    grid: TileGrid,
19    tilejson: TileJSON,
20}
21
22impl DeclaredGridSource {
23    /// Declares that `inner` serves its tiles on `grid`.
24    #[must_use]
25    pub fn new(inner: BackendSource, grid: TileGrid) -> Self {
26        let mut tilejson = inner.get_tilejson().clone();
27        tilejson.other.insert(
28            "tileGrid".to_owned(),
29            serde_json::to_value(&grid).expect("a validated tile grid holds only finite numbers and strings"),
30        );
31        Self { inner, grid, tilejson }
32    }
33
34    /// The source the grid was declared for.
35    #[must_use]
36    pub fn inner(&self) -> &BackendSource {
37        &self.inner
38    }
39
40    /// This source as a shared handle, ready for a registry.
41    #[must_use]
42    pub fn boxed(self) -> BoxedSource {
43        Arc::new(AnySource::DeclaredGrid(self))
44    }
45}
46
47impl Source for DeclaredGridSource {
48    fn get_id(&self) -> &str {
49        self.inner.get_id()
50    }
51
52    fn get_tilejson(&self) -> &TileJSON {
53        &self.tilejson
54    }
55
56    fn get_tile_info(&self) -> TileInfo {
57        self.inner.get_tile_info()
58    }
59
60    fn tile_grid(&self) -> &TileGrid {
61        &self.grid
62    }
63
64    fn get_version(&self) -> Option<String> {
65        self.inner.get_version()
66    }
67
68    fn support_url_query(&self) -> bool {
69        self.inner.support_url_query()
70    }
71
72    fn benefits_from_concurrent_scraping(&self) -> bool {
73        self.inner.benefits_from_concurrent_scraping()
74    }
75
76    fn empty_tile_implies_empty_children(&self) -> bool {
77        self.inner.empty_tile_implies_empty_children()
78    }
79
80    fn cache_zoom(&self) -> CacheZoomRange {
81        self.inner.cache_zoom()
82    }
83
84    async fn get_tile(&self, xyz: TileCoord, url_query: Option<&UrlQuery>) -> MartinCoreResult<TileData> {
85        self.inner.get_tile(xyz, url_query).await
86    }
87
88    async fn get_tile_with_etag(&self, xyz: TileCoord, url_query: Option<&UrlQuery>) -> MartinCoreResult<Tile> {
89        self.inner.get_tile_with_etag(xyz, url_query).await
90    }
91
92    async fn try_reload(&self) -> MartinCoreResult<Self> {
93        let inner = self.inner.try_reload().await?;
94        Ok(Self::new(inner, self.grid.clone()))
95    }
96}