maplibre_native 0.8.7

Rust bindings to the MapLibre Native map rendering engine
Documentation
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
use std::fmt::Display;
use std::ops::Sub;

use crate::renderer::callbacks::{
    camera_did_change_callback, failing_loading_map_callback, finish_rendering_frame_callback,
    void_callback, CameraDidChangeCallback, FailingLoadingMapCallback,
    FinishRenderingFrameCallback, VoidCallback,
};
use crate::renderer::file_source::{BoxedFileSource, RequestHandleFfi};

// https://maplibre.org/maplibre-native/docs/book/design/ten-thousand-foot-view.html

/// Enable or disable the internal logging thread
///
/// By default, logs are generated asynchronously except for Error level messages.
/// In crash scenarios, pending async log entries may be lost.
pub fn set_log_thread_enabled(enable: bool) {
    ffi::Log_useLogThread(enable);
}

fn log_from_cpp(severity: ffi::EventSeverity, event: ffi::Event, code: i64, message: &str) {
    #[cfg(not(feature = "log"))]
    let _ = (severity, event, code, message);

    #[cfg(feature = "log")]
    match severity {
        ffi::EventSeverity::Debug => log::debug!("{event:?} (code={code}) {message}"),
        ffi::EventSeverity::Info => log::info!("{event:?} (code={code}) {message}"),
        ffi::EventSeverity::Warning => log::warn!("{event:?} (code={code}) {message}"),
        ffi::EventSeverity::Error => log::error!("{event:?} (code={code}) {message}"),
        ffi::EventSeverity { repr } => {
            log::error!("{event:?} (severity={repr}, code={code}) {message}");
        }
    }
}

/// A position in screen coordinates
#[repr(C)]
#[derive(Default, Debug, Clone, Copy, PartialEq)]
pub struct ScreenCoordinate {
    /// Horizontal position in screen pixels.
    pub x: f64,
    /// Vertical position in screen pixels.
    pub y: f64,
}

impl Sub for ScreenCoordinate {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self::Output {
        Self { x: self.x - rhs.x, y: self.y - rhs.y }
    }
}

/// A size
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct Size {
    /// Width in pixels.
    pub width: u32,
    /// Height in pixels.
    pub height: u32,
}

#[cxx::bridge()]
/// FFI bindings for GeoJSON values.
pub mod geojson {
    #[namespace = "mln::bridge::geojson"]
    unsafe extern "C++" {
        include!("geojson/geojson.h");

        /// A MapLibre Native GeoJSON value.
        type GeoJson;

        /// Parses a GeoJSON string into a MapLibre Native GeoJSON value.
        fn parse(json: &str) -> Result<UniquePtr<GeoJson>>;
        /// Copies a MapLibre Native GeoJSON value.
        fn clone(geojson: &GeoJson) -> UniquePtr<GeoJson>;
        // TODO(maplibre-native#4345): can be restored once the precompiled core exposes
        // a public GeoJSON serializer
        // /// Serializes a MapLibre Native GeoJSON value to a JSON string.
        // fn stringify(geojson: &GeoJson) -> Result<String>;
    }
}

impl std::fmt::Debug for geojson::GeoJson {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("GeoJson").finish()
    }
}

/// FFI bindings for map source operations.
///
/// This module provides C++/Rust interoperability for various source types.
/// Currently supports GeoJSON sources, with extensibility for additional source types.
#[cxx::bridge()]
pub mod sources {
    /// MapLibre style-spec source type.
    /// Rust mirror of `mbgl::style::SourceType`.
    #[derive(Debug)]
    #[namespace = "mbgl::style"]
    enum SourceType {
        /// Vector tile source.
        Vector,
        /// Raster tile source.
        Raster,
        /// Raster DEM source.
        RasterDEM,
        /// GeoJSON source.
        GeoJSON,
        /// Video source.
        Video,
        /// Annotations source.
        Annotations,
        /// Image source.
        Image,
        /// Custom vector source.
        CustomVector,
    }

    #[namespace = "mbgl::style"]
    extern "C++" {
        include!("mbgl/style/source.hpp");
        include!("mbgl/style/sources/geojson_source.hpp");
        include!("mbgl/style/types.hpp");
        // Opaque types
        /// Base class for all MapLibre Native style sources.
        type Source;
        /// A GeoJSON source for MapLibre rendering.
        type GeoJSONSource;
        /// `mbgl::style::SourceType`
        type SourceType;
    }

    #[namespace = "mln::bridge::geojson"]
    extern "C++" {
        include!("geojson/geojson.h");

        /// A MapLibre Native GeoJSON value.
        #[rust_name = "CxxGeoJson"]
        type GeoJson = super::geojson::GeoJson;
    }

    #[namespace = "mln::bridge::style::sources"]
    unsafe extern "C++" {
        include!("sources/sources.h");

        /// A non-owning handle to a style-owned source.
        type SourceHandle;
        /// A non-owning handle to a style-owned GeoJSON source.
        type GeoJSONSourceHandle;

        /// Returns the source ID.
        fn sourceId(self: &SourceHandle) -> String;
        /// Returns the MapLibre style-spec source type.
        fn sourceType(self: &SourceHandle) -> SourceType;
        /// Downcasts this handle to a GeoJSON source handle, if it is one.
        fn asGeoJson(self: &SourceHandle) -> UniquePtr<GeoJSONSourceHandle>;
        /// Returns the GeoJSON source ID.
        fn sourceId(self: &GeoJSONSourceHandle) -> String;
        /// Sets the GeoJSON data for this source.
        fn setGeoJson(self: Pin<&mut GeoJSONSourceHandle>, geojson: &CxxGeoJson);

        /// Upcasts a GeoJSON source handle to the base `Source` type.
        fn geojson_into_source(source: UniquePtr<GeoJSONSource>) -> UniquePtr<Source>;
    }

