1#![deny(unsafe_op_in_unsafe_fn)]
9
10mod camera;
11mod custom_geometry;
12mod events;
13mod geojson;
14mod geometry;
15mod handle;
16mod json;
17mod logging;
18mod map;
19mod options;
20mod projection;
21mod render;
22mod resource;
23mod runtime;
24mod values;
25
26use crate::values::NativeValue;
27use maplibre_native_core as maplibre_core;
28use maplibre_native_sys as sys;
29
30pub use camera::{
31 AnimationOptions, BoundOptions, BoundsConstraint, CameraFitOptions, CameraOptions,
32 FreeCameraOptions, ProjectionMode,
33};
34pub use custom_geometry::{CanonicalTileId, CustomGeometrySourceOptions};
35pub use events::{
36 CameraTransitionFinishedEvent, MapId, OfflineOperationCompletedEvent,
37 OfflineRegionResponseErrorEvent, OfflineRegionStatus, OfflineRegionStatusEvent,
38 OfflineRegionTileCountLimitEvent, RenderFrameEvent, RenderMapEvent, RenderingStats,
39 RuntimeEvent, RuntimeEventPayload, RuntimeEventSource, StyleImageMissingEvent, TileActionEvent,
40 TileId, UnknownRuntimeEventPayload,
41};
42pub use geojson::{Feature, FeatureIdentifier, GeoJson};
43pub use geometry::Geometry;
44pub use json::{JsonMember, JsonValue};
45pub use logging::{LogRecord, clear_log_callback, set_async_log_severity_mask, set_log_callback};
46pub use map::{
47 GeoJsonSourceOptions, LocationIndicatorImageKind, MapHandle, RasterDemEncoding, SourceInfo,
48 SourceType, StyleImage, StyleImageInfo, StyleImageOptions, TileScheme, TileSourceOptions,
49 VectorTileEncoding,
50};
51pub use maplibre_core::{
52 AmbientCacheOperation, CameraChangeMode, ConstrainMode, Error, ErrorKind, LogEvent,
53 LogSeverity, LogSeverityMask, MapDebugOptions, MapMode, MapOptions, MapTileOptions,
54 MapViewportOptions, NetworkStatus, NorthOrientation, OfflineOperationKind,
55 OfflineOperationResultKind, OfflineRegionDownloadState, OpenGLContextProviderMask,
56 RenderBackendMask, RenderMode, ResourceErrorReason, ResourceKind, ResourceLoadingMethod,
57 ResourcePriority, ResourceResponseStatus, ResourceStoragePolicy, ResourceUsage, Result,
58 RuntimeEventType, TileLodMode, TileOperation, ViewportMode,
59};
60pub use projection::MapProjectionHandle;
61pub use render::{
62 DetachedRenderSessionHandle, EglContextDescriptor, FeatureExtensionResult,
63 FeatureStateSelector, FrameNativePointer, FrameOpenGLTextureName,
64 MetalBorrowedTextureDescriptor, MetalContextDescriptor, MetalOwnedTextureDescriptor,
65 MetalOwnedTextureFrame, MetalOwnedTextureFrameHandle, MetalSurfaceDescriptor, NativePointer,
66 OpenGLBorrowedTextureDescriptor, OpenGLContextDescriptor, OpenGLOwnedTextureDescriptor,
67 OpenGLOwnedTextureFrame, OpenGLOwnedTextureFrameHandle, OpenGLSurfaceDescriptor,
68 PremultipliedRgba8Image, QueriedFeature, RenderSessionHandle, RenderTargetExtent,
69 RenderedFeatureQueryOptions, RenderedQueryGeometry, SourceFeatureQueryOptions,
70 TextureImageInfo, VulkanBorrowedTextureDescriptor, VulkanContextDescriptor,
71 VulkanOwnedTextureDescriptor, VulkanOwnedTextureFrame, VulkanOwnedTextureFrameHandle,
72 VulkanSurfaceDescriptor, WglContextDescriptor,
73};
74pub use resource::{
75 ByteRange, ResourceProviderDecision, ResourceRequest, ResourceRequestHandle, ResourceResponse,
76 ResourceTransformRequest,
77};
78pub use runtime::{
79 OfflineOperationHandle, OfflineRegionDefinition, OfflineRegionInfo, RuntimeHandle,
80 RuntimeOptions, WakeSource,
81};
82pub use values::{
83 EdgeInsets, LatLng, LatLngBounds, ProjectedMeters, Quaternion, ScreenBox, ScreenPoint,
84 UnitBezier, Vec3,
85};
86
87#[derive(Debug)]
90pub struct HandleOperationError<T> {
91 error: Error,
92 handle: T,
93}
94
95impl<T> HandleOperationError<T> {
96 pub(crate) fn new(error: Error, handle: T) -> Self {
97 Self { error, handle }
98 }
99
100 pub fn error(&self) -> &Error {
102 &self.error
103 }
104
105 pub fn kind(&self) -> ErrorKind {
107 self.error.kind()
108 }
109
110 pub fn raw_status(&self) -> Option<i32> {
112 self.error.raw_status()
113 }
114
115 pub fn diagnostic(&self) -> &str {
117 self.error.diagnostic()
118 }
119
120 pub fn into_error(self) -> Error {
122 self.error
123 }
124
125 pub fn into_handle(self) -> T {
127 self.handle
128 }
129
130 pub fn into_parts(self) -> (Error, T) {
132 (self.error, self.handle)
133 }
134}
135
136impl<T> std::fmt::Display for HandleOperationError<T> {
137 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
138 self.error.fmt(f)
139 }
140}
141
142impl<T: std::fmt::Debug> std::error::Error for HandleOperationError<T> {}
143
144#[derive(Debug)]
146pub enum OfflineOperationTakeError<T> {
147 Retryable(HandleOperationError<T>),
149 Consumed(Error),
151}
152
153impl<T> OfflineOperationTakeError<T> {
154 pub(crate) fn retryable(error: Error, handle: T) -> Self {
155 Self::Retryable(HandleOperationError::new(error, handle))
156 }
157
158 pub(crate) fn consumed(error: Error) -> Self {
159 Self::Consumed(error)
160 }
161
162 pub fn error(&self) -> &Error {
164 match self {
165 Self::Retryable(error) => error.error(),
166 Self::Consumed(error) => error,
167 }
168 }
169
170 pub fn kind(&self) -> ErrorKind {
172 self.error().kind()
173 }
174
175 pub fn raw_status(&self) -> Option<i32> {
177 self.error().raw_status()
178 }
179
180 pub fn diagnostic(&self) -> &str {
182 self.error().diagnostic()
183 }
184
185 pub fn into_retryable(self) -> Option<HandleOperationError<T>> {
187 match self {
188 Self::Retryable(error) => Some(error),
189 Self::Consumed(_) => None,
190 }
191 }
192
193 pub fn into_error(self) -> Error {
195 match self {
196 Self::Retryable(error) => error.into_error(),
197 Self::Consumed(error) => error,
198 }
199 }
200}
201
202impl<T> std::fmt::Display for OfflineOperationTakeError<T> {
203 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
204 self.error().fmt(f)
205 }
206}
207
208impl<T: std::fmt::Debug> std::error::Error for OfflineOperationTakeError<T> {}
209
210pub fn c_version() -> u32 {
212 unsafe { sys::mln_c_version() }
215}
216
217pub fn supported_render_backends() -> RenderBackendMask {
219 let mask = unsafe { sys::mln_supported_render_backend_mask() };
222 RenderBackendMask::from_bits_retain(mask)
223}
224
225pub fn supported_opengl_context_providers() -> OpenGLContextProviderMask {
227 let mask = unsafe { sys::mln_opengl_supported_context_provider_mask() };
230 OpenGLContextProviderMask::from_bits_retain(mask)
231}
232
233pub fn projected_meters_for_lat_lng(coordinate: LatLng) -> Result<ProjectedMeters> {
235 let mut raw_meters = sys::mln_projected_meters {
236 northing: 0.0,
237 easting: 0.0,
238 };
239 maplibre_core::check(unsafe {
242 sys::mln_projected_meters_for_lat_lng(coordinate.to_native(), &mut raw_meters)
243 })?;
244 Ok(ProjectedMeters::from_native(raw_meters))
245}
246
247pub fn lat_lng_for_projected_meters(meters: ProjectedMeters) -> Result<LatLng> {
249 let mut raw_coordinate = sys::mln_lat_lng {
250 latitude: 0.0,
251 longitude: 0.0,
252 };
253 maplibre_core::check(unsafe {
256 sys::mln_lat_lng_for_projected_meters(meters.to_native(), &mut raw_coordinate)
257 })?;
258 Ok(LatLng::from_native(raw_coordinate))
259}
260
261pub fn network_status() -> Result<NetworkStatus> {
263 maplibre_core::network_status()
264}
265
266pub fn set_network_status(status: NetworkStatus) -> Result<()> {
268 maplibre_core::set_network_status(status)
269}
270
271#[cfg(test)]
272fn set_network_status_raw(raw_status: u32) -> Result<()> {
273 maplibre_core::set_network_status_raw(raw_status)
274}
275
276#[cfg(test)]
277mod tests {
278 use static_assertions::assert_not_impl_any;
279
280 use super::*;
281
282 assert_not_impl_any!(RuntimeHandle: Send, Sync);
283 assert_not_impl_any!(MapHandle: Send, Sync);
284 assert_not_impl_any!(MapProjectionHandle: Send, Sync);
285 assert_not_impl_any!(NativePointer: Send, Sync);
286 assert_not_impl_any!(FrameNativePointer<'static>: Send, Sync);
287 assert_not_impl_any!(RenderSessionHandle: Send, Sync);
288
289 #[test]
290 fn projected_meter_helpers_round_trip() {
292 let coordinate = LatLng::new(45.0, -122.0);
293 let meters = projected_meters_for_lat_lng(coordinate).unwrap();
294 let round_tripped = lat_lng_for_projected_meters(meters).unwrap();
295
296 assert!((round_tripped.latitude - coordinate.latitude).abs() < 1e-9);
297 assert!((round_tripped.longitude - coordinate.longitude).abs() < 1e-9);
298 }
299
300 #[test]
301 fn invalid_network_status_reports_public_error() {
303 let error = set_network_status_raw(999_999).unwrap_err();
304
305 assert_eq!(error.kind(), ErrorKind::InvalidArgument);
306 assert_eq!(error.raw_status(), Some(sys::MLN_STATUS_INVALID_ARGUMENT));
307 assert!(error.diagnostic().contains("network status"));
308 }
309
310 #[test]
311 fn unknown_network_status_is_rejected_before_calling_c() {
313 let error = set_network_status(NetworkStatus::Unknown(999_999)).unwrap_err();
314
315 assert_eq!(error.kind(), ErrorKind::InvalidArgument);
316 assert_eq!(error.raw_status(), None);
317 assert!(error.diagnostic().contains("cannot be set"));
318 }
319}