use std::future::Future;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use martin_tile_utils::{Encoding, Format, TileCoord, TileData, TileInfo};
use tilejson::{TileJSON, tilejson};
use crate::CacheZoomRange;
use crate::tiles::{BoxedSource, MartinCoreError, MartinCoreResult, Source, UrlQuery};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum Behaviour {
#[default]
Serve,
Fail,
NeedsReload,
}
#[derive(Debug, Clone)]
pub struct TestSource {
id: String,
tilejson: TileJSON,
info: TileInfo,
data: TileData,
cache_zoom: CacheZoomRange,
url_query: bool,
empty_children: bool,
behaviour: Behaviour,
reloads: u32,
fetches: Option<Arc<AtomicU64>>,
block_after_fetch: Option<Arc<AtomicBool>>,
empty_if: Option<fn(TileCoord) -> bool>,
}
impl TestSource {
#[must_use]
pub fn new(id: impl Into<String>, data: impl Into<TileData>) -> Self {
Self {
id: id.into(),
tilejson: tilejson! { tiles: vec![] },
info: TileInfo::new(Format::Mvt, Encoding::Uncompressed),
data: data.into(),
cache_zoom: CacheZoomRange::default(),
url_query: false,
empty_children: false,
behaviour: Behaviour::Serve,
reloads: 0,
fetches: None,
block_after_fetch: None,
empty_if: None,
}
}
#[must_use]
pub fn empty(id: impl Into<String>) -> Self {
Self::new(id, TileData::new())
}
#[must_use]
pub fn with_tilejson(mut self, tilejson: TileJSON) -> Self {
self.tilejson = tilejson;
self
}
#[must_use]
pub const fn with_info(mut self, info: TileInfo) -> Self {
self.info = info;
self
}
#[must_use]
pub const fn with_format(mut self, format: Format) -> Self {
self.info = TileInfo::new(format, Encoding::Uncompressed);
self
}
#[must_use]
pub fn with_behaviour(mut self, behaviour: Behaviour) -> Self {
self.behaviour = behaviour;
self
}
#[must_use]
pub const fn with_url_query(mut self) -> Self {
self.url_query = true;
self
}
#[must_use]
pub const fn with_empty_children(mut self) -> Self {
self.empty_children = true;
self
}
#[must_use]
pub fn counting(mut self, counter: Arc<AtomicU64>) -> Self {
self.fetches = Some(counter);
self
}
#[must_use]
pub fn blocking(mut self, flag: Arc<AtomicBool>) -> Self {
self.block_after_fetch = Some(flag);
self
}
#[must_use]
pub const fn empty_if(mut self, predicate: fn(TileCoord) -> bool) -> Self {
self.empty_if = Some(predicate);
self
}
#[must_use]
pub fn boxed(self) -> BoxedSource {
crate::tiles::BackendSource::Test(self).boxed()
}
}
impl Source for TestSource {
fn get_id(&self) -> &str {
&self.id
}
fn get_tilejson(&self) -> &TileJSON {
&self.tilejson
}
fn get_tile_info(&self) -> TileInfo {
self.info
}
fn support_url_query(&self) -> bool {
self.url_query
}
fn empty_tile_implies_empty_children(&self) -> bool {
self.empty_children
}
fn cache_zoom(&self) -> CacheZoomRange {
self.cache_zoom
}
async fn get_tile(&self, xyz: TileCoord, _url_query: Option<&UrlQuery>) -> MartinCoreResult<TileData> {
if let Some(flag) = &self.block_after_fetch {
flag.store(true, Ordering::Release);
std::future::pending::<()>().await;
}
if let Some(fetches) = &self.fetches {
fetches.fetch_add(1, Ordering::Relaxed);
}
match self.behaviour {
Behaviour::Fail => {
return Err(MartinCoreError::OtherError(Box::new(std::io::Error::other(
"some error".to_owned(),
))));
}
Behaviour::NeedsReload if self.reloads == 0 => {
return Err(MartinCoreError::SourceNeedsReload);
}
Behaviour::Serve | Behaviour::NeedsReload => {}
}
if self.empty_if.is_some_and(|f| f(xyz)) {
return Ok(TileData::new());
}
Ok(self.data.clone())
}
fn try_reload(&self) -> impl Future<Output = MartinCoreResult<Self>> + Send {
let mut reloaded = self.clone();
reloaded.reloads += 1;
std::future::ready(Ok(reloaded))
}
}