mln-rs-style 0.0.21

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
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
//! The style's JSON-text entry points: they take and return style-spec JSON
//! as text, so a caller on another thread hands over text and the style's
//! own thread parses it.

use std::sync::Arc;
use std::time::Duration;

use crate::constants::DEFAULT_TRANSITION_DURATION;
use crate::conversion::borrowed::{Json, borrowed_json_to_value, member, members};
use crate::conversion::{Converter, Convertible, value_to_json};
use crate::error::Error;
use crate::expression::GlobalStateMap;
use crate::feature::Value;
use crate::layer::{Layer, SourceKind, convert_layer};
use crate::light::{Light, convert_light};
use crate::member::is_extension_member;
use crate::projection::{Projection, convert_projection};
use crate::property::table::{Kind, Properties, StyleProperty, defined_properties};
use crate::property::transition::TransitionOptions;
use crate::sky::convert_sky;
use crate::style::Style;
use crate::style::edit::Edit;
use crate::terrain::convert_terrain;

impl Style {
    /// Adds the [layer](https://maplibre.org/maplibre-style-spec/layers/)
    /// that `json` describes, below the layer named `before_layer_id`, or
    /// above every layer when it is `None`.
    ///
    /// Fails with [`ErrorKind::NotFound`](crate::error::ErrorKind) if the style has no layer named
    /// `before_layer_id` or no source with the ID that the layer uses, with
    /// `AlreadyExists` if the style has a layer with the new layer's ID, and
    /// with `InvalidArgument` if `json` isn't a valid layer or its source
    /// can't feed a layer of its type.
    pub fn add_layer_json(&mut self, json: &str, before_layer_id: Option<&str>) -> Result<(), Error> {
        if let Some(before) = before_layer_id
            && self.layer(before).is_none()
        {
            return Err(Error::not_found(format!("before_layer_id {} does not exist", quote(before))));
        }

        let value = parse(json, "style layer")?;
        let mut error = crate::conversion::Error::default();
        let Some(layer) = convert_layer(self.registry(), &Convertible::from(&value), &mut error) else {
            return Err(conversion_error("style layer", &error));
        };

        self.add_layer(layer, before_layer_id)
    }

    /// Adds `layer` below the layer named `before_layer_id`, or above every
    /// layer when it is `None`.
    ///
    /// Fails as [`Style::add_layer_json`] does for a valid layer. The style
    /// drops `layer`, and what it keeps alive, when it fails.
    pub fn add_layer(&mut self, layer: Layer, before_layer_id: Option<&str>) -> Result<(), Error> {
        if let Some(before) = before_layer_id
            && self.layer(before).is_none()
        {
            return Err(Error::not_found(format!("before_layer_id {} does not exist", quote(before))));
        }
        let id = layer.id();
        if self.layer(id).is_some() {
            return Err(Error::already_exists(format!("layer {} already exists", quote(id))));
        }
        let type_info = layer.info();
        if type_info.source != SourceKind::None {
            let source_id = layer.source_id();
            let Some(source) = self.source(source_id) else {
                return Err(Error::not_found(format!(
                    "layer {} uses source {}, which does not exist",
                    quote(id),
                    quote(source_id)
                )));
            };
            if source.info().layer_kind != type_info.source {
                return Err(Error::invalid_argument(format!(
                    "source {} does not support {} layer {}",
                    quote(source_id),
                    type_info.name,
                    quote(id)
                )));
            }
        }
        self.apply(Edit::AddLayer { layer, before: before_layer_id.map(str::to_owned) })
    }

    /// Moves the layer named `id` below the layer named `before_layer_id`,
    /// or above every layer when it is `None`. A move below itself changes
    /// nothing.
    ///
    /// Fails with [`ErrorKind::NotFound`](crate::error::ErrorKind) if the style has no layer
    /// named `id` or `before_layer_id`.
    pub fn move_layer(&mut self, id: &str, before_layer_id: Option<&str>) -> Result<(), Error> {
        if self.layer(id).is_none() {
            return Err(layer_not_found(id));
        }
        if let Some(before) = before_layer_id {
            if before == id {
                return Ok(());
            }
            if self.layer(before).is_none() {
                return Err(Error::not_found(format!("before_layer_id {} does not exist", quote(before))));
            }
        }
        self.apply(Edit::MoveLayer { id: id.to_owned(), before: before_layer_id.map(str::to_owned) })
    }

    /// The layer named `id` as a
    /// [layer](https://maplibre.org/maplibre-style-spec/layers/) object, or
    /// `None` if the style has no such layer. Adding the JSON back gives the
    /// same layer.
    #[must_use]
    pub fn layer_json(&self, id: &str) -> Option<String> {
        self.layer(id).map(|layer| json_text(&layer.serialize()))
    }

