diff options
author | Hubert Figuière <hub@figuiere.net> | 2024-01-02 19:27:29 -0500 |
---|---|---|
committer | Hubert Figuière <hub@figuiere.net> | 2024-01-02 19:27:29 -0500 |
commit | caeb2970c953a5459980ac7bc6bd7b368b91a6f9 (patch) | |
tree | fec437c0165759f70e21bfade5cc03f7305d0d9f | |
parent | 8932b91179786e41b1b58f727527b2a83d10b5ee (diff) |
exif: exif::TagType is only TryFrom<>
- This fixes a clippy warning about infaillible
Signed-off-by: Hubert Figuière <hub@figuiere.net>
-rw-r--r-- | src/tiff/entry.rs | 6 | ||||
-rw-r--r-- | src/tiff/exif.rs | 30 | ||||
-rw-r--r-- | src/tiff/iterator.rs | 5 |
3 files changed, 23 insertions, 18 deletions
diff --git a/src/tiff/entry.rs b/src/tiff/entry.rs index b5b2319..d98915c 100644 --- a/src/tiff/entry.rs +++ b/src/tiff/entry.rs @@ -2,7 +2,7 @@ /* * libopenraw - tiff/entry.rs * - * Copyright (C) 2022-2023 Hubert Figuière + * Copyright (C) 2022-2024 Hubert Figuière * * This library is free software: you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -548,8 +548,8 @@ impl Entry { .as_ref() .map_or_else(|| array_to_str(e.data()), |d| array_to_str(d)), ), - Err(_) => None, - _ => Some("VALUE".to_string()), + Ok(TagType::Invalid) => Some("INVALID".to_string()), + Err(n) => Some(n.to_string()), } .or_else(|| Some("ERROR".to_string())) .unwrap() diff --git a/src/tiff/exif.rs b/src/tiff/exif.rs index cad3cb2..ebff503 100644 --- a/src/tiff/exif.rs +++ b/src/tiff/exif.rs @@ -2,7 +2,7 @@ /* * libopenraw - tiff/exif.rs * - * Copyright (C) 2022-2023 Hubert Figuière + * Copyright (C) 2022-2024 Hubert Figuière * * This library is free software: you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -55,12 +55,14 @@ pub enum TagType { Invalid = 13, } -impl std::convert::From<i16> for TagType { - fn from(value: i16) -> Self { +impl std::convert::TryFrom<i16> for TagType { + type Error = i16; + + fn try_from(value: i16) -> Result<Self, Self::Error> { if value <= 0 || value > 13 { - Self::Invalid + Err(value) } else { - unsafe { std::mem::transmute(value) } + Ok(unsafe { std::mem::transmute(value) }) } } } @@ -427,25 +429,27 @@ pub enum LightsourceValue { #[cfg(test)] mod test { + use std::convert::TryFrom; + use byteorder::LittleEndian; use super::{ExifValue, Rational, SRational, TagType}; #[test] fn test_tag_type_convert() { - let tag = TagType::from(1); - assert_eq!(tag, TagType::Byte); + let tag = TagType::try_from(1); + assert_eq!(tag, Ok(TagType::Byte)); - let tag = TagType::from(4); - assert_eq!(tag, TagType::Long); + let tag = TagType::try_from(4); + assert_eq!(tag, Ok(TagType::Long)); // Invalid value - let tag = TagType::from(-1); - assert_eq!(tag, TagType::Invalid); + let tag = TagType::try_from(-1); + assert_eq!(tag, Err(-1)); // Invalid value - let tag = TagType::from(42); - assert_eq!(tag, TagType::Invalid); + let tag = TagType::try_from(42); + assert_eq!(tag, Err(42)); } #[test] diff --git a/src/tiff/iterator.rs b/src/tiff/iterator.rs index 31a0667..29fa5e5 100644 --- a/src/tiff/iterator.rs +++ b/src/tiff/iterator.rs @@ -2,7 +2,7 @@ /* * libopenraw - tiff/iterator.rs * - * Copyright (C) 2023 Hubert Figuière + * Copyright (C) 2023-2024 Hubert Figuière * * This library is free software: you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -22,6 +22,7 @@ //! The iterator over the ifd entries as `metadata::Value` use std::collections::btree_map; +use std::convert::TryFrom; use crate::container::Endian; use crate::metadata::{Metadata, Value as MetadataValue}; @@ -70,7 +71,7 @@ impl<'a> std::iter::Iterator for Iterator<'a> { } fn from_entry(entry: &Entry, endian: Endian) -> MetadataValue { - match exif::TagType::from(entry.type_) { + match exif::TagType::try_from(entry.type_).unwrap_or(exif::TagType::Invalid) { exif::TagType::Ascii => { MetadataValue::String(utils::to_nul_terminated(&entry.string_value().unwrap())) } |