1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use gltf::json;
use json::validation::Checked;
use serde::Deserialize;

/*
 * Parsing textures from command line
 * The following structs are designed to reduce verbosity on command line.
 */

/// Magnification filter.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
pub enum MagFilter {
    /// Corresponds to `GL_NEAREST`.
    #[serde(alias = "nearest")]
    Nearest,
    /// Corresponds to `GL_LINEAR`.
    #[serde(alias = "linear")]
    Linear,
    None,
}

impl Into<Option<Checked<json::texture::MagFilter>>> for MagFilter {
    fn into(self) -> Option<Checked<json::texture::MagFilter>> {
        match self {
            MagFilter::Nearest => Some(Checked::Valid(json::texture::MagFilter::Nearest)),
            MagFilter::Linear => Some(Checked::Valid(json::texture::MagFilter::Linear)),
            MagFilter::None => None,
        }
    }
}

impl Default for MagFilter {
    fn default() -> Self {
        MagFilter::None
    }
}

/// Minification filter.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
pub enum MinFilter {
    /// Corresponds to `GL_NEAREST`.
    #[serde(alias = "nearest")]
    Nearest,
    /// Corresponds to `GL_LINEAR`.
    #[serde(alias = "linear")]
    Linear,
    /// Corresponds to `GL_NEAREST_MIPMAP_NEAREST`.
    #[serde(alias = "nearest_mipmap_nearest")]
    NearestMipmapNearest,
    /// Corresponds to `GL_LINEAR_MIPMAP_NEAREST`.
    #[serde(alias = "linear_mipmap_nearest")]
    LinearMipmapNearest,
    /// Corresponds to `GL_NEAREST_MIPMAP_LINEAR`.
    #[serde(alias = "nearest_mipmap_linear")]
    NearestMipmapLinear,
    /// Corresponds to `GL_LINEAR_MIPMAP_LINEAR`.
    #[serde(alias = "linear_mipmap_linear")]
    LinearMipmapLinear,
    None,
}

impl Into<Option<Checked<json::texture::MinFilter>>> for MinFilter {
    fn into(self) -> Option<Checked<json::texture::MinFilter>> {
        match self {
            MinFilter::Nearest => Some(Checked::Valid(json::texture::MinFilter::Nearest)),
            MinFilter::Linear => Some(Checked::Valid(json::texture::MinFilter::Linear)),
            MinFilter::NearestMipmapNearest => Some(Checked::Valid(
                json::texture::MinFilter::NearestMipmapNearest,
            )),
            MinFilter::LinearMipmapNearest => Some(Checked::Valid(
                json::texture::MinFilter::LinearMipmapNearest,
            )),
            MinFilter::NearestMipmapLinear => Some(Checked::Valid(
                json::texture::MinFilter::NearestMipmapLinear,
            )),
            MinFilter::LinearMipmapLinear => {
                Some(Checked::Valid(json::texture::MinFilter::LinearMipmapLinear))
            }
            MinFilter::None => None,
        }
    }
}

impl Default for MinFilter {
    fn default() -> Self {
        MinFilter::None
    }
}

/// Texture co-ordinate wrapping mode.
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq)]
pub enum WrappingMode {
    /// Corresponds to `GL_CLAMP_TO_EDGE`.
    #[serde(alias = "clamp_to_edge")]
    ClampToEdge,
    /// Corresponds to `GL_MIRRORED_REPEAT`.
    #[serde(alias = "mirrored_repeat")]
    MirroredRepeat,
    /// Corresponds to `GL_REPEAT`.
    #[serde(alias = "repeat")]
    Repeat,
}

impl Into<Checked<json::texture::WrappingMode>> for WrappingMode {
    fn into(self) -> Checked<json::texture::WrappingMode> {
        match self {
            WrappingMode::ClampToEdge => Checked::Valid(json::texture::WrappingMode::ClampToEdge),
            WrappingMode::MirroredRepeat => {
                Checked::Valid(json::texture::WrappingMode::MirroredRepeat)
            }
            WrappingMode::Repeat => Checked::Valid(json::texture::WrappingMode::Repeat),
        }
    }
}

impl Default for WrappingMode {
    fn default() -> Self {
        WrappingMode::Repeat
    }
}

#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct TextureInfo {
    pub image: ImageInfo,
    #[serde(default)]
    pub wrap_s: WrappingMode,
    #[serde(default)]
    pub wrap_t: WrappingMode,
    #[serde(default)]
    pub mag_filter: MagFilter,
    #[serde(default)]
    pub min_filter: MinFilter,
}

#[derive(Clone, Debug, PartialEq, Deserialize)]
pub enum ImageInfo {
    Uri(String),
    Embed(String),
}

impl std::str::FromStr for TextureInfo {
    type Err = ron::de::Error;
    fn from_str(input: &str) -> Result<TextureInfo, Self::Err> {
        ron::de::from_str::<TextureInfo>(input)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn deserialize_texture() {
        let tex = TextureInfo {
            image: ImageInfo::Uri("t.jpg".to_string()),
            wrap_s: WrappingMode::Repeat,
            wrap_t: WrappingMode::Repeat,
            mag_filter: MagFilter::Nearest,
            min_filter: MinFilter::None,
        };
        let expected: TextureInfo = ron::de::from_str(
            "(image:Uri(\"t.jpg\"),wrap_s:Repeat,wrap_t:Repeat,mag_filter:Nearest,)",
        )
        .unwrap();
        assert_eq!(expected, tex);
    }
}