1use thiserror::Error;
2use tiff_core::Compression;
3
4pub type Result<T> = std::result::Result<T, Error>;
5
6#[derive(Debug, Error)]
7pub enum Error {
8 #[error("I/O error reading {1}: {0}")]
9 Io(#[source] std::io::Error, String),
10
11 #[error("not a TIFF file: invalid magic bytes")]
12 InvalidMagic,
13
14 #[error("unsupported TIFF version: {0}")]
15 UnsupportedVersion(u16),
16
17 #[error("IFD index {0} not found")]
18 IfdNotFound(usize),
19
20 #[error("tag {0} not found in IFD")]
21 TagNotFound(u16),
22
23 #[error("unexpected tag type {actual} for tag {tag}, expected {expected}")]
24 UnexpectedTagType {
25 tag: u16,
26 expected: &'static str,
27 actual: u16,
28 },
29
30 #[error(
31 "unsupported compression: {name} ({0})",
32 name = unsupported_compression_name(*.0)
33 )]
34 UnsupportedCompression(u16),
35
36 #[error("unsupported predictor: {0}")]
37 UnsupportedPredictor(u16),
38
39 #[error("unsupported planar configuration: {0}")]
40 UnsupportedPlanarConfiguration(u16),
41
42 #[error("unsupported bits per sample: {0}")]
43 UnsupportedBitsPerSample(u16),
44
45 #[error("unsupported sample format: {0}")]
46 UnsupportedSampleFormat(u16),
47
48 #[error("decompression failed for strip/tile {index}: {reason}")]
49 DecompressionFailed { index: usize, reason: String },
50
51 #[error("data truncated at offset {offset}: need {needed} bytes, have {available}")]
52 Truncated {
53 offset: u64,
54 needed: u64,
55 available: u64,
56 },
57
58 #[error("offset {offset} with length {length} is out of bounds for a {data_len}-byte file")]
59 OffsetOutOfBounds {
60 offset: u64,
61 length: u64,
62 data_len: u64,
63 },
64
65 #[error("invalid TIFF tag {tag}: {reason}")]
66 InvalidTagValue { tag: u16, reason: String },
67
68 #[error("invalid image layout: {0}")]
69 InvalidImageLayout(String),
70
71 #[error("type mismatch: expected {expected}, found {actual}")]
72 TypeMismatch {
73 expected: &'static str,
74 actual: String,
75 },
76
77 #[error("{0}")]
78 Other(String),
79}
80
81fn unsupported_compression_name(code: u16) -> &'static str {
82 Compression::from_code(code)
83 .map(Compression::name)
84 .unwrap_or("unknown")
85}