alpm_compress/
error.rs

1//! Error handling.
2
3use std::{fmt::Debug, num::TryFromIntError};
4
5use alpm_types::CompressionAlgorithmFileExtension;
6use fluent_i18n::t;
7
8use crate::compression::CompressionSettings;
9
10/// An error that can occur when using compression.
11#[derive(Debug, thiserror::Error)]
12pub enum Error {
13    /// An error occurred while creating a Zstandard encoder.
14    #[error("{msg}", msg = t!("error-create-zstd-encoder", {
15        "context" => context,
16        "compression_settings" => format!("{compression_settings:?}"),
17        "source" => source.to_string()
18    }))]
19    CreateZstandardEncoder {
20        /// The context in which the error occurred.
21        ///
22        /// This is meant to complete the sentence "Error creating a Zstandard encoder while
23        /// {context} with {compression_settings}".
24        context: String,
25        /// The compression settings chosen for the encoder.
26        compression_settings: CompressionSettings,
27        /// The source error.
28        source: std::io::Error,
29    },
30
31    /// An error occurred while creating a Zstandard decoder.
32    #[error("{msg}", msg = t!("error-create-zstd-decoder", { "source" => .0.to_string() }))]
33    CreateZstandardDecoder(#[source] std::io::Error),
34
35    /// An error occurred while finishing a compression encoder.
36    #[error("{msg}", msg = t!("error-finish-encoder", {
37        "compression_type" => compression_type.to_string(),
38        "source" => source.to_string()
39    }))]
40    FinishEncoder {
41        /// The compression chosen for the encoder.
42        compression_type: CompressionAlgorithmFileExtension,
43        /// The source error
44        source: std::io::Error,
45    },
46
47    /// An error occurred while trying to get the available parallelism.
48    #[error("{msg}", msg = t!("error-get-parallelism", { "source" => .0.to_string() }))]
49    GetParallelism(#[source] std::io::Error),
50
51    /// An error occurred while trying to convert an integer.
52    #[error("{msg}", msg = t!("error-integer-conversion", { "source" => .0.to_string() }))]
53    IntegerConversion(#[source] TryFromIntError),
54
55    /// An I/O error occurred while reading.
56    #[error("{msg}", msg = t!("error-io-read", {
57        "context" => context,
58        "source" => source.to_string()
59    }))]
60    IoRead {
61        /// The context in which the error occurred.
62        ///
63        /// This is meant to complete the sentence "I/O read error while ".
64        context: String,
65        /// The source error.
66        source: std::io::Error,
67    },
68
69    /// An I/O error occurred while writing.
70    #[error("{msg}", msg = t!("error-io-write", {
71        "context" => context,
72        "source" => source.to_string()
73    }))]
74    IoWrite {
75        /// The context in which the error occurred.
76        ///
77        /// This is meant to complete the sentence "I/O write error while ".
78        context: String,
79        /// The source error.
80        source: std::io::Error,
81    },
82
83    /// A compression level is not valid.
84    #[error("{msg}", msg = t!("error-invalid-compression-level", {
85        "level" => level.to_string(),
86        "min" => min.to_string(),
87        "max" => max.to_string()
88    }))]
89    InvalidCompressionLevel {
90        /// The invalid compression level.
91        level: u8,
92        /// The minimum valid compression level.
93        min: u8,
94        /// The maximum valid compression level.
95        max: u8,
96    },
97
98    /// A compression algorithm file extension is not known.
99    #[error("{msg}", msg = t!("error-unknown-compression-extension", { "source" => .0.to_string() }))]
100    UnknownCompressionAlgorithmFileExtension(#[source] alpm_types::Error),
101
102    /// An unsupported compression algorithm was used.
103    #[error("{msg}", msg = t!("error-unsupported-compression", { "value" => value }))]
104    UnsupportedCompressionAlgorithm {
105        /// The unsupported compression algorithm.
106        value: String,
107    },
108}