xray_docker/
lib.rs

1use std::borrow::Cow;
2
3use thiserror::Error;
4
5mod api;
6
7pub use api::DockerApi;
8
9pub type Result<T> = std::result::Result<T, DockerError>;
10
11#[derive(Error, Debug)]
12pub enum DockerError {
13    #[error("failed to get the home directory of the current user")]
14    GetHomeError(#[from] homedir::GetHomeError),
15    #[error("failed to resolve the '{var_name}' env variable")]
16    EnvLookupError {
17        var_name: Cow<'static, str>,
18        #[source]
19        source: std::env::VarError,
20    },
21    #[error("failed to perform an I/O operation: {description}")]
22    IoError {
23        description: Cow<'static, str>,
24        #[source]
25        source: std::io::Error,
26    },
27    #[error("failed to deserialize a JSON {description}")]
28    JsonDeserializationError {
29        description: Cow<'static, str>,
30        #[source]
31        source: serde_json::Error,
32    },
33    #[error("{0}")]
34    Other(Cow<'static, str>),
35}
36
37impl DockerError {
38    fn from_var_error_with_var_name(source: std::env::VarError, var_name: Cow<'static, str>) -> DockerError {
39        DockerError::EnvLookupError { var_name, source }
40    }
41
42    fn from_io_error_with_description(
43        source: std::io::Error,
44        description: impl Fn() -> Cow<'static, str>,
45    ) -> DockerError {
46        DockerError::IoError {
47            description: description(),
48            source,
49        }
50    }
51
52    fn from_serde_error_with_description(
53        source: serde_json::Error,
54        description: impl Fn() -> Cow<'static, str>,
55    ) -> DockerError {
56        DockerError::JsonDeserializationError {
57            description: description(),
58            source,
59        }
60    }
61}