    #[namespace = "mln::bridge::style::sources::geojson"]
    unsafe extern "C++" {
        include!("sources/sources.h");

        /// Creates a new GeoJSON source with the given ID.
        fn create(id: &str) -> UniquePtr<GeoJSONSource>;
        /// Sets the URL for loading GeoJSON data.
        fn setURL(source: &UniquePtr<GeoJSONSource>, url: &str);
        /// Sets the GeoJSON data for the source.
        fn setGeoJson(source: Pin<&mut GeoJSONSource>, geojson: &CxxGeoJson);
    }

    // Generate `UniquePtr<SourceHandle>` support. cxx emits it only in the
    // module that uses the type in a signature, but `SourceHandle` is only
    // returned via the `ffi` module's alias, so request it explicitly here.
    impl UniquePtr<SourceHandle> {}
}

impl std::fmt::Debug for sources::GeoJSONSource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("GeoJSONSource").finish()
    }
}

impl std::fmt::Debug for sources::Source {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Source").finish()
    }
}

#[cxx::bridge()]
/// FFI bindings for map layer operations.
///
/// This module provides C++/Rust interoperability for various layer types.
/// Currently supports circle, fill, line, and symbol layers.
pub mod layers {
    // Must have the same namespace than on the C++ side
    #[namespace = "mbgl::style"]
    /// Symbol anchor position type.
    pub enum SymbolAnchorType {
        /// Center anchor point.
        Center,
        /// Left anchor point.
        Left,
        /// Right anchor point.
        Right,
        /// Top anchor point.
        Top,
        /// Bottom anchor point.
        Bottom,
        /// Top-left anchor point.
        TopLeft,
        /// Top-right anchor point.
        TopRight,
        /// Bottom-left anchor point.
        BottomLeft,
        /// Bottom-right anchor point.
        BottomRight,
    }

    #[namespace = "mbgl::style"]
    /// Line cap type.
    pub enum LineCapType {
        /// Round line cap.
        Round,
        /// Butt line cap.
        Butt,
        /// Square line cap.
        Square,
    }

    #[namespace = "mbgl::style"]
    /// Line join type.
    pub enum LineJoinType {
        /// Miter line join.
        Miter,
        /// Bevel line join.
        Bevel,
        /// Round line join.
        Round,
        /// Internal MapLibre Native line join type.
        FakeRound,
        /// Internal MapLibre Native line join type.
        FlipBevel,
    }

    #[namespace = "mbgl::style"]
    extern "C++" {
        include!("mbgl/style/layer.hpp");
        include!("mbgl/style/layers/circle_layer.hpp");
        include!("mbgl/style/layers/fill_layer.hpp");
        include!("mbgl/style/layers/line_layer.hpp");
        include!("mbgl/style/layers/symbol_layer.hpp");
        include!("mbgl/style/types.hpp");

        /// Base class for all MapLibre Native style layers.
        type Layer;
        /// A circle layer for rendering point data.
        type CircleLayer;
        /// A fill layer for rendering polygon data.
        type FillLayer;
        /// A line layer for rendering line data.
        type LineLayer;
        /// Line cap type.
        type LineCapType;
        /// Line join type.
        type LineJoinType;
        // Opaque types
        /// A symbol layer for rendering labels and icons on the map.
        type SymbolLayer;

        /// Symbol anchor position type.
        type SymbolAnchorType;
    }

    #[namespace = "mbgl"]
    extern "C++" {
        include!("mbgl/util/color.hpp");

        /// A MapLibre Native premultiplied RGBA color.
        type Color = crate::style::Color;
    }

