mln_style/style/edit.rs
1//! Changes to a style, as values: [`Edit`], which
2//! [`Style::apply`](crate::Style::apply) makes.
3
4use std::sync::Arc;
5
6use crate::error::Error;
7use crate::expression::GlobalStateMap;
8use crate::feature::Value;
9use crate::filter::Filter;
10use crate::image;
11use crate::layer::Layer;
12use crate::light::Light;
13use crate::projection::Projection;
14use crate::property::transition::TransitionOptions;
15use crate::sky::Sky;
16use crate::source::{ErasedDescription, Source, SourceCommon, SourceData, SourceInstance, StyleSourceType};
17use crate::terrain::Terrain;
18use crate::types::VisibilityType;
19
20/// A change to a style, which [`Style::apply`](crate::Style::apply) makes.
21#[expect(clippy::large_enum_variant, reason = "an edit is made and applied at once, not stored")]
22pub enum Edit {
23 /// Adds `layer` below the layer `before`, or on top when `before` is
24 /// `None` or names no layer.
25 AddLayer {
26 layer: Layer,
27 before: Option<String>,
28 },
29 RemoveLayer(String),
30 /// Moves the layer `id` below the layer `before`, or on top when
31 /// `before` is `None` or names no layer. A move below itself changes
32 /// nothing.
33 MoveLayer {
34 id: String,
35 before: Option<String>,
36 },
37 /// Replaces the layer with `layer`'s id.
38 SetLayer(Layer),
39 /// Sets a layer type's property, `<name>-transition`, `filter`,
40 /// `minzoom`, `maxzoom`, `visibility`, `source`, `source-layer` or an
41 /// extension member from a style JSON value; null resets the ones that
42 /// have a default, and unsets an extension member.
43 SetLayerProperty {
44 layer: String,
45 name: String,
46 value: serde_json::Value,
47 },
48 SetLayerFilter {
49 layer: String,
50 filter: Filter,
51 },
52 SetLayerZoomRange {
53 layer: String,
54 min: f64,
55 max: f64,
56 },
57 SetLayerVisibility {
58 layer: String,
59 visibility: VisibilityType,
60 },
61 SetLight(Light),
62 /// Sets the sky; `None` removes it.
63 SetSky(Option<Sky>),
64 /// Sets the terrain; `None` removes it. Fails with `InvalidArgument` if
65 /// the terrain's exaggeration is below 0 or not finite, or its source is
66 /// in the style and isn't a `raster-dem` source.
67 SetTerrain(Option<Terrain>),
68 SetProjection(Projection),
69 /// Sets the extension member `name` of the style's root from a style
70 /// JSON value; null unsets it. Fails with `InvalidArgument` for a value
71 /// that the registry's member of the name doesn't take.
72 SetMember {
73 name: String,
74 value: serde_json::Value,
75 },
76 SetTransition(TransitionOptions),
77 /// Sets a value that `global-state` expressions read. Null resets it to
78 /// the default that the style's `state` gives.
79 SetGlobalState {
80 name: String,
81 value: Value,
82 },
83 /// Replaces the defaults that the style's root `state` gives, and sets
84 /// every global state value to its new default, as a style that loads
85 /// with this `state` would.
86 SetGlobalStateDefaults(GlobalStateMap),
87 /// Adds an image, replacing the image with its id.
88 AddImage(Arc<image::StyleImage>),
89 /// Removes the image with this id; an id the style lacks is logged.
90 RemoveImage(String),
91 /// A sprite's images, each replacing the image with its id.
92 AddImages(Vec<Arc<image::StyleImage>>),
93 /// Adds `source` last. Fails when a layer that names its id can't draw
94 /// a source of its kind.
95 AddSource(Source),
96 RemoveSource(String),
97 /// Gives the source `id` the description a load of it found: `None`
98 /// for one that found nothing. The load is the source's `instance` with
99 /// its data's `load`; once the source moved on from that load, this
100 /// changes nothing.
101 SetSourceDescription {
102 id: String,
103 instance: SourceInstance,
104 load: u64,
105 description: Option<ErasedDescription>,
106 },
107 /// Sets the members every source has.
108 SetSourceCommon {
109 id: String,
110 common: SourceCommon,
111 },
112 /// Changes the data of the source `id` as `update` says. A source type
113 /// makes the updates its sources take, such as
114 /// [`GeoJsonSourceType::set_url`](crate::sources::GeoJsonSourceType::set_url);
115 /// [`Edit::update_source`] makes one from a change to a source's data.
116 UpdateSource {
117 id: String,
118 update: SourceUpdate,
119 },
120}
121
122/// What [`Edit::UpdateSource`] does to a source: it returns the source with
123/// new data, made by [`Source::with_data`], or `None` if it changes nothing,
124/// or fails, as for a source of a type it doesn't update.
125pub type SourceUpdate = Box<dyn FnOnce(&Source) -> Result<Option<Source>, Error> + Send>;
126
127impl Edit {
128 /// The edit that changes the data of the source `id`, a `T` source:
129 /// `change` changes a copy of the data and returns whether it changed
130 /// anything. It fails with `InvalidArgument` for a source of another
131 /// type, which isn't `kind`, as in `source "water" is not a GeoJSON
132 /// source`.
133 pub fn update_source<T: StyleSourceType>(
134 id: impl Into<String>,
135 kind: &'static str,
136 change: impl FnOnce(&mut SourceData<T>) -> bool + Send + 'static,
137 ) -> Edit {
138 let update = move |source: &Source| {
139 let Some(data) = SourceData::<T>::recover(source) else {
140 return Err(Error::invalid_argument(format!("source \"{}\" is not {kind}", source.id())));
141 };
142 let mut data = data.clone();
143 Ok(change(&mut data).then(|| source.with_data(data)))
144 };
145 Edit::UpdateSource { id: id.into(), update: Box::new(update) }
146 }
147}