shine_gltf/
image.rs

1use crate::validation::{Error, Validate};
2use crate::{buffer, extensions, Index, Path, Root};
3use serde_derive::{Deserialize, Serialize};
4use shine_gltf_macro::Validate;
5
6/// All valid MIME types.
7pub const VALID_MIME_TYPES: &[&str] = &["image/jpeg", "image/png"];
8
9/// Image data used to create a texture.
10#[derive(Clone, Debug, Deserialize, Serialize, Validate)]
11pub struct Image {
12    /// The index of the buffer view that contains the image. Use this instead of
13    /// the image's uri property.
14    #[serde(rename = "bufferView")]
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub buffer_view: Option<Index<buffer::View>>,
17
18    /// The image's MIME type.
19    #[serde(rename = "mimeType")]
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub mime_type: Option<MimeType>,
22
23    /// The uri of the image.  Relative paths are relative to the .gltf file.
24    /// Instead of referencing an external file, the uri can also be a data-uri.
25    /// The image format must be jpg or png.
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub uri: Option<String>,
28
29    /// Extension specific data.
30    #[serde(default, skip_serializing_if = "Option::is_none")]
31    pub extensions: Option<extensions::image::Image>,
32}
33
34/// An image MIME type.
35#[derive(Clone, Debug, Deserialize, Serialize)]
36pub struct MimeType(pub String);
37
38impl Validate for MimeType {
39    fn validate_completely<P, R>(&self, _: &Root, path: P, report: &mut R)
40    where
41        P: Fn() -> Path,
42        R: FnMut(&dyn Fn() -> Path, Error),
43    {
44        if !VALID_MIME_TYPES.contains(&self.0.as_str()) {
45            report(&path, Error::Invalid);
46        }
47    }
48}