chunkify/chunk/
error.rs

1use crate::*;
2
3/// Errors that can occur during chunking operations.
4///
5/// Represents various failure scenarios when processing file chunks.
6#[derive(Debug)]
7pub enum ChunkStrategyError {
8    /// Missing file ID header in request.
9    MissingFileId,
10    /// Invalid chunk index value.
11    InvalidChunkIndex,
12    /// Missing chunk index header in request.
13    MissingChunkIndex,
14    /// Invalid total chunks value.
15    InvalidTotalChunks,
16    /// Missing total chunks header in request.
17    MissingTotalChunks,
18    /// Missing file name header in request.
19    MissingFileName,
20    /// Received empty chunk data.
21    EmptyChunkData,
22    /// Chunk index exceeds total chunks.
23    IndexOutOfBounds(usize, usize),
24    /// Failed to merge chunks.
25    Merge,
26    /// Failed to create directory.
27    CreateDirectory(String),
28    /// Failed to create chunk file.
29    CreateChunkFile(String),
30    /// Failed to write chunk data.
31    WriteChunk(String),
32    /// Failed to create output file.
33    CreateOutputFile(String),
34    /// Failed to read chunk file.
35    ReadChunk(String),
36    /// Failed to write to output file.
37    WriteOutput(String),
38}
39
40/// Provides display formatting for chunk strategy errors.
41impl fmt::Display for ChunkStrategyError {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        let message = match self {
44            ChunkStrategyError::MissingFileId => "Missing X-File-Id header",
45            ChunkStrategyError::InvalidChunkIndex => "Invalid X-Chunk-Index header",
46            ChunkStrategyError::MissingChunkIndex => "Missing X-Chunk-Index header",
47            ChunkStrategyError::InvalidTotalChunks => "Invalid X-Total-Chunks header",
48            ChunkStrategyError::MissingTotalChunks => "Missing X-Total-Chunks header",
49            ChunkStrategyError::MissingFileName => "Missing X-File-Name header",
50            ChunkStrategyError::EmptyChunkData => "Empty chunk data",
51            ChunkStrategyError::CreateDirectory(msg) => {
52                &format!("Failed to create directory: {msg}")
53            }
54            ChunkStrategyError::CreateChunkFile(msg) => {
55                &format!("Failed to create chunk file: {msg}")
56            }
57            ChunkStrategyError::WriteChunk(msg) => &format!("Failed to write chunk: {msg}"),
58            ChunkStrategyError::CreateOutputFile(msg) => {
59                &format!("Failed to create output file: {msg}")
60            }
61            ChunkStrategyError::ReadChunk(msg) => &format!("Failed to read chunk: {msg}"),
62            ChunkStrategyError::WriteOutput(msg) => {
63                &format!("Failed to write to output file: {msg}")
64            }
65            ChunkStrategyError::Merge => "Failed to complete the file merge operation",
66            ChunkStrategyError::IndexOutOfBounds(chunk_index, total_chunks) => {
67                &format!("Index {chunk_index} out of bounds(total: {total_chunks})")
68            }
69        };
70        write!(f, "{message}")
71    }
72}
73
74/// Marks ChunkStrategyError as a standard error type.
75impl std::error::Error for ChunkStrategyError {}
76
77/// Converts ChunkStrategyError to a byte vector.
78///
79/// Used for error responses in HTTP handlers.
80impl From<ChunkStrategyError> for Vec<u8> {
81    fn from(error: ChunkStrategyError) -> Self {
82        error.to_string().into_bytes()
83    }
84}