1use crate::{buffer, Document};
2
3#[derive(Clone, Debug)]
5pub enum IndexType {
6 U8 = 5121,
8
9 U16 = 5123,
11
12 U32 = 5125,
14}
15
16pub struct Indices<'a> {
18 document: &'a Document,
20
21 json: &'a json::accessor::sparse::Indices,
23}
24
25impl<'a> Indices<'a> {
26 pub(crate) fn new(document: &'a Document, json: &'a json::accessor::sparse::Indices) -> Self {
28 Self { document, json }
29 }
30
31 pub fn view(&self) -> buffer::View<'a> {
33 self.document
34 .views()
35 .nth(self.json.buffer_view.value())
36 .unwrap()
37 }
38
39 pub fn offset(&self) -> usize {
41 self.json.byte_offset.0 as usize
42 }
43
44 pub fn index_type(&self) -> IndexType {
46 match self.json.component_type.unwrap().0 {
47 json::accessor::ComponentType::U8 => IndexType::U8,
48 json::accessor::ComponentType::U16 => IndexType::U16,
49 json::accessor::ComponentType::U32 => IndexType::U32,
50 _ => unreachable!(),
51 }
52 }
53
54 pub fn extras(&self) -> &'a json::Extras {
56 &self.json.extras
57 }
58}
59
60pub struct Sparse<'a> {
62 document: &'a Document,
64
65 json: &'a json::accessor::sparse::Sparse,
67}
68
69impl<'a> Sparse<'a> {
70 pub(crate) fn new(document: &'a Document, json: &'a json::accessor::sparse::Sparse) -> Self {
72 Self { document, json }
73 }
74
75 pub fn count(&self) -> usize {
77 self.json.count.0 as usize
78 }
79
80 pub fn indices(&self) -> Indices<'a> {
83 Indices::new(self.document, &self.json.indices)
84 }
85
86 pub fn values(&self) -> Values<'a> {
89 Values::new(self.document, &self.json.values)
90 }
91
92 pub fn extras(&self) -> &'a json::Extras {
94 &self.json.extras
95 }
96}
97
98pub struct Values<'a> {
101 document: &'a Document,
103
104 json: &'a json::accessor::sparse::Values,
106}
107
108impl<'a> Values<'a> {
109 pub(crate) fn new(document: &'a Document, json: &'a json::accessor::sparse::Values) -> Self {
111 Self { document, json }
112 }
113
114 pub fn view(&self) -> buffer::View<'a> {
116 self.document
117 .views()
118 .nth(self.json.buffer_view.value())
119 .unwrap()
120 }
121
122 pub fn offset(&self) -> usize {
124 self.json.byte_offset.0 as usize
125 }
126
127 pub fn extras(&self) -> &'a json::Extras {
129 &self.json.extras
130 }
131}
132
133impl IndexType {
134 pub fn size(&self) -> usize {
136 use self::IndexType::*;
137 match *self {
138 U8 => 1,
139 U16 => 2,
140 U32 => 4,
141 }
142 }
143}