diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:59:35 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:59:35 +0000 |
commit | d1b2d29528b7794b41e66fc2136e395a02f8529b (patch) | |
tree | a4a17504b260206dec3cf55b2dca82929a348ac2 /vendor/escargot/src/error.rs | |
parent | Releasing progress-linux version 1.72.1+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-d1b2d29528b7794b41e66fc2136e395a02f8529b.tar.xz rustc-d1b2d29528b7794b41e66fc2136e395a02f8529b.zip |
Merging upstream version 1.73.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/escargot/src/error.rs')
-rw-r--r-- | vendor/escargot/src/error.rs | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/vendor/escargot/src/error.rs b/vendor/escargot/src/error.rs new file mode 100644 index 000000000..4f19c4013 --- /dev/null +++ b/vendor/escargot/src/error.rs @@ -0,0 +1,91 @@ +//! Error reporting API. + +use std::error::Error; +use std::fmt; + +/// Result of a cargo command. +pub type CargoResult<T> = Result<T, CargoError>; + +/// For programmatically processing failures. +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub enum ErrorKind { + /// Spawning the cargo subommand failed. + InvalidCommand, + /// The cargo subcommand returned an error. + CommandFailed, + /// Parsing the cargo subcommand's output failed. + InvalidOutput, +} + +impl fmt::Display for ErrorKind { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match *self { + ErrorKind::InvalidOutput => write!(f, "Spawning the cargo subommand failed."), + ErrorKind::CommandFailed => write!(f, "The cargo subcommand returned an error."), + ErrorKind::InvalidCommand => write!(f, "Parsing the cargo subcommand's output failed."), + } + } +} + +/// Cargo command failure information. +#[derive(Debug)] +pub struct CargoError { + kind: ErrorKind, + context: Option<String>, + cause: Option<Box<dyn Error + Send + Sync + 'static>>, +} + +impl CargoError { + pub(crate) fn new(kind: ErrorKind) -> Self { + Self { + kind, + context: None, + cause: None, + } + } + + pub(crate) fn set_context<S>(mut self, context: S) -> Self + where + S: Into<String>, + { + let context = context.into(); + self.context = Some(context); + self + } + + pub(crate) fn set_cause<E>(mut self, cause: E) -> Self + where + E: Error + Send + Sync + 'static, + { + let cause = Box::new(cause); + self.cause = Some(cause); + self + } + + /// For programmatically processing failures. + pub fn kind(&self) -> ErrorKind { + self.kind + } +} + +impl Error for CargoError { + fn cause(&self) -> Option<&dyn Error> { + self.cause.as_ref().map(|c| { + let c: &dyn Error = c.as_ref(); + c + }) + } +} + +impl fmt::Display for CargoError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + writeln!(f, "Cargo command failed: {}", self.kind)?; + if let Some(ref context) = self.context { + writeln!(f, "{}", context)?; + } + if let Some(ref cause) = self.cause { + writeln!(f, "Cause: {}", cause)?; + } + Ok(()) + } +} |