#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FieldType {
Byte,
Ascii,
Short,
Long,
Rational,
SByte,
Undefined,
SShort,
SLong,
SRational,
Float,
Double,
#[cfg(feature = "bigtiff")]
Long8,
#[cfg(feature = "bigtiff")]
SLong8,
#[cfg(feature = "bigtiff")]
Ifd8,
}
impl FieldType {
#[must_use]
pub fn from_code(code: u16) -> Option<Self> {
Some(match code {
1 => FieldType::Byte,
2 => FieldType::Ascii,
3 => FieldType::Short,
4 => FieldType::Long,
5 => FieldType::Rational,
6 => FieldType::SByte,
7 => FieldType::Undefined,
8 => FieldType::SShort,
9 => FieldType::SLong,
10 => FieldType::SRational,
11 => FieldType::Float,
12 => FieldType::Double,
#[cfg(feature = "bigtiff")]
16 => FieldType::Long8,
#[cfg(feature = "bigtiff")]
17 => FieldType::SLong8,
#[cfg(feature = "bigtiff")]
18 => FieldType::Ifd8,
_ => return None,
})
}
#[must_use]
pub fn code(self) -> u16 {
match self {
FieldType::Byte => 1,
FieldType::Ascii => 2,
FieldType::Short => 3,
FieldType::Long => 4,
FieldType::Rational => 5,
FieldType::SByte => 6,
FieldType::Undefined => 7,
FieldType::SShort => 8,
FieldType::SLong => 9,
FieldType::SRational => 10,
FieldType::Float => 11,
FieldType::Double => 12,
#[cfg(feature = "bigtiff")]
FieldType::Long8 => 16,
#[cfg(feature = "bigtiff")]
FieldType::SLong8 => 17,
#[cfg(feature = "bigtiff")]
FieldType::Ifd8 => 18,
}
}
#[must_use]
pub fn size(self) -> usize {
match self {
FieldType::Byte | FieldType::Ascii | FieldType::SByte | FieldType::Undefined => 1,
FieldType::Short | FieldType::SShort => 2,
FieldType::Long | FieldType::SLong | FieldType::Float => 4,
FieldType::Rational | FieldType::SRational | FieldType::Double => 8,
#[cfg(feature = "bigtiff")]
FieldType::Long8 | FieldType::SLong8 | FieldType::Ifd8 => 8,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn field_type_codes_round_trip() {
for code in 1..=12u16 {
let ty = FieldType::from_code(code).expect("known type");
assert_eq!(ty.code(), code);
}
assert_eq!(FieldType::from_code(0), None);
assert_eq!(FieldType::from_code(13), None);
assert_eq!(FieldType::Rational.size(), 8);
assert_eq!(FieldType::Short.size(), 2);
}
#[cfg(feature = "bigtiff")]
#[test]
fn bigtiff_field_type_codes_round_trip() {
for code in 16..=18u16 {
let ty = FieldType::from_code(code).expect("known BigTIFF type");
assert_eq!(ty.code(), code);
}
assert_eq!(FieldType::from_code(19), None);
assert_eq!(FieldType::Long8.size(), 8);
assert_eq!(FieldType::SLong8.size(), 8);
assert_eq!(FieldType::Ifd8.size(), 8);
}
#[cfg(not(feature = "bigtiff"))]
#[test]
fn bigtiff_codes_unknown_without_feature() {
assert_eq!(FieldType::from_code(16), None);
assert_eq!(FieldType::from_code(17), None);
assert_eq!(FieldType::from_code(18), None);
}
}