    #[namespace = "mln::bridge::style::layers"]
    unsafe extern "C++" {
        include!("layers/layers.h");

        /// Returns the layer's ID (the style-spec `"id"` field).
        fn layer_id(layer: &UniquePtr<Layer>) -> String;
        /// Returns the layer's type name (e.g. `"circle"`, `"fill"`).
        fn layer_type(layer: &UniquePtr<Layer>) -> String;

        /// Upcasts a circle layer handle to the base `Layer` type.
        #[must_use]
        fn circle_into_layer(layer: UniquePtr<CircleLayer>) -> UniquePtr<Layer>;
        /// Upcasts a fill layer handle to the base `Layer` type.
        #[must_use]
        fn fill_into_layer(layer: UniquePtr<FillLayer>) -> UniquePtr<Layer>;
        /// Upcasts a line layer handle to the base `Layer` type.
        #[must_use]
        fn line_into_layer(layer: UniquePtr<LineLayer>) -> UniquePtr<Layer>;
        /// Upcasts a symbol layer handle to the base `Layer` type.
        #[must_use]
        fn symbol_into_layer(layer: UniquePtr<SymbolLayer>) -> UniquePtr<Layer>;

        /// Downcasts a base layer handle to a circle layer. Returns null on type mismatch.
        fn try_into_circle(layer: UniquePtr<Layer>) -> UniquePtr<CircleLayer>;
        /// Downcasts a base layer handle to a fill layer. Returns null on type mismatch.
        fn try_into_fill(layer: UniquePtr<Layer>) -> UniquePtr<FillLayer>;
        /// Downcasts a base layer handle to a line layer. Returns null on type mismatch.
        fn try_into_line(layer: UniquePtr<Layer>) -> UniquePtr<LineLayer>;
        /// Downcasts a base layer handle to a symbol layer. Returns null on type mismatch.
        fn try_into_symbol(layer: UniquePtr<Layer>) -> UniquePtr<SymbolLayer>;

        /// Creates a new circle layer.
        #[must_use]
        pub(crate) fn create_circle_layer(
            layer_id: &str,
            source_id: &str,
        ) -> UniquePtr<CircleLayer>;
        /// Sets the circle color.
        fn setCircleColor(layer: &UniquePtr<CircleLayer>, color: &Color);
        /// Sets the circle opacity.
        fn setCircleOpacity(layer: &UniquePtr<CircleLayer>, opacity: f32);
        /// Sets the circle radius in pixels.
        fn setCircleRadius(layer: &UniquePtr<CircleLayer>, radius: f32);
        /// Sets the circle stroke color.
        fn setCircleStrokeColor(layer: &UniquePtr<CircleLayer>, color: &Color);
        /// Sets the circle stroke opacity.
        fn setCircleStrokeOpacity(layer: &UniquePtr<CircleLayer>, opacity: f32);
        /// Sets the circle stroke width in pixels.
        fn setCircleStrokeWidth(layer: &UniquePtr<CircleLayer>, width: f32);

        /// Creates a new fill layer.
        #[must_use]
        pub(crate) fn create_fill_layer(layer_id: &str, source_id: &str) -> UniquePtr<FillLayer>;
        /// Sets the fill color.
        fn setFillColor(layer: &UniquePtr<FillLayer>, color: &Color);
        /// Sets the fill opacity.
        fn setFillOpacity(layer: &UniquePtr<FillLayer>, opacity: f32);
        /// Sets the fill outline color.
        fn setFillOutlineColor(layer: &UniquePtr<FillLayer>, color: &Color);

        /// Creates a new line layer.
        #[must_use]
        pub(crate) fn create_line_layer(layer_id: &str, source_id: &str) -> UniquePtr<LineLayer>;
        /// Sets the line color.
        fn setLineColor(layer: &UniquePtr<LineLayer>, color: &Color);
        /// Sets the line cap.
        fn setLineCap(layer: &UniquePtr<LineLayer>, cap: LineCapType);
        /// Sets the line join.
        fn setLineJoin(layer: &UniquePtr<LineLayer>, join: LineJoinType);
        /// Sets the line opacity.
        fn setLineOpacity(layer: &UniquePtr<LineLayer>, opacity: f32);
        /// Sets the line width in pixels.
        fn setLineWidth(layer: &UniquePtr<LineLayer>, width: f32);

        /// Creates a new symbol layer.
        #[must_use]
        pub(crate) fn create_symbol_layer(
            layer_id: &str,
            source_id: &str,
        ) -> UniquePtr<SymbolLayer>;
        /// Sets the icon image for a layer by image ID.
        fn setIconImage(layer: &UniquePtr<SymbolLayer>, image_id: &str);
        /// Sets the anchor point for layer icons.
        fn setIconAnchor(layer: &UniquePtr<SymbolLayer>, anchor: SymbolAnchorType);
    }
}

#[cfg(feature = "json")]
#[cxx::bridge(namespace = "mln::bridge")]
/// FFI bindings for the style-spec value adapter.
///
/// Rust builds a C++ `StyleValue` tree, and MapLibre Native's conversion layer
/// reads it through `ConversionTraits<const StyleValue*>`.
pub mod style_value {
    #[namespace = "mbgl::style"]
    extern "C++" {
        include!("mbgl/style/layer.hpp");
        include!("mbgl/style/source.hpp");

        #[rust_name = "StyleLayer"]
        type Layer = crate::bridge::layers::Layer;
        #[rust_name = "StyleSource"]
        type Source = crate::bridge::sources::Source;
    }

    unsafe extern "C++" {
        include!("style_value.h");

        /// Opaque C++ JSON-like value used as input to MapLibre's conversion layer.
        type StyleValue;

        /// Constructs a null `StyleValue`.
        #[must_use]
        fn make_null() -> UniquePtr<StyleValue>;
        /// Constructs a boolean `StyleValue`.
        #[must_use]
        fn make_bool(b: bool) -> UniquePtr<StyleValue>;
        /// Constructs a numeric `StyleValue`.
        #[must_use]
        fn make_number(n: f64) -> UniquePtr<StyleValue>;
        /// Constructs a string `StyleValue`.
        #[must_use]
        fn make_string(s: &str) -> UniquePtr<StyleValue>;
        /// Constructs an empty array `StyleValue`.
        #[must_use]
        fn make_array() -> UniquePtr<StyleValue>;
        /// Appends a child to an array `StyleValue`.
        fn array_push(arr: Pin<&mut StyleValue>, child: UniquePtr<StyleValue>);
        /// Constructs an empty object `StyleValue`.
        #[must_use]
        fn make_object() -> UniquePtr<StyleValue>;
        /// Inserts a child under `key` in an object `StyleValue`.
        fn object_insert(obj: Pin<&mut StyleValue>, key: &str, child: UniquePtr<StyleValue>);

        /// Parses a style-spec layer object from a `StyleValue` tree.
        /// On failure, `error_message` is populated and the returned pointer is null.
        fn layer_from_value(
            value: &StyleValue,
            error_message: &mut String,
        ) -> UniquePtr<StyleLayer>;

        /// Parses a style-spec source object from a `StyleValue` tree.
        /// On failure, `error_message` is populated and the returned pointer is null.
        fn source_from_value(
            id: &str,
            value: &StyleValue,
            error_message: &mut String,
        ) -> UniquePtr<StyleSource>;
    }
}

#[cfg(feature = "json")]
impl std::fmt::Debug for style_value::StyleValue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("StyleValue").finish()
    }
}

impl std::fmt::Debug for layers::CircleLayer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CircleLayer").finish()
    }
}

impl std::fmt::Debug for layers::FillLayer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FillLayer").finish()
    }
}

impl std::fmt::Debug for layers::LineLayer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LineLayer").finish()
    }
}

