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
use arrayvec::ArrayVec;

use crate::Error;
use std::fmt::{Display, Formatter};
use std::{convert::TryFrom, fmt::Debug, iter::FromIterator};

#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
#[repr(u8)]
pub enum TextEncoding {
  UTF16LE = 0,
  UTF8 = 1,
}

impl Display for TextEncoding {
  fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
    match self {
      TextEncoding::UTF16LE => write!(f, "utf16 le"),
      TextEncoding::UTF8 => write!(f, "utf8"),
    }
  }
}

impl TryFrom<u8> for TextEncoding {
  type Error = Error;

  fn try_from(value: u8) -> Result<Self, Self::Error> {
    match value {
      0 => Ok(TextEncoding::UTF16LE),
      1 => Ok(TextEncoding::UTF8),
      e => Err(Error::UnknownTextEncoding(e)),
    }
  }
}

#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
#[repr(u8)]
pub enum IndexSize {
  I8 = 1,
  I16 = 2,
  I32 = 4,
}

impl Display for IndexSize {
  fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
    match self {
      IndexSize::I8 => write!(f, "8-bit"),
      IndexSize::I16 => write!(f, "16-bit"),
      IndexSize::I32 => write!(f, "32-bit"),
    }
  }
}

impl TryFrom<u8> for IndexSize {
  type Error = Error;

  fn try_from(value: u8) -> Result<Self, Self::Error> {
    match value {
      1 => Ok(IndexSize::I8),
      2 => Ok(IndexSize::I16),
      4 => Ok(IndexSize::I32),
      e => Err(Error::UnknownIndexSize(e)),
    }
  }
}

pub trait Index: TryFrom<i8> + TryFrom<i16> + TryFrom<i32> + Clone + Debug + Eq {}
impl<I: TryFrom<i8> + TryFrom<i16> + TryFrom<i32> + Clone + Debug + Eq> Index for I {}

pub trait VertexIndex: TryFrom<u8> + TryFrom<u16> + TryFrom<i32> + Clone + Debug + Eq {}
impl<I: TryFrom<u8> + TryFrom<u16> + TryFrom<i32> + Clone + Debug + Eq> VertexIndex for I {}

pub trait Config {
  type VertexIndex: VertexIndex;
  type TextureIndex: Index;
  type MaterialIndex: Index;
  type BoneIndex: Index;
  type MorphIndex: Index;
  type RigidbodyIndex: Index;

  type Vec2: From<[f32; 2]> + Clone + Debug + PartialEq;
  type Vec3: From<[f32; 3]> + Clone + Debug + PartialEq;
  type Vec4: From<[f32; 4]> + Clone + Debug + PartialEq;
  type AdditionalVec4s: FromIterator<Self::Vec4> + Clone + Debug + PartialEq;
}

pub struct DefaultConfig;

impl Config for DefaultConfig {
  type VertexIndex = i32;
  type TextureIndex = i32;
  type MaterialIndex = i32;
  type BoneIndex = i32;
  type MorphIndex = i32;
  type RigidbodyIndex = i32;

  #[cfg(feature = "vek")]
  type Vec2 = vek::Vec2<f32>;
  #[cfg(not(feature = "vek"))]
  type Vec2 = [f32; 2];

  #[cfg(feature = "vek")]
  type Vec3 = vek::Vec3<f32>;
  #[cfg(not(feature = "vek"))]
  type Vec3 = [f32; 3];

  #[cfg(feature = "vek")]
  type Vec4 = vek::Vec4<f32>;
  #[cfg(not(feature = "vek"))]
  type Vec4 = [f32; 4];

  #[cfg(feature = "arrayvec")]
  type AdditionalVec4s = ArrayVec<[Self::Vec4; 4]>;
  #[cfg(not(feature = "arrayvec"))]
  type AdditionalVec4s = Vec<Self::Vec4>;
}