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::buffer::BufferView;
use crate::{Extensions, Extras, Idx};

/// glTF known mime types.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MimeTypeEnum {
    Jpeg,
    Png,
}

/// The image's media type.
#[derive(Clone, PartialEq, Eq, Deserialize, IntoOwned)]
#[serde(transparent)]
pub struct MimeType<'a>(#[serde(borrow)] pub Cow<'a, str>);

impl core::fmt::Debug for MimeType<'_> {
    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 MimeType<'_> {
    pub const JPEG: Self = Self(Cow::Borrowed("image/jpeg"));
    pub const PNG: Self = Self(Cow::Borrowed("image/png"));

    pub fn to_enum(&self) -> Option<MimeTypeEnum> {
        if *self == Self::JPEG {
            return Some(MimeTypeEnum::Jpeg);
        } else if *self == Self::PNG {
            return Some(MimeTypeEnum::Png);
        }

        None
    }
}

/// Image data used to create a texture.
#[derive(Debug, Clone, Deserialize, IntoOwned)]
pub struct Image<'a> {
    /// The user-defined name of this object.
    #[serde(borrow)]
    pub name: Option<Cow<'a, str>>,

    /// The URI (or IRI) of the image.
    #[serde(borrow)]
    pub uri: Option<Cow<'a, str>>,
    /// The index of the buffer view.
    #[serde(rename = "bufferView")]
    pub buffer_view: Option<Idx<BufferView<'static>>>,
    /// The image's media type.
    #[serde(borrow)]
    pub mime_type: Option<MimeType<'a>>,

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