maplibre/
kernel.rs

1use crate::{
2    environment::Environment,
3    io::source_client::{HttpSourceClient, SourceClient},
4};
5
6/// Holds references to core constructs of maplibre. Based on the compile-time initialization
7/// different implementations for handling windows, asynchronous work, or data sources are provided
8/// through a [`Kernel`].
9///
10/// An [`Environment`] defines the types which are used.
11///
12/// A Kernel lives as long as a [Map](crate::map::Map) usually. It is shared through out various
13/// components of the maplibre library.
14pub struct Kernel<E: Environment> {
15    map_window_config: E::MapWindowConfig,
16    apc: E::AsyncProcedureCall,
17    scheduler: E::Scheduler,
18    source_client: SourceClient<E::HttpClient>,
19}
20
21impl<E: Environment> Kernel<E> {
22    pub fn map_window_config(&self) -> &E::MapWindowConfig {
23        &self.map_window_config
24    }
25
26    pub fn apc(&self) -> &E::AsyncProcedureCall {
27        &self.apc
28    }
29
30    pub fn scheduler(&self) -> &E::Scheduler {
31        &self.scheduler
32    }
33
34    pub fn source_client(&self) -> &SourceClient<E::HttpClient> {
35        &self.source_client
36    }
37}
38
39/// A convenient builder for [Kernels](Kernel).
40pub struct KernelBuilder<E: Environment> {
41    map_window_config: Option<E::MapWindowConfig>,
42    apc: Option<E::AsyncProcedureCall>,
43    scheduler: Option<E::Scheduler>,
44    http_client: Option<E::HttpClient>,
45}
46
47impl<E: Environment> Default for KernelBuilder<E> {
48    fn default() -> Self {
49        Self::new()
50    }
51}
52
53impl<E: Environment> KernelBuilder<E> {
54    pub fn new() -> Self {
55        Self {
56            scheduler: None,
57            apc: None,
58            http_client: None,
59            map_window_config: None,
60        }
61    }
62
63    pub fn with_map_window_config(mut self, map_window_config: E::MapWindowConfig) -> Self {
64        self.map_window_config = Some(map_window_config);
65        self
66    }
67
68    pub fn with_scheduler(mut self, scheduler: E::Scheduler) -> Self {
69        self.scheduler = Some(scheduler);
70        self
71    }
72
73    pub fn with_apc(mut self, apc: E::AsyncProcedureCall) -> Self {
74        self.apc = Some(apc);
75        self
76    }
77
78    pub fn with_http_client(mut self, http_client: E::HttpClient) -> Self {
79        self.http_client = Some(http_client);
80        self
81    }
82
83    pub fn build(self) -> Kernel<E> {
84        Kernel {
85            scheduler: self.scheduler.unwrap(), // TODO: Remove unwrap
86            apc: self.apc.unwrap(),             // TODO: Remove unwrap
87            source_client: SourceClient::new(HttpSourceClient::new(self.http_client.unwrap())), // TODO: Remove unwrap
88            map_window_config: self.map_window_config.unwrap(), // TODO: Remove unwrap
89        }
90    }
91}