versatiles_geometry 3.8.1

A toolbox for converting, checking and serving map tiles in various formats.
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
//! Typed property values for GeoJSON-like features.
//!
//! This module defines [`GeoValue`], a small, ordered sum type used for feature
//! properties in the `versatiles_geometry` crate. It supports construction from
//! primitive Rust types, lexicographic/total ordering for deterministic output,
//! parsing from strings, hashing/equality, and conversion to the crate’s `JsonValue`.

use anyhow::{Result, bail};
use regex::Regex;
use std::{
	cmp::Ordering,
	fmt::{Debug, Display},
	hash::Hash,
	sync::LazyLock,
};
use versatiles_core::json::JsonValue;

/// A compact, typed representation of a property value used in GeoJSON-like features.
///
/// Variants cover the common scalar JSON types plus separate `Float`/`Double` and
/// signed/unsigned integer distinctions. `Ord` and `Hash` are implemented to allow
/// use as map values with deterministic orderings.
#[derive(Clone, PartialEq)]
pub enum GeoValue {
	/// Boolean value.
	Bool(bool),
	/// 64-bit floating-point number.
	Double(f64),
	/// 32-bit floating-point number.
	Float(f32),
	/// 64-bit signed integer.
	Int(i64),
	/// JSON null.
	Null,
	/// UTF‑8 string.
	String(String),
	/// 64-bit unsigned integer.
	UInt(u64),
}

/// Formats the value as `Variant(inner)` to mirror the enum structure for developers.
impl Debug for GeoValue {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			Self::String(v) => f.debug_tuple("String").field(v).finish(),
			Self::Float(v) => f.debug_tuple("Float").field(v).finish(),
			Self::Double(v) => f.debug_tuple("Double").field(v).finish(),
			Self::Int(v) => f.debug_tuple("Int").field(v).finish(),
			Self::UInt(v) => f.debug_tuple("UInt").field(v).finish(),
			Self::Bool(v) => f.debug_tuple("Bool").field(v).finish(),
			Self::Null => f.debug_tuple("Null").finish(),
		}
	}
}

/// Converts a `&str` into `GeoValue::String`.
impl From<&str> for GeoValue {
	fn from(value: &str) -> Self {
		GeoValue::String(value.to_string())
	}
}

/// Converts a `&String` into `GeoValue::String`.
impl From<&String> for GeoValue {
	fn from(value: &String) -> Self {
		GeoValue::String(value.clone())
	}
}

/// Converts a `String` into `GeoValue::String`.
impl From<String> for GeoValue {
	fn from(value: String) -> Self {
		GeoValue::String(value)
	}
}

/// Converts a `u8` into `GeoValue::UInt`.
impl From<u8> for GeoValue {
	fn from(value: u8) -> Self {
		GeoValue::UInt(u64::from(value))
	}
}

/// Converts an `i32` into `GeoValue::Int` if negative, otherwise `GeoValue::UInt`.
impl From<i32> for GeoValue {
	fn from(value: i32) -> Self {
		if value < 0 {
			GeoValue::Int(i64::from(value))
		} else {
			GeoValue::UInt(u64::from(value.cast_unsigned()))
		}
	}
}

/// Converts a `u32` into `GeoValue::UInt`.
impl From<u32> for GeoValue {
	fn from(value: u32) -> Self {
		GeoValue::UInt(u64::from(value))
	}
}

/// Converts a `usize` into `GeoValue::UInt`.
impl From<usize> for GeoValue {
	fn from(value: usize) -> Self {
		GeoValue::UInt(value as u64)
	}
}

/// Converts an `i64` into `GeoValue::Int`.
impl From<i64> for GeoValue {
	fn from(value: i64) -> Self {
		GeoValue::Int(value)
	}
}

/// Converts a `u64` into `GeoValue::UInt`.
impl From<u64> for GeoValue {
	fn from(value: u64) -> Self {
		GeoValue::UInt(value)
	}
}

/// Converts an `f32` into `GeoValue::Float`.
impl From<f32> for GeoValue {
	fn from(value: f32) -> Self {
		GeoValue::Float(value)
	}
}

/// Converts an `f64` into `GeoValue::Double`.
impl From<f64> for GeoValue {
	fn from(value: f64) -> Self {
		GeoValue::Double(value)
	}
}

/// Converts a `bool` into `GeoValue::Bool`.
impl From<bool> for GeoValue {
	fn from(value: bool) -> Self {
		GeoValue::Bool(value)
	}
}

/// Equality is defined per-variant and value; see also `Ord` for cross-variant ordering.
impl Eq for GeoValue {}