impl std::fmt::Debug for layers::Layer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Layer").finish()
    }
}

impl std::fmt::Debug for layers::LineCapType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match *self {
            Self::Round => f.write_str("Round"),
            Self::Butt => f.write_str("Butt"),
            Self::Square => f.write_str("Square"),
            _ => f.write_str("LineCapType"),
        }
    }
}

impl std::fmt::Debug for layers::LineJoinType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match *self {
            Self::Miter => f.write_str("Miter"),
            Self::Bevel => f.write_str("Bevel"),
            Self::Round => f.write_str("Round"),
            Self::FakeRound => f.write_str("FakeRound"),
            Self::FlipBevel => f.write_str("FlipBevel"),
            _ => f.write_str("LineJoinType"),
        }
    }
}

impl std::fmt::Debug for layers::SymbolLayer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SymbolLayer").finish()
    }
}

impl std::fmt::Debug for layers::SymbolAnchorType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SymbolAnchorType").finish()
    }
}

#[cxx::bridge()]
/// Resource and configuration options for MapLibre.
pub mod resource_options {

    #[namespace = "mbgl"]
    extern "C++" {
        // Opaque types
        /// Resource configuration options.
        type ResourceOptions;

        // The name must be unique but for some reason this is required
        #[rust_name = "CxxTileServerOptions"]
        type TileServerOptions = super::tile_server_options::TileServerOptions;
    }

    #[namespace = "mln::bridge::resource_options"]
    unsafe extern "C++" {
        include!("resource_options.h");

        #[rust_name = "new"]
        fn new_() -> UniquePtr<ResourceOptions>;

        fn withApiKey(obj: Pin<&mut ResourceOptions>, key: &str);
        fn withAssetPath(obj: Pin<&mut ResourceOptions>, path: &[u8]);
        fn withCachePath(obj: Pin<&mut ResourceOptions>, path: &[u8]);

        fn withMaximumCacheSize(obj: Pin<&mut ResourceOptions>, max_cache_size: u64);
        fn withTileServerOptions(
            obj: Pin<&mut ResourceOptions>,
            tile_server_options: &CxxTileServerOptions,
        );
    }
}

impl std::fmt::Debug for resource_options::ResourceOptions {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ResourceOptions").finish()
    }
}

#[cxx::bridge()]
/// Tile server configuration options.
pub mod tile_server_options {
    #[namespace = "mbgl"]
    extern "C++" {
        // Opaque types
        /// Tile server configuration.
        type TileServerOptions;
    }

    #[namespace = "mln::bridge::tile_server_options"]
    unsafe extern "C++" {
        include!("tile_server_options.h");

        #[rust_name = "new_tile_server_options"]
        fn new_() -> UniquePtr<TileServerOptions>;

        fn withBaseUrl(obj: Pin<&mut TileServerOptions>, path: &[u8]);
        fn withUriSchemeAlias(obj: Pin<&mut TileServerOptions>, path: &[u8]);
        fn withSourceTemplate(
            obj: Pin<&mut TileServerOptions>,
            styleTemplate: &[u8],
            domainName: &[u8],
            versionPrefix: &[u8],
        );
        fn withSpritesTemplate(
            obj: Pin<&mut TileServerOptions>,
            spritesTemplate: &[u8],
            domainName: &[u8],
            versionPrefix: &[u8],
        );
        fn withGlyphsTemplate(
            obj: Pin<&mut TileServerOptions>,
            glyphsTemplate: &[u8],
            domainName: &[u8],
            versionPrefix: &[u8],
        );
        fn withTileTemplate(
            obj: Pin<&mut TileServerOptions>,
            tileTemplate: &[u8],
            domainName: &[u8],
            versionPrefix: &[u8],
        );
        fn withApiKeyParameterName(obj: Pin<&mut TileServerOptions>, apiKeyParameterName: &[u8]);
        fn setRequiresApiKey(obj: Pin<&mut TileServerOptions>, apiKeyRequired: bool);
    }
}

impl std::fmt::Debug for tile_server_options::TileServerOptions {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TileServerOptions").finish()
    }
}

impl Display for map_observer::MapLoadError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match *self {
            Self::StyleParseError => "Failed parsing style",
            Self::StyleLoadError => "Failed loading style",
            Self::NotFoundError => "Style not found",
            Self::UnknownError => "Unknown error",
            _ => "Unrecognized error",
        };
        write!(f, "{s}")
    }
}

impl std::error::Error for map_observer::MapLoadError {}

#[allow(clippy::borrow_as_ptr)]
#[cxx::bridge(namespace = "mln::bridge")]
/// Map observer callbacks and related types.
pub mod map_observer {
    #[namespace = "mln::bridge"]
    #[repr(u32)]
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    /// Camera change mode for map observer callbacks.
    pub enum MapObserverCameraChangeMode {
        /// Camera changed immediately without animation.
        Immediate,
        /// Camera changed using an animated transition.
        Animated,
    }

    #[namespace = "mbgl"]
    #[repr(u32)]
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    /// Map loading error types.
    pub enum MapLoadError {
        /// Style parsing error.
        StyleParseError,
        /// Style loading error.
        StyleLoadError,
        /// Resource not found.
        NotFoundError,
        /// Unknown error.
        UnknownError,
    }

    #[namespace = "mbgl"]
    extern "C++" {
        include!("mbgl/map/map_observer.hpp");
        type MapLoadError;
    }

    // Declarations for C++ with implementations in Rust
    extern "Rust" {
        type VoidCallback;
        type FinishRenderingFrameCallback;
        type CameraDidChangeCallback;
        type FailingLoadingMapCallback;

        fn void_callback(callback: &VoidCallback);
        fn finish_rendering_frame_callback(
            callback: &FinishRenderingFrameCallback,
            needsRepaint: bool,
            placementChanged: bool,
        );
        fn camera_did_change_callback(
            callback: &CameraDidChangeCallback,
            mode: MapObserverCameraChangeMode,
        );
        fn failing_loading_map_callback(
            callback: &FailingLoadingMapCallback,
            error: MapLoadError,
            what: &str,
        );
    }

