use crate::validation::{Checked, Error, Validate};
use crate::{extensions, Extras, Path, Root};
use gltf_derive::Validate;
use serde::{de, ser};
use serde_derive::{Deserialize, Serialize};
use std::fmt;
pub const VALID_CAMERA_TYPES: &[&str] = &["perspective", "orthographic"];
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Type {
Perspective = 1,
Orthographic,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Camera {
#[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 orthographic: Option<Orthographic>,
#[serde(skip_serializing_if = "Option::is_none")]
pub perspective: Option<Perspective>,
#[serde(rename = "type")]
pub type_: Checked<Type>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub extensions: Option<extensions::camera::Camera>,
#[serde(default)]
#[cfg_attr(feature = "extras", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(not(feature = "extras"), serde(skip_serializing))]
pub extras: Extras,
}
#[derive(Clone, Debug, Deserialize, Serialize, Validate)]
pub struct Orthographic {
pub xmag: f32,
pub ymag: f32,
pub zfar: f32,
pub znear: f32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub extensions: Option<extensions::camera::Orthographic>,
#[serde(default)]
#[cfg_attr(feature = "extras", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(not(feature = "extras"), serde(skip_serializing))]
pub extras: Extras,
}
#[derive(Clone, Debug, Deserialize, Serialize, Validate)]
pub struct Perspective {
#[serde(rename = "aspectRatio")]
#[serde(skip_serializing_if = "Option::is_none")]
pub aspect_ratio: Option<f32>,
pub yfov: f32,
#[serde(skip_serializing_if = "Option::is_none")]
pub zfar: Option<f32>,
pub znear: f32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub extensions: Option<extensions::camera::Perspective>,
#[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 Camera {
fn validate<P, R>(&self, root: &Root, path: P, report: &mut R)
where
P: Fn() -> Path,
R: FnMut(&dyn Fn() -> Path, Error),
{
if self.orthographic.is_none() && self.perspective.is_none() {
report(&path, Error::Missing);
}
self.orthographic
.validate(root, || path().field("orthographic"), report);
self.perspective
.validate(root, || path().field("perspective"), report);
self.type_.validate(root, || path().field("type"), report);
self.extensions
.validate(root, || path().field("extensions"), report);
self.extras
.validate(root, || path().field("extras"), report);
}
}
impl<'de> de::Deserialize<'de> for Checked<Type> {
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<Type>;
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "any of: {:?}", VALID_CAMERA_TYPES)
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
use self::Type::*;
use crate::validation::Checked::*;
Ok(match value {
"perspective" => Valid(Perspective),
"orthographic" => Valid(Orthographic),
_ => Invalid,
})
}
}
deserializer.deserialize_str(Visitor)
}
}
impl ser::Serialize for Type {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
match *self {
Type::Perspective => serializer.serialize_str("perspective"),
Type::Orthographic => serializer.serialize_str("orthographic"),
}
}
}