Skip to main content

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_with_limit(buf, offset, self.reader.data_section_len)
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                        decoder.validate_skip_end().map_err(with_path)?;
285                        return Ok(None);
286                    }
287                }
288                PathElement::Index(idx) | PathElement::IndexFromEnd(idx) => {
289                    let (_, type_num) = decoder.peek_type().map_err(with_path)?;
290                    if type_num != TYPE_ARRAY {
291                        let elem = match *element {
292                            PathElement::Index(i) => format!("Index({i})"),
293                            PathElement::IndexFromEnd(i) => format!("IndexFromEnd({i})"),
294                            PathElement::Key(_) => unreachable!(),
295                        };
296                        return Err(MaxMindDbError::decoding_at_path(
297                            format!("expected array for {elem}, got type {type_num}"),
298                            decoder.offset(),
299                            render_path(&path[..=i]),
300                        ));
301                    }
302
303                    // Consume the array header and get size
304                    let size = decoder.consume_array_header().map_err(with_path)?;
305
306                    if idx >= size {
307                        return Ok(None); // Out of bounds
308                    }
309
310                    let actual_idx = match *element {
311                        PathElement::Index(i) => i,
312                        PathElement::IndexFromEnd(i) => size - 1 - i,
313                        PathElement::Key(_) => unreachable!(),
314                    };
315
316                    // Skip to the target index
317                    for _ in 0..actual_idx {
318                        decoder.skip_value().map_err(with_path)?;
319                    }
320                }
321            }
322        }
323
324        // Decode the value at the current position
325        T::deserialize(&mut decoder)
326            .map(Some)
327            .map_err(|e| add_path_context(e, path))
328    }
329}
330
331/// Adds path context to a Decoding error if it doesn't already have one.
332fn add_path_context(err: MaxMindDbError, path: &[PathElement<'_>]) -> MaxMindDbError {
333    match err {
334        MaxMindDbError::Decoding {
335            message,
336            offset,
337            path: None,
338        } => MaxMindDbError::Decoding {
339            message,
340            offset,
341            path: Some(render_path(path)),
342        },
343        _ => err,
344    }
345}
346
347/// Renders path elements as a JSON-pointer-like string (e.g., "/city/names/0").
348fn render_path(path: &[PathElement<'_>]) -> String {
349    use std::fmt::Write;
350    let mut s = String::new();
351    for elem in path {
352        s.push('/');
353        match elem {
354            PathElement::Key(k) => s.push_str(k),
355            PathElement::Index(i) => write!(s, "{i}").unwrap(),
356            PathElement::IndexFromEnd(i) => write!(s, "{}", -((*i as isize) + 1)).unwrap(),
357        }
358    }
359    s
360}
361
362/// A path element for navigating into nested data structures.
363///
364/// Used with [`LookupResult::decode_path()`] to selectively decode
365/// specific fields without parsing the entire record.
366///
367/// # Creating Path Elements
368///
369/// You can create path elements directly or use the [`path!`](crate::path) macro
370/// for a more convenient syntax:
371///
372/// ```
373/// use maxminddb::{path, PathElement};
374///
375/// // Direct construction
376/// let path = [PathElement::Key("country"), PathElement::Key("iso_code")];
377///
378/// // Using the macro - string literals become Keys, integers become Indexes
379/// let path = path!["country", "iso_code"];
380/// let path = path!["subdivisions", 0, "names"];  // Mixed keys and indexes
381/// let path = path!["array", -1];  // Negative indexes count from the end
382/// ```
383#[derive(Debug, Clone, PartialEq, Eq)]
384pub enum PathElement<'a> {
385    /// Navigate into a map by key.
386    Key(&'a str),
387    /// Navigate into an array by index (0-based from the start).
388    ///
389    /// - `Index(0)` - first element
390    /// - `Index(1)` - second element
391    Index(usize),
392    /// Navigate into an array by index from the end.
393    ///
394    /// - `IndexFromEnd(0)` - last element
395    /// - `IndexFromEnd(1)` - second-to-last element
396    IndexFromEnd(usize),
397}
398
399impl<'a> From<&'a str> for PathElement<'a> {
400    fn from(s: &'a str) -> Self {
401        PathElement::Key(s)
402    }
403}
404
405impl From<i32> for PathElement<'_> {
406    /// Converts an integer to a path element.
407    ///
408    /// - Non-negative values become `Index(n)`
409    /// - Negative values become `IndexFromEnd(-n - 1)`, so `-1` is the last element
410    fn from(n: i32) -> Self {
411        signed_index_to_path_element(n as isize)
412    }
413}
414
415impl From<usize> for PathElement<'_> {
416    fn from(n: usize) -> Self {
417        PathElement::Index(n)
418    }
419}
420
421impl From<isize> for PathElement<'_> {
422    /// Converts a signed integer to a path element.
423    ///
424    /// - Non-negative values become `Index(n)`
425    /// - Negative values become `IndexFromEnd(-n - 1)`, so `-1` is the last element
426    /// - `isize::MIN` saturates to `IndexFromEnd(usize::MAX)` because its
427    ///   absolute value is unrepresentable as `isize`
428    fn from(n: isize) -> Self {
429        signed_index_to_path_element(n)
430    }
431}
432
433fn signed_index_to_path_element<'a>(n: isize) -> PathElement<'a> {
434    if n >= 0 {
435        PathElement::Index(n as usize)
436    } else {
437        let index = n
438            .checked_neg()
439            .and_then(|n| n.checked_sub(1))
440            .map(|n| n as usize)
441            .unwrap_or(usize::MAX);
442        PathElement::IndexFromEnd(index)
443    }
444}
445
446/// Creates a path for use with [`LookupResult::decode_path()`](crate::LookupResult::decode_path).
447///
448/// This macro provides a convenient way to construct paths with mixed string keys
449/// and integer indexes.
450///
451/// # Syntax
452///
453/// - String literals become [`PathElement::Key`]
454/// - Non-negative integers become [`PathElement::Index`]
455/// - Negative integers become [`PathElement::IndexFromEnd`] (e.g., `-1` is the last element)
456///
457/// # Examples
458///
459/// ```
460/// use maxminddb::{Reader, path};
461/// use std::net::IpAddr;
462///
463/// let reader = Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
464/// let ip: IpAddr = "89.160.20.128".parse().unwrap();
465/// let result = reader.lookup(ip).unwrap();
466///
467/// // Navigate to country.iso_code
468/// let iso_code: Option<String> = result.decode_path(&path!["country", "iso_code"]).unwrap();
469///
470/// // Navigate to subdivisions[0].names.en
471/// let subdiv: Option<String> = result.decode_path(&path!["subdivisions", 0, "names", "en"]).unwrap();
472/// ```
473///
474/// ```
475/// use maxminddb::{Reader, path};
476/// use std::net::IpAddr;
477///
478/// let reader = Reader::open_readfile("test-data/test-data/MaxMind-DB-test-decoder.mmdb").unwrap();
479/// let ip: IpAddr = "::1.1.1.0".parse().unwrap();
480/// let result = reader.lookup(ip).unwrap();
481///
482/// // Access the last element of an array
483/// let last: Option<u32> = result.decode_path(&path!["array", -1]).unwrap();
484/// assert_eq!(last, Some(3));
485///
486/// // Access the second-to-last element
487/// let second_to_last: Option<u32> = result.decode_path(&path!["array", -2]).unwrap();
488/// assert_eq!(second_to_last, Some(2));
489/// ```
490#[macro_export]
491macro_rules! path {
492    ($($elem:expr),* $(,)?) => {
493        [$($crate::PathElement::from($elem)),*]
494    };
495}
496
497/// Masks an IP address to its network address given a prefix length.
498fn mask_ip(ip: IpAddr, prefix: u8) -> IpAddr {
499    match ip {
500        IpAddr::V4(v4) => {
501            if prefix >= 32 {
502                IpAddr::V4(v4)
503            } else {
504                let int: u32 = v4.into();
505                let mask = if prefix == 0 {
506                    0
507                } else {
508                    !0u32 << (32 - prefix)
509                };
510                IpAddr::V4((int & mask).into())
511            }
512        }
513        IpAddr::V6(v6) => {
514            if prefix >= 128 {
515                IpAddr::V6(v6)
516            } else {
517                let int: u128 = v6.into();
518                let mask = if prefix == 0 {
519                    0
520                } else {
521                    !0u128 << (128 - prefix)
522                };
523                IpAddr::V6((int & mask).into())
524            }
525        }
526    }
527}
528
529#[cfg(test)]
530mod tests {
531    use super::*;
532
533    #[test]
534    fn test_mask_ipv4() {
535        let ip: IpAddr = "192.168.1.100".parse().unwrap();
536        assert_eq!(mask_ip(ip, 24), "192.168.1.0".parse::<IpAddr>().unwrap());
537        assert_eq!(mask_ip(ip, 16), "192.168.0.0".parse::<IpAddr>().unwrap());
538        assert_eq!(mask_ip(ip, 32), "192.168.1.100".parse::<IpAddr>().unwrap());
539        assert_eq!(mask_ip(ip, 0), "0.0.0.0".parse::<IpAddr>().unwrap());
540    }
541
542    #[test]
543    fn test_mask_ipv6() {
544        let ip: IpAddr = "2001:db8:85a3::8a2e:370:7334".parse().unwrap();
545        assert_eq!(
546            mask_ip(ip, 64),
547            "2001:db8:85a3::".parse::<IpAddr>().unwrap()
548        );
549        assert_eq!(mask_ip(ip, 32), "2001:db8::".parse::<IpAddr>().unwrap());
550    }
551
552    #[test]
553    fn test_path_element_debug() {
554        assert_eq!(format!("{:?}", PathElement::Key("test")), "Key(\"test\")");
555        assert_eq!(format!("{:?}", PathElement::Index(5)), "Index(5)");
556        assert_eq!(
557            format!("{:?}", PathElement::IndexFromEnd(0)),
558            "IndexFromEnd(0)"
559        );
560    }
561
562    #[test]
563    fn test_path_element_from_str() {
564        let elem: PathElement = "key".into();
565        assert_eq!(elem, PathElement::Key("key"));
566    }
567
568    #[test]
569    fn test_path_element_from_i32() {
570        // Positive values become Index
571        let elem: PathElement = PathElement::from(0i32);
572        assert_eq!(elem, PathElement::Index(0));
573
574        let elem: PathElement = PathElement::from(5i32);
575        assert_eq!(elem, PathElement::Index(5));
576
577        // Negative values become IndexFromEnd
578        // -1 → IndexFromEnd(0) (last element)
579        let elem: PathElement = PathElement::from(-1i32);
580        assert_eq!(elem, PathElement::IndexFromEnd(0));
581
582        // -2 → IndexFromEnd(1) (second-to-last)
583        let elem: PathElement = PathElement::from(-2i32);
584        assert_eq!(elem, PathElement::IndexFromEnd(1));
585
586        // -3 → IndexFromEnd(2)
587        let elem: PathElement = PathElement::from(-3i32);
588        assert_eq!(elem, PathElement::IndexFromEnd(2));
589    }
590
591    #[test]
592    fn test_path_element_from_usize() {
593        let elem: PathElement = PathElement::from(0usize);
594        assert_eq!(elem, PathElement::Index(0));
595
596        let elem: PathElement = PathElement::from(42usize);
597        assert_eq!(elem, PathElement::Index(42));
598    }
599
600    #[test]
601    fn test_path_element_from_isize() {
602        let elem: PathElement = PathElement::from(0isize);
603        assert_eq!(elem, PathElement::Index(0));
604
605        let elem: PathElement = PathElement::from(-1isize);
606        assert_eq!(elem, PathElement::IndexFromEnd(0));
607
608        let elem: PathElement = PathElement::from(isize::MIN);
609        assert_eq!(elem, PathElement::IndexFromEnd(usize::MAX));
610    }
611
612    #[test]
613    fn test_path_macro_keys_only() {
614        let p = path!["country", "iso_code"];
615        assert_eq!(p.len(), 2);
616        assert_eq!(p[0], PathElement::Key("country"));
617        assert_eq!(p[1], PathElement::Key("iso_code"));
618    }
619
620    #[test]
621    fn test_path_macro_mixed() {
622        let p = path!["subdivisions", 0, "names", "en"];
623        assert_eq!(p.len(), 4);
624        assert_eq!(p[0], PathElement::Key("subdivisions"));
625        assert_eq!(p[1], PathElement::Index(0));
626        assert_eq!(p[2], PathElement::Key("names"));
627        assert_eq!(p[3], PathElement::Key("en"));
628    }
629
630    #[test]
631    fn test_path_macro_negative_indexes() {
632        let p = path!["array", -1];
633        assert_eq!(p.len(), 2);
634        assert_eq!(p[0], PathElement::Key("array"));
635        assert_eq!(p[1], PathElement::IndexFromEnd(0)); // last element
636
637        let p = path!["data", -2, "value"];
638        assert_eq!(p[1], PathElement::IndexFromEnd(1)); // second-to-last
639    }
640
641    #[test]
642    fn test_path_macro_trailing_comma() {
643        let p = path!["a", "b",];
644        assert_eq!(p.len(), 2);
645    }
646
647    #[test]
648    fn test_path_macro_empty() {
649        let p: [PathElement; 0] = path![];
650        assert_eq!(p.len(), 0);
651    }
652
653    #[test]
654    fn test_render_path() {
655        assert_eq!(render_path(&[]), "");
656        assert_eq!(render_path(&[PathElement::Key("city")]), "/city");
657        assert_eq!(
658            render_path(&[PathElement::Key("city"), PathElement::Key("names")]),
659            "/city/names"
660        );
661        assert_eq!(
662            render_path(&[PathElement::Key("arr"), PathElement::Index(0)]),
663            "/arr/0"
664        );
665        assert_eq!(
666            render_path(&[PathElement::Key("arr"), PathElement::Index(42)]),
667            "/arr/42"
668        );
669        // IndexFromEnd(0) = last = -1, IndexFromEnd(1) = second-to-last = -2
670        assert_eq!(
671            render_path(&[PathElement::Key("arr"), PathElement::IndexFromEnd(0)]),
672            "/arr/-1"
673        );
674        assert_eq!(
675            render_path(&[PathElement::Key("arr"), PathElement::IndexFromEnd(1)]),
676            "/arr/-2"
677        );
678    }
679
680    #[test]
681    fn test_decode_path_error_includes_path() {
682        use crate::Reader;
683
684        let reader = Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
685        let ip: IpAddr = "89.160.20.128".parse().unwrap();
686        let result = reader.lookup(ip).unwrap();
687
688        // Try to navigate with Index on a map (root is a map, not array)
689        let err = result
690            .decode_path::<String>(&[PathElement::Index(0)])
691            .unwrap_err();
692        let err_str = err.to_string();
693        assert!(
694            err_str.contains("path: /0"),
695            "error should include path context: {err_str}"
696        );
697        assert!(
698            err_str.contains("expected array"),
699            "error should mention expected type: {err_str}"
700        );
701
702        // Try to navigate deeper and fail at second element
703        let err = result
704            .decode_path::<String>(&[PathElement::Key("city"), PathElement::Index(0)])
705            .unwrap_err();
706        let err_str = err.to_string();
707        assert!(
708            err_str.contains("path: /city/0"),
709            "error should include full path to failure: {err_str}"
710        );
711    }
712}