    /// Sets the property `name` of the layer named `layer_id` to the JSON
    /// value `json`.
    ///
    /// `name` is one of the layer's layout or paint
    /// [properties](https://maplibre.org/maplibre-style-spec/layers/),
    /// `filter`, `minzoom`, `maxzoom`, `source` or `source-layer`, or an
    /// extension member (`<namespace>:<name>`). A `null` resets any of these
    /// but `source` and `source-layer` to its default, and unsets an
    /// extension member. The value of an extension member that the registry
    /// lacks is kept as it is, with a warning.
    ///
    /// Fails with [`ErrorKind::NotFound`](crate::error::ErrorKind) if the style has no layer named
    /// `layer_id`, and with `InvalidArgument` if `json` isn't valid JSON,
    /// the layer has no property `name` or the value doesn't fit it.
    pub fn set_layer_property_json(&mut self, layer_id: &str, name: &str, json: &str) -> Result<(), Error> {
        let context = format!("property {} of layer {}", quote(name), quote(layer_id));
        let value = parse_value(json, &context)?;
        self.apply(Edit::SetLayerProperty { layer: layer_id.to_owned(), name: name.to_owned(), value })
    }

    /// The JSON value of the property `name` of the layer named `layer_id`,
    /// or `None` if the layer doesn't set it. `name` is one of the names
    /// [`set_layer_property_json`](Self::set_layer_property_json) takes. The
    /// value can differ in form from the value that was set: a color set as
    /// `"#ff0000"` reads back as `["rgba",255.0,0.0,0.0,1.0]`.
    ///
    /// Fails with [`ErrorKind::NotFound`](crate::error::ErrorKind) if the style
    /// has no layer named `layer_id`.
    pub fn layer_property_json(&self, layer_id: &str, name: &str) -> Result<Option<String>, Error> {
        let Some(layer) = self.layer(layer_id) else {
            return Err(layer_not_found(layer_id));
        };
        let value = match name {
            "filter" => {
                let filter = layer.filter();
                filter.is_set().then(|| filter.serialize())
            }
            "source" | "source-layer" => {
                let source = if name == "source" { layer.source_id() } else { layer.source_layer() };
                (!source.is_empty()).then(|| Value::String(source.to_owned()))
            }
            "minzoom" => {
                let zoom = layer.min_zoom();
                (zoom != f64::NEG_INFINITY).then_some(Value::Double(zoom))
            }
            "maxzoom" => {
                let zoom = layer.max_zoom();
                (zoom != f64::INFINITY).then_some(Value::Double(zoom))
            }
            _ if is_extension_member(name) => layer.members().json(name).cloned(),
            _ => {
                let property = layer.property(name);
                (property.kind() != Kind::Undefined).then(|| property.value().clone())
            }
        };
        Ok(value.as_ref().map(json_text))
    }

    /// Sets the [root property](https://maplibre.org/maplibre-style-spec/root/)
    /// `name` to the JSON value `json`, without reloading the style. A `null`
    /// resets it to its default, as if the style didn't set it.
    ///
    /// `light`, `sky`, `terrain`, `projection`, `state`, `transition` and the
    /// extension members (`<namespace>:<name>`) change. A `state` sets every
    /// global state property to its new default. A `transition` keeps
    /// whether placements fade, which the style spec's transition doesn't
    /// hold. The value of an extension member that the registry lacks is
    /// kept as it is, with a warning.
    ///
    /// Fails with [`ErrorKind::InvalidArgument`](crate::error::ErrorKind) if
    /// `name` isn't a root property, or is `sources` or `layers`, or if
    /// `json` isn't a valid value for it; and with `Unsupported` for the root properties that only
    /// take effect as a style loads (the default camera, the sprite, the
    /// glyphs and font faces, and the metadata).
    pub fn set_root_property_json(&mut self, name: &str, json: &str) -> Result<(), Error> {
        require_root_property(name)?;
        let context = format!("style root property {}", quote(name));
        if is_extension_member(name) {
            let value = parse_value(json, &context)?;
            return self.apply(Edit::SetMember { name: name.to_owned(), value });
        }
        let value = parse(json, &context)?;
        let convertible = Convertible::from(&value);
        let mut error = crate::conversion::Error::default();
        let functions = Arc::clone(self.registry().functions());
        match name {
            "light" => {
                let light = if value.is_null() {
                    Light::default()
                } else {
                    convert_light(&convertible, &functions, &mut error)
                        .ok_or_else(|| conversion_error(&context, &error))?
                };
                self.apply(Edit::SetLight(light))?;
            }
            "sky" => {
                let sky = if value.is_null() {
                    None
                } else {
                    Some(
                        convert_sky(&convertible, &functions, &mut error)
                            .ok_or_else(|| conversion_error(&context, &error))?,
                    )
                };
                self.apply(Edit::SetSky(sky))?;
            }
            "terrain" => {
                let terrain = if value.is_null() {
                    None
                } else {
                    Some(convert_terrain(&convertible, &mut error).ok_or_else(|| conversion_error(&context, &error))?)
                };
                self.apply(Edit::SetTerrain(terrain))?;
            }
            "projection" => {
                let projection = if value.is_null() {
                    Projection::default()
                } else {
                    convert_projection(&convertible, &functions, &mut error)
                        .ok_or_else(|| conversion_error(&context, &error))?
                };
                self.apply(Edit::SetProjection(projection))?;
            }
            "state" => {
                let defaults = if value.is_null() { GlobalStateMap::new() } else { state_defaults(&value, &context)? };
                self.apply(Edit::SetGlobalStateDefaults(defaults))?;
            }
            "transition" => {
                let placement = self.transition_options().enable_placement_transitions;
                let mut transition = if value.is_null() {
                    TransitionOptions::with_duration(DEFAULT_TRANSITION_DURATION)
                } else {
                    TransitionOptions::convert(&convertible, &mut error)
                        .ok_or_else(|| conversion_error(&context, &error))?
                };
                transition.enable_placement_transitions = placement;
                self.apply(Edit::SetTransition(transition))?;
            }
            _ => return Err(Error::unsupported(format!("{context} can't change while the style is loaded"))),
        }
        Ok(())
    }

