libarchive_oxide_core/
error.rs1use alloc::string::String;
8use alloc::vec::Vec;
9use core::fmt;
10
11pub(crate) type Result<T> = core::result::Result<T, Error>;
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
19#[non_exhaustive]
20pub(crate) enum Error {
21 Malformed(&'static str),
23 Unsupported(&'static str),
25 LimitExceeded(&'static str),
27 InvalidState(&'static str),
29}
30
31impl fmt::Display for Error {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 match self {
34 Self::Malformed(m) => write!(f, "malformed archive: {m}"),
35 Self::Unsupported(m) => write!(f, "unsupported feature: {m}"),
36 Self::LimitExceeded(m) => write!(f, "safety limit exceeded: {m}"),
37 Self::InvalidState(m) => write!(f, "invalid state: {m}"),
38 }
39 }
40}
41
42impl core::error::Error for Error {}
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46#[non_exhaustive]
47pub enum ErrorKind {
48 Malformed,
50 Unsupported,
52 Limit,
54 Integrity,
56 Capability,
58 SizeRequired,
60 Protocol,
62 Policy,
64}
65
66#[derive(Debug, Clone, PartialEq, Eq)]
68pub struct ArchiveError {
69 kind: ErrorKind,
70 format: Option<&'static str>,
71 offset: Option<u64>,
72 entry_index: Option<u64>,
73 entry_path: Option<Vec<u8>>,
74 context: Vec<String>,
75}
76
77impl ArchiveError {
78 #[must_use]
80 pub const fn new(kind: ErrorKind) -> Self {
81 Self {
82 kind,
83 format: None,
84 offset: None,
85 entry_index: None,
86 entry_path: None,
87 context: Vec::new(),
88 }
89 }
90
91 #[must_use]
93 pub fn with_context(mut self, context: impl Into<String>) -> Self {
94 self.context.push(context.into());
95 self
96 }
97
98 #[must_use]
100 pub const fn with_format(mut self, format: &'static str) -> Self {
101 self.format = Some(format);
102 self
103 }
104
105 #[must_use]
107 pub const fn with_offset(mut self, offset: u64) -> Self {
108 self.offset = Some(offset);
109 self
110 }
111
112 #[must_use]
114 pub fn with_entry(mut self, index: u64, path: impl Into<Vec<u8>>) -> Self {
115 self.entry_index = Some(index);
116 self.entry_path = Some(path.into());
117 self
118 }
119
120 #[must_use]
122 pub const fn kind(&self) -> ErrorKind {
123 self.kind
124 }
125
126 #[must_use]
128 pub const fn format(&self) -> Option<&'static str> {
129 self.format
130 }
131
132 #[must_use]
134 pub const fn offset(&self) -> Option<u64> {
135 self.offset
136 }
137
138 #[must_use]
140 pub fn entry(&self) -> Option<(u64, &[u8])> {
141 Some((self.entry_index?, self.entry_path.as_deref()?))
142 }
143
144 #[must_use]
146 pub fn context(&self) -> &[String] {
147 &self.context
148 }
149}
150
151impl fmt::Display for ArchiveError {
152 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
153 write!(f, "{:?} archive error", self.kind)?;
154 if let Some(format) = self.format {
155 write!(f, " in {format}")?;
156 }
157 if let Some(offset) = self.offset {
158 write!(f, " at byte {offset}")?;
159 }
160 for context in &self.context {
161 write!(f, ": {context}")?;
162 }
163 Ok(())
164 }
165}
166
167impl core::error::Error for ArchiveError {}
168
169impl From<Error> for ArchiveError {
170 fn from(value: Error) -> Self {
171 match value {
172 Error::Malformed(message) => Self::new(ErrorKind::Malformed).with_context(message),
173 Error::Unsupported(message) => Self::new(ErrorKind::Unsupported).with_context(message),
174 Error::LimitExceeded(message) => Self::new(ErrorKind::Limit).with_context(message),
175 Error::InvalidState(message) => Self::new(ErrorKind::Protocol).with_context(message),
176 }
177 }
178}