use std::sync::Arc;
use martin_tile_utils::{TileCoord, TileData, TileGrid, TileInfo};
use tilejson::TileJSON;
use crate::CacheZoomRange;
use crate::tiles::catalog::CatalogSourceEntry;
#[cfg(feature = "cog")]
use crate::tiles::cog::CogSource;
use crate::tiles::declared_grid::DeclaredGridSource;
#[cfg(feature = "unstable-duckdb")]
use crate::tiles::duckdb::DuckDBSource;
#[cfg(feature = "processing")]
use crate::tiles::geojson::source::GeoJsonSource;
#[cfg(feature = "mbtiles")]
use crate::tiles::mbtiles::MbtSource;
#[cfg(feature = "passthrough")]
use crate::tiles::passthrough::PassthroughSource;
#[cfg(feature = "pmtiles")]
use crate::tiles::pmtiles::PmtilesSource;
#[cfg(feature = "postgres")]
use crate::tiles::postgres::{ActiveQueryRegistry, PostgresSource, PostgresTileFeatures};
#[cfg(feature = "_testing")]
use crate::tiles::testing::TestSource;
use crate::tiles::{BoxedSource, MartinCoreResult, Source as _, Tile, UrlQuery};
#[derive(Debug)]
#[expect(
clippy::large_enum_variant,
reason = "always behind an Arc, and a declared grid always wraps a backend"
)]
pub enum AnySource {
Backend(BackendSource),
DeclaredGrid(DeclaredGridSource),
}
#[derive(Debug)]
pub enum BackendSource {
#[cfg(feature = "pmtiles")]
Pmtiles(PmtilesSource),
#[cfg(feature = "mbtiles")]
Mbtiles(MbtSource),
#[cfg(feature = "postgres")]
Postgres(PostgresSource),
#[cfg(feature = "passthrough")]
Passthrough(PassthroughSource),
#[cfg(feature = "processing")]
GeoJson(GeoJsonSource),
#[cfg(feature = "cog")]
Cog(CogSource),
#[cfg(feature = "unstable-duckdb")]
DuckDb(DuckDBSource),
#[cfg(feature = "_testing")]
Test(TestSource),
}
macro_rules! dispatch_backend {
($self:expr, |$s:ident| $body:expr) => {
match *$self {
#[cfg(feature = "pmtiles")]
BackendSource::Pmtiles(ref $s) => $body,
#[cfg(feature = "mbtiles")]
BackendSource::Mbtiles(ref $s) => $body,
#[cfg(feature = "postgres")]
BackendSource::Postgres(ref $s) => $body,
#[cfg(feature = "passthrough")]
BackendSource::Passthrough(ref $s) => $body,
#[cfg(feature = "processing")]
BackendSource::GeoJson(ref $s) => $body,
#[cfg(feature = "cog")]
BackendSource::Cog(ref $s) => $body,
#[cfg(feature = "unstable-duckdb")]
BackendSource::DuckDb(ref $s) => $body,
#[cfg(feature = "_testing")]
BackendSource::Test(ref $s) => $body,
}
};
}
macro_rules! dispatch {
($self:expr, |$s:ident| $body:expr) => {
match $self {
AnySource::Backend(b) => dispatch_backend!(b, |$s| $body),
AnySource::DeclaredGrid($s) => $body,
}
};
}
macro_rules! impl_from_backend {
($($(#[$cfg:meta])* $variant:ident($ty:ty)),* $(,)?) => {
$(
$(#[$cfg])*
impl From<$ty> for BackendSource {
fn from(source: $ty) -> Self {
Self::$variant(source)
}
}
)*
};
}
impl_from_backend! {
#[cfg(feature = "pmtiles")] Pmtiles(PmtilesSource),
#[cfg(feature = "mbtiles")] Mbtiles(MbtSource),
#[cfg(feature = "postgres")] Postgres(PostgresSource),
#[cfg(feature = "passthrough")] Passthrough(PassthroughSource),
#[cfg(feature = "processing")] GeoJson(GeoJsonSource),
#[cfg(feature = "cog")] Cog(CogSource),
#[cfg(feature = "unstable-duckdb")] DuckDb(DuckDBSource),
#[cfg(feature = "_testing")] Test(TestSource),
}
#[cfg_attr(
not(any(
feature = "pmtiles",
feature = "mbtiles",
feature = "postgres",
feature = "passthrough",
feature = "processing",
feature = "cog",
feature = "unstable-duckdb",
feature = "_testing",
)),
expect(unused_variables, reason = "no backend to dispatch to")
)]
impl BackendSource {
#[must_use]
pub fn boxed(self) -> BoxedSource {
Arc::new(AnySource::Backend(self))
}
#[must_use]
pub fn boxed_on(self, grid: Option<&TileGrid>) -> BoxedSource {
match grid {
Some(grid) => DeclaredGridSource::new(self, grid.clone()).boxed(),
None => self.boxed(),
}
}
#[must_use]
pub fn get_id(&self) -> &str {
dispatch_backend!(self, |s| s.get_id())
}
#[must_use]
pub fn get_tilejson(&self) -> &TileJSON {
dispatch_backend!(self, |s| s.get_tilejson())
}
#[must_use]
pub fn get_tile_info(&self) -> TileInfo {
dispatch_backend!(self, |s| s.get_tile_info())
}
#[must_use]
pub fn get_version(&self) -> Option<String> {
dispatch_backend!(self, |s| s.get_version())
}
#[must_use]
pub fn support_url_query(&self) -> bool {
dispatch_backend!(self, |s| s.support_url_query())
}
#[must_use]
pub fn benefits_from_concurrent_scraping(&self) -> bool {
dispatch_backend!(self, |s| s.benefits_from_concurrent_scraping())
}
#[must_use]
pub fn empty_tile_implies_empty_children(&self) -> bool {
dispatch_backend!(self, |s| s.empty_tile_implies_empty_children())
}
#[must_use]
pub fn cache_zoom(&self) -> CacheZoomRange {
dispatch_backend!(self, |s| s.cache_zoom())
}
#[cfg(feature = "postgres")]
#[must_use]
#[cfg_attr(
not(any(
feature = "pmtiles",
feature = "mbtiles",
feature = "passthrough",
feature = "processing",
feature = "cog",
feature = "unstable-duckdb",
feature = "_testing",
)),
expect(
irrefutable_let_patterns,
reason = "Postgres is the only variant in a postgres-only build"
)
)]
pub fn as_postgres(&self) -> Option<&PostgresSource> {
if let Self::Postgres(s) = self { Some(s) } else { None }
}
pub async fn get_tile(&self, xyz: TileCoord, url_query: Option<&UrlQuery>) -> MartinCoreResult<TileData> {
dispatch_backend!(self, |s| Box::pin(s.get_tile(xyz, url_query)).await)
}
pub async fn get_tile_with_etag(&self, xyz: TileCoord, url_query: Option<&UrlQuery>) -> MartinCoreResult<Tile> {
dispatch_backend!(self, |s| Box::pin(s.get_tile_with_etag(xyz, url_query)).await)
}
pub async fn try_reload(&self) -> MartinCoreResult<Self> {
dispatch_backend!(self, |s| s.try_reload().await.map(Self::from))
}
}
impl AnySource {
#[must_use]
pub fn backend(&self) -> &BackendSource {
match self {
Self::Backend(b) => b,
Self::DeclaredGrid(s) => s.inner(),
}
}
#[must_use]
pub fn get_id(&self) -> &str {
dispatch!(self, |s| s.get_id())
}
#[must_use]
pub fn get_tilejson(&self) -> &TileJSON {
dispatch!(self, |s| s.get_tilejson())
}
#[must_use]
pub fn get_tile_info(&self) -> TileInfo {
dispatch!(self, |s| s.get_tile_info())
}
#[must_use]
pub fn tile_grid(&self) -> &TileGrid {
dispatch!(self, |s| s.tile_grid())
}
#[must_use]
pub fn get_version(&self) -> Option<String> {
self.backend().get_version()
}
#[must_use]
pub fn support_url_query(&self) -> bool {
self.backend().support_url_query()
}
#[must_use]
pub fn benefits_from_concurrent_scraping(&self) -> bool {
self.backend().benefits_from_concurrent_scraping()
}
#[must_use]
pub fn empty_tile_implies_empty_children(&self) -> bool {
self.backend().empty_tile_implies_empty_children()
}
#[cfg(feature = "postgres")]
#[must_use]
pub fn cancel_registry(&self) -> Option<ActiveQueryRegistry> {
self.backend().as_postgres().map(PostgresSource::active_query_registry)
}
#[must_use]
pub fn cache_zoom(&self) -> CacheZoomRange {
self.backend().cache_zoom()
}
#[must_use]
pub fn is_valid_zoom(&self, zoom: u8) -> bool {
dispatch!(self, |s| s.is_valid_zoom(zoom))
}
#[must_use]
pub fn get_catalog_entry(&self) -> CatalogSourceEntry {
dispatch!(self, |s| s.get_catalog_entry())
}
pub async fn get_tile(&self, xyz: TileCoord, url_query: Option<&UrlQuery>) -> MartinCoreResult<TileData> {
self.backend().get_tile(xyz, url_query).await
}
#[cfg(feature = "postgres")]
pub async fn get_tile_features(
&self,
xyz: TileCoord,
url_query: Option<&UrlQuery>,
keep_measures: bool,
) -> MartinCoreResult<Option<PostgresTileFeatures>> {
match self.backend().as_postgres() {
Some(source) => source.get_tile_features(xyz, url_query, keep_measures).await,
None => Ok(None),
}
}
#[cfg(feature = "unstable-generate")]
pub async fn get_tile_block(&self, xyz: TileCoord) -> MartinCoreResult<Option<Vec<(TileCoord, Tile)>>> {
match self {
Self::Backend(backend) => match backend.as_postgres() {
Some(source) => source.get_tile_block(xyz).await,
None => Ok(None),
},
Self::DeclaredGrid(_) => Ok(None),
}
}
pub async fn get_tile_with_etag(&self, xyz: TileCoord, url_query: Option<&UrlQuery>) -> MartinCoreResult<Tile> {
self.backend().get_tile_with_etag(xyz, url_query).await
}
pub async fn try_reload(&self) -> MartinCoreResult<BoxedSource> {
let reloaded = match self {
Self::Backend(b) => Self::Backend(b.try_reload().await?),
Self::DeclaredGrid(s) => Self::DeclaredGrid(s.try_reload().await?),
};
Ok(Arc::new(reloaded))
}
}