    /// The JSON value of the [root property](https://maplibre.org/maplibre-style-spec/root/)
    /// `name`, or `None` if neither the style nor the map sets it: the map
    /// gives a style without a `transition` a default duration. The value can
    /// differ in form from the value that was set, as with
    /// [`layer_property_json`](Self::layer_property_json). The root
    /// properties that [`set_root_property_json`](Self::set_root_property_json)
    /// can't change read as the style loaded them. `terrain` reads as the
    /// style holds it: with its default exaggeration filled in, and absent if
    /// the style dropped it as it loaded.
    ///
    /// Fails with [`ErrorKind::InvalidArgument`](crate::error::ErrorKind) if
    /// `name` isn't a root property, or is `sources` or `layers`.
    pub fn root_property_json(&self, name: &str) -> Result<Option<String>, Error> {
        require_root_property(name)?;
        if is_extension_member(name) {
            return Ok(self.members().json(name).map(json_text));
        }
        let value = match name {
            "light" => defined_properties_json(&**self.light()),
            "sky" => self.sky().and_then(|sky| defined_properties_json(&**sky)),
            "terrain" => self.terrain().map(|terrain| {
                serde_json::json!({ "source": terrain.source, "exaggeration": shortest_f64(terrain.exaggeration) })
            }),
            "projection" => {
                let projection = self.projection();
                properties_json(&["type"], |name| projection.property(name))
            }
            "state" => {
                let defaults = self.global_state_defaults();
                (!defaults.is_empty()).then(|| {
                    let definitions = defaults.iter().map(|(name, default)| {
                        (name.clone(), serde_json::json!({ "default": value_to_json(default) }))
                    });
                    serde_json::Value::Object(definitions.collect())
                })
            }
            "transition" => {
                let transition = self.transition_options();
                let members = [("duration", transition.duration), ("delay", transition.delay)]
                    .into_iter()
                    .filter_map(|(name, duration)| Some((name.to_owned(), milliseconds_json(duration?))))
                    .collect::<serde_json::Map<_, _>>();
                (!members.is_empty()).then_some(serde_json::Value::Object(members))
            }
            _ => serde_json::from_str::<serde_json::Value>(self.json())
                .ok()
                .and_then(|mut document| document.get_mut(name).map(serde_json::Value::take)),
        };
        Ok(value.map(|value| value.to_string()))
    }

    /// Sets the property `name` of the style's light to the JSON value
    /// `json`. A `null` resets it to its default.
    ///
    /// Fails with [`ErrorKind::InvalidArgument`](crate::error::ErrorKind) if `json` isn't valid
    /// JSON, the light has no property `name` or the value doesn't fit it.
    pub fn set_light_property_json(&mut self, name: &str, json: &str) -> Result<(), Error> {
        let context = format!("style light property {}", quote(name));
        let value = parse_value(json, &context)?;
        self.apply(Edit::SetLightProperty { name: name.to_owned(), value })
    }

    /// The JSON value of the property `name` of the style's light, or `None`
    /// if the light doesn't set it. The value can differ in form from the
    /// value that was set, as with
    /// [`layer_property_json`](Self::layer_property_json).
    #[must_use]
    pub fn light_property_json(&self, name: &str) -> Option<String> {
        let property = self.light().property(name);
        (property.kind() != Kind::Undefined).then(|| json_text(property.value()))
    }

