Skip to main content

easy_archive/
error.rs

1/// Error types for archive operations
2use thiserror::Error;
3
4/// Result type alias for archive operations
5pub type Result<T> = std::result::Result<T, ArchiveError>;
6
7/// Errors that can occur during archive operations
8#[derive(Error, Debug)]
9pub enum ArchiveError {
10    /// I/O error occurred during archive operation
11    #[error("I/O error: {0}")]
12    Io(#[from] std::io::Error),
13
14    /// Failed to decode an archive
15    #[error("Failed to decode {format} archive: {reason}")]
16    DecodeFailed {
17        /// The archive format that failed to decode
18        format: String,
19        /// The reason for the failure
20        reason: String,
21    },
22
23    /// Failed to encode an archive
24    #[error("Failed to encode {format} archive: {reason}")]
25    EncodeFailed {
26        /// The archive format that failed to encode
27        format: String,
28        /// The reason for the failure
29        reason: String,
30    },
31
32    /// Duplicate file paths detected in the archive
33    #[error("Duplicate file paths detected: {}", .paths.join(", "))]
34    DuplicateFiles {
35        /// List of duplicate file paths
36        paths: Vec<String>,
37    },
38
39    /// The archive format is not supported or not enabled
40    #[error("Unsupported format: {0}")]
41    UnsupportedFormat(String),
42
43    /// The archive structure is invalid
44    #[error("Invalid archive structure: {0}")]
45    InvalidArchive(String),
46
47    /// Compression operation failed
48    #[error("Compression error: {0}")]
49    CompressionError(String),
50
51    /// Decompression operation failed
52    #[error("Decompression error: {0}")]
53    DecompressionError(String),
54}
55
56impl ArchiveError {
57    /// Wrap an [`std::io::Error`] with additional context.
58    ///
59    /// The original [`std::io::ErrorKind`] is preserved, while the message is
60    /// prefixed with a description of the operation that failed. This keeps
61    /// errors specific (for example, which path could not be written) without
62    /// discarding the underlying operating-system error.
63    pub fn io_context(error: std::io::Error, context: impl std::fmt::Display) -> Self {
64        ArchiveError::Io(std::io::Error::new(
65            error.kind(),
66            format!("{}: {}", context, error),
67        ))
68    }
69}