imessage_database/util/typedstream.rs
1/*!
2 Helpers for working with `Property` types in the [Crabstep](https://github.com/ReagentX/crabstep) deserializer.
3*/
4
5use crabstep::{OutputData, deserializer::iter::Property};
6
7/// Pair used by attributed-body ranges: attribute dictionary index plus UTF-16
8/// range length.
9#[derive(Debug)]
10pub struct TypeLengthPair {
11 /// Index of the attribute dictionary referenced by this range.
12 pub type_index: i64,
13 /// Length of the range in UTF-16 code units.
14 pub length: u64,
15}
16
17// MARK: Type Length
18/// Extract a [`TypeLengthPair`] from a two-integer typedstream group.
19///
20/// No Foundation accessor reads "two primitives in a group", so this stays on
21/// the generic [`Property`]/[`OutputData`] API.
22#[inline(always)]
23pub fn as_type_length_pair(property: &Property<'_, '_>) -> Option<TypeLengthPair> {
24 if let Property::Group(group) = property {
25 let mut iter = group.iter();
26 if let Some(Property::Primitive(OutputData::SignedInteger(type_index))) = iter.next()
27 && let Some(Property::Primitive(OutputData::UnsignedInteger(length))) = iter.next()
28 {
29 return Some(TypeLengthPair {
30 type_index: *type_index,
31 length: *length,
32 });
33 }
34 }
35 None
36}