use std::fmt::{Display, Formatter, Result as FmtResult};
use std::sync::Arc;
use oxdock_diagnostics::Diagnostic;
use oxdock_parser::KEYWORD_EXIT;
#[derive(Debug, Clone)]
pub struct ExitRequest {
code: i64,
source: Option<Arc<dyn std::error::Error + Send + Sync>>,
}
impl ExitRequest {
pub fn new(code: i64) -> Self {
Self { code, source: None }
}
pub fn with_source(code: i64, source: Arc<dyn std::error::Error + Send + Sync>) -> Self {
Self {
code,
source: Some(source),
}
}
pub fn code(&self) -> i64 {
self.code
}
}
impl PartialEq for ExitRequest {
fn eq(&self, other: &Self) -> bool {
self.code == other.code
}
}
impl Eq for ExitRequest {}
impl Display for ExitRequest {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
write!(f, "{KEYWORD_EXIT} requested with code {}", self.code)
}
}
impl std::error::Error for ExitRequest {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.source
.as_deref()
.map(|cause| cause as &(dyn std::error::Error + 'static))
}
}
pub fn exit_code_of_diag(err: &Diagnostic) -> Option<i64> {
let mut current: Option<&(dyn std::error::Error + 'static)> =
Some(err as &(dyn std::error::Error + 'static));
while let Some(node) = current {
if let Some(exit) = node.downcast_ref::<ExitRequest>() {
return Some(exit.code());
}
if let Some(diag) = node.downcast_ref::<Diagnostic>()
&& let Some(code) = super::diagnostics::eval_exit_requested_code(diag)
{
return Some(code);
}
current = node.source();
}
None
}
pub fn exit_status_code(status: &std::process::ExitStatus) -> Option<i32> {
status.code()
}
pub fn describe_exit_status(status: &std::process::ExitStatus) -> String {
match exit_status_code(status) {
Some(code) => code.to_string(),
None => status.to_string(),
}
}
#[cfg(test)]
mod tests {
use super::{ExitRequest, exit_code_of_diag};
use oxdock_diagnostics::Diagnostic;
use std::sync::Arc;
#[test]
fn display_matches_historic_message() {
assert_eq!(
ExitRequest::new(3).to_string(),
"EXIT requested with code 3"
);
}
#[test]
fn diag_finds_code_through_source_chain() {
let inner = Diagnostic::new(
"dsl.eval.async_cancelled",
"ASYNC task cancelled".to_string(),
Diagnostic::make_fields(&[]),
)
.with_source(Arc::new(ExitRequest::new(7)));
assert_eq!(exit_code_of_diag(&inner), Some(7));
}
#[test]
fn chained_exit_keeps_code_and_prior_cause() {
use std::error::Error as _;
let cause = Arc::new(std::io::Error::new(std::io::ErrorKind::NotFound, "gone"));
let exit = ExitRequest::with_source(7, cause);
assert_eq!(exit.code(), 7);
let prior = exit
.source()
.expect("prior cause rides behind the request")
.downcast_ref::<std::io::Error>()
.expect("prior cause keeps its type");
assert_eq!(prior.kind(), std::io::ErrorKind::NotFound);
let diag = Diagnostic::new(
"dsl.eval.async_cancelled",
"ASYNC task cancelled".to_string(),
Diagnostic::make_fields(&[]),
)
.with_source(Arc::new(exit));
assert_eq!(exit_code_of_diag(&diag), Some(7));
assert!(
diag.source()
.and_then(|cause| cause.downcast_ref::<ExitRequest>())
.is_some()
);
assert_eq!(
ExitRequest::new(7),
ExitRequest::with_source(
7,
Arc::new(std::io::Error::new(std::io::ErrorKind::NotFound, "gone"))
)
);
}
#[test]
fn diag_none_without_exit() {
let diag = Diagnostic::new(
"dsl.eval.async_cancelled",
"ASYNC task cancelled".to_string(),
Diagnostic::make_fields(&[]),
);
assert_eq!(exit_code_of_diag(&diag), None);
}
#[test]
fn keyed_exit_requested_carries_code_without_source() {
use std::error::Error as _;
let diag = crate::exec::diagnostics::eval_exit_requested(7);
assert_eq!(diag.key(), "dsl.eval.exit_requested");
assert_eq!(diag.message(), "EXIT requested with code 7");
assert_eq!(diag.fields().get("code"), Some(&"7".to_string()));
assert!(
diag.source().is_none(),
"no identical source to duplicate rendering"
);
assert_eq!(exit_code_of_diag(&diag), Some(7));
}
#[test]
fn describe_prefers_numeric_code() {
let status = oxdock_sys_test_utils::exit_status_from_code(3);
assert_eq!(super::describe_exit_status(&status), "3");
assert_eq!(super::exit_status_code(&status), Some(3));
}
}