/// Hashes both the variant tag and the inner value to ensure stable hashing across variants.
impl Hash for GeoValue {
	fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
		core::mem::discriminant(self).hash(state);
		match self {
			GeoValue::Bool(v) => v.hash(state),
			GeoValue::Double(v) => v.to_bits().hash(state),
			GeoValue::Float(v) => v.to_bits().hash(state),
			GeoValue::Int(v) => v.hash(state),
			GeoValue::Null => (),
			GeoValue::String(v) => v.hash(state),
			GeoValue::UInt(v) => v.hash(state),
		}
	}
}

/// Provides total ordering. Values are first compared within the same variant; otherwise
/// a fixed variant precedence is used (see `variant_order`).
impl PartialOrd for GeoValue {
	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
		Some(self.cmp(other))
	}
}

/// Total ordering used for deterministic sorting across mixed variants.
impl Ord for GeoValue {
	fn cmp(&self, other: &Self) -> Ordering {
		use GeoValue::{Bool, Double, Float, Int, String, UInt};
		match (self, other) {
			(String(a), String(b)) => a.cmp(b),
			(Float(a), Float(b)) => a.partial_cmp(b).unwrap_or(Ordering::Equal),
			(Double(a), Double(b)) => a.partial_cmp(b).unwrap_or(Ordering::Equal),
			(Int(a), Int(b)) => a.cmp(b),
			(UInt(a), UInt(b)) => a.cmp(b),
			(Bool(a), Bool(b)) => a.cmp(b),
			_ => self.variant_order().cmp(&other.variant_order()),
		}
	}
}

/// Displays the value as a plain string (e.g., numbers as decimals, booleans as `true`/`false`, `null`).
impl Display for GeoValue {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		write!(
			f,
			"{}",
			match self {
				GeoValue::Bool(v) => v.to_string(),
				GeoValue::Double(v) => v.to_string(),
				GeoValue::Float(v) => v.to_string(),
				GeoValue::Int(v) => v.to_string(),
				GeoValue::Null => "null".to_string(),
				GeoValue::String(v) => v.clone(),
				GeoValue::UInt(v) => v.to_string(),
			}
		)
	}
}

impl GeoValue {
	/// Internal: variant precedence used when ordering mixed types (`String` < `Float` < `Double` < `Int` < `UInt` < `Bool` < `Null`).
	fn variant_order(&self) -> u8 {
		match self {
			GeoValue::String(_) => 0,
			GeoValue::Float(_) => 1,
			GeoValue::Double(_) => 2,
			GeoValue::Int(_) => 3,
			GeoValue::UInt(_) => 4,
			GeoValue::Bool(_) => 5,
			GeoValue::Null => 6,
		}
	}

	/// Parses a string into a `GeoValue` by detecting booleans, integers, unsigned integers,
	/// and floating-point numbers; falls back to `String` (empty input yields empty string).
	///
	/// Numbers with leading zeros (except `0` itself or `0.x`) are treated as strings.
	/// Supports exponential notation (e.g., `1.5e10`, `1E-3`).
	#[must_use]
	pub fn parse_str(value: &str) -> Self {
		// Double: requires decimal point and/or exponent, no leading zeros
		// Format: -?[0|1-9...](.[digits])([eE][+-]?digits) where . or e/E is required
		static REG_DOUBLE: LazyLock<Regex> =
			LazyLock::new(|| Regex::new(r"^-?(?:0|[1-9]\d*)(?:(?:\.\d+)(?:[eE][+-]?\d+)?|[eE][+-]?\d+)$").unwrap());
		// Signed integer (negative): -[0|1-9...]
		static REG_INT: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^-(?:0|[1-9]\d*)$").unwrap());
		// Unsigned integer: 0 or [1-9...]
		static REG_UINT: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^(?:0|[1-9]\d*)$").unwrap());

		match value {
			"" => GeoValue::String(String::new()),
			"true" => GeoValue::Bool(true),
			"false" => GeoValue::Bool(false),
			_ => {
				if REG_DOUBLE.is_match(value) {
					GeoValue::Double(value.parse::<f64>().unwrap())
				} else if REG_INT.is_match(value) {
					GeoValue::Int(value.parse::<i64>().unwrap())
				} else if REG_UINT.is_match(value) {
					GeoValue::UInt(value.parse::<u64>().unwrap())
				} else {
					GeoValue::String(value.to_string())
				}
			}
		}
	}

	/// Returns the value as `u64` if it is `Int` (non-negative) or `UInt`; otherwise returns an error.
	pub fn as_u64(&self) -> Result<u64> {
		match self {
			GeoValue::Int(v) => Ok(u64::try_from(*v)?),
			GeoValue::UInt(v) => Ok(*v),
			_ => bail!("value is not an integer"),
		}
	}

