Skip to main content

rust_motion_affinity/
access.rs

1//! Typed reads of [`Stream`] objects. Every accessor returns `None` when a field is missing or has
2//! another type, so the document mapping treats odd data as absent rather than failing.
3
4use crate::stream::{ObjId, Object, Stream, Tag, Value};
5
6impl Stream {
7    pub fn object(&self, id: ObjId) -> Option<&Object> {
8        self.objects.get(id)
9    }
10
11    pub fn field(&self, id: ObjId, tag: &[u8; 4]) -> Option<&Value> {
12        self.object(id)?.get(Tag::of(tag))
13    }
14
15    pub fn class(&self, id: ObjId) -> Option<Tag> {
16        self.object(id).map(|o| o.class)
17    }
18
19    pub fn is(&self, id: ObjId, class: &[u8; 4]) -> bool {
20        self.class(id) == Some(Tag::of(class))
21    }
22
23    /// A child object (shared, linked or inline); `None` for null or missing.
24    pub fn obj(&self, id: ObjId, tag: &[u8; 4]) -> Option<ObjId> {
25        match self.field(id, tag)? {
26            Value::Obj(o) => *o,
27            _ => None,
28        }
29    }
30
31    /// An array of objects (nulls skipped).
32    pub fn objs(&self, id: ObjId, tag: &[u8; 4]) -> Vec<ObjId> {
33        match self.field(id, tag) {
34            Some(Value::Array(a)) => a
35                .iter()
36                .filter_map(|v| {
37                    if let Value::Obj(Some(o)) = v {
38                        Some(*o)
39                    } else {
40                        None
41                    }
42                })
43                .collect(),
44            _ => Vec::new(),
45        }
46    }
47
48    pub fn f64(&self, id: ObjId, tag: &[u8; 4]) -> Option<f64> {
49        match self.field(id, tag)? {
50            Value::Float(f) if f.is_finite() => Some(*f),
51            _ => None,
52        }
53    }
54
55    pub fn int(&self, id: ObjId, tag: &[u8; 4]) -> Option<i64> {
56        match self.field(id, tag)? {
57            Value::Int(i) => Some(*i),
58            Value::UInt(u) => i64::try_from(*u).ok(),
59            _ => None,
60        }
61    }
62
63    pub fn bool(&self, id: ObjId, tag: &[u8; 4]) -> Option<bool> {
64        match self.field(id, tag)? {
65            Value::Bool(b) => Some(*b),
66            _ => None,
67        }
68    }
69
70    pub fn str(&self, id: ObjId, tag: &[u8; 4]) -> Option<&str> {
71        match self.field(id, tag)? {
72            Value::Str(s) => Some(s),
73            _ => None,
74        }
75    }
76
77    pub fn enumeration(&self, id: ObjId, tag: &[u8; 4]) -> Option<(u16, u16)> {
78        match self.field(id, tag)? {
79            Value::Enum { id, version } => Some((*id, *version)),
80            _ => None,
81        }
82    }
83
84    /// A fixed vector of `N` finite floats (points, rectangles, transforms).
85    pub fn floats<const N: usize>(&self, id: ObjId, tag: &[u8; 4]) -> Option<[f64; N]> {
86        match self.field(id, tag)? {
87            Value::Floats(v) if v.len() == N && v.iter().all(|f| f.is_finite()) => {
88                let mut a = [0.0; N];
89                a.copy_from_slice(v);
90                Some(a)
91            }
92            _ => None,
93        }
94    }
95
96    pub fn bytes(&self, id: ObjId, tag: &[u8; 4]) -> Option<&[u8]> {
97        match self.field(id, tag)? {
98            Value::Bytes(b) | Value::Blob(b) => Some(b),
99            _ => None,
100        }
101    }
102
103    pub fn entry(&self, id: ObjId, tag: &[u8; 4]) -> Option<&str> {
104        match self.field(id, tag)? {
105            Value::Entry(s) => Some(s),
106            _ => None,
107        }
108    }
109
110    /// Untagged fields of a positional object, in order.
111    pub fn positional(&self, id: ObjId) -> impl Iterator<Item = &Value> {
112        self.object(id)
113            .into_iter()
114            .flat_map(|o| o.fields.iter().map(|(_, v)| v))
115    }
116}