    // Declarations for Rust with implementations in C++
    extern "C++" {
        include!("map_observer.h"); // Required to find functions below

        type MapObserverCameraChangeMode;

        // C++ Opaque types
        #[rust_name = "CxxMapObserver"]
        type MapObserver = super::ffi::MapObserver; // Created custom map observer
    }

    unsafe extern "C++" {
        // With `self: Pin<&mut MapObserver>` as first argument, it is a non static method of that object.
        // cxx searches for such a method
        /// Sets the callback for when loading of the map will start.
        fn setWillStartLoadingMapCallback(self: &CxxMapObserver, callback: Box<VoidCallback>);
        /// Sets the callback for when the style has finished loading.
        fn setFinishLoadingStyleCallback(self: &CxxMapObserver, callback: Box<VoidCallback>);
        /// Sets the callback for when the map becomes idle.
        fn setBecomeIdleCallback(self: &CxxMapObserver, callback: Box<VoidCallback>);
        /// Sets the callback for when loading of the map fails.
        fn setFailLoadingMapCallback(
            self: &CxxMapObserver,
            callback: Box<FailingLoadingMapCallback>,
        );
        /// Sets the callback for when a frame finishes rendering.
        fn setFinishRenderingFrameCallback(
            self: &CxxMapObserver,
            callback: Box<FinishRenderingFrameCallback>,
        );
        /// Sets the callback for when the camera finishes changing.
        fn setCameraDidChangeCallback(
            self: &CxxMapObserver,
            callback: Box<CameraDidChangeCallback>,
        );
    }
}

#[allow(clippy::borrow_as_ptr, clippy::unnecessary_box_returns, unused_qualifications)]
#[cxx::bridge(namespace = "mln::bridge")]
/// Rust-backed FileSource bridge. See `src/cpp/rust_file_source.{h,cpp}`
/// for the C++ side.
pub mod file_source {
    #[namespace = "mln::bridge"]
    #[repr(u8)]
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    /// Resource kinds — mirror of `mbgl::Resource::Kind`.
    pub enum ResourceKind {
        /// Unknown / unspecified resource kind.
        Unknown = 0,
        /// A style.json.
        Style = 1,
        /// A TileJSON / source descriptor.
        Source = 2,
        /// A single tile (vector or raster).
        Tile = 3,
        /// A glyph PBF range.
        Glyphs = 4,
        /// A sprite sheet PNG.
        SpriteImage = 5,
        /// A sprite sheet JSON.
        SpriteJSON = 6,
        /// A generic image resource.
        Image = 7,
    }

    #[namespace = "mln::bridge"]
    #[repr(u8)]
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    /// Error reason categories — mirror of `mbgl::Response::Error::Reason`.
    pub enum ErrorReason {
        /// mbgl's "no error" sentinel; not a meaningful error category.
        Success = 1,
        /// Resource not found at the requested URL.
        NotFound = 2,
        /// Server-side error (5xx and similar).
        Server = 3,
        /// Transport-level connection failure.
        Connection = 4,
        /// Rate-limit response.
        RateLimit = 5,
        /// Any other error.
        Other = 6,
    }

    #[namespace = "mln::bridge"]
    #[repr(u8)]
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    /// MapLibre Native file source slot a Rust `FileSource` can serve.
    ///
    /// Discriminants mirror `mbgl::FileSourceType`.
    pub enum FileSourceType {
        /// Bundled asset source (`asset://`).
        Asset = 0,
        /// Database-backed source for cache/offline storage.
        Database = 1,
        /// Local filesystem source (`file://`).
        FileSystem = 2,
        /// Network (HTTP) source.
        Network = 3,
        /// `MBTiles` source.
        Mbtiles = 4,
        /// `PMTiles` source.
        Pmtiles = 5,
        /// Top-level resource loader.
        ResourceLoader = 6,
    }

    /// Flat FFI shape mirroring `mbgl::Response`. Optional fields use a `has_*`
    /// flag; timestamps are Unix epoch seconds. Used both for delivering a
    /// response and for cache `forward`.
    #[derive(Debug)]
    pub struct RawResponse {
        pub has_error: bool,
        pub error_reason: ErrorReason,
        pub error_message: String,
        pub has_retry_after: bool,
        pub retry_after_epoch_s: i64,
        pub no_content: bool,
        pub not_modified: bool,
        pub must_revalidate: bool,
        pub has_data: bool,
        pub data: Vec<u8>,
        pub has_modified: bool,
        pub modified_epoch_s: i64,
        pub has_expires: bool,
        pub expires_epoch_s: i64,
        pub has_etag: bool,
        pub etag: String,
    }

    /// Flat FFI shape mirroring the mbgl resource request metadata used by
    /// Rust-backed file sources.
    #[derive(Debug)]
    pub struct RawResourceRequest {
        pub url: String,
        pub kind: ResourceKind,
        pub loading_methods: u8,
        pub is_volatile: bool,
        pub is_low_priority: bool,
        pub is_offline: bool,
        pub has_tile: bool,
        pub tile_url_template: String,
        pub tile_pixel_ratio: u8,
        pub tile_x: i32,
        pub tile_y: i32,
        pub tile_z: i8,
        pub has_data_range: bool,
        pub data_range_start: u64,
        pub data_range_end: u64,
        pub has_prior_modified: bool,
        pub prior_modified_epoch_s: i64,
        pub has_prior_expires: bool,
        pub prior_expires_epoch_s: i64,
        pub has_prior_etag: bool,
        pub prior_etag: String,
        pub has_prior_data: bool,
        pub prior_data: Vec<u8>,
        pub minimum_update_interval_ms: i64,
    }