	/// Converts the `GeoValue` to the crate’s `JsonValue` representation.
	/// Note: integer types are converted to `Number` (as `f64`) to match JSON semantics.
	#[must_use]
	pub fn to_json(&self) -> JsonValue {
		match self {
			GeoValue::Bool(v) => JsonValue::from(*v),
			GeoValue::Double(v) => JsonValue::from(*v),
			GeoValue::Float(v) => JsonValue::from(f64::from(*v)),
			GeoValue::Int(v) => JsonValue::from(*v as f64),
			GeoValue::Null => JsonValue::Null,
			GeoValue::String(v) => JsonValue::from(v.clone()),
			GeoValue::UInt(v) => JsonValue::from(*v as f64),
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use rstest::rstest;

	#[test]
	fn test_geo_value_ord() {
		// Test ordering within the same variant
		assert!(GeoValue::from("a") < GeoValue::from("b"));
		assert!(GeoValue::from(1.0f32) < GeoValue::from(2.0f32));
		assert!(GeoValue::from(1.0f64) < GeoValue::from(2.0f64));
		assert!(GeoValue::from(1) < GeoValue::from(2));
		assert!(GeoValue::from(-1) < GeoValue::from(0));
		assert!(GeoValue::from(1u64) < GeoValue::from(2u64));
		assert!(GeoValue::from(false) < GeoValue::from(true));

		// Test ordering between different variants
		assert!(GeoValue::from("a") < GeoValue::from(1.0f32));
		assert!(GeoValue::from(1.0f32) < GeoValue::from(1.0f64));
		assert!(GeoValue::from(1.0f64) < GeoValue::from(1));
		assert!(GeoValue::from(1u64) < GeoValue::from(false));
	}

	#[rstest]
	#[case(GeoValue::from("a"), GeoValue::from("b"))]
	#[case(GeoValue::from(1.0f32), GeoValue::from(2.0f32))]
	#[case(GeoValue::from(1.0f64), GeoValue::from(2.0f64))]
	#[case(GeoValue::from(1), GeoValue::from(2))]
	#[case(GeoValue::from(-1), GeoValue::from(2))]
	#[case(GeoValue::from(-2), GeoValue::from(-1))]
	#[case(GeoValue::from(1u64), GeoValue::from(2u64))]
	#[case(GeoValue::from(false), GeoValue::from(true))]
	#[case(GeoValue::from("a"), GeoValue::from(1.0f32))]
	#[case(GeoValue::from(1.0f32), GeoValue::from(1.0f64))]
	#[case(GeoValue::from(1.0f64), GeoValue::from(1))]
	#[case(GeoValue::from(1u64), GeoValue::from(false))]
	fn test_geo_value_partial_cmp(#[case] a: GeoValue, #[case] b: GeoValue) {
		// Test partial_cmp within the same variant
		assert_eq!(a.partial_cmp(&b), Some(Ordering::Less));
	}

	#[test]
	fn test_geo_value_eq() {
		// Test equality within the same variant
		assert_eq!(GeoValue::from("a"), GeoValue::from("a"));
		assert_eq!(GeoValue::from(1.0f32), GeoValue::from(1.0f32));
		assert_eq!(GeoValue::from(1.0f64), GeoValue::from(1.0f64));
		assert_eq!(GeoValue::from(1), GeoValue::from(1));
		assert_eq!(GeoValue::from(1u64), GeoValue::from(1u64));
		assert_eq!(GeoValue::from(false), GeoValue::from(false));

		// Test inequality within the same variant
		assert_ne!(GeoValue::from("a"), GeoValue::from("b"));
		assert_ne!(GeoValue::from(1.0f32), GeoValue::from(2.0f32));
		assert_ne!(GeoValue::from(1.0f64), GeoValue::from(2.0f64));
		assert_ne!(GeoValue::from(1), GeoValue::from(2));
		assert_ne!(GeoValue::from(1u64), GeoValue::from(2u64));
		assert_ne!(GeoValue::from(false), GeoValue::from(true));
	}

	#[rstest]
	// Booleans
	#[case(GeoValue::Bool(false), "false")]
	#[case(GeoValue::Bool(true), "true")]
	// Doubles with decimal point
	#[case(GeoValue::Double(-0.42), "-0.42")]
	#[case(GeoValue::Double(-0.42), "-0.420")]
	#[case(GeoValue::Double(-23.42), "-23.42")]
	#[case(GeoValue::Double(0.0), "0.0")]
	#[case(GeoValue::Double(0.42), "0.42")]
	#[case(GeoValue::Double(0.42), "0.420")]
	#[case(GeoValue::Double(23.42), "23.42")]
	// Exponential notation
	#[case(GeoValue::Double(-1.5e10), "-1.5e10")]
	#[case(GeoValue::Double(0.0), "0.0e0")]
	#[case(GeoValue::Double(0.0), "0e0")]
	#[case(GeoValue::Double(1.23e-4), "1.23e-4")]
	#[case(GeoValue::Double(1.23e4), "1.23e4")]
	#[case(GeoValue::Double(1.5e-10), "1.5e-10")]
	#[case(GeoValue::Double(1.5e10), "1.5e+10")]
	#[case(GeoValue::Double(1.5e10), "1.5e10")]
	#[case(GeoValue::Double(1.5e10), "1.5e10")]
	#[case(GeoValue::Double(1.5e10), "1.5E10")]
	#[case(GeoValue::Double(1e-3), "1e-3")]
	#[case(GeoValue::Double(1e10), "1e10")]
	#[case(GeoValue::Double(5e0), "5e0")]
	// Signed integers
	#[case(GeoValue::Int(-4), "-4")]
	#[case(GeoValue::Int(-42), "-42")]
	#[case(GeoValue::Int(0), "-0")]
	// Unsigned integers
	#[case(GeoValue::UInt(0), "0")]
	#[case(GeoValue::UInt(42), "42")]
	#[case(GeoValue::UInt(123456789), "123456789")]
	// Strings (invalid number formats)
	#[case(GeoValue::String(" 42".to_string()), " 42")]
	#[case(GeoValue::String("-.42".to_string()), "-.42")]
	#[case(GeoValue::String("-00.0".to_string()), "-00.0")]
	#[case(GeoValue::String("-042".to_string()), "-042")]
	#[case(GeoValue::String(".42".to_string()), ".42")]
	#[case(GeoValue::String(".420".to_string()), ".420")]
	#[case(GeoValue::String(String::new()), "")]
	#[case(GeoValue::String("+42".to_string()), "+42")]
	#[case(GeoValue::String("00.0".to_string()), "00.0")]
	#[case(GeoValue::String("00".to_string()), "00")]
	#[case(GeoValue::String("01e5".to_string()), "01e5")]
	#[case(GeoValue::String("042".to_string()), "042")]
	#[case(GeoValue::String("1.2.3".to_string()), "1.2.3")]
	#[case(GeoValue::String("123abc".to_string()), "123abc")]
	#[case(GeoValue::String("1e".to_string()), "1e")]
	#[case(GeoValue::String("1e1e1".to_string()), "1e1e1")]
	#[case(GeoValue::String("42 ".to_string()), "42 ")]
	#[case(GeoValue::String("e10".to_string()), "e10")]
	#[case(GeoValue::String("hello".to_string()), "hello")]
	fn test_parse_str(#[case] value: GeoValue, #[case] text: &str) {
		assert_eq!(GeoValue::parse_str(text), value);
	}

	#[rstest]
	#[case(GeoValue::Bool(true), "Bool(true)")]
	#[case(GeoValue::Bool(false), "Bool(false)")]
	#[case(GeoValue::Float(23.42), "Float(23.42)")]
	#[case(GeoValue::Double(23.42), "Double(23.42)")]
	#[case(GeoValue::Double(-23.42), "Double(-23.42)")]
	#[case(GeoValue::Int(-42), "Int(-42)")]
	#[case(GeoValue::UInt(42), "UInt(42)")]
	#[case(GeoValue::Null, "Null")]
	fn test_debug(#[case] value: GeoValue, #[case] text: &str) {
		assert_eq!(format!("{value:?}"), text);
	}

	#[rstest]
	#[case(GeoValue::Bool(true), JsonValue::Boolean(true))]
	#[case(GeoValue::Bool(false), JsonValue::Boolean(false))]
	#[case(GeoValue::Float(32.0), JsonValue::Number(32.0))]
	#[case(GeoValue::Double(23.42), JsonValue::Number(23.42))]
	#[case(GeoValue::Double(-23.42), JsonValue::Number(-23.42))]
	#[case(GeoValue::Int(-42),JsonValue::Number(-42.0))]
	#[case(GeoValue::UInt(42), JsonValue::Number(42.0))]
	#[case(GeoValue::Null, JsonValue::Null)]
	fn test_json(#[case] value: GeoValue, #[case] json: JsonValue) {
		assert_eq!(value.to_json(), json);
	}
}