use Document;
use {buffer, json};
#[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: document,
json: json,
}
}
pub fn view(&self) -> buffer::View<'a> {
self.document.views().nth(self.json.buffer_view.value()).unwrap()
}
pub fn offset(&self) -> u32 {
self.json.byte_offset
}
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) -> &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: document,
json: json,
}
}
pub fn count(&self) -> u32 {
self.json.count
}
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) -> &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: document,
json: json,
}
}
pub fn view(&self) -> buffer::View {
self.document.views().nth(self.json.buffer_view.value()).unwrap()
}
pub fn offset(&self) -> u32 {
self.json.byte_offset
}
pub fn extras(&self) -> &json::Extras {
&self.json.extras
}
}
impl IndexType {
pub fn size(&self) -> usize {
use self::IndexType::*;
match *self {
U8 => 1,
U16 => 2,
U32 => 4,
}
}
}