use crate::buffer;
use crate::extensions;
use crate::texture;
use crate::validation;
use serde;
use serde_derive::{Deserialize, Serialize};
use serde_json;
use shine_gltf_macro::Validate;
use std::{self, cmp, fmt, io, marker, str};
use crate::path::Path;
use crate::validation::Validate;
use crate::{Accessor, Animation, Asset, Buffer, Camera, Error, Image, Material, Mesh, Node, Scene, Skin, Texture, Value};
pub trait Get<T> {
fn get(&self, id: &Index<T>) -> Option<&T>;
}
pub trait GetMut<T> {
fn get_mut(&mut self, id: &Index<T>) -> Option<&mut T>;
}
#[derive(Clone, Copy, PartialOrd)]
pub struct Index<T>(u32, marker::PhantomData<T>);
#[derive(Clone, Debug, Default, Deserialize, Serialize, Validate)]
pub struct Root {
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub accessors: Vec<Accessor>,
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub animations: Vec<Animation>,
pub asset: Asset,
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub buffers: Vec<Buffer>,
#[serde(default, rename = "bufferViews")]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub buffer_views: Vec<buffer::View>,
#[serde(skip_serializing_if = "Option::is_none")]
pub scene: Option<Index<Scene>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub extensions: Option<extensions::root::Root>,
#[serde(default, rename = "extensionsUsed")]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub extensions_used: Vec<String>,
#[serde(default, rename = "extensionsRequired")]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub extensions_required: Vec<String>,
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub cameras: Vec<Camera>,
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub images: Vec<Image>,
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub materials: Vec<Material>,
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub meshes: Vec<Mesh>,
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub nodes: Vec<Node>,
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub samplers: Vec<texture::Sampler>,
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub scenes: Vec<Scene>,
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub skins: Vec<Skin>,
#[serde(default)]
#[serde(skip_serializing_if = "Vec::is_empty")]
pub textures: Vec<Texture>,
}
impl Root {
pub fn get<T>(&self, index: &Index<T>) -> Option<&T>
where
Self: Get<T>,
{
(self as &dyn Get<T>).get(index)
}
pub fn from_slice(slice: &[u8]) -> Result<Self, Error> {
serde_json::from_slice(slice)
}
pub fn from_reader<R>(reader: R) -> Result<Self, Error>
where
R: io::Read,
{
serde_json::from_reader(reader)
}
pub fn to_string(&self) -> Result<String, Error> {
serde_json::to_string(self)
}
pub fn to_string_pretty(&self) -> Result<String, Error> {
serde_json::to_string_pretty(self)
}
pub fn to_value(&self) -> Result<Value, Error> {
serde_json::to_value(self)
}
pub fn to_vec(&self) -> Result<Vec<u8>, Error> {
serde_json::to_vec(self)
}
pub fn to_vec_pretty(&self) -> Result<Vec<u8>, Error> {
serde_json::to_vec_pretty(self)
}
pub fn to_writer<W>(&self, writer: W) -> Result<(), Error>
where
W: io::Write,
{
serde_json::to_writer(writer, self)
}
pub fn to_writer_pretty<W>(&self, writer: W) -> Result<(), Error>
where
W: io::Write,
{
serde_json::to_writer_pretty(writer, self)
}
}
impl str::FromStr for Root {
type Err = Error;
fn from_str(str_: &str) -> Result<Self, Self::Err> {
serde_json::from_str(str_)
}
}
impl<T> Index<T> {
pub fn new(value: u32) -> Self {
Index(value, std::marker::PhantomData)
}
pub fn value(&self) -> usize {
self.0 as usize
}
}
impl<T> serde::Serialize for Index<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ::serde::Serializer,
{
serializer.serialize_u64(self.value() as u64)
}
}
impl<'de, T> serde::Deserialize<'de> for Index<T> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct Visitor<T>(marker::PhantomData<T>);
impl<'de, T> serde::de::Visitor<'de> for Visitor<T> {
type Value = Index<T>;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("index into child of root")
}
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(Index::new(value as u32))
}
}
deserializer.deserialize_u64(Visitor::<T>(marker::PhantomData))
}
}
impl<T> fmt::Debug for Index<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl<T> cmp::PartialEq for Index<T> {
fn eq(&self, other: &Index<T>) -> bool {
self.value() == other.value()
}
}
impl<T> fmt::Display for Index<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl<T: Validate> Validate for Index<T>
where
Root: Get<T>,
{
fn validate_minimally<P, R>(&self, root: &Root, path: P, report: &mut R)
where
P: Fn() -> Path,
R: FnMut(&dyn Fn() -> Path, validation::Error),
{
if root.get(self).is_none() {
report(&path, validation::Error::IndexOutOfBounds);
}
}
}
macro_rules! impl_get {
($ty:ty, $field:ident) => {
impl<'a> Get<$ty> for Root {
fn get(&self, index: &Index<$ty>) -> Option<&$ty> {
self.$field.get(index.value())
}
}
};
}
impl_get!(Accessor, accessors);
impl_get!(Animation, animations);
impl_get!(Buffer, buffers);
impl_get!(buffer::View, buffer_views);
impl_get!(Camera, cameras);
impl_get!(Image, images);
impl_get!(Material, materials);
impl_get!(Mesh, meshes);
impl_get!(Node, nodes);
impl_get!(texture::Sampler, samplers);
impl_get!(Scene, scenes);
impl_get!(Skin, skins);
impl_get!(Texture, textures);
macro_rules! impl_get_mut {
($ty:ty, $field:ident) => {
impl<'a> GetMut<$ty> for Root {
fn get_mut(&mut self, index: &Index<$ty>) -> Option<&mut $ty> {
self.$field.get_mut(index.value())
}
}
};
}
impl_get_mut!(Accessor, accessors);
impl_get_mut!(Animation, animations);
impl_get_mut!(Buffer, buffers);
impl_get_mut!(buffer::View, buffer_views);
impl_get_mut!(Camera, cameras);
impl_get_mut!(Image, images);
impl_get_mut!(Material, materials);
impl_get_mut!(Mesh, meshes);
impl_get_mut!(Node, nodes);
impl_get_mut!(texture::Sampler, samplers);
impl_get_mut!(Scene, scenes);
impl_get_mut!(Skin, skins);
impl_get_mut!(Texture, textures);
macro_rules! impl_add_vec {
($ty:ty, $fn:ident, $field:ident) => {
impl Root {
pub fn $fn(&mut self, data: $ty) -> Index<$ty> {
let id = self.$field.len() as u32;
self.$field.push(data);
Index::new(id)
}
}
};
}
impl_add_vec!(Accessor, add_accessor, accessors);
impl_add_vec!(Buffer, add_buffer, buffers);
impl_add_vec!(buffer::View, add_buffer_view, buffer_views);
impl_add_vec!(Node, add_node, nodes);
impl_add_vec!(Mesh, add_mesh, meshes);
impl_add_vec!(Scene, add_scene, scenes);