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