maxminddb/result.rs
1//! Lookup result types for deferred decoding.
2//!
3//! This module provides `LookupResult`, which enables lazy decoding of
4//! MaxMind DB records. Instead of immediately deserializing data, you
5//! get a lightweight handle that can be decoded later or navigated
6//! selectively via paths.
7
8use std::net::IpAddr;
9
10use ipnetwork::IpNetwork;
11use serde::Deserialize;
12
13use crate::decoder::{TYPE_ARRAY, TYPE_MAP};
14use crate::error::MaxMindDbError;
15use crate::reader::Reader;
16
17/// The result of looking up an IP address in a MaxMind DB.
18///
19/// This is a lightweight handle (~40 bytes) that stores the lookup result
20/// without immediately decoding the data. You can:
21///
22/// - Check if data exists with [`has_data()`](Self::has_data)
23/// - Get the network containing the IP with [`network()`](Self::network)
24/// - Decode the full record with [`decode()`](Self::decode)
25/// - Decode a specific path with [`decode_path()`](Self::decode_path)
26///
27/// # Example
28///
29/// ```
30/// use maxminddb::{geoip2, path, Reader};
31/// use std::net::IpAddr;
32///
33/// let reader = Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
34/// let ip: IpAddr = "89.160.20.128".parse().unwrap();
35///
36/// let result = reader.lookup(ip).unwrap();
37///
38/// if result.has_data() {
39/// // Full decode
40/// let city: geoip2::City = result.decode().unwrap().unwrap();
41///
42/// // Or selective decode via path
43/// let country_code: Option<String> = result
44/// .decode_path(&path!["country", "iso_code"])
45/// .unwrap();
46/// println!("Country: {:?}", country_code);
47/// }
48/// ```
49#[derive(Debug, Clone, Copy)]
50pub struct LookupResult<'a, S: AsRef<[u8]>> {
51 reader: &'a Reader<S>,
52 /// Offset into the data section, or None if not found.
53 data_offset: Option<usize>,
54 prefix_len: u8,
55 ip: IpAddr,
56 source: LookupSource,
57 network_kind: NetworkKind,
58}
59
60#[derive(Debug, Clone, Copy, PartialEq, Eq)]
61pub(crate) enum LookupSource {
62 Lookup,
63 Iter,
64}
65
66#[derive(Debug, Clone, Copy, PartialEq, Eq)]
67pub(crate) enum NetworkKind {
68 V4,
69 V6,
70 V4InV6Subtree,
71}
72
73impl<'a, S: AsRef<[u8]>> LookupResult<'a, S> {
74 #[inline]
75 fn decoder(&self, offset: usize) -> super::decoder::Decoder<'a> {
76 let buf = &self.reader.buf.as_ref()[self.reader.pointer_base..];
77 super::decoder::Decoder::new(buf, offset)
78 }
79
80 /// Creates a new LookupResult for a found IP.
81 pub(crate) fn new_found(
82 reader: &'a Reader<S>,
83 data_offset: usize,
84 prefix_len: u8,
85 ip: IpAddr,
86 source: LookupSource,
87 network_kind: NetworkKind,
88 ) -> Self {
89 LookupResult {
90 reader,
91 data_offset: Some(data_offset),
92 prefix_len,
93 ip,
94 source,
95 network_kind,
96 }
97 }
98
99 /// Creates a new LookupResult for an IP not in the database.
100 pub(crate) fn new_not_found(
101 reader: &'a Reader<S>,
102 prefix_len: u8,
103 ip: IpAddr,
104 source: LookupSource,
105 network_kind: NetworkKind,
106 ) -> Self {
107 LookupResult {
108 reader,
109 data_offset: None,
110 prefix_len,
111 ip,
112 source,
113 network_kind,
114 }
115 }
116
117 /// Returns true if the database contains data for this IP address.
118 ///
119 /// Note that `false` means the database has no data for this IP,
120 /// which is different from an error during lookup.
121 #[inline]
122 pub fn has_data(&self) -> bool {
123 self.data_offset.is_some()
124 }
125
126 /// Returns the network containing the looked-up IP address.
127 ///
128 /// This is the most specific network in the database that contains
129 /// the IP, regardless of whether data was found.
130 ///
131 /// The returned network preserves the IP version of the original lookup:
132 /// - IPv4 lookups return IPv4 networks (unless the match occurs before the
133 /// IPv4 subtree begins, see below)
134 /// - IPv6 lookups return IPv6 networks (including IPv4-mapped addresses)
135 ///
136 /// Special case: If an IPv4 address is looked up in an IPv6 database but
137 /// the matching record is above the IPv4 subtree (e.g., a database with
138 /// no IPv4 subtree), an IPv6 network is returned since there's no valid
139 /// IPv4 representation.
140 pub fn network(&self) -> Result<IpNetwork, MaxMindDbError> {
141 let (ip, prefix) = match (self.source, self.network_kind, self.ip) {
142 (_, NetworkKind::V4, IpAddr::V4(v4)) => (IpAddr::V4(v4), self.prefix_len),
143 (_, NetworkKind::V4InV6Subtree, IpAddr::V4(v4)) => (
144 IpAddr::V4(v4),
145 self.prefix_len - self.reader.ipv4_start_bit_depth as u8,
146 ),
147 (LookupSource::Lookup, NetworkKind::V6, IpAddr::V4(_)) => {
148 use std::net::Ipv6Addr;
149 (IpAddr::V6(Ipv6Addr::UNSPECIFIED), self.prefix_len)
150 }
151 (_, NetworkKind::V6, IpAddr::V6(v6)) => (IpAddr::V6(v6), self.prefix_len),
152 (_, _, ip) => unreachable!("unexpected lookup result state for network: {ip:?}"),
153 };
154
155 // Mask the IP to the network address
156 let network_ip = mask_ip(ip, prefix);
157 IpNetwork::new(network_ip, prefix).map_err(MaxMindDbError::InvalidNetwork)
158 }
159
160 /// Returns the data section offset if found, for use as a cache key.
161 ///
162 /// Multiple IP addresses often point to the same data record. This
163 /// offset can be used to deduplicate decoding or cache results.
164 ///
165 /// Returns `None` if the IP was not found.
166 #[inline]
167 pub fn offset(&self) -> Option<usize> {
168 self.data_offset
169 }
170
171 /// Decodes the full record into the specified type.
172 ///
173 /// Returns:
174 /// - `Ok(Some(T))` if found and successfully decoded
175 /// - `Ok(None)` if the IP was not found in the database
176 /// - `Err(...)` if decoding fails
177 ///
178 /// # Example
179 ///
180 /// ```
181 /// use maxminddb::{Reader, geoip2};
182 /// use std::net::IpAddr;
183 ///
184 /// let reader = Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
185 /// let ip: IpAddr = "89.160.20.128".parse().unwrap();
186 ///
187 /// let result = reader.lookup(ip).unwrap();
188 /// if let Some(city) = result.decode::<geoip2::City>()? {
189 /// println!("Found city data");
190 /// }
191 /// # Ok::<(), maxminddb::MaxMindDbError>(())
192 /// ```
193 pub fn decode<T>(&self) -> Result<Option<T>, MaxMindDbError>
194 where
195 T: Deserialize<'a>,
196 {
197 let Some(offset) = self.data_offset else {
198 return Ok(None);
199 };
200
201 let mut decoder = self.decoder(offset);
202 T::deserialize(&mut decoder).map(Some)
203 }
204
205 /// Decodes a value at a specific path within the record.
206 ///
207 /// Returns:
208 /// - `Ok(Some(T))` if the path exists and was successfully decoded
209 /// - `Ok(None)` if the path doesn't exist (key missing, index out of bounds)
210 /// - `Err(...)` if there's a type mismatch during navigation (e.g., `Key` on an array)
211 ///
212 /// If `has_data() == false`, returns `Ok(None)`.
213 ///
214 /// # Path Elements
215 ///
216 /// - `PathElement::Key("name")` - Navigate into a map by key
217 /// - `PathElement::Index(0)` - Navigate into an array by index (0 = first element)
218 /// - `PathElement::IndexFromEnd(0)` - Navigate from the end (0 = last element)
219 ///
220 /// # Example
221 ///
222 /// ```
223 /// use maxminddb::{path, Reader};
224 /// use std::net::IpAddr;
225 ///
226 /// let reader = Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
227 /// let ip: IpAddr = "89.160.20.128".parse().unwrap();
228 ///
229 /// let result = reader.lookup(ip).unwrap();
230 ///
231 /// // Navigate to country.iso_code
232 /// let iso_code: Option<String> = result
233 /// .decode_path(&path!["country", "iso_code"])
234 /// .unwrap();
235 ///
236 /// // Navigate to subdivisions[0].names.en
237 /// let subdiv_name: Option<String> = result
238 /// .decode_path(&path!["subdivisions", 0, "names", "en"])
239 /// .unwrap();
240 /// ```
241 pub fn decode_path<T>(&self, path: &[PathElement<'_>]) -> Result<Option<T>, MaxMindDbError>
242 where
243 T: Deserialize<'a>,
244 {
245 let Some(offset) = self.data_offset else {
246 return Ok(None);
247 };
248
249 let mut decoder = self.decoder(offset);
250
251 // Navigate through the path, tracking position for error context
252 for (i, element) in path.iter().enumerate() {
253 // Closure to add path context to errors during navigation.
254 // Shows path up to and including the current element where the error occurred.
255 let with_path = |e| add_path_context(e, &path[..=i]);
256
257 match *element {
258 PathElement::Key(key) => {
259 let (_, type_num) = decoder.peek_type().map_err(with_path)?;
260 if type_num != TYPE_MAP {
261 return Err(MaxMindDbError::decoding_at_path(
262 format!("expected map for Key(\"{key}\"), got type {type_num}"),
263 decoder.offset(),
264 render_path(&path[..=i]),
265 ));
266 }
267
268 // Consume the map header and get size
269 let size = decoder.consume_map_header().map_err(with_path)?;
270
271 let mut found = false;
272 let key_bytes = key.as_bytes();
273 for _ in 0..size {
274 let k = decoder.read_str_as_bytes().map_err(with_path)?;
275 if k == key_bytes {
276 found = true;
277 break;
278 } else {
279 decoder.skip_value().map_err(with_path)?;
280 }
281 }
282
283 if !found {
284 return Ok(None);
285 }
286 }
287 PathElement::Index(idx) | PathElement::IndexFromEnd(idx) => {
288 let (_, type_num) = decoder.peek_type().map_err(with_path)?;
289 if type_num != TYPE_ARRAY {
290 let elem = match *element {
291 PathElement::Index(i) => format!("Index({i})"),
292 PathElement::IndexFromEnd(i) => format!("IndexFromEnd({i})"),
293 PathElement::Key(_) => unreachable!(),
294 };
295 return Err(MaxMindDbError::decoding_at_path(
296 format!("expected array for {elem}, got type {type_num}"),
297 decoder.offset(),
298 render_path(&path[..=i]),
299 ));
300 }
301
302 // Consume the array header and get size
303 let size = decoder.consume_array_header().map_err(with_path)?;
304
305 if idx >= size {
306 return Ok(None); // Out of bounds
307 }
308
309 let actual_idx = match *element {
310 PathElement::Index(i) => i,
311 PathElement::IndexFromEnd(i) => size - 1 - i,
312 PathElement::Key(_) => unreachable!(),
313 };
314
315 // Skip to the target index
316 for _ in 0..actual_idx {
317 decoder.skip_value().map_err(with_path)?;
318 }
319 }
320 }
321 }
322
323 // Decode the value at the current position
324 T::deserialize(&mut decoder)
325 .map(Some)
326 .map_err(|e| add_path_context(e, path))
327 }
328}
329
330/// Adds path context to a Decoding error if it doesn't already have one.
331fn add_path_context(err: MaxMindDbError, path: &[PathElement<'_>]) -> MaxMindDbError {
332 match err {
333 MaxMindDbError::Decoding {
334 message,
335 offset,
336 path: None,
337 } => MaxMindDbError::Decoding {
338 message,
339 offset,
340 path: Some(render_path(path)),
341 },
342 _ => err,
343 }
344}
345
346/// Renders path elements as a JSON-pointer-like string (e.g., "/city/names/0").
347fn render_path(path: &[PathElement<'_>]) -> String {
348 use std::fmt::Write;
349 let mut s = String::new();
350 for elem in path {
351 s.push('/');
352 match elem {
353 PathElement::Key(k) => s.push_str(k),
354 PathElement::Index(i) => write!(s, "{i}").unwrap(),
355 PathElement::IndexFromEnd(i) => write!(s, "{}", -((*i as isize) + 1)).unwrap(),
356 }
357 }
358 s
359}
360
361/// A path element for navigating into nested data structures.
362///
363/// Used with [`LookupResult::decode_path()`] to selectively decode
364/// specific fields without parsing the entire record.
365///
366/// # Creating Path Elements
367///
368/// You can create path elements directly or use the [`path!`](crate::path) macro
369/// for a more convenient syntax:
370///
371/// ```
372/// use maxminddb::{path, PathElement};
373///
374/// // Direct construction
375/// let path = [PathElement::Key("country"), PathElement::Key("iso_code")];
376///
377/// // Using the macro - string literals become Keys, integers become Indexes
378/// let path = path!["country", "iso_code"];
379/// let path = path!["subdivisions", 0, "names"]; // Mixed keys and indexes
380/// let path = path!["array", -1]; // Negative indexes count from the end
381/// ```
382#[derive(Debug, Clone, PartialEq, Eq)]
383pub enum PathElement<'a> {
384 /// Navigate into a map by key.
385 Key(&'a str),
386 /// Navigate into an array by index (0-based from the start).
387 ///
388 /// - `Index(0)` - first element
389 /// - `Index(1)` - second element
390 Index(usize),
391 /// Navigate into an array by index from the end.
392 ///
393 /// - `IndexFromEnd(0)` - last element
394 /// - `IndexFromEnd(1)` - second-to-last element
395 IndexFromEnd(usize),
396}
397
398impl<'a> From<&'a str> for PathElement<'a> {
399 fn from(s: &'a str) -> Self {
400 PathElement::Key(s)
401 }
402}
403
404impl From<i32> for PathElement<'_> {
405 /// Converts an integer to a path element.
406 ///
407 /// - Non-negative values become `Index(n)`
408 /// - Negative values become `IndexFromEnd(-n - 1)`, so `-1` is the last element
409 fn from(n: i32) -> Self {
410 signed_index_to_path_element(n as isize)
411 }
412}
413
414impl From<usize> for PathElement<'_> {
415 fn from(n: usize) -> Self {
416 PathElement::Index(n)
417 }
418}
419
420impl From<isize> for PathElement<'_> {
421 /// Converts a signed integer to a path element.
422 ///
423 /// - Non-negative values become `Index(n)`
424 /// - Negative values become `IndexFromEnd(-n - 1)`, so `-1` is the last element
425 /// - `isize::MIN` saturates to `IndexFromEnd(usize::MAX)` because its
426 /// absolute value is unrepresentable as `isize`
427 fn from(n: isize) -> Self {
428 signed_index_to_path_element(n)
429 }
430}
431
432fn signed_index_to_path_element<'a>(n: isize) -> PathElement<'a> {
433 if n >= 0 {
434 PathElement::Index(n as usize)
435 } else {
436 let index = n
437 .checked_neg()
438 .and_then(|n| n.checked_sub(1))
439 .map(|n| n as usize)
440 .unwrap_or(usize::MAX);
441 PathElement::IndexFromEnd(index)
442 }
443}
444
445/// Creates a path for use with [`LookupResult::decode_path()`](crate::LookupResult::decode_path).
446///
447/// This macro provides a convenient way to construct paths with mixed string keys
448/// and integer indexes.
449///
450/// # Syntax
451///
452/// - String literals become [`PathElement::Key`]
453/// - Non-negative integers become [`PathElement::Index`]
454/// - Negative integers become [`PathElement::IndexFromEnd`] (e.g., `-1` is the last element)
455///
456/// # Examples
457///
458/// ```
459/// use maxminddb::{Reader, path};
460/// use std::net::IpAddr;
461///
462/// let reader = Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
463/// let ip: IpAddr = "89.160.20.128".parse().unwrap();
464/// let result = reader.lookup(ip).unwrap();
465///
466/// // Navigate to country.iso_code
467/// let iso_code: Option<String> = result.decode_path(&path!["country", "iso_code"]).unwrap();
468///
469/// // Navigate to subdivisions[0].names.en
470/// let subdiv: Option<String> = result.decode_path(&path!["subdivisions", 0, "names", "en"]).unwrap();
471/// ```
472///
473/// ```
474/// use maxminddb::{Reader, path};
475/// use std::net::IpAddr;
476///
477/// let reader = Reader::open_readfile("test-data/test-data/MaxMind-DB-test-decoder.mmdb").unwrap();
478/// let ip: IpAddr = "::1.1.1.0".parse().unwrap();
479/// let result = reader.lookup(ip).unwrap();
480///
481/// // Access the last element of an array
482/// let last: Option<u32> = result.decode_path(&path!["array", -1]).unwrap();
483/// assert_eq!(last, Some(3));
484///
485/// // Access the second-to-last element
486/// let second_to_last: Option<u32> = result.decode_path(&path!["array", -2]).unwrap();
487/// assert_eq!(second_to_last, Some(2));
488/// ```
489#[macro_export]
490macro_rules! path {
491 ($($elem:expr),* $(,)?) => {
492 [$($crate::PathElement::from($elem)),*]
493 };
494}
495
496/// Masks an IP address to its network address given a prefix length.
497fn mask_ip(ip: IpAddr, prefix: u8) -> IpAddr {
498 match ip {
499 IpAddr::V4(v4) => {
500 if prefix >= 32 {
501 IpAddr::V4(v4)
502 } else {
503 let int: u32 = v4.into();
504 let mask = if prefix == 0 {
505 0
506 } else {
507 !0u32 << (32 - prefix)
508 };
509 IpAddr::V4((int & mask).into())
510 }
511 }
512 IpAddr::V6(v6) => {
513 if prefix >= 128 {
514 IpAddr::V6(v6)
515 } else {
516 let int: u128 = v6.into();
517 let mask = if prefix == 0 {
518 0
519 } else {
520 !0u128 << (128 - prefix)
521 };
522 IpAddr::V6((int & mask).into())
523 }
524 }
525 }
526}
527
528#[cfg(test)]
529mod tests {
530 use super::*;
531
532 #[test]
533 fn test_mask_ipv4() {
534 let ip: IpAddr = "192.168.1.100".parse().unwrap();
535 assert_eq!(mask_ip(ip, 24), "192.168.1.0".parse::<IpAddr>().unwrap());
536 assert_eq!(mask_ip(ip, 16), "192.168.0.0".parse::<IpAddr>().unwrap());
537 assert_eq!(mask_ip(ip, 32), "192.168.1.100".parse::<IpAddr>().unwrap());
538 assert_eq!(mask_ip(ip, 0), "0.0.0.0".parse::<IpAddr>().unwrap());
539 }
540
541 #[test]
542 fn test_mask_ipv6() {
543 let ip: IpAddr = "2001:db8:85a3::8a2e:370:7334".parse().unwrap();
544 assert_eq!(
545 mask_ip(ip, 64),
546 "2001:db8:85a3::".parse::<IpAddr>().unwrap()
547 );
548 assert_eq!(mask_ip(ip, 32), "2001:db8::".parse::<IpAddr>().unwrap());
549 }
550
551 #[test]
552 fn test_path_element_debug() {
553 assert_eq!(format!("{:?}", PathElement::Key("test")), "Key(\"test\")");
554 assert_eq!(format!("{:?}", PathElement::Index(5)), "Index(5)");
555 assert_eq!(
556 format!("{:?}", PathElement::IndexFromEnd(0)),
557 "IndexFromEnd(0)"
558 );
559 }
560
561 #[test]
562 fn test_path_element_from_str() {
563 let elem: PathElement = "key".into();
564 assert_eq!(elem, PathElement::Key("key"));
565 }
566
567 #[test]
568 fn test_path_element_from_i32() {
569 // Positive values become Index
570 let elem: PathElement = PathElement::from(0i32);
571 assert_eq!(elem, PathElement::Index(0));
572
573 let elem: PathElement = PathElement::from(5i32);
574 assert_eq!(elem, PathElement::Index(5));
575
576 // Negative values become IndexFromEnd
577 // -1 → IndexFromEnd(0) (last element)
578 let elem: PathElement = PathElement::from(-1i32);
579 assert_eq!(elem, PathElement::IndexFromEnd(0));
580
581 // -2 → IndexFromEnd(1) (second-to-last)
582 let elem: PathElement = PathElement::from(-2i32);
583 assert_eq!(elem, PathElement::IndexFromEnd(1));
584
585 // -3 → IndexFromEnd(2)
586 let elem: PathElement = PathElement::from(-3i32);
587 assert_eq!(elem, PathElement::IndexFromEnd(2));
588 }
589
590 #[test]
591 fn test_path_element_from_usize() {
592 let elem: PathElement = PathElement::from(0usize);
593 assert_eq!(elem, PathElement::Index(0));
594
595 let elem: PathElement = PathElement::from(42usize);
596 assert_eq!(elem, PathElement::Index(42));
597 }
598
599 #[test]
600 fn test_path_element_from_isize() {
601 let elem: PathElement = PathElement::from(0isize);
602 assert_eq!(elem, PathElement::Index(0));
603
604 let elem: PathElement = PathElement::from(-1isize);
605 assert_eq!(elem, PathElement::IndexFromEnd(0));
606
607 let elem: PathElement = PathElement::from(isize::MIN);
608 assert_eq!(elem, PathElement::IndexFromEnd(usize::MAX));
609 }
610
611 #[test]
612 fn test_path_macro_keys_only() {
613 let p = path!["country", "iso_code"];
614 assert_eq!(p.len(), 2);
615 assert_eq!(p[0], PathElement::Key("country"));
616 assert_eq!(p[1], PathElement::Key("iso_code"));
617 }
618
619 #[test]
620 fn test_path_macro_mixed() {
621 let p = path!["subdivisions", 0, "names", "en"];
622 assert_eq!(p.len(), 4);
623 assert_eq!(p[0], PathElement::Key("subdivisions"));
624 assert_eq!(p[1], PathElement::Index(0));
625 assert_eq!(p[2], PathElement::Key("names"));
626 assert_eq!(p[3], PathElement::Key("en"));
627 }
628
629 #[test]
630 fn test_path_macro_negative_indexes() {
631 let p = path!["array", -1];
632 assert_eq!(p.len(), 2);
633 assert_eq!(p[0], PathElement::Key("array"));
634 assert_eq!(p[1], PathElement::IndexFromEnd(0)); // last element
635
636 let p = path!["data", -2, "value"];
637 assert_eq!(p[1], PathElement::IndexFromEnd(1)); // second-to-last
638 }
639
640 #[test]
641 fn test_path_macro_trailing_comma() {
642 let p = path!["a", "b",];
643 assert_eq!(p.len(), 2);
644 }
645
646 #[test]
647 fn test_path_macro_empty() {
648 let p: [PathElement; 0] = path![];
649 assert_eq!(p.len(), 0);
650 }
651
652 #[test]
653 fn test_render_path() {
654 assert_eq!(render_path(&[]), "");
655 assert_eq!(render_path(&[PathElement::Key("city")]), "/city");
656 assert_eq!(
657 render_path(&[PathElement::Key("city"), PathElement::Key("names")]),
658 "/city/names"
659 );
660 assert_eq!(
661 render_path(&[PathElement::Key("arr"), PathElement::Index(0)]),
662 "/arr/0"
663 );
664 assert_eq!(
665 render_path(&[PathElement::Key("arr"), PathElement::Index(42)]),
666 "/arr/42"
667 );
668 // IndexFromEnd(0) = last = -1, IndexFromEnd(1) = second-to-last = -2
669 assert_eq!(
670 render_path(&[PathElement::Key("arr"), PathElement::IndexFromEnd(0)]),
671 "/arr/-1"
672 );
673 assert_eq!(
674 render_path(&[PathElement::Key("arr"), PathElement::IndexFromEnd(1)]),
675 "/arr/-2"
676 );
677 }
678
679 #[test]
680 fn test_decode_path_error_includes_path() {
681 use crate::Reader;
682
683 let reader = Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
684 let ip: IpAddr = "89.160.20.128".parse().unwrap();
685 let result = reader.lookup(ip).unwrap();
686
687 // Try to navigate with Index on a map (root is a map, not array)
688 let err = result
689 .decode_path::<String>(&[PathElement::Index(0)])
690 .unwrap_err();
691 let err_str = err.to_string();
692 assert!(
693 err_str.contains("path: /0"),
694 "error should include path context: {err_str}"
695 );
696 assert!(
697 err_str.contains("expected array"),
698 "error should mention expected type: {err_str}"
699 );
700
701 // Try to navigate deeper and fail at second element
702 let err = result
703 .decode_path::<String>(&[PathElement::Key("city"), PathElement::Index(0)])
704 .unwrap_err();
705 let err_str = err.to_string();
706 assert!(
707 err_str.contains("path: /city/0"),
708 "error should include full path to failure: {err_str}"
709 );
710 }
711}