Skip to main content

mln_engine/source/
loading.rs

1//! What a source type's loading half returns: how a source's description
2//! loads, what it reports about itself, and what an offline download fetches
3//! for it.
4
5use std::sync::Arc;
6
7use mln_style::error::Error;
8use mln_style::sources::GeoJSONOptions;
9use mln_style::tileset::{Tileset, VectorEncoding};
10
11/// Where a job hands its result: called at most once, from any thread,
12/// while the job runs or later. Dropping it uncalled drops the result.
13pub type Respond<R> = Box<dyn FnOnce(R) + Send>;
14
15/// How a source's description loads, as its type says from its data.
16pub enum Load<D> {
17    /// Nothing loads: the source is loaded with the description it has.
18    Done,
19    /// The description, known without a fetch, such as a tileset that the
20    /// style lists.
21    Ready(D),
22    /// The description that [`SourceType::describe`](super::SourceType::describe)
23    /// makes from the source's options alone, with no data, such as GeoJSON
24    /// that the style lists.
25    Describe,
26    /// The resource that [`SourceType::describe`](super::SourceType::describe)
27    /// or, for an image, [`SourceType::describe_image`](super::SourceType::describe_image)
28    /// turns into the description.
29    Fetch(Fetch),
30}
31
32/// A resource that a source loads its description from, by its URL.
33#[derive(Clone, Debug, PartialEq, Eq)]
34pub enum Fetch {
35    /// A document that describes the source, such as TileJSON or GeoJSON.
36    Source(String),
37    /// An image, which the map's loader decodes.
38    Image(String),
39}
40
41/// What describing a source found: its description, `None` for data that
42/// describes nothing, or why the source failed to load.
43pub type Described<D> = Result<Option<D>, Error>;
44
45/// What a source reports about itself, whichever its type. A member that
46/// the type doesn't have is `None`.
47#[derive(Clone, Debug, Default)]
48pub struct SourceSummary {
49    /// The URL that the source loads its description from.
50    pub url: Option<String>,
51    /// The size of the source's tiles, in pixels.
52    pub tile_size: Option<u16>,
53    /// The tileset that the style lists, for a tileset source without a URL.
54    pub inline_tileset: Option<Tileset>,
55    /// How a vector source's tiles are encoded: as its loaded tileset says,
56    /// else as the style does.
57    pub vector_encoding: Option<VectorEncoding>,
58    /// A GeoJSON source's options.
59    pub geojson: Option<Arc<GeoJSONOptions>>,
60}
61
62/// What an offline download of a source fetches.
63#[derive(Clone, Debug, Default, PartialEq)]
64pub enum OfflineResources {
65    /// Nothing: the host gives the source's data.
66    #[default]
67    None,
68    /// The tiles of a tileset that's known without a fetch, of tiles
69    /// `tile_size` pixels wide, which cover a view at the zoom level
70    /// nearest its own if `round_zoom` (see [`Cover::round_zoom`](super::Cover::round_zoom)).
71    Tiles { tileset: Tileset, tile_size: u16, round_zoom: bool },
72    /// The TileJSON at `url`, then the tiles, as [`OfflineResources::Tiles`]'s,
73    /// of the tileset that
74    /// [`SourceType::offline_tileset`](super::SourceType::offline_tileset)
75    /// makes of it.
76    TileJson { url: String, tile_size: u16, round_zoom: bool },
77    /// The source's data at `url`: GeoJSON.
78    Source { url: String },
79    /// The source's image at `url`, which is nothing to fetch if empty.
80    Image { url: String },
81}