    extern "Rust" {
        type BoxedFileSource;
        type RequestHandleFfi;

        /// Whether this source can serve the resource.
        fn can_request(self: &BoxedFileSource, request: &RawResourceRequest) -> bool;

        /// Begin a request.
        fn request(
            self: &BoxedFileSource,
            request: &RawResourceRequest,
            responder: SharedPtr<RequestState>,
        ) -> Box<RequestHandleFfi>;

        /// Store a response into a cache source (`FileSource::forward`).
        fn forward(
            self: &BoxedFileSource,
            request: &RawResourceRequest,
            response: &RawResponse,
            completion: SharedPtr<ForwardState>,
        );

        /// Run the request's cancellation hook.
        fn cancel(self: &RequestHandleFfi);
    }

    unsafe extern "C++" {
        include!("rust_file_source.h");
        type ResourceKind;
        type ErrorReason;
        type FileSourceType;
        /// Native per-request state, owned via `SharedPtr` and handed back to C++ on
        /// completion/cancellation (replaces a raw pointer token).
        type RequestState;
        /// Native per-`forward` state, owned via `SharedPtr`.
        type ForwardState;

        /// Register a Rust file source for `source_type`.
        fn register_rust_file_source(source_type: FileSourceType, source: Box<BoxedFileSource>);

        /// Deliver a response for `state` (an error response if dropped uncompleted).
        fn responder_complete(state: SharedPtr<RequestState>, response: &RawResponse);

        /// Cancel `state` without delivering a response.
        fn responder_cancel(state: SharedPtr<RequestState>);

        #[allow(dead_code)]
        fn roundtrip_response_for_test(response: &RawResponse) -> RawResponse;

        /// Notify mbgl that a cache `forward` call finished.
        fn forward_complete(state: SharedPtr<ForwardState>);
    }
}

#[allow(clippy::borrow_as_ptr, unused_qualifications)]
#[cxx::bridge(namespace = "mln::bridge")]
/// Core FFI definitions and types for the MapLibre bridge.
pub mod ffi {
    // CXX validates enum types against the C++ definition during compilation

    // The mbgl enums must be defined in the same namespace than on the C++ side
    #[namespace = "mbgl"]
    #[repr(u32)]
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    /// Map rendering mode configuration.
    pub enum MapMode {
        /// Continually updating map
        Continuous,
        /// Once-off still image of an arbitrary viewport
        Static,
        /// Once-off still image of a single tile
        Tile,
    }

    #[namespace = "mbgl"]
    #[repr(u32)]
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    /// Debug visualization options for map rendering.
    pub enum MapDebugOptions {
        /// No debug visualization.
        NoDebug = 0,
        /// Edges of tile boundaries are shown as thick, red lines.
        ///
        /// Can help diagnose tile clipping issues.
        TileBorders = 0b0000_0010, // 1 << 1
        /// Shows tile parsing status information.
        ParseStatus = 0b0000_0100, // 1 << 2
        /// Each tile shows a timestamp indicating when it was loaded.
        Timestamps = 0b0000_1000, // 1 << 3
        /// Edges of glyphs and symbols are shown as faint, green lines.
        ///
        /// Can help diagnose collision and label placement issues.
        Collision = 0b0001_0000, // 1 << 4
        /// Each drawing operation is replaced by a translucent fill.
        ///
        /// Overlapping drawing operations appear more prominent to help diagnose overdrawing.
        Overdraw = 0b0010_0000, // 1 << 5
        /// The stencil buffer is shown instead of the color buffer.
        ///
        /// Note: This option does nothing in Release builds of the SDK.
        StencilClip = 0b0100_0000, // 1 << 6
        /// The depth buffer is shown instead of the color buffer.
        ///
        /// Note: This option does nothing in Release builds of the SDK
        DepthBuffer = 0b1000_0000, // 1 << 7
    }

    /// A geographic coordinate.
    #[derive(Debug, Clone, Copy, PartialEq, Default)]
    pub struct LatLng {
        /// Latitude in degrees.
        pub lat: f64,
        /// Longitude in degrees.
        pub lng: f64,
    }

    /// A geographic bounding box.
    #[derive(Debug, Clone, Copy, PartialEq, Default)]
    pub struct LatLngBounds {
        /// Southwest corner of the bounds.
        pub southwest: LatLng,
        /// Northeast corner of the bounds.
        pub northeast: LatLng,
    }

    /// Insets from each edge of the renderer viewport.
    #[derive(Default, Debug, Clone, Copy, PartialEq)]
    pub struct EdgeInsets {
        /// Top inset in logical pixels.
        pub top: f64,
        /// Left inset in logical pixels.
        pub left: f64,
        /// Bottom inset in logical pixels.
        pub bottom: f64,
        /// Right inset in logical pixels.
        pub right: f64,
    }

    /// FFI representation of partial camera options.
    #[derive(Debug, Clone, Copy, PartialEq, Default)]
    pub struct FfiCameraOptions {
        pub has_center: bool,
        pub center: LatLng,
        pub has_center_altitude: bool,
        pub center_altitude: f64,
        pub has_padding: bool,
        pub padding: EdgeInsets,
        pub has_anchor: bool,
        pub anchor: ScreenCoordinate,
        pub has_zoom: bool,
        pub zoom: f64,
        pub has_bearing: bool,
        pub bearing: f64,
        pub has_pitch: bool,
        pub pitch: f64,
        pub has_roll: bool,
        pub roll: f64,
        pub has_fov: bool,
        pub fov: f64,
    }

