macro_rules! path {
($($elem:expr),* $(,)?) => { ... };
}Expand description
Creates a path for use with LookupResult::decode_path().
This macro provides a convenient way to construct paths with mixed string keys and integer indexes.
§Syntax
- String literals become
PathElement::Key - Non-negative integers become
PathElement::Index - Negative integers become
PathElement::IndexFromEnd(e.g.,-1is the last element)
§Examples
use maxminddb::{Reader, path};
use std::net::IpAddr;
let reader = Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
let ip: IpAddr = "89.160.20.128".parse().unwrap();
let result = reader.lookup(ip).unwrap();
// Navigate to country.iso_code
let iso_code: Option<String> = result.decode_path(&path!["country", "iso_code"]).unwrap();
// Navigate to subdivisions[0].names.en
let subdiv: Option<String> = result.decode_path(&path!["subdivisions", 0, "names", "en"]).unwrap();use maxminddb::{Reader, path};
use std::net::IpAddr;
let reader = Reader::open_readfile("test-data/test-data/MaxMind-DB-test-decoder.mmdb").unwrap();
let ip: IpAddr = "::1.1.1.0".parse().unwrap();
let result = reader.lookup(ip).unwrap();
// Access the last element of an array
let last: Option<u32> = result.decode_path(&path!["array", -1]).unwrap();
assert_eq!(last, Some(3));
// Access the second-to-last element
let second_to_last: Option<u32> = result.decode_path(&path!["array", -2]).unwrap();
assert_eq!(second_to_last, Some(2));