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}
36
37impl From<Infallible> for RuntimeError {
38    fn from(value: Infallible) -> Self {
39        match value {}
40    }
41}
42
43impl RuntimeError {
44    /// Convert error into exit code.
45    pub fn code(&self) -> ExitCode {
46        ExitCode::from(match self {
47            RuntimeError::SerializationFailure(_) => 2,
48            RuntimeError::DeserializationFailure(_) => 3,
49            RuntimeError::JsonInputArgConflict => 4,
50            RuntimeError::InvalidInputReflection(_) => 5,
51            RuntimeError::UnsupportedFeature(_) => 6,
52        })
53    }
54}