1 | #[cfg (unix)] |
2 | type RawFd = std::os::unix::io::RawFd; |
3 | #[cfg (not(unix))] |
4 | type RawFd = std::convert::Infallible; |
5 | |
6 | /// Error type for [`Client::from_env_ext`] function. |
7 | /// |
8 | /// [`Client::from_env_ext`]: crate::Client::from_env_ext |
9 | #[derive (Debug)] |
10 | pub struct FromEnvError { |
11 | pub(crate) inner: FromEnvErrorInner, |
12 | } |
13 | |
14 | /// Kind of an error returned from [`Client::from_env_ext`] function. |
15 | /// |
16 | /// [`Client::from_env_ext`]: crate::Client::from_env_ext |
17 | #[derive (Debug)] |
18 | #[non_exhaustive ] |
19 | pub enum FromEnvErrorKind { |
20 | /// There is no environment variable that describes jobserver to inherit. |
21 | NoEnvVar, |
22 | /// There is no jobserver in the environment variable. |
23 | /// Variables associated with Make can be used for passing data other than jobserver info. |
24 | NoJobserver, |
25 | /// Cannot parse jobserver environment variable value, incorrect format. |
26 | CannotParse, |
27 | /// Cannot open path or name from the jobserver environment variable value. |
28 | CannotOpenPath, |
29 | /// Cannot open file descriptor from the jobserver environment variable value. |
30 | CannotOpenFd, |
31 | /// The jobserver style is a simple pipe, but at least one of the file descriptors |
32 | /// is negative, which means it is disabled for this process |
33 | /// ([GNU `make` manual: POSIX Jobserver Interaction](https://www.gnu.org/software/make/manual/make.html#POSIX-Jobserver)). |
34 | NegativeFd, |
35 | /// File descriptor from the jobserver environment variable value is not a pipe. |
36 | NotAPipe, |
37 | /// Jobserver inheritance is not supported on this platform. |
38 | Unsupported, |
39 | } |
40 | |
41 | impl FromEnvError { |
42 | /// Get the error kind. |
43 | pub fn kind(&self) -> FromEnvErrorKind { |
44 | match self.inner { |
45 | FromEnvErrorInner::NoEnvVar => FromEnvErrorKind::NoEnvVar, |
46 | FromEnvErrorInner::NoJobserver => FromEnvErrorKind::NoJobserver, |
47 | FromEnvErrorInner::CannotParse(_) => FromEnvErrorKind::CannotParse, |
48 | FromEnvErrorInner::CannotOpenPath(..) => FromEnvErrorKind::CannotOpenPath, |
49 | FromEnvErrorInner::CannotOpenFd(..) => FromEnvErrorKind::CannotOpenFd, |
50 | FromEnvErrorInner::NegativeFd(..) => FromEnvErrorKind::NegativeFd, |
51 | FromEnvErrorInner::NotAPipe(..) => FromEnvErrorKind::NotAPipe, |
52 | FromEnvErrorInner::Unsupported => FromEnvErrorKind::Unsupported, |
53 | } |
54 | } |
55 | } |
56 | |
57 | impl std::fmt::Display for FromEnvError { |
58 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
59 | match &self.inner { |
60 | FromEnvErrorInner::NoEnvVar => write!(f, "there is no environment variable that describes jobserver to inherit" ), |
61 | FromEnvErrorInner::NoJobserver => write!(f, "there is no `--jobserver-fds=` or `--jobserver-auth=` in the environment variable" ), |
62 | FromEnvErrorInner::CannotParse(s: &String) => write!(f, "cannot parse jobserver environment variable value: {s}" ), |
63 | FromEnvErrorInner::CannotOpenPath(s: &String, err: &Error) => write!(f, "cannot open path or name {s} from the jobserver environment variable value: {err}" ), |
64 | FromEnvErrorInner::CannotOpenFd(fd: &i32, err: &Error) => write!(f, "cannot open file descriptor {fd} from the jobserver environment variable value: {err}" ), |
65 | FromEnvErrorInner::NegativeFd(fd: &i32) => write!(f, "file descriptor {fd} from the jobserver environment variable value is negative" ), |
66 | FromEnvErrorInner::NotAPipe(fd: &i32, None) => write!(f, "file descriptor {fd} from the jobserver environment variable value is not a pipe" ), |
67 | FromEnvErrorInner::NotAPipe(fd: &i32, Some(err: &Error)) => write!(f, "file descriptor {fd} from the jobserver environment variable value is not a pipe: {err}" ), |
68 | FromEnvErrorInner::Unsupported => write!(f, "jobserver inheritance is not supported on this platform" ), |
69 | } |
70 | } |
71 | } |
72 | impl std::error::Error for FromEnvError { |
73 | fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { |
74 | match &self.inner { |
75 | FromEnvErrorInner::CannotOpenPath(_, err: &Error) => Some(err), |
76 | FromEnvErrorInner::NotAPipe(_, Some(err: &Error)) | FromEnvErrorInner::CannotOpenFd(_, err: &Error) => { |
77 | Some(err) |
78 | } |
79 | _ => None, |
80 | } |
81 | } |
82 | } |
83 | |
84 | #[allow (dead_code)] |
85 | #[derive (Debug)] |
86 | pub(crate) enum FromEnvErrorInner { |
87 | NoEnvVar, |
88 | NoJobserver, |
89 | CannotParse(String), |
90 | CannotOpenPath(String, std::io::Error), |
91 | CannotOpenFd(RawFd, std::io::Error), |
92 | NegativeFd(RawFd), |
93 | NotAPipe(RawFd, Option<std::io::Error>), |
94 | Unsupported, |
95 | } |
96 | |