1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use serde::{Deserialize, Serialize};

use crate::{
    io::{
        apc::AsyncProcedureCall,
        scheduler::Scheduler,
        source_client::{HttpClient, SourceClient},
    },
    window::MapWindowConfig,
};

/// The environment defines which types must be injected into maplibre at compile time.
/// Essentially, this trait implements the
/// [dependency injection](https://en.wikipedia.org/wiki/Dependency_injection) design pattern.
/// By instantiating this trait at compile time with concrete types, it is possible to create
/// different compile-time instances of maplibre.
///
/// For example it is possible to change the way tasks are scheduled. It is also possible to change
/// the HTTP implementation for fetching tiles over the network.
pub trait Environment: 'static {
    type MapWindowConfig: MapWindowConfig;

    type AsyncProcedureCall: AsyncProcedureCall<Self::OffscreenKernelEnvironment>;

    type Scheduler: Scheduler;

    type HttpClient: HttpClient;

    type OffscreenKernelEnvironment: OffscreenKernel;
}

#[derive(Serialize, Deserialize, Clone)]
pub struct OffscreenKernelConfig {
    pub cache_directory: Option<String>,
}

pub trait OffscreenKernel: Send + Sync + 'static {
    type HttpClient: HttpClient;
    fn create(config: OffscreenKernelConfig) -> Self;

    fn source_client(&self) -> SourceClient<Self::HttpClient>;
}