Skip to main content

parallel_disk_usage/
runtime_error.rs

1use derive_more::{Display, Error};
2use std::{convert::Infallible, process::ExitCode};
3
4/// Error caused by the CLI program.
5#[derive(Debug, Display, Error)]
6#[non_exhaustive]
7pub enum RuntimeError {
8    /// When it fails to write JSON representation of
9    /// [DataTreeReflection](crate::data_tree::Reflection) to stdout.
10    #[display("SerializationFailure: {_0}")]
11    SerializationFailure(serde_json::Error),
12    /// When it fails to read JSON representation of
13    /// [DataTreeReflection](crate::data_tree::Reflection) from stdin.
14    #[display("DeserializationFailure: {_0}")]
15    DeserializationFailure(serde_json::Error),
16    /// When `--json-input` and file names are both specified.
17    #[display("JsonInputArgConflict: Arguments exist alongside --json-input")]
18    JsonInputArgConflict,
19    /// When input JSON data is not a valid tree.
20    #[display("InvalidInputReflection: {_0}")]
21    InvalidInputReflection(#[error(not(source))] String),
22    /// When the user attempts to use unavailable platform-specific features.
23    #[display("UnsupportedFeature: {_0}")]
24    UnsupportedFeature(UnsupportedFeature),
25}
26
27/// Error caused by the user attempting to use unavailable platform-specific features.
28#[derive(Debug, Display, Error)]
29#[non_exhaustive]
30pub enum UnsupportedFeature {
31    /// Using `--deduplicate-hardlinks` on non-POSIX.
32    #[cfg(not(unix))]
33    #[display("Feature --deduplicate-hardlinks is not available on this platform")]
34    DeduplicateHardlink,
35    /// Using `--one-file-system` on non-POSIX.
36    #[cfg(not(unix))]
37    #[display("Feature --one-file-system is not available on this platform")]
38    OneFileSystem,
39}
40
41impl From<Infallible> for RuntimeError {
42    fn from(value: Infallible) -> Self {
43        match value {}
44    }
45}
46
47impl RuntimeError {
48    /// Convert error into exit code.
49    pub fn code(&self) -> ExitCode {
50        ExitCode::from(match self {
51            RuntimeError::SerializationFailure(_) => 2,
52            RuntimeError::DeserializationFailure(_) => 3,
53            RuntimeError::JsonInputArgConflict => 4,
54            RuntimeError::InvalidInputReflection(_) => 5,
55            RuntimeError::UnsupportedFeature(_) => 6,
56        })
57    }
58}