    /// Sets the value `name` that the style's `global-state` expressions
    /// read to the JSON value `json`. A `null` resets it to the default that
    /// the style's `state` gives it.
    ///
    /// Fails with [`ErrorKind::InvalidArgument`](crate::error::ErrorKind) if `json` isn't valid
    /// JSON.
    pub fn set_global_state_property_json(&mut self, name: &str, json: &str) -> Result<(), Error> {
        let value = parse(json, &format!("global state property {}", quote(name)))?;
        self.apply(Edit::SetGlobalState { name: name.to_owned(), value: borrowed_json_to_value(&value) })
    }

    /// The values that the style's `global-state` expressions read, as a
    /// JSON object.
    #[must_use]
    pub fn global_state_json(&self) -> String {
        json_text(&Value::Object((**self.global_state()).clone()))
    }
}

/// The style spec's root properties, other than `sources` and `layers`,
/// which the source and layer calls change.
const ROOT_PROPERTIES: &[&str] = &[
    "version",
    "name",
    "metadata",
    "center",
    "centerAltitude",
    "zoom",
    "bearing",
    "pitch",
    "roll",
    "state",
    "light",
    "sky",
    "projection",
    "terrain",
    "sprite",
    "glyphs",
    "font-faces",
    "transition",
];

fn require_root_property(name: &str) -> Result<(), Error> {
    if ROOT_PROPERTIES.contains(&name) || is_extension_member(name) {
        Ok(())
    } else {
        Err(Error::invalid_argument(format!("{} is not a style root property that this call takes", quote(name))))
    }
}

/// An object of the properties of `owner` that are set, or `None` when none
/// is.
fn defined_properties_json<S: Properties>(owner: &S) -> Option<serde_json::Value> {
    let members = defined_properties(owner)
        .into_iter()
        .map(|(name, _, property)| (name, value_to_json(property.value())))
        .collect::<serde_json::Map<_, _>>();
    (!members.is_empty()).then_some(serde_json::Value::Object(members))
}

/// An object of the properties that are set, or `None` when none is.
fn properties_json<'a>(names: &[&'a str], property: impl Fn(&'a str) -> StyleProperty) -> Option<serde_json::Value> {
    let members = names
        .iter()
        .filter_map(|&name| {
            let property = property(name);
            (property.kind() != Kind::Undefined).then(|| (name.to_owned(), value_to_json(property.value())))
        })
        .collect::<serde_json::Map<_, _>>();
    (!members.is_empty()).then_some(serde_json::Value::Object(members))
}

/// Whole milliseconds as an integer, and finer durations with their
/// fraction.
fn milliseconds_json(duration: Duration) -> serde_json::Value {
    if duration.subsec_nanos().is_multiple_of(1_000_000) {
        serde_json::Value::from(u64::try_from(duration.as_millis()).unwrap_or(u64::MAX))
    } else {
        serde_json::Value::from(duration.as_nanos() as f64 / 1e6)
    }
}

/// `value` as the `f64` with the same shortest decimal digits, so that an
/// `f32` of 0.3 reads back as 0.3.
fn shortest_f64(value: f32) -> f64 {
    value.to_string().parse().unwrap_or_else(|_| f64::from(value))
}

/// The defaults of a root `state` object: each member is an object, whose
/// `default` is the property's default.
fn state_defaults(value: &Json<'_>, context: &str) -> Result<GlobalStateMap, Error> {
    let Some(object) = value.as_object() else {
        return Err(Error::invalid_argument(format!("{context}: state must be an object")));
    };
    let mut defaults = GlobalStateMap::new();
    for (name, definition) in members(object) {
        let Some(definition) = definition.as_object() else {
            return Err(Error::invalid_argument(format!(
                "{context}: state property {} must be an object",
                quote(name)
            )));
        };
        if let Some(default) = member(definition, "default") {
            defaults.insert(name.to_owned(), borrowed_json_to_value(default));
        }
    }
    Ok(defaults)
}

fn quote(id: &str) -> String {
    format!("\"{id}\"")
}

/// `json` as an owned value, for an [`Edit`].
fn parse_value(json: &str, context: &str) -> Result<serde_json::Value, Error> {
    serde_json::from_str(json).map_err(|error| Error::invalid_argument(format!("{context} is not valid JSON: {error}")))
}

fn parse<'a>(json: &'a str, context: &str) -> Result<Json<'a>, Error> {
    serde_json::from_str(json).map_err(|error| Error::invalid_argument(format!("{context} is not valid JSON: {error}")))
}

fn conversion_error(context: &str, error: &crate::conversion::Error) -> Error {
    Error::invalid_argument(format!("{context}: {}", error.message))
}

fn layer_not_found(id: &str) -> Error {
    Error::not_found(format!("layer {} does not exist", quote(id)))
}

fn json_text(value: &Value) -> String {
    value_to_json(value).to_string()
}