mod tests;
const I8_TYPE_C_API_NAME: &str = "NC_BYTE";
const U8_TYPE_C_API_NAME: &str = "NC_CHAR";
const I16_TYPE_C_API_NAME: &str = "NC_SHORT";
const I32_TYPE_C_API_NAME: &str = "NC_INT";
const F32_TYPE_C_API_NAME: &str = "NC_FLOAT";
const F64_TYPE_C_API_NAME: &str = "NC_DOUBLE";
#[repr(u32)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DataType {
I8 = 1,
U8 = 2,
I16 = 3,
I32 = 4,
F32 = 5,
F64 = 6,
}
impl std::fmt::Display for DataType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"DataType::{}",
match self {
DataType::I8 => "I8",
DataType::U8 => "U8",
DataType::I16 => "I16",
DataType::I32 => "I32",
DataType::F32 => "F32",
DataType::F64 => "F64",
}
)
}
}
impl std::convert::TryFrom<u32> for DataType {
type Error = &'static str;
fn try_from(value: u32) -> Result<DataType, &'static str> {
match value {
1_u32 => Ok(DataType::I8),
2_u32 => Ok(DataType::U8),
3_u32 => Ok(DataType::I16),
4_u32 => Ok(DataType::I32),
5_u32 => Ok(DataType::F32),
6_u32 => Ok(DataType::F64),
_ => Err("Invalid value for a NetCDF-3 data type."),
}
}
}
impl DataType {
pub fn size_of(&self) -> usize {
match self {
DataType::I8 => std::mem::size_of::<i8>(),
DataType::U8 => std::mem::size_of::<u8>(),
DataType::I16 => std::mem::size_of::<i16>(),
DataType::I32 => std::mem::size_of::<i32>(),
DataType::F32 => std::mem::size_of::<f32>(),
DataType::F64 => std::mem::size_of::<f64>(),
}
}
pub fn c_api_name(&self) -> &'static str {
match self {
DataType::I8 => I8_TYPE_C_API_NAME,
DataType::U8 => U8_TYPE_C_API_NAME,
DataType::I16 => I16_TYPE_C_API_NAME,
DataType::I32 => I32_TYPE_C_API_NAME,
DataType::F32 => F32_TYPE_C_API_NAME,
DataType::F64 => F64_TYPE_C_API_NAME,
}
}
}