gltf-reader 0.1.0

A simple glTF 2.0 reader using `serde` and `serde_json`
Documentation
use alloc::borrow::Cow;
use ownable::IntoOwned;
use serde::Deserialize;

use crate::{Extensions, Extras};

/// glTF known sampling filters.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FilterEnum {
    Nearest,
    Linear,
    NearestMipmapNearest,
    LinearMipmapNearest,
    NearestMipmapLinear,
    LinearMipmapLinear,
}

/// Sampling filter.
#[derive(Clone, Copy, PartialEq, Eq, Deserialize, IntoOwned)]
#[serde(transparent)]
pub struct Filter(pub u64);

impl core::fmt::Debug for Filter {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        if let Some(e) = self.to_enum() {
            e.fmt(f)
        } else {
            self.0.fmt(f)
        }
    }
}

impl Filter {
    pub const NEAREST: Self = Self(9728);
    pub const LINEAR: Self = Self(9729);
    pub const NEAREST_MIPMAP_NEAREST: Self = Self(9984);
    pub const LINEAR_MIPMAP_NEAREST: Self = Self(9985);
    pub const NEAREST_MIPMAP_LINEAR: Self = Self(9986);
    pub const LINEAR_MIPMAP_LINEAR: Self = Self(9987);

    pub fn to_enum(self) -> Option<FilterEnum> {
        Some(match self {
            Self::NEAREST => FilterEnum::Nearest,
            Self::LINEAR => FilterEnum::Linear,
            Self::NEAREST_MIPMAP_NEAREST => FilterEnum::NearestMipmapNearest,
            Self::LINEAR_MIPMAP_NEAREST => FilterEnum::LinearMipmapNearest,
            Self::NEAREST_MIPMAP_LINEAR => FilterEnum::NearestMipmapLinear,
            Self::LINEAR_MIPMAP_LINEAR => FilterEnum::LinearMipmapLinear,
            _ => return None,
        })
    }
}

/// glTF known wrapping modes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WrapEnum {
    ClampToEdge,
    MirroredRepeat,
    Repeat,
}

/// Wrapping mode.
#[derive(Clone, Copy, PartialEq, Eq, Deserialize, IntoOwned)]
#[serde(transparent)]
pub struct Wrap(pub u64);

impl Default for Wrap {
    fn default() -> Self {
        Self::REPEAT
    }
}

impl core::fmt::Debug for Wrap {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        if let Some(e) = self.to_enum() {
            e.fmt(f)
        } else {
            self.0.fmt(f)
        }
    }
}

impl Wrap {
    pub const CLAMP_TO_EDGE: Self = Self(33071);
    pub const MIRRORED_REPEAT: Self = Self(33648);
    pub const REPEAT: Self = Self(10497);

    pub fn to_enum(self) -> Option<WrapEnum> {
        Some(match self {
            Self::CLAMP_TO_EDGE => WrapEnum::ClampToEdge,
            Self::MIRRORED_REPEAT => WrapEnum::MirroredRepeat,
            Self::REPEAT => WrapEnum::Repeat,
            _ => return None,
        })
    }
}

/// Texture sampler properties for filtering and wrapping modes.
#[derive(Debug, Clone, Deserialize, IntoOwned)]
pub struct Sampler<'a> {
    /// The user-defined name of this object.
    #[serde(borrow)]
    pub name: Option<Cow<'a, str>>,

    /// Magnification filter.
    #[serde(rename = "magFilter")]
    pub mag_filter: Option<Filter>,
    /// Minification filter.
    #[serde(rename = "minFilter")]
    pub min_filter: Option<Filter>,
    /// S (U) wrapping mode.
    #[serde(rename = "wrapS")]
    #[serde(default)]
    pub wrap_s: Wrap,
    /// T (V) wrapping mode.
    #[serde(rename = "wrapT")]
    #[serde(default)]
    pub wrap_t: Wrap,

    #[serde(borrow)]
    pub extensions: Option<Extensions<'a>>,
    #[serde(borrow)]
    pub extras: Option<Extras<'a>>,
}