Deduplicate primitive conversions for tag enum
This commit is contained in:
1 parent
d510a9f4ab
commit
f9cace84da
3 files changed
+30
-60
No files matched your search
+27
-17
@@ -9,6 +9,30 @@ use {TiffError, TiffFormatError, TiffResult, TiffUnsupportedError};
|
||||
|
||||
use self::Value::{Ascii, List, Rational, Unsigned, Signed, SRational};
|
||||
|
||||
pub(crate) mod seals {
|
||||
pub trait TagConversion<T>: Sized {
|
||||
fn __from_inner_type(T) -> Result<Self, T>;
|
||||
|
||||
fn __to_inner_type(&self) -> T;
|
||||
}
|
||||
}
|
||||
|
||||
pub trait U16Tag: Sized {
|
||||
fn from_u16(val: u16) -> Option<Self>;
|
||||
|
||||
fn to_u16(&self) -> u16;
|
||||
}
|
||||
|
||||
impl<T: seals::TagConversion<u16>> U16Tag for T {
|
||||
fn from_u16(val: u16) -> Option<Self> {
|
||||
Self::__from_inner_type(val).ok()
|
||||
}
|
||||
|
||||
fn to_u16(&self) -> u16 {
|
||||
Self::__to_inner_type(self)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! tags {
|
||||
{
|
||||
// Permit arbitrary meta items, which include documentation.
|
||||
@@ -31,7 +55,7 @@ macro_rules! tags {
|
||||
)*
|
||||
}
|
||||
|
||||
impl $name {
|
||||
impl ::decoder::ifd::seals::TagConversion<$ty> for $name {
|
||||
#[inline(always)]
|
||||
fn __from_inner_type(n: $ty) -> Result<Self, $ty> {
|
||||
match n {
|
||||
@@ -101,12 +125,8 @@ pub enum Tag(u16) unknown("A private or extension tag") {
|
||||
}
|
||||
|
||||
impl Tag {
|
||||
pub fn from_u16(val: u16) -> Self {
|
||||
Self::__from_inner_type(val).unwrap_or_else(Tag::Unknown)
|
||||
}
|
||||
|
||||
pub fn to_u16(&self) -> u16 {
|
||||
Self::__to_inner_type(self)
|
||||
pub fn from_u16_exhaustive(val: u16) -> Self {
|
||||
Self::from_u16(val).unwrap_or_else(|| Tag::Unknown(val))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,16 +147,6 @@ pub enum Type(u16) {
|
||||
}
|
||||
}
|
||||
|
||||
impl Type {
|
||||
pub fn from_u16(val: u16) -> Option<Self> {
|
||||
Self::__from_inner_type(val).ok()
|
||||
}
|
||||
|
||||
pub fn to_u16(&self) -> u16 {
|
||||
Self::__to_inner_type(self)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[allow(unused_qualifications)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
|
||||
+2
-42
@@ -5,7 +5,7 @@ use std::cmp;
|
||||
|
||||
use {ColorType, TiffError, TiffFormatError, TiffResult, TiffUnsupportedError};
|
||||
|
||||
use self::ifd::Directory;
|
||||
use self::ifd::{Directory, U16Tag};
|
||||
|
||||
use self::stream::{ByteOrder, EndianReader, LZWReader, DeflateReader, PackBitsReader, SmartReader};
|
||||
|
||||
@@ -154,46 +154,6 @@ enum Predictor(u16) {
|
||||
}
|
||||
}
|
||||
|
||||
impl PhotometricInterpretation {
|
||||
pub fn from_u16(val: u16) -> Option<Self> {
|
||||
Self::__from_inner_type(val).ok()
|
||||
}
|
||||
|
||||
pub fn to_u16(&self) -> u16 {
|
||||
Self::__to_inner_type(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl CompressionMethod {
|
||||
pub fn from_u16(val: u16) -> Option<Self> {
|
||||
Self::__from_inner_type(val).ok()
|
||||
}
|
||||
|
||||
pub fn to_u16(&self) -> u16 {
|
||||
Self::__to_inner_type(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl PlanarConfiguration {
|
||||
pub fn from_u16(val: u16) -> Option<Self> {
|
||||
Self::__from_inner_type(val).ok()
|
||||
}
|
||||
|
||||
pub fn to_u16(&self) -> u16 {
|
||||
Self::__to_inner_type(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Predictor {
|
||||
pub fn from_u16(val: u16) -> Option<Self> {
|
||||
Self::__from_inner_type(val).ok()
|
||||
}
|
||||
|
||||
pub fn to_u16(&self) -> u16 {
|
||||
Self::__to_inner_type(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct StripDecodeState {
|
||||
strip_index: usize,
|
||||
@@ -526,7 +486,7 @@ impl<R: Read + Seek> Decoder<R> {
|
||||
// Count 4 bytes
|
||||
// Value 4 bytes either a pointer the value itself
|
||||
fn read_entry(&mut self) -> TiffResult<Option<(ifd::Tag, ifd::Entry)>> {
|
||||
let tag = ifd::Tag::from_u16(self.read_short()?);
|
||||
let tag = ifd::Tag::from_u16_exhaustive(self.read_short()?);
|
||||
let type_ = match ifd::Type::from_u16(self.read_short()?) {
|
||||
Some(t) => t,
|
||||
None => {
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@ use std::collections::BTreeMap;
|
||||
use std::io::{Seek, Write};
|
||||
use std::mem;
|
||||
|
||||
use decoder::ifd::{self, Tag};
|
||||
use decoder::ifd::{self, Tag, U16Tag};
|
||||
use error::{TiffError, TiffFormatError, TiffResult};
|
||||
|
||||
pub mod colortype;
|
||||
|
||||
Reference in new issue
Block a user