    /// MapLibre Native Event Severity levels
    #[namespace = "mbgl"]
    #[repr(u8)]
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub enum EventSeverity {
        /// Debug severity level.
        Debug = 0,
        /// Info severity level.
        Info = 1,
        /// Warning severity level.
        Warning = 2,
        /// Error severity level.
        Error = 3,
    }

    /// MapLibre Native Event types
    #[namespace = "mbgl"]
    #[repr(u8)]
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
    pub enum Event {
        /// General event.
        General = 0,
        /// Setup event.
        Setup = 1,
        /// Shader event.
        Shader = 2,
        /// Style parsing event.
        ParseStyle = 3,
        /// Tile parsing event.
        ParseTile = 4,
        /// Render event.
        Render = 5,
        /// Style event.
        Style = 6,
        /// Database event.
        Database = 7,
        /// HTTP request event.
        HttpRequest = 8,
        /// Sprite event.
        Sprite = 9,
        /// Image event.
        Image = 10,
        /// OpenGL event.
        OpenGL = 11,
        /// JNI event.
        JNI = 12,
        /// Android event.
        Android = 13,
        /// Crash event.
        Crash = 14,
        /// Glyph event.
        Glyph = 15,
        /// Timing event.
        Timing = 16,
    }

    #[namespace = "mbgl"]
    extern "C++" {
        include!("mbgl/map/mode.hpp");

        type MapMode;
        type MapDebugOptions;
        // The name must be unique but for some reason this is required
        /// Resource configuration options.
        #[rust_name = "CxxResourceOptions"]
        type ResourceOptions = super::resource_options::ResourceOptions;
        /// Event severity enumeration.
        pub type EventSeverity;
        /// Event type enumeration.
        pub type Event;
    }

    #[namespace = "mbgl"]
    extern "C++" {
        /// Screen coordinate type.
        type ScreenCoordinate = super::ScreenCoordinate;
        /// Size type.
        type Size = super::Size;
    }

    #[namespace = "mbgl::style"]
    extern "C++" {
        /// Base source opaque type.
        #[rust_name = "CxxSource"]
        type Source = super::sources::Source;
        /// Base layer opaque type.
        #[rust_name = "CxxLayer"]
        type Layer = super::layers::Layer;
    }

    #[namespace = "mln::bridge::style::sources"]
    extern "C++" {
        /// Source handle opaque type.
        #[rust_name = "CxxSourceHandle"]
        type SourceHandle = super::sources::SourceHandle;
    }

    #[namespace = "mln::bridge::geojson"]
    extern "C++" {
        /// GeoJSON value opaque type.
        #[rust_name = "FfiGeoJson"]
        type GeoJson = super::geojson::GeoJson;
    }

    #[namespace = "mbgl::webgpu"]
    extern "C++" {
        #[cfg(feature = "wgpu")]
        type Texture2D;
    }

    #[namespace = ""]
    extern "C++" {
        #[cfg(feature = "wgpu")]
        type WGPUDevice = webgpu_shim::WGPUDeviceWrapper;
        #[cfg(feature = "wgpu")]
        type WGPUQueue = webgpu_shim::WGPUQueueWrapper;
        #[cfg(feature = "wgpu")]
        type WGPUTexture = webgpu_shim::WGPUTextureWrapper;
    }

