leaflet_velocity_sys/
lib.rs

1//! # leaflet-velocity-sys
2//!
3//! Low-level wasm-bindgen bindings for [leaflet-velocity.js](https://github.com/onaci/leaflet-velocity).
4//!
5//! This crate provides raw JavaScript bindings. For a higher-level Leptos component,
6//! see the [`leptos-leaflet-velocity`](https://crates.io/crates/leptos-leaflet-velocity) crate.
7
8use js_sys::Array;
9#[cfg(not(feature = "leptos"))]
10use leaflet::{Layer, Map};
11#[cfg(feature = "leptos")]
12use leptos_leaflet::leaflet::{Layer, Map};
13use paste::paste;
14use wasm_bindgen::prelude::*;
15
16#[wasm_bindgen]
17extern "C" {
18    #[wasm_bindgen(extends = Layer)]
19    #[derive(Debug, Clone)]
20    pub type VelocityLayer;
21
22    #[wasm_bindgen(js_namespace = L, js_name = "velocityLayer")]
23    pub fn velocity_layer(options: &VelocityLayerOption) -> VelocityLayer;
24
25    #[wasm_bindgen(method, js_name = addTo)]
26    pub fn add_to(this: &VelocityLayer, map: &Map) -> VelocityLayer;
27
28    /// Clears the wind animation from the layer.
29    #[wasm_bindgen(method, js_name = "_clearWind")]
30    pub fn _clear_wind(this: &VelocityLayer);
31
32    /// Sets the options for the velocity layer.
33    #[wasm_bindgen(method, js_name = "setOptions")]
34    pub fn set_options(this: &VelocityLayer, options: &VelocityLayerOption);
35}
36
37macro_rules! create_object_with_properties {
38    (($t:ident, $t_js:ident), $(($rust:ident, $js:ident, $b:ty)),+) => {
39        $crate::paste! {
40            #[wasm_bindgen]
41            extern "C" {
42                #[wasm_bindgen (extends = js_sys::Object , js_name = $t_js)]
43                #[derive(Debug, Clone, PartialEq, Eq)]
44                pub type $t;
45
46                $(
47                #[wasm_bindgen(method, getter, js_name = $js)]
48                pub fn $rust(this: &$t) -> $b;
49                )*
50
51                $(
52                #[wasm_bindgen(method, setter, js_name = $js)]
53                pub fn [<set_ $rust>](this: &$t, val: $b);
54                )*
55            }
56        }
57        impl $t {
58            #[must_use]
59            pub fn new() -> Self {
60                #[allow(unused_mut)]
61                let mut r = JsCast::unchecked_into(js_sys::Object::new());
62                r
63            }
64        }
65
66        impl Default for $t {
67            fn default() -> Self {
68                Self::new()
69            }
70        }
71    };
72}
73
74create_object_with_properties!(
75    (VelocityDataHeader, VelocityDataHeader),
76    (parameter_category, parameterCategory, usize),
77    (parameter_number, parameterNumber, usize),
78    (nx, nx, usize),
79    (ny, ny, usize),
80    (lo1, lo1, f64),
81    (la1, la1, f64),
82    (lo2, lo2, f64),
83    (la2, la2, f64),
84    (dx, dx, f64),
85    (dy, dy, f64)
86);
87
88create_object_with_properties!(
89    (VelocityLayerData, VelocityLayerData),
90    (header, header, VelocityDataHeader),
91    (data, data, Array)
92);
93
94create_object_with_properties!(
95    (VelocityLayerOption, VelocityLayerOption),
96    (display_values, displayValues, bool),
97    (max_velocity, maxVelocity, f64),
98    (frame_rate, frameRate, f64),
99    (color_scale, colorScale, Array),
100    (line_width, lineWidth, f64),
101    (particle_multiplier, particleMultiplier, f64),
102    (particle_age, particleAge, f64),
103    (velocity_scale, velocityScale, f64),
104    (keyboard, keyboard, bool),
105    (data, data, Array)
106);