-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from belovdv/err-extend-type
Error type for `from_env_ext` function
- Loading branch information
Showing
5 changed files
with
195 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#[cfg(unix)] | ||
type RawFd = std::os::fd::RawFd; | ||
#[cfg(not(unix))] | ||
type RawFd = std::convert::Infallible; | ||
|
||
/// Error type for `from_env_ext` function. | ||
#[derive(Debug)] | ||
pub struct FromEnvError { | ||
pub(crate) inner: FromEnvErrorInner, | ||
} | ||
|
||
/// Kind of an error returned from `from_env_ext` function. | ||
#[derive(Debug)] | ||
#[non_exhaustive] | ||
pub enum FromEnvErrorKind { | ||
/// There is no environment variable that describes jobserver to inherit. | ||
NoEnvVar, | ||
/// Cannot parse jobserver environment variable value, incorrect format. | ||
CannotParse, | ||
/// Cannot open path or name from the jobserver environment variable value. | ||
CannotOpenPath, | ||
/// Cannot open file descriptor from the jobserver environment variable value. | ||
CannotOpenFd, | ||
/// File descriptor from the jobserver environment variable value is not a pipe. | ||
NotAPipe, | ||
/// Jobserver inheritance is not supported on this platform. | ||
Unsupported, | ||
} | ||
|
||
impl FromEnvError { | ||
/// Get the error kind. | ||
pub fn kind(&self) -> FromEnvErrorKind { | ||
match self.inner { | ||
FromEnvErrorInner::NoEnvVar => FromEnvErrorKind::NoEnvVar, | ||
FromEnvErrorInner::CannotParse(_) => FromEnvErrorKind::CannotParse, | ||
FromEnvErrorInner::CannotOpenPath(..) => FromEnvErrorKind::CannotOpenPath, | ||
FromEnvErrorInner::CannotOpenFd(..) => FromEnvErrorKind::CannotOpenFd, | ||
FromEnvErrorInner::NotAPipe(..) => FromEnvErrorKind::NotAPipe, | ||
FromEnvErrorInner::Unsupported => FromEnvErrorKind::Unsupported, | ||
} | ||
} | ||
} | ||
|
||
impl std::fmt::Display for FromEnvError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match &self.inner { | ||
FromEnvErrorInner::NoEnvVar => write!(f, "there is no environment variable that describes jobserver to inherit"), | ||
FromEnvErrorInner::CannotParse(s) => write!(f, "cannot parse jobserver environment variable value: {s}"), | ||
FromEnvErrorInner::CannotOpenPath(s, err) => write!(f, "cannot open path or name {s} from the jobserver environment variable value: {err}"), | ||
FromEnvErrorInner::CannotOpenFd(fd, err) => write!(f, "cannot open file descriptor {fd} from the jobserver environment variable value: {err}"), | ||
FromEnvErrorInner::NotAPipe(fd, None) => write!(f, "file descriptor {fd} from the jobserver environment variable value is not a pipe"), | ||
FromEnvErrorInner::NotAPipe(fd, Some(err)) => write!(f, "file descriptor {fd} from the jobserver environment variable value is not a pipe: {err}"), | ||
FromEnvErrorInner::Unsupported => write!(f, "jobserver inheritance is not supported on this platform"), | ||
} | ||
} | ||
} | ||
impl std::error::Error for FromEnvError { | ||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { | ||
match &self.inner { | ||
FromEnvErrorInner::CannotOpenPath(_, err) => Some(err), | ||
FromEnvErrorInner::NotAPipe(_, Some(err)) | FromEnvErrorInner::CannotOpenFd(_, err) => { | ||
Some(err) | ||
} | ||
_ => None, | ||
} | ||
} | ||
} | ||
|
||
#[allow(dead_code)] | ||
#[derive(Debug)] | ||
pub(crate) enum FromEnvErrorInner { | ||
NoEnvVar, | ||
CannotParse(String), | ||
CannotOpenPath(String, std::io::Error), | ||
CannotOpenFd(RawFd, std::io::Error), | ||
NotAPipe(RawFd, Option<std::io::Error>), | ||
Unsupported, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.