use crate::{buffer, Document};
#[derive(Clone, Debug)]
pub enum IndexType {
U8 = 5121,
U16 = 5123,
U32 = 5125,
}
pub struct Indices<'a> {
document: &'a Document,
json: &'a json::accessor::sparse::Indices,
}
impl<'a> Indices<'a> {
pub(crate) fn new(document: &'a Document, json: &'a json::accessor::sparse::Indices) -> Self {
Self { document, json }
}
pub fn view(&self) -> buffer::View<'a> {
self.document
.views()
.nth(self.json.buffer_view.value())
.unwrap()
}
pub fn offset(&self) -> usize {
self.json.byte_offset.0 as usize
}
pub fn index_type(&self) -> IndexType {
match self.json.component_type.unwrap().0 {
json::accessor::ComponentType::U8 => IndexType::U8,
json::accessor::ComponentType::U16 => IndexType::U16,
json::accessor::ComponentType::U32 => IndexType::U32,
_ => unreachable!(),
}
}
pub fn extras(&self) -> &'a json::Extras {
&self.json.extras
}
}
pub struct Sparse<'a> {
document: &'a Document,
json: &'a json::accessor::sparse::Sparse,
}
impl<'a> Sparse<'a> {
pub(crate) fn new(document: &'a Document, json: &'a json::accessor::sparse::Sparse) -> Self {
Self { document, json }
}
pub fn count(&self) -> usize {
self.json.count.0 as usize
}
pub fn indices(&self) -> Indices<'a> {
Indices::new(self.document, &self.json.indices)
}
pub fn values(&self) -> Values<'a> {
Values::new(self.document, &self.json.values)
}
pub fn extras(&self) -> &'a json::Extras {
&self.json.extras
}
}
pub struct Values<'a> {
document: &'a Document,
json: &'a json::accessor::sparse::Values,
}
impl<'a> Values<'a> {
pub(crate) fn new(document: &'a Document, json: &'a json::accessor::sparse::Values) -> Self {
Self { document, json }
}
pub fn view(&self) -> buffer::View<'a> {
self.document
.views()
.nth(self.json.buffer_view.value())
.unwrap()
}
pub fn offset(&self) -> usize {
self.json.byte_offset.0 as usize
}
pub fn extras(&self) -> &'a json::Extras {
&self.json.extras
}
}
impl IndexType {
pub fn size(&self) -> usize {
use self::IndexType::*;
match *self {
U8 => 1,
U16 => 2,
U32 => 4,
}
}
}