Locate the value for `field` in a MessagePack map starting at `offset`. Returns the byte range `(value_start, value_end)` of the matched value. Scans map keys sequentially — O(n) in number of keys. For documents with many fields queried repeatedly, see structural indexing (Phase 8). # Returns - `Some((start, end))` — the value's byte range (use offset `start` with readers) - `None` — field not f
(buf: &[u8], offset: usize, field: &str)
| 22 | /// - `Some((start, end))` — the value's byte range (use offset `start` with readers) |
| 23 | /// - `None` — field not found, or buffer is not a valid map |
| 24 | pub fn extract_field(buf: &[u8], offset: usize, field: &str) -> Option<FieldRange> { |
| 25 | let (count, mut pos) = map_header(buf, offset)?; |
| 26 | let field_bytes = field.as_bytes(); |
| 27 | |
| 28 | for _ in 0..count { |
| 29 | // Read key string bounds |
| 30 | let key_match = match str_bounds(buf, pos) { |
| 31 | Some((start, len)) => buf |
| 32 | .get(start..start + len) |
| 33 | .map(|kb| kb == field_bytes) |
| 34 | .unwrap_or(false), |
| 35 | None => false, |
| 36 | }; |
| 37 | |
| 38 | // Skip past the key |
| 39 | pos = skip_value(buf, pos)?; |
| 40 | |
| 41 | if key_match { |
| 42 | // Found — return the value's byte range |
| 43 | let value_start = pos; |
| 44 | let value_end = skip_value(buf, pos)?; |
| 45 | return Some((value_start, value_end)); |
| 46 | } |
| 47 | |
| 48 | // Skip the value |
| 49 | pos = skip_value(buf, pos)?; |
| 50 | } |
| 51 | |
| 52 | None |
| 53 | } |
| 54 | |
| 55 | /// Extract a value at a nested path (e.g., `["address", "city"]`). |
| 56 | /// Each segment must be a string key in a nested map. |