use std::marker::PhantomData;
use std::sync::Arc;
use mln_style::error::Error;
use mln_style::source::SourceData;
use mln_style::sources::tileset::{
RasterDemKind, RasterKind, TilesetKind, TilesetSourceOptions, TilesetSourceType, VectorKind,
};
use mln_style::tileset::{RasterEncoding, Scheme, Tileset};
use mln_style::{Source, constants};
use crate::source::{
Cover, DataOrigin, DemTiles, Described, Fetch, GeometryTiles, Load, LoadContext, OfflineResources, OriginUpdate,
RasterTiles, Respond, SourceType, Supply, TileKind, TileNecessity, TileOperation, TileTimes, WorldCopy,
};
use crate::storage::resource::TileFormat;
use crate::storage::response::{Caching, Reason};
use crate::storage::{Data, FileSource, LoadingMethod, Request, Resource, Response};
use crate::tile::{MvtTile, TileData};
pub(crate) struct TestTileset<K>(PhantomData<fn() -> K>);
pub(crate) trait TestTilesetKind: TilesetKind + Sized {
type Data: TileKind;
const VECTOR: bool;
fn set_encoding(_update: &mut OriginUpdate<'_, TestTilesetOrigin<Self>>, _encoding: RasterEncoding) {}
}
impl TestTilesetKind for VectorKind {
type Data = GeometryTiles;
const VECTOR: bool = true;
}
impl TestTilesetKind for RasterKind {
type Data = RasterTiles;
const VECTOR: bool = false;
}
impl TestTilesetKind for RasterDemKind {
type Data = DemTiles;
const VECTOR: bool = false;
fn set_encoding(update: &mut OriginUpdate<'_, TestTilesetOrigin<Self>>, encoding: RasterEncoding) {
update.set_encoding(encoding);
}
}
impl<K: TestTilesetKind> SourceType for TestTileset<K> {
type Style = TilesetSourceType<K>;
type Data = K::Data;
type Description = Tileset;
fn load(source: &SourceData<Self::Style>) -> Load<Tileset> {
match &source.options.url {
Some(url) => Load::Fetch(Fetch::Source(url.clone())),
None => Load::Ready(source.options.tileset.clone().into()),
}
}
fn describe(options: &TilesetSourceOptions, data: Option<Data>, respond: Respond<Described<Tileset>>) {
let tileset = match data {
None => Err("unexpectedly empty TileJSON".to_owned()),
Some(data) => options.tileset_with_tilejson(&data, K::INFO.layer_kind),
};
respond(tileset.map(Some).map_err(Error::native));
}
fn metadata_changed(old: Option<&Tileset>, new: Option<&Tileset>) -> bool {
old != new
}
fn attribution(source: &SourceData<Self::Style>) -> Option<String> {
source.description::<Tileset>().map(|tileset| tileset.attribution.clone())
}
fn offline(source: &SourceData<Self::Style>) -> OfflineResources {
let options = &source.options;
let (tile_size, round_zoom) = (options.tile_size, !K::VECTOR);
match &options.url {
Some(url) => OfflineResources::TileJson { url: url.clone(), tile_size, round_zoom },
None => match options.inline_tileset() {
Some(tileset) => OfflineResources::Tiles { tileset, tile_size, round_zoom },
None => OfflineResources::None,
},
}
}
fn offline_tileset(source: &SourceData<Self::Style>, tilejson: &[u8]) -> Result<Tileset, String> {
source.options.tileset_with_tilejson(tilejson, K::INFO.layer_kind)
}
fn supply(_source: &Source) -> Supply<K::Data> {
Supply::tiles(TestTilesetOrigin::<K> { tileset: None, pixel_ratio: 1.0, file_source: None, kind: PhantomData })
}
}
pub(crate) struct TestTilesetOrigin<K> {
tileset: Option<Tileset>,
pixel_ratio: f32,
file_source: Option<Arc<dyn FileSource>>,
kind: PhantomData<fn() -> K>,
}
pub(crate) struct TestTilesetTile {
pub(crate) resource: Resource,
request: Option<Request>,
failed: bool,
has_data: bool,
caching: Caching,
}
impl TestTilesetTile {
pub(crate) fn is_requesting(&self) -> bool {
self.request.is_some()
}
}
impl<K: TestTilesetKind> TestTilesetOrigin<K> {
fn request(&self, tile: &mut TestTilesetTile, cx: &mut LoadContext<'_, '_, Self>) {
let Some(file_source) = &self.file_source else {
cx.fail(Error::native("Can't load tile."));
return;
};
cx.report(TileOperation::RequestedFromNetwork);
let replier = cx.begin_load_of::<Response>();
let (request, responder) = Request::new(move |response| replier.send(response));
file_source.load(&tile.resource, responder);
tile.failed = false;
tile.request = Some(request);
}
fn deliver(&self, res: &Response, cx: &mut LoadContext<'_, '_, Self>) {
let sparse = self.tileset.as_ref().is_some_and(|tileset| tileset.sparse);
let not_found = res.tile_not_found || res.error.as_ref().is_some_and(|e| e.reason == Reason::NotFound);
let empty = res.no_content || not_found;
if empty && (sparse || (not_found && !K::VECTOR)) {
cx.missing();
return;
}
let data = if empty { None } else { res.data.clone() };
if K::VECTOR {
cx.deliver_of::<GeometryTiles>(data.map(|data| TileData::Mvt(MvtTile::new(data))));
} else {
cx.deliver_of::<RasterTiles>(data);
}
}
}
impl<K: TestTilesetKind> DataOrigin for TestTilesetOrigin<K> {
type Data = K::Data;
type Tile = TestTilesetTile;
type Reply = Response;
fn update(&mut self, update: &mut OriginUpdate<'_, Self>) -> Option<Cover> {
let source = SourceData::<TilesetSourceType<K>>::recover(update.source())?;
let loaded = source.description::<Tileset>();
self.pixel_ratio = update.map().pixel_ratio;
self.file_source.clone_from(&update.map().file_source);
let can_update = loaded.is_some() || update.map().mode != crate::map::MapMode::Continuous;
if can_update && self.tileset.as_ref() != loaded {
self.tileset = loaded.cloned();
update.clear_tiles();
}
let tileset = self.tileset.as_ref().filter(|tileset| !tileset.tiles.is_empty())?;
let cover = Cover {
tile_size: if K::VECTOR { constants::TILE_SIZE_I } else { source.options.tile_size },
zoom_range: tileset.zoom_range,
bounds: tileset.bounds,
round_zoom: !K::VECTOR,
overscale: K::VECTOR,
prefetch: true,
synchronous: false,
};
let encoding = tileset.raster_encoding.unwrap_or(RasterEncoding::Mapbox);
K::set_encoding(update, encoding);
Some(cover)
}
fn start(
&mut self,
cx: &mut LoadContext<'_, '_, Self>,
copy: Option<WorldCopy<'_, TestTilesetTile>>,
) -> TestTilesetTile {
let (url, scheme) = self.tileset.as_ref().map_or(("", Scheme::default()), |tileset| {
(tileset.tiles.first().map_or("", String::as_str), tileset.scheme)
});
let canonical = cx.id().canonical;
let format = if K::VECTOR { TileFormat::Mvt } else { TileFormat::Image };
let (x, y, z) = (canonical.x as i32, canonical.y as i32, canonical.z as i8);
let mut resource = Resource::tile(url, self.pixel_ratio, x, y, z, scheme, format, LoadingMethod::NetworkOnly);
resource.order = cx.order();
let mut tile =
TestTilesetTile { resource, request: None, failed: false, has_data: false, caching: Caching::default() };
let taken = copy.and_then(|copy| {
let state = copy.state();
copy.take_into(cx).then_some(state)
});
match taken {
Some(copy) => {
(tile.caching, tile.has_data, tile.resource.prior_caching) =
(copy.caching, true, copy.resource.prior_caching);
}
None if self.tileset.is_some() && self.file_source.is_none() => cx.fail(Error::native("Can't load tile.")),
None => {}
}
tile
}
fn receive(&mut self, tile: &mut TestTilesetTile, res: Response, cx: &mut LoadContext<'_, '_, Self>) {
if tile.request.is_none() {
return;
}
if let Some(error) = res.error.as_ref().filter(|error| error.reason != Reason::NotFound) {
if !std::mem::replace(&mut tile.failed, true) {
tile.has_data = false;
cx.fail_retrying(Error::native(error.message.clone()));
}
return;
}
tile.failed = false;
if res.not_modified {
tile.resource.prior_caching = res.caching;
} else {
tile.resource.set_prior(&res);
}
(tile.caching, tile.has_data) = (res.caching, true);
cx.set_times(TileTimes {
modified: res.modified,
expires: res.caching.expires,
usable_until: res.caching.usable_until(),
});
cx.report(TileOperation::LoadFromNetwork);
if res.not_modified {
cx.keep_data();
} else {
self.deliver(&res, cx);
}
}
fn set_necessity(
&mut self,
tile: &mut TestTilesetTile,
previous: TileNecessity,
cx: &mut LoadContext<'_, '_, Self>,
) {
if previous == cx.necessity() {
return;
}
if cx.necessity() == TileNecessity::Required {
let lasting = tile.has_data && tile.caching.expires.is_none();
if tile.request.is_none() && !lasting {
self.request(tile, cx);
}
} else {
tile.request = None;
}
}
fn retry(&mut self, tile: &mut TestTilesetTile, cx: &mut LoadContext<'_, '_, Self>) {
if tile.failed {
tile.request = None;
self.request(tile, cx);
}
}
fn failed(&mut self, tile: &mut TestTilesetTile, _cx: &mut LoadContext<'_, '_, Self>) {
tile.has_data = false;
}
}