Skip to main content

source_vtf/
error.rs

1use std::io;
2use std::path::{Path, PathBuf};
3
4use thiserror::Error;
5
6pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10pub enum Section {
11    Header,
12    ResourceTable,
13    ThumbnailData,
14    ImageData,
15    ResourceData,
16}
17
18impl std::fmt::Display for Section {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        f.write_str(match self {
21            Self::Header => "header",
22            Self::ResourceTable => "resource table",
23            Self::ThumbnailData => "thumbnail data",
24            Self::ImageData => "image data",
25            Self::ResourceData => "resource data",
26        })
27    }
28}
29
30#[derive(Debug, Error)]
31pub enum Error {
32    #[error("I/O error for `{path}`: {source}")]
33    Io {
34        path: PathBuf,
35        #[source]
36        source: io::Error,
37    },
38
39    #[error("invalid VTF signature 0x{0:08x}")]
40    InvalidSignature(u32),
41
42    #[error("unsupported VTF version {major}.{minor}")]
43    UnsupportedVersion { major: u32, minor: u32 },
44
45    #[error("invalid header size {declared} for VTF {major}.{minor}; expected {expected}")]
46    InvalidHeaderSize {
47        major: u32,
48        minor: u32,
49        declared: u32,
50        expected: u32,
51    },
52
53    #[error(
54        "unexpected end of {section} at byte {offset}: need {needed} bytes, {remaining} remain"
55    )]
56    UnexpectedEof {
57        section: Section,
58        offset: usize,
59        needed: usize,
60        remaining: usize,
61    },
62
63    #[error("integer overflow while calculating {context}")]
64    ArithmeticOverflow { context: &'static str },
65
66    #[error("invalid {field} value {value}: {reason}")]
67    InvalidField {
68        field: &'static str,
69        value: u64,
70        reason: &'static str,
71    },
72
73    #[error("unknown {field} image-format ID {value} in VTF {major}.{minor}")]
74    UnknownImageFormat {
75        field: &'static str,
76        value: i32,
77        major: u32,
78        minor: u32,
79    },
80
81    #[error("image-format ID {value} in VTF {major}.{minor} is not supported: {reason}")]
82    UnsupportedImageFormat {
83        value: i32,
84        major: u32,
85        minor: u32,
86        reason: &'static str,
87    },
88
89    #[error("VTF declares {count} resources; the safety limit is {limit}")]
90    ResourceCountLimit { count: u32, limit: u32 },
91
92    #[error("cannot reserve metadata for {count} VTF resources")]
93    ResourceAllocation { count: u32 },
94
95    #[error("resource 0x{tag:06x} is required but missing")]
96    MissingResource { tag: u32 },
97
98    #[error("resource 0x{tag:06x} occurs more than once")]
99    DuplicateResource { tag: u32 },
100
101    #[error("resource 0x{tag:06x} stores texture data inline in its four-byte directory slot")]
102    InlineTextureData { tag: u32 },
103
104    #[error("multiple non-inline resources point to byte {offset}")]
105    DuplicateResourceOffset { offset: usize },
106
107    #[error("resource 0x{tag:06x} points to byte {offset}, before header end {header_size}")]
108    ResourceInsideHeader {
109        tag: u32,
110        offset: usize,
111        header_size: usize,
112    },
113
114    #[error("{section} range {offset}..{end} exceeds the VTF size {file_size}")]
115    RangeOutOfBounds {
116        section: Section,
117        offset: usize,
118        end: usize,
119        file_size: usize,
120    },
121
122    #[error("{section} is too short: expected at least {expected} bytes, found {actual}")]
123    DataTooShort {
124        section: Section,
125        expected: usize,
126        actual: usize,
127    },
128
129    #[error("{axis} index {index} is out of range for {count} values")]
130    SubresourceOutOfRange {
131        axis: &'static str,
132        index: u32,
133        count: u32,
134    },
135}
136
137impl Error {
138    pub(crate) fn io(path: impl AsRef<Path>, source: io::Error) -> Self {
139        Self::Io {
140            path: path.as_ref().to_path_buf(),
141            source,
142        }
143    }
144}