use crate::validation::{Checked, Validate};
use crate::{extensions, image, Extras, Index};
use gltf_derive::Validate;
use serde::{de, ser};
use serde_derive::{Deserialize, Serialize};
use std::fmt;
pub const NEAREST: u32 = 9728;
pub const LINEAR: u32 = 9729;
pub const NEAREST_MIPMAP_NEAREST: u32 = 9984;
pub const LINEAR_MIPMAP_NEAREST: u32 = 9985;
pub const NEAREST_MIPMAP_LINEAR: u32 = 9986;
pub const LINEAR_MIPMAP_LINEAR: u32 = 9987;
pub const CLAMP_TO_EDGE: u32 = 33_071;
pub const MIRRORED_REPEAT: u32 = 33_648;
pub const REPEAT: u32 = 10_497;
pub const VALID_MAG_FILTERS: &[u32] = &[NEAREST, LINEAR];
pub const VALID_MIN_FILTERS: &[u32] = &[
NEAREST,
LINEAR,
NEAREST_MIPMAP_NEAREST,
LINEAR_MIPMAP_NEAREST,
NEAREST_MIPMAP_LINEAR,
LINEAR_MIPMAP_LINEAR,
];
pub const VALID_WRAPPING_MODES: &[u32] = &[CLAMP_TO_EDGE, MIRRORED_REPEAT, REPEAT];
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
pub enum MagFilter {
Nearest = 1,
Linear,
}
impl MagFilter {
pub fn as_gl_enum(&self) -> u32 {
match *self {
MagFilter::Nearest => NEAREST,
MagFilter::Linear => LINEAR,
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
pub enum MinFilter {
Nearest = 1,
Linear,
NearestMipmapNearest,
LinearMipmapNearest,
NearestMipmapLinear,
LinearMipmapLinear,
}
impl MinFilter {
pub fn as_gl_enum(&self) -> u32 {
match *self {
MinFilter::Nearest => NEAREST,
MinFilter::Linear => LINEAR,
MinFilter::NearestMipmapNearest => NEAREST_MIPMAP_NEAREST,
MinFilter::LinearMipmapNearest => LINEAR_MIPMAP_NEAREST,
MinFilter::NearestMipmapLinear => NEAREST_MIPMAP_LINEAR,
MinFilter::LinearMipmapLinear => LINEAR_MIPMAP_LINEAR,
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
pub enum WrappingMode {
ClampToEdge = 1,
MirroredRepeat,
Repeat,
}
impl WrappingMode {
pub fn as_gl_enum(&self) -> u32 {
match *self {
WrappingMode::ClampToEdge => CLAMP_TO_EDGE,
WrappingMode::MirroredRepeat => MIRRORED_REPEAT,
WrappingMode::Repeat => REPEAT,
}
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize, Validate)]
#[serde(default)]
pub struct Sampler {
#[serde(rename = "magFilter")]
#[serde(skip_serializing_if = "Option::is_none")]
pub mag_filter: Option<Checked<MagFilter>>,
#[serde(rename = "minFilter")]
#[serde(skip_serializing_if = "Option::is_none")]
pub min_filter: Option<Checked<MinFilter>>,
#[cfg(feature = "names")]
#[cfg_attr(feature = "names", serde(skip_serializing_if = "Option::is_none"))]
pub name: Option<String>,
#[serde(default, rename = "wrapS")]
pub wrap_s: Checked<WrappingMode>,
#[serde(default, rename = "wrapT")]
pub wrap_t: Checked<WrappingMode>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub extensions: Option<extensions::texture::Sampler>,
#[serde(default)]
#[cfg_attr(feature = "extras", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(not(feature = "extras"), serde(skip_serializing))]
pub extras: Extras,
}
fn source_default() -> Index<image::Image> {
Index::new(u32::MAX)
}
fn source_is_empty(source: &Index<image::Image>) -> bool {
source.value() == u32::MAX as usize
}
fn source_validate<P, R>(source: &Index<image::Image>, root: &crate::Root, path: P, report: &mut R)
where
P: Fn() -> crate::Path,
R: FnMut(&dyn Fn() -> crate::Path, crate::validation::Error),
{
if cfg!(feature = "allow_empty_texture") {
if !source_is_empty(source) {
source.validate(root, path, report);
}
} else if source_is_empty(source) {
report(&path, crate::validation::Error::Missing);
} else {
source.validate(root, &path, report);
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Texture {
#[cfg(feature = "names")]
#[cfg_attr(feature = "names", serde(skip_serializing_if = "Option::is_none"))]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sampler: Option<Index<Sampler>>,
#[serde(default = "source_default", skip_serializing_if = "source_is_empty")]
pub source: Index<image::Image>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub extensions: Option<extensions::texture::Texture>,
#[serde(default)]
#[cfg_attr(feature = "extras", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(not(feature = "extras"), serde(skip_serializing))]
pub extras: Extras,
}
impl Validate for Texture {
fn validate<P, R>(&self, root: &crate::Root, path: P, report: &mut R)
where
P: Fn() -> crate::Path,
R: FnMut(&dyn Fn() -> crate::Path, crate::validation::Error),
{
self.sampler
.validate(root, || path().field("sampler"), report);
self.extensions
.validate(root, || path().field("extensions"), report);
source_validate(&self.source, root, || path().field("source"), report);
}
}
#[derive(Clone, Debug, Deserialize, Serialize, Validate)]
pub struct Info {
pub index: Index<Texture>,
#[serde(default, rename = "texCoord")]
pub tex_coord: u32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub extensions: Option<extensions::texture::Info>,
#[serde(default)]
#[cfg_attr(feature = "extras", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(not(feature = "extras"), serde(skip_serializing))]
pub extras: Extras,
}
impl<'de> de::Deserialize<'de> for Checked<MagFilter> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
struct Visitor;
impl<'de> de::Visitor<'de> for Visitor {
type Value = Checked<MagFilter>;
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "any of: {:?}", VALID_MAG_FILTERS)
}
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
use self::MagFilter::*;
use crate::validation::Checked::*;
Ok(match value as u32 {
NEAREST => Valid(Nearest),
LINEAR => Valid(Linear),
_ => Invalid,
})
}
}
deserializer.deserialize_u64(Visitor)
}
}
impl<'de> de::Deserialize<'de> for Checked<MinFilter> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
struct Visitor;
impl<'de> de::Visitor<'de> for Visitor {
type Value = Checked<MinFilter>;
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "any of: {:?}", VALID_MIN_FILTERS)
}
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
use self::MinFilter::*;
use crate::validation::Checked::*;
Ok(match value as u32 {
NEAREST => Valid(Nearest),
LINEAR => Valid(Linear),
NEAREST_MIPMAP_NEAREST => Valid(NearestMipmapNearest),
LINEAR_MIPMAP_NEAREST => Valid(LinearMipmapNearest),
NEAREST_MIPMAP_LINEAR => Valid(NearestMipmapLinear),
LINEAR_MIPMAP_LINEAR => Valid(LinearMipmapLinear),
_ => Invalid,
})
}
}
deserializer.deserialize_u64(Visitor)
}
}
impl ser::Serialize for MinFilter {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
serializer.serialize_u32(self.as_gl_enum())
}
}
impl<'de> de::Deserialize<'de> for Checked<WrappingMode> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
struct Visitor;
impl<'de> de::Visitor<'de> for Visitor {
type Value = Checked<WrappingMode>;
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "any of: {:?}", VALID_WRAPPING_MODES)
}
fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
where
E: de::Error,
{
use self::WrappingMode::*;
use crate::validation::Checked::*;
Ok(match value as u32 {
CLAMP_TO_EDGE => Valid(ClampToEdge),
MIRRORED_REPEAT => Valid(MirroredRepeat),
REPEAT => Valid(Repeat),
_ => Invalid,
})
}
}
deserializer.deserialize_u64(Visitor)
}
}
impl ser::Serialize for MagFilter {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
serializer.serialize_u32(self.as_gl_enum())
}
}
impl Default for WrappingMode {
fn default() -> Self {
WrappingMode::Repeat
}
}
impl ser::Serialize for WrappingMode {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
serializer.serialize_u32(self.as_gl_enum())
}
}
#[cfg(test)]
mod tests {
#[test]
fn deserialize_source() {
let json = r#"{"asset":{"version":"2.0"},"textures":[{"source": 0}]}"#;
let root = serde_json::from_str::<crate::Root>(json).unwrap();
assert_eq!(0, root.textures[0].source.value());
}
#[test]
fn deserialize_empty_source() {
let json = r#"{"asset":{"version":"2.0"},"textures":[{}]}"#;
let root = serde_json::from_str::<crate::Root>(json).unwrap();
assert_eq!(u32::MAX as usize, root.textures[0].source.value());
}
#[test]
fn serialize_source() {
let root = crate::Root {
textures: vec![crate::Texture {
#[cfg(feature = "names")]
name: None,
sampler: None,
source: crate::Index::new(0),
extensions: None,
extras: Default::default(),
}],
..Default::default()
};
let json = serde_json::to_string(&root).unwrap();
assert_eq!(
r#"{"asset":{"version":"2.0"},"textures":[{"source":0}]}"#,
&json
);
}
#[test]
fn serialize_empty_source() {
let root = crate::Root {
textures: vec![crate::Texture {
#[cfg(feature = "names")]
name: None,
sampler: None,
source: crate::Index::new(u32::MAX),
extensions: None,
extras: Default::default(),
}],
..Default::default()
};
let json = serde_json::to_string(&root).unwrap();
assert_eq!(r#"{"asset":{"version":"2.0"},"textures":[{}]}"#, &json);
}
#[test]
fn validate_source() {
use crate::validation::{Error, Validate};
use crate::Path;
let json = r#"{"asset":{"version":"2.0"},"textures":[{"source":0}]}"#;
let root = serde_json::from_str::<crate::Root>(json).unwrap();
let mut errors = Vec::new();
root.textures[0].validate(
&root,
|| Path::new().field("textures").index(0),
&mut |path, error| {
errors.push((path(), error));
},
);
assert_eq!(1, errors.len());
let (path, error) = &errors[0];
assert_eq!("textures[0].source", path.as_str());
assert_eq!(Error::IndexOutOfBounds, *error);
}
#[test]
fn validate_empty_source() {
use crate::validation::{Error, Validate};
use crate::Path;
let json = r#"{"asset":{"version":"2.0"},"textures":[{}]}"#;
let root = serde_json::from_str::<crate::Root>(json).unwrap();
let mut errors = Vec::new();
root.textures[0].validate(
&root,
|| Path::new().field("textures").index(0),
&mut |path, error| {
errors.push((path(), error));
},
);
if cfg!(feature = "allow_empty_texture") {
assert!(errors.is_empty());
} else {
assert_eq!(1, errors.len());
let (path, error) = &errors[0];
assert_eq!("textures[0].source", path.as_str());
assert_eq!(Error::Missing, *error);
}
}
}