use {buffer, json};
use Document;
pub use json::accessor::ComponentType as DataType;
pub use json::accessor::Type as Dimensions;
#[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,
view: buffer::View<'a>,
}
impl<'a> Accessor<'a> {
pub(crate) fn new(
document: &'a Document,
index: usize,
json: &'a json::accessor::Accessor,
) -> Self {
let view = document.views().nth(json.buffer_view.value()).unwrap();
Self {
document,
index,
json,
view,
}
}
pub fn index(&self) -> usize {
self.index
}
pub fn size(&self) -> usize {
self.data_type().size() * self.dimensions().multiplicity()
}
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 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) -> &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")]
pub fn name(&self) -> Option<&str> {
self.json.name.as_ref().map(String::as_str)
}
pub fn normalized(&self) -> bool {
self.json.normalized
}
pub fn sparse(&self) -> Option<sparse::Sparse> {
self.json.sparse.as_ref().map(|json| {
sparse::Sparse::new(self.document, json)
})
}
}