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