1use crate::{Codec, Dataset, GeomEncoding, PayloadHeader, QuantBits, SimplifyAlgorithm};
18
19#[derive(Debug, Clone, Copy, PartialEq)]
22pub struct Recipe {
23 pub name: &'static str,
26 pub dataset: Dataset,
28 pub epsilon_m: f64,
31 pub quant_bits: QuantBits,
33 pub grid_deg: f64,
36 pub simplify_algorithm: SimplifyAlgorithm,
38 pub geom: GeomEncoding,
40 pub codec: Codec,
42 pub density_weight_floor_e4: u16,
46}
47
48pub const TINY: Recipe = Recipe {
51 name: "tiny",
52 dataset: Dataset::Now,
53 epsilon_m: 10_000.0,
54 quant_bits: QuantBits::Bits16,
55 grid_deg: 2.0,
56 simplify_algorithm: SimplifyAlgorithm::Rdp,
57 geom: GeomEncoding::VarintArcs,
58 codec: Codec::Gzip,
59 density_weight_floor_e4: 10,
60};
61
62pub const TINY_STATIC: Recipe = Recipe {
65 name: "tiny-static",
66 codec: Codec::Uncompressed,
67 ..TINY
68};
69
70pub const COMPACT: Recipe = Recipe {
73 name: "compact",
74 epsilon_m: 1_000.0,
75 quant_bits: QuantBits::Bits24,
76 grid_deg: 4.0 / 3.0,
77 codec: Codec::Xz,
78 ..TINY
79};
80
81pub const BALANCED: Recipe = Recipe {
84 name: "balanced",
85 epsilon_m: 50.0,
86 quant_bits: QuantBits::Bits24,
87 grid_deg: 2.0 / 3.0,
88 codec: Codec::Brotli,
89 density_weight_floor_e4: 200,
90 ..TINY
91};
92
93pub const ACCURATE: Recipe = Recipe {
97 name: "accurate",
98 dataset: Dataset::All,
99 epsilon_m: 10.0,
100 quant_bits: QuantBits::Bits32,
101 grid_deg: 0.5,
102 codec: Codec::Brotli,
103 density_weight_floor_e4: 1_000,
104 ..TINY
105};
106
107pub static ALL: [Recipe; 5] = [TINY, TINY_STATIC, COMPACT, BALANCED, ACCURATE];
111
112#[must_use]
115pub fn by_name(name: &str) -> Option<&'static Recipe> {
116 ALL.iter().find(|recipe| recipe.name == name)
117}
118
119impl Recipe {
120 #[must_use]
127 pub fn density_weight_floor(&self) -> Option<f64> {
128 (self.density_weight_floor_e4 != 0)
129 .then(|| f64::from(self.density_weight_floor_e4) / 10_000.0)
130 }
131}
132
133#[derive(Debug, Clone, Copy, PartialEq)]
138pub struct Provenance {
139 pub dataset: Dataset,
141 pub epsilon_m: f32,
143 pub quant_bits: QuantBits,
145 pub grid_deg: f32,
147 pub simplify_algorithm: SimplifyAlgorithm,
149 pub geom: GeomEncoding,
151 pub codec: Codec,
153 pub density_weight_floor_e4: u16,
155}
156
157impl From<&PayloadHeader> for Provenance {
158 fn from(header: &PayloadHeader) -> Provenance {
159 Provenance {
160 dataset: header.dataset,
161 epsilon_m: header.epsilon_m,
162 quant_bits: header.quant_bits,
163 grid_deg: header.grid_deg,
164 simplify_algorithm: header.simplify_algorithm,
165 geom: header.geom,
166 codec: header.codec,
167 density_weight_floor_e4: header.density_weight_floor_e4,
168 }
169 }
170}
171
172impl From<&Recipe> for Provenance {
173 fn from(recipe: &Recipe) -> Provenance {
174 #[expect(
175 clippy::cast_possible_truncation,
176 reason = "the encoder stamps epsilon_m and grid_deg through this same f64-to-f32 cast, so the provenance stamp matches it bit-exactly"
177 )]
178 Provenance {
179 dataset: recipe.dataset,
180 epsilon_m: recipe.epsilon_m as f32,
181 quant_bits: recipe.quant_bits,
182 grid_deg: recipe.grid_deg as f32,
183 simplify_algorithm: recipe.simplify_algorithm,
184 geom: recipe.geom,
185 codec: recipe.codec,
186 density_weight_floor_e4: recipe.density_weight_floor_e4,
187 }
188 }
189}
190
191#[cfg(test)]
192mod tests {
193 use super::{ACCURATE, ALL, BALANCED, COMPACT, Provenance, Recipe, TINY, TINY_STATIC, by_name};
194 use crate::Codec;
195
196 #[test]
197 fn names_are_unique_and_by_name_round_trips() {
198 for (index, recipe) in ALL.iter().enumerate() {
199 assert_eq!(
200 by_name(recipe.name).map(|found| found.name),
201 Some(recipe.name)
202 );
203 assert!(
204 ALL[..index].iter().all(|other| other.name != recipe.name),
205 "duplicate preset name {:?}",
206 recipe.name
207 );
208 }
209 assert!(by_name("no-such-preset").is_none());
210 }
211
212 #[test]
213 fn tiny_static_differs_from_tiny_only_in_codec() {
214 let restored = Recipe {
215 codec: Codec::Gzip,
216 name: "tiny",
217 ..TINY_STATIC
218 };
219 assert_eq!(restored, TINY);
220 assert_eq!(TINY_STATIC.codec, Codec::Uncompressed);
221 }
222
223 #[test]
224 fn density_floor_fractions_match_the_builder_literals_exactly() {
225 assert_eq!(TINY.density_weight_floor(), Some(0.001));
226 assert_eq!(COMPACT.density_weight_floor(), Some(0.001));
227 assert_eq!(BALANCED.density_weight_floor(), Some(0.020));
228 assert_eq!(ACCURATE.density_weight_floor(), Some(0.10));
229 let unweighted = Recipe {
230 density_weight_floor_e4: 0,
231 ..TINY
232 };
233 assert_eq!(unweighted.density_weight_floor(), None);
234 }
235
236 #[test]
237 fn provenance_casts_match_the_encoder_stamps() {
238 #[expect(
239 clippy::cast_possible_truncation,
240 reason = "the assertion reproduces the encoder's f64-to-f32 stamp on purpose"
241 )]
242 for recipe in &ALL {
243 let provenance = Provenance::from(recipe);
244 assert_eq!(
245 provenance.epsilon_m.to_bits(),
246 (recipe.epsilon_m as f32).to_bits()
247 );
248 assert_eq!(
249 provenance.grid_deg.to_bits(),
250 (recipe.grid_deg as f32).to_bits()
251 );
252 assert_eq!(provenance.dataset, recipe.dataset);
253 assert_eq!(provenance.codec, recipe.codec);
254 }
255 }
256}