use crate::{buffer, Document};
pub use json::accessor::ComponentType as DataType;
pub use json::accessor::Type as Dimensions;
#[cfg(feature = "utils")]
#[cfg_attr(docsrs, doc(cfg(feature = "utils")))]
pub mod util;
pub mod sparse;
#[cfg(feature = "utils")]
#[doc(inline)]
pub use self::util::{Item, Iter};
#[derive(Clone, Debug)]
pub struct Accessor<'a> {
document: &'a Document,
index: usize,
json: &'a json::accessor::Accessor,
}
impl<'a> Accessor<'a> {
pub(crate) fn new(
document: &'a Document,
index: usize,
json: &'a json::accessor::Accessor,
) -> Self {
Self {
document,
index,
json,
}
}
pub fn index(&self) -> usize {
self.index
}
pub fn size(&self) -> usize {
self.data_type().size() * self.dimensions().multiplicity()
}
pub fn view(&self) -> Option<buffer::View<'a>> {
self.json
.buffer_view
.map(|view| self.document.views().nth(view.value()).unwrap())
}
pub fn offset(&self) -> usize {
self.json.byte_offset as usize
}
pub fn count(&self) -> usize {
self.json.count as usize
}
pub fn data_type(&self) -> DataType {
self.json.component_type.unwrap().0
}
pub fn extras(&self) -> &'a json::Extras {
&self.json.extras
}
pub fn dimensions(&self) -> Dimensions {
self.json.type_.unwrap()
}
pub fn min(&self) -> Option<json::Value> {
self.json.min.clone()
}
pub fn max(&self) -> Option<json::Value> {
self.json.max.clone()
}
#[cfg(feature = "names")]
#[cfg_attr(docsrs, doc(cfg(feature = "names")))]
pub fn name(&self) -> Option<&'a str> {
self.json.name.as_deref()
}
pub fn normalized(&self) -> bool {
self.json.normalized
}
pub fn sparse(&self) -> Option<sparse::Sparse<'a>> {
self.json
.sparse
.as_ref()
.map(|json| sparse::Sparse::new(self.document, json))
}
}