Skip to main content

libfw_core/
error.rs

1//! Error types for the libfw core contracts.
2
3use std::io;
4
5/// Error produced by [`Compressor`](crate::compress::Compressor)
6/// implementations.
7#[derive(Debug, thiserror::Error)]
8pub enum CompressError {
9    /// An invalid compression level was requested.
10    #[error("invalid compression level: {0}")]
11    InvalidLevel(i32),
12    /// The underlying encoder failed.
13    #[error("io error while compressing: {0}")]
14    Io(#[from] io::Error),
15    /// A zrip encoder error occurred.
16    #[error("zrip compression error: {0}")]
17    Zrip(#[from] zrip::CompressError),
18}
19
20/// Error produced by [`Decompressor`](crate::compress::Decompressor)
21/// implementations.
22#[derive(Debug, thiserror::Error)]
23pub enum DecompressError {
24    /// The stream ended before a complete frame was delivered.
25    #[error("truncated compressed stream: {0}")]
26    Truncated(io::Error),
27    /// The underlying decoder failed.
28    #[error("io error while decompressing: {0}")]
29    Io(#[from] io::Error),
30    /// A decoded frame exceeded the safety output limit.
31    #[error("frame output exceeds safety limit of {limit} bytes")]
32    TooLarge {
33        /// The configured safety limit.
34        limit: usize,
35    },
36}
37
38/// Error produced by [`StorageBackend`](crate::storage::StorageBackend)
39/// implementations.
40#[derive(Debug, thiserror::Error)]
41pub enum StorageError {
42    /// The requested path does not exist.
43    #[error("path not found: {0}")]
44    NotFound(String),
45    /// The requested path already exists and must not be overwritten.
46    #[error("path already exists: {0}")]
47    AlreadyExists(String),
48    /// The backend has insufficient capacity / the file is too large.
49    #[error("file too large: {0}")]
50    TooLarge(u64),
51    /// A write failed mid-stream (e.g. resume offset mismatch).
52    #[error("write failed at offset {offset}: {source}")]
53    WriteFailed {
54        /// Offset at which the write was attempted.
55        offset: u64,
56        /// Underlying cause.
57        #[source]
58        source: io::Error,
59    },
60    /// The backend is not read-only writable, etc.
61    #[error("operation not supported: {0}")]
62    Unsupported(&'static str),
63    /// Any other backend failure.
64    #[error("storage error: {0}")]
65    Other(#[from] io::Error),
66}
67
68impl StorageError {
69    /// Convenience constructor for a failed write.
70    pub fn write_failed(offset: u64, source: io::Error) -> Self {
71        StorageError::WriteFailed { offset, source }
72    }
73}