Debian Codegraph @

src/result.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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
8 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::netrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/::IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#;
9
10 use ipnetworkrust-analyzer cargo ipnetwork 0.21.1 crate/::IpNetworkrust-analyzer cargo ipnetwork 0.21.1 IpNetwork#;
11 use serderust-analyzer cargo serde 1.0.229 crate/::Deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#;
12
13 use craterust-analyzer cargo maxminddb 0.30.0 crate/::decoderrust-analyzer cargo maxminddb 0.30.0 decoder/::{TYPE_ARRAYrust-analyzer cargo maxminddb 0.30.0 decoder/TYPE_ARRAY., TYPE_MAPrust-analyzer cargo maxminddb 0.30.0 decoder/TYPE_MAP.};
14 use craterust-analyzer cargo maxminddb 0.30.0 crate/::errorrust-analyzer cargo maxminddb 0.30.0 error/::MaxMindDbErrorrust-analyzer cargo maxminddb 0.30.0 error/MaxMindDbError#;
15 use craterust-analyzer cargo maxminddb 0.30.0 crate/::readerrust-analyzer cargo maxminddb 0.30.0 reader/::Readerrust-analyzer cargo maxminddb 0.30.0 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 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:)]
50 pub struct LookupResultrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#<'a, S: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>> {
51 readerrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#reader.: &'a Readerrust-analyzer cargo maxminddb 0.30.0 reader/Reader#<S>,
52 /// Offset into the data section, or None if not found.
53 data_offsetrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#data_offset.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>,
54 prefix_lenrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#prefix_len.: u8,
55 iprust-analyzer cargo maxminddb 0.30.0 result/LookupResult#ip.: IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#,
56 sourcerust-analyzer cargo maxminddb 0.30.0 result/LookupResult#source.: LookupSourcerust-analyzer cargo maxminddb 0.30.0 result/LookupSource#,
57 network_kindrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#network_kind.: NetworkKindrust-analyzer cargo maxminddb 0.30.0 result/NetworkKind#,
58 }
59
60 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)]
61 pub(craterust-analyzer cargo maxminddb 0.30.0 crate/) enum LookupSourcerust-analyzer cargo maxminddb 0.30.0 result/LookupSource# {
62 Lookuprust-analyzer cargo maxminddb 0.30.0 result/LookupSource#Lookup#,
63 Iterrust-analyzer cargo maxminddb 0.30.0 result/LookupSource#Iter#,
64 }
65
66 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)]
67 pub(craterust-analyzer cargo maxminddb 0.30.0 crate/) enum NetworkKindrust-analyzer cargo maxminddb 0.30.0 result/NetworkKind# {
68 V4rust-analyzer cargo maxminddb 0.30.0 result/NetworkKind#V4#,
69 V6rust-analyzer cargo maxminddb 0.30.0 result/NetworkKind#V6#,
70 V4InV6Subtreerust-analyzer cargo maxminddb 0.30.0 result/NetworkKind#V4InV6Subtree#,
71 }
72
73 impl<'a, S: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>> LookupResultrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#<'a, S> {
74 #[inline]
75 fn decoderrust-analyzer cargo maxminddb 0.30.0 result/impl#[`LookupResult<'a, S>`]decoder().(&self, offset: usize) -> superrust-analyzer cargo maxminddb 0.30.0 crate/::decoderrust-analyzer cargo maxminddb 0.30.0 decoder/::Decoderrust-analyzer cargo maxminddb 0.30.0 decoder/Decoder#<'a> {
76 let buf = &self.readerrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#reader..bufrust-analyzer cargo maxminddb 0.30.0 reader/Reader#buf..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().self.readerrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#reader..pointer_baserust-analyzer cargo maxminddb 0.30.0 reader/Reader#pointer_base...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().;
77 superrust-analyzer cargo maxminddb 0.30.0 crate/::decoderrust-analyzer cargo maxminddb 0.30.0 decoder/::Decoderrust-analyzer cargo maxminddb 0.30.0 decoder/Decoder#::new_with_limitrust-analyzer cargo maxminddb 0.30.0 decoder/impl#[`Decoder<'de>`]new_with_limit().(buf, offset, self.readerrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#reader..data_section_lenrust-analyzer cargo maxminddb 0.30.0 reader/Reader#data_section_len.)
78 }
79
80 /// Creates a new LookupResult for a found IP.
81 pub(craterust-analyzer cargo maxminddb 0.30.0 crate/) fn new_foundrust-analyzer cargo maxminddb 0.30.0 result/impl#[`LookupResult<'a, S>`]new_found().(
82 reader: &'a Readerrust-analyzer cargo maxminddb 0.30.0 reader/Reader#<S>,
83 data_offset: usize,
84 prefix_len: u8,
85 ip: IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#,
86 source: LookupSourcerust-analyzer cargo maxminddb 0.30.0 result/LookupSource#,
87 network_kind: NetworkKindrust-analyzer cargo maxminddb 0.30.0 result/NetworkKind#,
88 ) -> Selfrust-analyzer cargo maxminddb 0.30.0 result/impl#[`LookupResult<'a, S>`] {
89 LookupResultrust-analyzer cargo maxminddb 0.30.0 result/LookupResult# {
90 readerrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#reader.,
91 data_offsetrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#data_offset.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(data_offset),
92 prefix_lenrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#prefix_len.,
93 iprust-analyzer cargo maxminddb 0.30.0 result/LookupResult#ip.,
94 sourcerust-analyzer cargo maxminddb 0.30.0 result/LookupResult#source.,
95 network_kindrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#network_kind.,
96 }
97 }
98
99 /// Creates a new LookupResult for an IP not in the database.
100 pub(craterust-analyzer cargo maxminddb 0.30.0 crate/) fn new_not_foundrust-analyzer cargo maxminddb 0.30.0 result/impl#[`LookupResult<'a, S>`]new_not_found().(
101 reader: &'a Readerrust-analyzer cargo maxminddb 0.30.0 reader/Reader#<S>,
102 prefix_len: u8,
103 ip: IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#,
104 source: LookupSourcerust-analyzer cargo maxminddb 0.30.0 result/LookupSource#,
105 network_kind: NetworkKindrust-analyzer cargo maxminddb 0.30.0 result/NetworkKind#,
106 ) -> Selfrust-analyzer cargo maxminddb 0.30.0 result/impl#[`LookupResult<'a, S>`] {
107 LookupResultrust-analyzer cargo maxminddb 0.30.0 result/LookupResult# {
108 readerrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#reader.,
109 data_offsetrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#data_offset.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
110 prefix_lenrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#prefix_len.,
111 iprust-analyzer cargo maxminddb 0.30.0 result/LookupResult#ip.,
112 sourcerust-analyzer cargo maxminddb 0.30.0 result/LookupResult#source.,
113 network_kindrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#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_datarust-analyzer cargo maxminddb 0.30.0 result/impl#[`LookupResult<'a, S>`]has_data().(&self) -> bool {
123 self.data_offsetrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#data_offset..is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]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 networkrust-analyzer cargo maxminddb 0.30.0 result/impl#[`LookupResult<'a, S>`]network().(&self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<IpNetworkrust-analyzer cargo ipnetwork 0.21.1 IpNetwork#, MaxMindDbErrorrust-analyzer cargo maxminddb 0.30.0 error/MaxMindDbError#> {
141 let (ip, prefix) = match (self.sourcerust-analyzer cargo maxminddb 0.30.0 result/LookupResult#source., self.network_kindrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#network_kind., self.iprust-analyzer cargo maxminddb 0.30.0 result/LookupResult#ip.) {
142 (_, NetworkKindrust-analyzer cargo maxminddb 0.30.0 result/NetworkKind#::V4rust-analyzer cargo maxminddb 0.30.0 result/NetworkKind#V4#, IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#::V4rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#V4#(v4)) => (IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#::V4rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#V4#(v4), self.prefix_lenrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#prefix_len.),
143 (_, NetworkKindrust-analyzer cargo maxminddb 0.30.0 result/NetworkKind#::V4InV6Subtreerust-analyzer cargo maxminddb 0.30.0 result/NetworkKind#V4InV6Subtree#, IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#::V4rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#V4#(v4)) => (
144 IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#::V4rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#V4#(v4),
145 self.prefix_lenrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#prefix_len. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().self.readerrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#reader..ipv4_start_bit_depthrust-analyzer cargo maxminddb 0.30.0 reader/Reader#ipv4_start_bit_depth. as u8,
146 ),
147 (LookupSourcerust-analyzer cargo maxminddb 0.30.0 result/LookupSource#::Lookuprust-analyzer cargo maxminddb 0.30.0 result/LookupSource#Lookup#, NetworkKindrust-analyzer cargo maxminddb 0.30.0 result/NetworkKind#::V6rust-analyzer cargo maxminddb 0.30.0 result/NetworkKind#V6#, IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#::V4rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#V4#(_)) => {
148 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::netrust-analyzer cargo std https://github.com/rust-lang/rust/library/std net/::Ipv6Addrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/Ipv6Addr#;
149 (IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#::V6rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#V6#(Ipv6Addrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/Ipv6Addr#::UNSPECIFIEDrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/impl#[Ipv6Addr]UNSPECIFIED.), self.prefix_lenrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#prefix_len.)
150 }
151 (_, NetworkKindrust-analyzer cargo maxminddb 0.30.0 result/NetworkKind#::V6rust-analyzer cargo maxminddb 0.30.0 result/NetworkKind#V6#, IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#::V6rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#V6#(v6)) => (IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#::V6rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#V6#(v6), self.prefix_lenrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#prefix_len.),
152 (_, _, ip) => unreachablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unreachable!!("unexpected lookup result state for network: {ip:?}"),
153 };
154
155 // Mask the IP to the network address
156 let network_ip = mask_iprust-analyzer cargo maxminddb 0.30.0 result/mask_ip().(ip, prefix);
157 IpNetworkrust-analyzer cargo ipnetwork 0.21.1 IpNetwork#::newrust-analyzer cargo ipnetwork 0.21.1 impl#[IpNetwork]new().(network_ip, prefix).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(MaxMindDbErrorrust-analyzer cargo maxminddb 0.30.0 error/MaxMindDbError#::InvalidNetworkrust-analyzer cargo maxminddb 0.30.0 error/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 offsetrust-analyzer cargo maxminddb 0.30.0 result/impl#[`LookupResult<'a, S>`]offset().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize> {
168 self.data_offsetrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#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 decoderust-analyzer cargo maxminddb 0.30.0 result/impl#[`LookupResult<'a, S>`]decode().<T>(&self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<T>, MaxMindDbErrorrust-analyzer cargo maxminddb 0.30.0 error/MaxMindDbError#>
194 where
195 T: Deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#<'a>,
196 {
197 let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(offset) = self.data_offsetrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#data_offset. else {
198 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
199 };
200
201 let mut decoder = self.decoderrust-analyzer cargo maxminddb 0.30.0 result/impl#[`LookupResult<'a, S>`]decoder().(offset);
202 T::deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#deserialize().(&mut decoder).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#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_pathrust-analyzer cargo maxminddb 0.30.0 result/impl#[`LookupResult<'a, S>`]decode_path().<T>(&self, path: &[PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#<'_>]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<T>, MaxMindDbErrorrust-analyzer cargo maxminddb 0.30.0 error/MaxMindDbError#>
242 where
243 T: Deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#<'a>,
244 {
245 let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(offset) = self.data_offsetrust-analyzer cargo maxminddb 0.30.0 result/LookupResult#data_offset. else {
246 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
247 };
248
249 let mut decoder = self.decoderrust-analyzer cargo maxminddb 0.30.0 result/impl#[`LookupResult<'a, S>`]decoder().(offset);
250
251 // Navigate through the path, tracking position for error context
252 for (i, element) in path.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#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_contextrust-analyzer cargo maxminddb 0.30.0 result/add_path_context().(e, &path[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeToInclusive#i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().);
256
257 match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().element {
258 PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Keyrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Key#(key) => {
259 let header_offset = decoder.offsetrust-analyzer cargo maxminddb 0.30.0 decoder/impl#[`Decoder<'de>`]offset().();
260 let (size, type_num) = decoder.consume_container_headerrust-analyzer cargo maxminddb 0.30.0 decoder/impl#[`Decoder<'de>`]consume_container_header().().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(with_path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
261 if type_num rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().TYPE_MAPrust-analyzer cargo maxminddb 0.30.0 decoder/TYPE_MAP. {
262 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(MaxMindDbErrorrust-analyzer cargo maxminddb 0.30.0 error/MaxMindDbError#::decoding_at_pathrust-analyzer cargo maxminddb 0.30.0 error/impl#[MaxMindDbError]decoding_at_path().(
263 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("expected map for Key(\"{key}\"), got type {type_num}"),
264 header_offset,
265 render_pathrust-analyzer cargo maxminddb 0.30.0 result/render_path().(&path[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeToInclusive#i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().),
266 ));
267 }
268
269 let mut found = false;
270 let key_bytes = key.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().();
271 for _ in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#size {
272 let k = decoder.read_str_as_bytesrust-analyzer cargo maxminddb 0.30.0 decoder/impl#[`Decoder<'de>`]read_str_as_bytes().().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(with_path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
273 if k rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().key_bytes {
274 found = true;
275 break;
276 } else {
277 decoder.skip_valuerust-analyzer cargo maxminddb 0.30.0 decoder/impl#[`Decoder<'de>`]skip_value().().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(with_path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
278 }
279 }
280
281 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().found {
282 decoder.validate_skip_endrust-analyzer cargo maxminddb 0.30.0 decoder/impl#[`Decoder<'de>`]validate_skip_end().().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(with_path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
283 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
284 }
285 }
286 PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Indexrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Index#(idx) | PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::IndexFromEndrust-analyzer cargo maxminddb 0.30.0 result/PathElement#IndexFromEnd#(idx) => {
287 let header_offset = decoder.offsetrust-analyzer cargo maxminddb 0.30.0 decoder/impl#[`Decoder<'de>`]offset().();
288 let (size, type_num) = decoder.consume_container_headerrust-analyzer cargo maxminddb 0.30.0 decoder/impl#[`Decoder<'de>`]consume_container_header().().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(with_path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
289 if type_num rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]ne().TYPE_ARRAYrust-analyzer cargo maxminddb 0.30.0 decoder/TYPE_ARRAY. {
290 let elem = match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().element {
291 PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Indexrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Index#(i) => formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("Index({i})"),
292 PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::IndexFromEndrust-analyzer cargo maxminddb 0.30.0 result/PathElement#IndexFromEnd#(i) => formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("IndexFromEnd({i})"),
293 PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Keyrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Key#(_) => unreachablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unreachable!!(),
294 };
295 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(MaxMindDbErrorrust-analyzer cargo maxminddb 0.30.0 error/MaxMindDbError#::decoding_at_pathrust-analyzer cargo maxminddb 0.30.0 error/impl#[MaxMindDbError]decoding_at_path().(
296 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("expected array for {elem}, got type {type_num}"),
297 header_offset,
298 render_pathrust-analyzer cargo maxminddb 0.30.0 result/render_path().(&path[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeToInclusive#i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().),
299 ));
300 }
301
302 if idx rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().size {
303 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#); // Out of bounds
304 }
305
306 let actual_idx = match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().element {
307 PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Indexrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Index#(i) => i,
308 PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::IndexFromEndrust-analyzer cargo maxminddb 0.30.0 result/PathElement#IndexFromEnd#(i) => size rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().i,
309 PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Keyrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Key#(_) => unreachablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unreachable!!(),
310 };
311
312 // Skip to the target index
313 for _ in 0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#actual_idx {
314 decoder.skip_valuerust-analyzer cargo maxminddb 0.30.0 decoder/impl#[`Decoder<'de>`]skip_value().().map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(with_path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
315 }
316 }
317 }
318 }
319
320 // Decode the value at the current position
321 T::deserializerust-analyzer cargo serde_core 1.0.229 de/Deserialize#deserialize().(&mut decoder)
322 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#)
323 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| add_path_contextrust-analyzer cargo maxminddb 0.30.0 result/add_path_context().(e, path))
324 }
325 }
326
327 /// Adds path context to a Decoding error if it doesn't already have one.
328 fn add_path_contextrust-analyzer cargo maxminddb 0.30.0 result/add_path_context().(err: MaxMindDbErrorrust-analyzer cargo maxminddb 0.30.0 error/MaxMindDbError#, path: &[PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#<'_>]) -> MaxMindDbErrorrust-analyzer cargo maxminddb 0.30.0 error/MaxMindDbError# {
329 match err {
330 MaxMindDbErrorrust-analyzer cargo maxminddb 0.30.0 error/MaxMindDbError#::Decodingrust-analyzer cargo maxminddb 0.30.0 error/MaxMindDbError#Decoding# {
331 messagerust-analyzer cargo maxminddb 0.30.0 error/MaxMindDbError#Decoding#message.,
332 offsetrust-analyzer cargo maxminddb 0.30.0 error/MaxMindDbError#Decoding#offset.,
333 pathrust-analyzer cargo maxminddb 0.30.0 error/MaxMindDbError#Decoding#path.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
334 } => MaxMindDbErrorrust-analyzer cargo maxminddb 0.30.0 error/MaxMindDbError#::Decodingrust-analyzer cargo maxminddb 0.30.0 error/MaxMindDbError#Decoding# {
335 messagerust-analyzer cargo maxminddb 0.30.0 error/MaxMindDbError#Decoding#message.,
336 offsetrust-analyzer cargo maxminddb 0.30.0 error/MaxMindDbError#Decoding#offset.,
337 pathrust-analyzer cargo maxminddb 0.30.0 error/MaxMindDbError#Decoding#path.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(render_pathrust-analyzer cargo maxminddb 0.30.0 result/render_path().(path)),
338 },
339 _ => err,
340 }
341 }
342
343 /// Renders path elements as a JSON-pointer-like string (e.g., "/city/names/0").
344 fn render_pathrust-analyzer cargo maxminddb 0.30.0 result/render_path().(path: &[PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#<'_>]) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
345 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write#;
346 let mut s = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
347 for elem in path {
348 s.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().('/');
349 match elem {
350 PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Keyrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Key#(k) => s.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(k),
351 PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Indexrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Index#(i) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(s, "{i}").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(),
352 PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::IndexFromEndrust-analyzer cargo maxminddb 0.30.0 result/PathElement#IndexFromEnd#(i) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(s, "-{}", (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().i as u128) +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u128][`Add<Self>`]add(). 1).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(),
353 }
354 }
355 s
356 }
357
358 /// A path element for navigating into nested data structures.
359 ///
360 /// Used with [`LookupResult::decode_path()`] to selectively decode
361 /// specific fields without parsing the entire record.
362 ///
363 /// # Creating Path Elements
364 ///
365 /// You can create path elements directly or use the [`path!`](crate::path) macro
366 /// for a more convenient syntax:
367 ///
368 /// ```
369 /// use maxminddb::{path, PathElement};
370 ///
371 /// // Direct construction
372 /// let path = [PathElement::Key("country"), PathElement::Key("iso_code")];
373 ///
374 /// // Using the macro - string literals become Keys, integers become Indexes
375 /// let path = path!["country", "iso_code"];
376 /// let path = path!["subdivisions", 0, "names"]; // Mixed keys and indexes
377 /// let path = path!["array", -1]; // Negative indexes count from the end
378 /// ```
379 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:)]
380 pub enum PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#<'a> {
381 /// Navigate into a map by key.
382 Keyrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Key#(&'a str),
383 /// Navigate into an array by index (0-based from the start).
384 ///
385 /// - `Index(0)` - first element
386 /// - `Index(1)` - second element
387 Indexrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Index#(usize),
388 /// Navigate into an array by index from the end.
389 ///
390 /// - `IndexFromEnd(0)` - last element
391 /// - `IndexFromEnd(1)` - second-to-last element
392 IndexFromEndrust-analyzer cargo maxminddb 0.30.0 result/PathElement#IndexFromEnd#(usize),
393 }
394
395 impl<'a> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<&'a str> for PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#<'a> {
396 fn fromrust-analyzer cargo maxminddb 0.30.0 result/impl#[`PathElement<'a>`][`From<&'a str>`]from().(s: &'a str) -> Selfrust-analyzer cargo maxminddb 0.30.0 result/impl#[`PathElement<'a>`][`From<&'a str>`] {
397 PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Keyrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Key#(s)
398 }
399 }
400
401 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<i32> for PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#<'_> {
402 /// Converts an integer to a path element.
403 ///
404 /// - Non-negative values become `Index(n)`
405 /// - Negative values become `IndexFromEnd(-n - 1)`, so `-1` is the last element
406 fn fromrust-analyzer cargo maxminddb 0.30.0 result/impl#[`PathElement<'_>`][`From<i32>`]from().(n: i32) -> Selfrust-analyzer cargo maxminddb 0.30.0 result/impl#[`PathElement<'_>`][`From<i32>`] {
407 signed_index_to_path_elementrust-analyzer cargo maxminddb 0.30.0 result/signed_index_to_path_element().(n as isize)
408 }
409 }
410
411 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<usize> for PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#<'_> {
412 fn fromrust-analyzer cargo maxminddb 0.30.0 result/impl#[`PathElement<'_>`][`From<usize>`]from().(n: usize) -> Selfrust-analyzer cargo maxminddb 0.30.0 result/impl#[`PathElement<'_>`][`From<usize>`] {
413 PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Indexrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Index#(n)
414 }
415 }
416
417 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<isize> for PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#<'_> {
418 /// Converts a signed integer to a path element.
419 ///
420 /// - Non-negative values become `Index(n)`
421 /// - Negative values become `IndexFromEnd(-n - 1)`, so `-1` is the last element
422 /// - `isize::MIN` saturates to `IndexFromEnd(usize::MAX)` because its
423 /// absolute value is unrepresentable as `isize`
424 fn fromrust-analyzer cargo maxminddb 0.30.0 result/impl#[`PathElement<'_>`][`From<isize>`]from().(n: isize) -> Selfrust-analyzer cargo maxminddb 0.30.0 result/impl#[`PathElement<'_>`][`From<isize>`] {
425 signed_index_to_path_elementrust-analyzer cargo maxminddb 0.30.0 result/signed_index_to_path_element().(n)
426 }
427 }
428
429 fn signed_index_to_path_elementrust-analyzer cargo maxminddb 0.30.0 result/signed_index_to_path_element().<'a>(n: isize) -> PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#<'a> {
430 if n rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[isize][`PartialOrd<Self>`]ge().0 {
431 PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Indexrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Index#(n as usize)
432 } else {
433 let index = n
434 .checked_negrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[isize]checked_neg().()
435 .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|n| n.checked_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[isize]checked_sub().(1))
436 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|n| n as usize)
437 .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(usize::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]MAX.);
438 PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::IndexFromEndrust-analyzer cargo maxminddb 0.30.0 result/PathElement#IndexFromEnd#(index)
439 }
440 }
441
442 /// Creates a path for use with [`LookupResult::decode_path()`](crate::LookupResult::decode_path).
443 ///
444 /// This macro provides a convenient way to construct paths with mixed string keys
445 /// and integer indexes.
446 ///
447 /// # Syntax
448 ///
449 /// - String literals become [`PathElement::Key`]
450 /// - Non-negative integers become [`PathElement::Index`]
451 /// - Negative integers become [`PathElement::IndexFromEnd`] (e.g., `-1` is the last element)
452 ///
453 /// # Examples
454 ///
455 /// ```
456 /// use maxminddb::{Reader, path};
457 /// use std::net::IpAddr;
458 ///
459 /// let reader = Reader::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
460 /// let ip: IpAddr = "89.160.20.128".parse().unwrap();
461 /// let result = reader.lookup(ip).unwrap();
462 ///
463 /// // Navigate to country.iso_code
464 /// let iso_code: Option<String> = result.decode_path(&path!["country", "iso_code"]).unwrap();
465 ///
466 /// // Navigate to subdivisions[0].names.en
467 /// let subdiv: Option<String> = result.decode_path(&path!["subdivisions", 0, "names", "en"]).unwrap();
468 /// ```
469 ///
470 /// ```
471 /// use maxminddb::{Reader, path};
472 /// use std::net::IpAddr;
473 ///
474 /// let reader = Reader::open_readfile("test-data/test-data/MaxMind-DB-test-decoder.mmdb").unwrap();
475 /// let ip: IpAddr = "::1.1.1.0".parse().unwrap();
476 /// let result = reader.lookup(ip).unwrap();
477 ///
478 /// // Access the last element of an array
479 /// let last: Option<u32> = result.decode_path(&path!["array", -1]).unwrap();
480 /// assert_eq!(last, Some(3));
481 ///
482 /// // Access the second-to-last element
483 /// let second_to_last: Option<u32> = result.decode_path(&path!["array", -2]).unwrap();
484 /// assert_eq!(second_to_last, Some(2));
485 /// ```
486 #[macro_export]
487 macro_rules! pathrust-analyzer cargo maxminddb 0.30.0 result/path! {
488 ($($elem:expr),* $(,)?) => {
489 [$($crate::PathElement::from($elem)),*]
490 };
491 }
492
493 /// Masks an IP address to its network address given a prefix length.
494 fn mask_iprust-analyzer cargo maxminddb 0.30.0 result/mask_ip().(ip: IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#, prefix: u8) -> IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr# {
495 match ip {
496 IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#::V4rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#V4#(v4) => {
497 if prefix rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]ge().32 {
498 IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#::V4rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#V4#(v4)
499 } else {
500 let int: u32 = v4.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().();
501 let mask = if prefix rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().0 {
502 0
503 } else {
504 !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][Not]not().0u32 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<u8>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<u8>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`Shl<u8>`]shl().(32 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().prefix)
505 };
506 IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#::V4rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#V4#((int rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().mask).intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())
507 }
508 }
509 IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#::V6rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#V6#(v6) => {
510 if prefix rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialOrd<Self>`]ge().128 {
511 IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#::V6rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#V6#(v6)
512 } else {
513 let int: u128 = v6.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().();
514 let mask = if prefix rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().0 {
515 0
516 } else {
517 !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][Not]not().0u128 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`Shl<u8>`]shl().<<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`Shl<u8>`]shl(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`Shl<u8>`]shl().(128 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().prefix)
518 };
519 IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#::V6rust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#V6#((int rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u128][`BitAnd<Self>`]bitand().mask).intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())
520 }
521 }
522 }
523 }
524
525 #[cfg(test)]
526 mod testsrust-analyzer cargo maxminddb 0.30.0 result/tests/ {
527 use superrust-analyzer cargo maxminddb 0.30.0 result/::*;
528
529 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
530 fn test_mask_ipv4rust-analyzer cargo maxminddb 0.30.0 result/tests/test_mask_ipv4().() {
531 let ip: IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr# = "192.168.1.100".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
532 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(mask_iprust-analyzer cargo maxminddb 0.30.0 result/mask_ip().(ip, 24), "192.168.1.0".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().());
533 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(mask_iprust-analyzer cargo maxminddb 0.30.0 result/mask_ip().(ip, 16), "192.168.0.0".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().());
534 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(mask_iprust-analyzer cargo maxminddb 0.30.0 result/mask_ip().(ip, 32), "192.168.1.100".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().());
535 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(mask_iprust-analyzer cargo maxminddb 0.30.0 result/mask_ip().(ip, 0), "0.0.0.0".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().());
536 }
537
538 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
539 fn test_mask_ipv6rust-analyzer cargo maxminddb 0.30.0 result/tests/test_mask_ipv6().() {
540 let ip: IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr# = "2001:db8:85a3::8a2e:370:7334".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
541 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
542 mask_iprust-analyzer cargo maxminddb 0.30.0 result/mask_ip().(ip, 64),
543 "2001:db8:85a3::".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()
544 );
545 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(mask_iprust-analyzer cargo maxminddb 0.30.0 result/mask_ip().(ip, 32), "2001:db8::".parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr#>().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().());
546 }
547
548 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
549 fn test_path_element_debugrust-analyzer cargo maxminddb 0.30.0 result/tests/test_path_element_debug().() {
550 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{:?}", PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Keyrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Key#("test")), "Key(\"test\")");
551 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{:?}", PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Indexrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Index#(5)), "Index(5)");
552 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
553 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{:?}", PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::IndexFromEndrust-analyzer cargo maxminddb 0.30.0 result/PathElement#IndexFromEnd#(0)),
554 "IndexFromEnd(0)"
555 );
556 }
557
558 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
559 fn test_path_element_from_strrust-analyzer cargo maxminddb 0.30.0 result/tests/test_path_element_from_str().() {
560 let elem: PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement# = "key".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().();
561 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(elem, PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Keyrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Key#("key"));
562 }
563
564 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
565 fn test_path_element_from_i32rust-analyzer cargo maxminddb 0.30.0 result/tests/test_path_element_from_i32().() {
566 // Positive values become Index
567 let elem: PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement# = PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::fromrust-analyzer cargo maxminddb 0.30.0 result/impl#[`PathElement<'_>`][`From<i32>`]from().(0i32);
568 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(elem, PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Indexrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Index#(0));
569
570 let elem: PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement# = PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::fromrust-analyzer cargo maxminddb 0.30.0 result/impl#[`PathElement<'_>`][`From<i32>`]from().(5i32);
571 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(elem, PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Indexrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Index#(5));
572
573 // Negative values become IndexFromEnd
574 // -1 → IndexFromEnd(0) (last element)
575 let elem: PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement# = PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::fromrust-analyzer cargo maxminddb 0.30.0 result/impl#[`PathElement<'_>`][`From<i32>`]from().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().1i32);
576 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(elem, PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::IndexFromEndrust-analyzer cargo maxminddb 0.30.0 result/PathElement#IndexFromEnd#(0));
577
578 // -2 → IndexFromEnd(1) (second-to-last)
579 let elem: PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement# = PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::fromrust-analyzer cargo maxminddb 0.30.0 result/impl#[`PathElement<'_>`][`From<i32>`]from().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().2i32);
580 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(elem, PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::IndexFromEndrust-analyzer cargo maxminddb 0.30.0 result/PathElement#IndexFromEnd#(1));
581
582 // -3 → IndexFromEnd(2)
583 let elem: PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement# = PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::fromrust-analyzer cargo maxminddb 0.30.0 result/impl#[`PathElement<'_>`][`From<i32>`]from().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().3i32);
584 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(elem, PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::IndexFromEndrust-analyzer cargo maxminddb 0.30.0 result/PathElement#IndexFromEnd#(2));
585 }
586
587 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
588 fn test_path_element_from_usizerust-analyzer cargo maxminddb 0.30.0 result/tests/test_path_element_from_usize().() {
589 let elem: PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement# = PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::fromrust-analyzer cargo maxminddb 0.30.0 result/impl#[`PathElement<'_>`][`From<usize>`]from().(0usize);
590 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(elem, PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Indexrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Index#(0));
591
592 let elem: PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement# = PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::fromrust-analyzer cargo maxminddb 0.30.0 result/impl#[`PathElement<'_>`][`From<usize>`]from().(42usize);
593 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(elem, PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Indexrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Index#(42));
594 }
595
596 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
597 fn test_path_element_from_isizerust-analyzer cargo maxminddb 0.30.0 result/tests/test_path_element_from_isize().() {
598 let elem: PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement# = PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::fromrust-analyzer cargo maxminddb 0.30.0 result/impl#[`PathElement<'_>`][`From<isize>`]from().(0isize);
599 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(elem, PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Indexrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Index#(0));
600
601 let elem: PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement# = PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::fromrust-analyzer cargo maxminddb 0.30.0 result/impl#[`PathElement<'_>`][`From<isize>`]from().(-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[isize][Neg]neg().1isize);
602 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(elem, PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::IndexFromEndrust-analyzer cargo maxminddb 0.30.0 result/PathElement#IndexFromEnd#(0));
603
604 let elem: PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement# = PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::fromrust-analyzer cargo maxminddb 0.30.0 result/impl#[`PathElement<'_>`][`From<isize>`]from().(isize::MINrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[isize]MIN.);
605 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(elem, PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::IndexFromEndrust-analyzer cargo maxminddb 0.30.0 result/PathElement#IndexFromEnd#(usize::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]MAX.));
606 }
607
608 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
609 fn test_path_macro_keys_onlyrust-analyzer cargo maxminddb 0.30.0 result/tests/test_path_macro_keys_only().() {
610 let p = pathrust-analyzer cargo maxminddb 0.30.0 result/path!!["country", "iso_code"];
611 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(p.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), 2);
612 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(p[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Keyrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Key#("country"));
613 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(p[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Keyrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Key#("iso_code"));
614 }
615
616 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
617 fn test_path_macro_mixedrust-analyzer cargo maxminddb 0.30.0 result/tests/test_path_macro_mixed().() {
618 let p = pathrust-analyzer cargo maxminddb 0.30.0 result/path!!["subdivisions", 0, "names", "en"];
619 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(p.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), 4);
620 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(p[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Keyrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Key#("subdivisions"));
621 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(p[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Indexrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Index#(0));
622 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(p[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Keyrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Key#("names"));
623 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(p[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().3]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Keyrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Key#("en"));
624 }
625
626 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
627 fn test_path_macro_negative_indexesrust-analyzer cargo maxminddb 0.30.0 result/tests/test_path_macro_negative_indexes().() {
628 let p = pathrust-analyzer cargo maxminddb 0.30.0 result/path!!["array", -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().1];
629 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(p.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), 2);
630 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(p[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Keyrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Key#("array"));
631 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(p[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::IndexFromEndrust-analyzer cargo maxminddb 0.30.0 result/PathElement#IndexFromEnd#(0)); // last element
632
633 let p = pathrust-analyzer cargo maxminddb 0.30.0 result/path!!["data", -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[i32][Neg]neg().2, "value"];
634 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(p[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::IndexFromEndrust-analyzer cargo maxminddb 0.30.0 result/PathElement#IndexFromEnd#(1)); // second-to-last
635 }
636
637 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
638 fn test_path_macro_trailing_commarust-analyzer cargo maxminddb 0.30.0 result/tests/test_path_macro_trailing_comma().() {
639 let p = pathrust-analyzer cargo maxminddb 0.30.0 result/path!!["a", "b",];
640 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(p.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), 2);
641 }
642
643 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
644 fn test_path_macro_emptyrust-analyzer cargo maxminddb 0.30.0 result/tests/test_path_macro_empty().() {
645 let p: [PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#; 0] = pathrust-analyzer cargo maxminddb 0.30.0 result/path!![];
646 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(p.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), 0);
647 }
648
649 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
650 fn test_render_pathrust-analyzer cargo maxminddb 0.30.0 result/tests/test_render_path().() {
651 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(render_pathrust-analyzer cargo maxminddb 0.30.0 result/render_path().(&[]), "");
652 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(render_pathrust-analyzer cargo maxminddb 0.30.0 result/render_path().(&[PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Keyrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Key#("city")]), "/city");
653 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
654 render_pathrust-analyzer cargo maxminddb 0.30.0 result/render_path().(&[PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Keyrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Key#("city"), PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Keyrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Key#("names")]),
655 "/city/names"
656 );
657 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
658 render_pathrust-analyzer cargo maxminddb 0.30.0 result/render_path().(&[PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Keyrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Key#("arr"), PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Indexrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Index#(0)]),
659 "/arr/0"
660 );
661 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
662 render_pathrust-analyzer cargo maxminddb 0.30.0 result/render_path().(&[PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Keyrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Key#("arr"), PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Indexrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Index#(42)]),
663 "/arr/42"
664 );
665 // IndexFromEnd(0) = last = -1, IndexFromEnd(1) = second-to-last = -2
666 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
667 render_pathrust-analyzer cargo maxminddb 0.30.0 result/render_path().(&[PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Keyrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Key#("arr"), PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::IndexFromEndrust-analyzer cargo maxminddb 0.30.0 result/PathElement#IndexFromEnd#(0)]),
668 "/arr/-1"
669 );
670 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
671 render_pathrust-analyzer cargo maxminddb 0.30.0 result/render_path().(&[PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Keyrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Key#("arr"), PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::IndexFromEndrust-analyzer cargo maxminddb 0.30.0 result/PathElement#IndexFromEnd#(1)]),
672 "/arr/-2"
673 );
674 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
675 render_pathrust-analyzer cargo maxminddb 0.30.0 result/render_path().(&[PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::IndexFromEndrust-analyzer cargo maxminddb 0.30.0 result/PathElement#IndexFromEnd#(isize::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[isize]MAX. as usize)]),
676 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("/-{}", (isize::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[isize]MAX. as u128) +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u128][`Add<Self>`]add(). 1)
677 );
678 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
679 render_pathrust-analyzer cargo maxminddb 0.30.0 result/render_path().(&[PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::IndexFromEndrust-analyzer cargo maxminddb 0.30.0 result/PathElement#IndexFromEnd#(usize::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]MAX.)]),
680 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("/-{}", (usize::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]MAX. as u128) +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u128][`Add<Self>`]add(). 1)
681 );
682 }
683
684 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
•685 #[cfg(feature = "__with-test-data")] fn test_decode_path_error_includes_path() {
686 use craterust-analyzer cargo maxminddb 0.30.0 crate/::Readerrust-analyzer cargo maxminddb 0.30.0 reader/Reader#;
687
688 let reader = Readerrust-analyzer cargo maxminddb 0.30.0 reader/Reader#::open_readfile("test-data/test-data/GeoIP2-City-Test.mmdb").unwrap();
689 let ip: IpAddrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core net/ip_addr/IpAddr# = "89.160.20.128".parse().unwrap();
690 let result = reader.lookup(ip).unwrap();
691
692 // Try to navigate with Index on a map (root is a map, not array)
693 let err = result
694 .decode_path::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>(&[PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Indexrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Index#(0)])
695 .unwrap_err();
696 let err_str = err.to_string();
697 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(
698 err_str.contains("path: /0"),
699 "error should include path context: {err_str}"
700 );
701 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(
702 err_str.contains("expected array"),
703 "error should mention expected type: {err_str}"
704 );
705
706 // Try to navigate deeper and fail at second element
707 let err = result
708 .decode_path::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>(&[PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Keyrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Key#("city"), PathElementrust-analyzer cargo maxminddb 0.30.0 result/PathElement#::Indexrust-analyzer cargo maxminddb 0.30.0 result/PathElement#Index#(0)])
709 .unwrap_err();
710 let err_str = err.to_string();
711 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(
712 err_str.contains("path: /city/0"),
713 "error should include full path to failure: {err_str}"
714 );
715 }
716 }