Skip to main content

maplibre_native_ffi/generated/
style_expression.rs

1// Generated from C headers by tools/bindgen. Do not edit.
2use super::*;
3
4native_owner! {
5    /// Owns one native handle.
6    ///
7    /// A parsed style expression.
8    ///
9    /// See `mln_style_expression` in the
10    /// [C API reference](https://maplibre.org/maplibre-native-ffi/reference/c/base_8h.html).
11    pub struct StyleExpressionHandle(mln_style_expression) dispose |raw| { unsafe { sys::mln_style_expression_release(raw) }; Ok(()) };
12    /// Returns the native handle value.
13    fn id;
14}
15
16impl StyleExpressionHandle {
17    /// Parses a style
18    /// \[expression\](https://maplibre.org/maplibre-style-spec/expressions/) to
19    /// evaluate without a map.
20    ///
21    /// See `mln_style_expression_create` in the
22    /// [C API reference](https://maplibre.org/maplibre-native-ffi/reference/c/style__expression_8h.html).
23    ///
24    /// # Errors
25    ///
26    /// - `Status::InvalidArgument` when `expression` isn't a valid expression,
27    ///   or its result can't have the expected type.
28    pub fn create(
29        expression: &str,
30        options: Option<&StyleExpressionOptions>,
31    ) -> Result<StyleExpressionHandle> {
32        let mut call = Call::global();
33        crate::abi::validate_abi_version()?;
34        let mut out_expression = sys::mln_style_expression(0);
35        let expression = call.input(&expression)?;
36        let options = call.optional_reference(options.as_ref())?;
37        call.status(|_, out_diagnostic| unsafe {
38            sys::mln_style_expression_create(
39                expression,
40                options,
41                &mut out_expression,
42                out_diagnostic,
43            )
44        })?;
45        Ok(StyleExpressionHandle::adopt(out_expression)?)
46    }
47
48    /// Evaluates a style expression and writes its result as JSON.
49    ///
50    /// A number in the result that isn't finite becomes null. A color is
51    /// `["rgba", r, g, b, a]`, with red, green, and blue from 0 to 255 and
52    /// alpha from 0 to 1, each exact only to single precision. Formatted text
53    /// is a `["format", …]` expression, an image is `{"name": …, "available":
54    /// …}`, padding is `[top, right, bottom, left]`, and a variable anchor
55    /// offset is `[anchor, [x, y], …]`.
56    ///
57    /// See `mln_style_expression_evaluate` in the
58    /// [C API reference](https://maplibre.org/maplibre-native-ffi/reference/c/style__expression_8h.html).
59    ///
60    /// # Errors
61    ///
62    /// - `Status::InvalidArgument` when the expression fails to evaluate, such
63    ///   as when it reads an input that `inputs` doesn't set.
64    pub fn evaluate(&self, inputs: Option<&StyleExpressionInputs<'_>>) -> Result<String> {
65        let mut call = self.inner.call();
66        let mut out_json = Vec::new();
67        let inputs = call.optional_reference(inputs.as_ref())?;
68        call.status(|expression, out_diagnostic| unsafe {
69            convert::caller_sized_text(&mut out_json, |out_json, json_capacity, out_json_length| {
70                sys::mln_style_expression_evaluate(
71                    expression,
72                    inputs,
73                    out_json,
74                    json_capacity,
75                    out_json_length,
76                    out_diagnostic,
77                )
78            })
79        })?;
80        Ok(convert::utf8(out_json)?)
81    }
82
83    /// Reads what a style expression's result depends on.
84    ///
85    /// See `mln_style_expression_get_info` in the
86    /// [C API reference](https://maplibre.org/maplibre-native-ffi/reference/c/style__expression_8h.html).
87    pub fn get_info(&self) -> Result<StyleExpressionInfo> {
88        let mut call = self.inner.call();
89        let mut out_info: sys::mln_style_expression_info = unsafe { std::mem::zeroed() };
90        out_info.size = std::mem::size_of::<sys::mln_style_expression_info>() as _;
91        call.status(|expression, out_diagnostic| unsafe {
92            sys::mln_style_expression_get_info(expression, &mut out_info, out_diagnostic)
93        })?;
94        Ok(unsafe { from_native(out_info) }?)
95    }
96
97    /// Releases a parsed style expression.
98    ///
99    /// See `mln_style_expression_release` in the
100    /// [C API reference](https://maplibre.org/maplibre-native-ffi/reference/c/style__expression_8h.html).
101    pub fn release(self) -> std::result::Result<(), ReleaseError<Self>> {
102        self.consume(|expression| {
103            let mut call = Call::new(expression);
104            call.run(|expression| unsafe { sys::mln_style_expression_release(expression) });
105            Ok(())
106        })
107    }
108}