    // Declarations for Rust with implementations in C++
    unsafe extern "C++" {
        include!("map_renderer.h");

        // C++ Opaque types
        /// Bridge image for rendering output.
        type BridgeImage;
        /// Map observer for handling map events.
        type MapObserver; // Created custom map observer
        /// Map renderer for rendering map content.
        type MapRenderer;
        /// In-flight render request.
        type RenderRequest;

        /// Ticks the current thread's MapLibre Native run loop once (non-blocking).
        fn currentThreadRunLoopTick();
        /// Blocks the calling thread, advancing the run loop until it is woken by
        /// pending work (a render or style-load completion), without busy-polling.
        fn currentThreadRunLoopWait();
        /// Wakes a thread blocked in `currentThreadRunLoopWait`. Only needed on the
        /// CoreFoundation (non-libuv) run loop; a no-op on the libuv backend.
        fn currentThreadRunLoopStop();
        /// Creates a new map renderer instance.
        #[allow(clippy::too_many_arguments)]
        fn MapRenderer_new(
            mapMode: MapMode,
            width: u32,
            height: u32,
            pixelRatio: f32,
            resource_options: &CxxResourceOptions,
        ) -> UniquePtr<MapRenderer>;
        /// Reads the current still image from the renderer.
        fn readStillImage(self: Pin<&mut MapRenderer>) -> UniquePtr<BridgeImage>;
        /// Gets the pixel data pointer from a bridge image.
        fn get(self: &BridgeImage) -> *const u8;
        /// Gets the size of a bridge image.
        fn size(self: &BridgeImage) -> Size;
        /// Gets the buffer length of a bridge image.
        fn bufferLength(self: &BridgeImage) -> usize;
        /// Renders a single frame.
        fn render_once(self: Pin<&mut MapRenderer>);
        /// Submits a render request without waiting for completion.
        fn submitRender(self: Pin<&mut MapRenderer>) -> UniquePtr<RenderRequest>;
        /// Calculates camera options that fit geographic bounds.
        fn cameraForLatLngBounds(
            self: Pin<&mut MapRenderer>,
            bounds: &LatLngBounds,
            padding: &EdgeInsets,
            bearing: f64,
            pitch: f64,
        ) -> FfiCameraOptions;
        /// Calculates camera options that fit geographic coordinates.
        fn cameraForLatLngs(
            self: Pin<&mut MapRenderer>,
            lat_lngs: &[LatLng],
            padding: &EdgeInsets,
            bearing: f64,
            pitch: f64,
        ) -> FfiCameraOptions;
        /// Calculates camera options that fit a GeoJSON value's geometry.
        fn cameraForGeoJson(
            self: Pin<&mut MapRenderer>,
            geojson: &FfiGeoJson,
            padding: &EdgeInsets,
            bearing: f64,
            pitch: f64,
        ) -> FfiCameraOptions;
        /// Returns whether a render request has completed.
        fn isReady(self: &RenderRequest) -> bool;
        /// Returns whether a completed render request failed.
        fn hasError(self: &RenderRequest) -> bool;
        /// Returns the native error message for a failed render request.
        fn errorMessage(self: &RenderRequest) -> String;
        /// Takes the rendered image bytes from a completed render request.
        fn takeImage(self: Pin<&mut RenderRequest>) -> UniquePtr<CxxString>;
        /// Sets debug visualization flags.
        fn setDebugFlags(self: Pin<&mut MapRenderer>, flags: MapDebugOptions);
        /// Jumps to the requested camera options.
        fn jumpTo(self: Pin<&mut MapRenderer>, camera: &FfiCameraOptions);
        /// Moves the camera by the given delta.
        fn moveBy(self: Pin<&mut MapRenderer>, delta: &ScreenCoordinate);
        /// Scales the camera based on the given scale factor.
        fn scaleBy(self: Pin<&mut MapRenderer>, scale: f64, pos: &ScreenCoordinate);
        /// Adjusts the camera pitch by the given delta in degrees.
        fn pitchBy(self: Pin<&mut MapRenderer>, pitch: f64);
        /// Rotates the camera using two screen coordinates that define the gesture delta.
        fn rotateBy(
            self: Pin<&mut MapRenderer>,
            first: &ScreenCoordinate,
            second: &ScreenCoordinate,
        );
        /// Loads a style from a URL.
        fn style_load_from_url(self: Pin<&mut MapRenderer>, url: &str);
        /// Loads a style from a JSON string.
        fn style_load_from_json(self: Pin<&mut MapRenderer>, json: &str);
        /// Sets the renderer size.
        fn setSize(self: Pin<&mut MapRenderer>, size: &Size);
        /// Gets the map observer.
        fn observer(self: Pin<&mut MapRenderer>) -> SharedPtr<MapObserver>;
        /// Adds an image to the style.
        fn style_add_image(
            self: Pin<&mut MapRenderer>,
            id: &str,
            data: &[u8],
            size: Size,
            pixel_ratio: f32,
            signed_distance_field: bool,
        ) -> Result<()>;
        /// Removes an image from the style.
        fn style_remove_image(self: Pin<&mut MapRenderer>, id: &str);
        /// Adds a source to the style.
        fn style_add_source(
            self: Pin<&mut MapRenderer>,
            source: UniquePtr<CxxSource>,
        ) -> Result<()>;
        /// Gets a mutable reference to a style source by ID.
        fn style_get_source_mut(
            self: Pin<&mut MapRenderer>,
            id: &str,
        ) -> UniquePtr<CxxSourceHandle>;
        /// Removes a source from the style by ID.
        fn style_remove_source(self: Pin<&mut MapRenderer>, id: &str);
        /// Adds a layer to the style, optionally before an existing layer
        /// (pass an empty `before_id` to append to the end of the style).
        fn style_add_layer(
            self: Pin<&mut MapRenderer>,
            layer: UniquePtr<CxxLayer>,
            before_id: &str,
        ) -> Result<()>;
        /// Removes a layer from the style by ID and returns it.
        fn style_remove_layer(self: Pin<&mut MapRenderer>, id: &str) -> UniquePtr<CxxLayer>;

        #[cfg(feature = "wgpu")]
        fn setDeviceAndQueue(self: Pin<&mut MapRenderer>, device: WGPUDevice, queue: WGPUQueue);

        #[cfg(feature = "wgpu")]
        fn takeTexture(self: Pin<&mut MapRenderer>) -> SharedPtr<Texture2D>;
    }

    #[cfg(feature = "wgpu")]
    #[namespace = "mln::bridge::texture"]
    unsafe extern "C++" {
        include!("texture.h");

        fn getWGPUTexture(texture: &SharedPtr<Texture2D>) -> WGPUTexture;
    }

    // Declarations for C++ with implementations in Rust
    extern "Rust" {
        /// Bridge logging from C++ to Rust log crate
        fn log_from_cpp(severity: EventSeverity, event: Event, code: i64, message: &str);
    }

    unsafe extern "C++" {
        include!("rust_log_observer.h");

        /// Enables or disables logging from a separate thread.
        fn Log_useLogThread(enable: bool);
    }
}

impl std::fmt::Debug for ffi::BridgeImage {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BridgeImage").finish()
    }
}

impl std::fmt::Debug for ffi::MapObserver {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MapObserver").finish()
    }
}

impl std::fmt::Debug for ffi::MapRenderer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MapRenderer").finish()
    }
}

unsafe impl cxx::ExternType for Size {
    type Id = cxx::type_id!("mbgl::Size");
    type Kind = cxx::kind::Trivial;
}

unsafe impl cxx::ExternType for ScreenCoordinate {
    type Id = cxx::type_id!("mbgl::ScreenCoordinate");
    type Kind = cxx::kind::Trivial;
}

#[cfg(test)]
mod test {
    use crate::ScreenCoordinate;

    #[test]
    fn screen_coordinate_diff() {
        let s1 = ScreenCoordinate { x: 5., y: -1. };
        let s2 = ScreenCoordinate { x: 3., y: -10. };

        let res = s1 - s2;
        assert!((res.x - 2.).abs() < 0.00001);
        assert!((res.y - 9.).abs() < 0.00001);
    }
}