1use std::fmt::{Display, Formatter, Result as FmtResult};
8use std::sync::Arc;
9
10use oxdock_diagnostics::Diagnostic;
11use oxdock_parser::KEYWORD_EXIT;
12
13#[derive(Debug, Clone)]
22pub struct ExitRequest {
23 code: i64,
24 source: Option<Arc<dyn std::error::Error + Send + Sync>>,
25}
26
27impl ExitRequest {
28 pub fn new(code: i64) -> Self {
30 Self { code, source: None }
31 }
32
33 pub fn with_source(code: i64, source: Arc<dyn std::error::Error + Send + Sync>) -> Self {
37 Self {
38 code,
39 source: Some(source),
40 }
41 }
42
43 pub fn code(&self) -> i64 {
45 self.code
46 }
47}
48
49impl PartialEq for ExitRequest {
50 fn eq(&self, other: &Self) -> bool {
51 self.code == other.code
52 }
53}
54
55impl Eq for ExitRequest {}
56
57impl Display for ExitRequest {
58 fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
59 write!(f, "{KEYWORD_EXIT} requested with code {}", self.code)
60 }
61}
62
63impl std::error::Error for ExitRequest {
64 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
65 self.source
66 .as_deref()
67 .map(|cause| cause as &(dyn std::error::Error + 'static))
68 }
69}
70
71pub fn exit_code_of_diag(err: &Diagnostic) -> Option<i64> {
77 let mut current: Option<&(dyn std::error::Error + 'static)> =
78 Some(err as &(dyn std::error::Error + 'static));
79 while let Some(node) = current {
80 if let Some(exit) = node.downcast_ref::<ExitRequest>() {
81 return Some(exit.code());
82 }
83 if let Some(diag) = node.downcast_ref::<Diagnostic>()
84 && let Some(code) = super::diagnostics::eval_exit_requested_code(diag)
85 {
86 return Some(code);
87 }
88 current = node.source();
89 }
90 None
91}
92
93pub fn exit_status_code(status: &std::process::ExitStatus) -> Option<i32> {
98 status.code()
99}
100
101pub fn describe_exit_status(status: &std::process::ExitStatus) -> String {
106 match exit_status_code(status) {
107 Some(code) => code.to_string(),
108 None => status.to_string(),
109 }
110}
111
112#[cfg(test)]
113mod tests {
114 use super::{ExitRequest, exit_code_of_diag};
115 use oxdock_diagnostics::Diagnostic;
116 use std::sync::Arc;
117
118 #[test]
119 fn display_matches_historic_message() {
120 assert_eq!(
121 ExitRequest::new(3).to_string(),
122 "EXIT requested with code 3"
123 );
124 }
125
126 #[test]
127 fn diag_finds_code_through_source_chain() {
128 let inner = Diagnostic::new(
129 "dsl.eval.async_cancelled",
130 "ASYNC task cancelled".to_string(),
131 Diagnostic::make_fields(&[]),
132 )
133 .with_source(Arc::new(ExitRequest::new(7)));
134 assert_eq!(exit_code_of_diag(&inner), Some(7));
135 }
136
137 #[test]
138 fn chained_exit_keeps_code_and_prior_cause() {
139 use std::error::Error as _;
140 let cause = Arc::new(std::io::Error::new(std::io::ErrorKind::NotFound, "gone"));
141 let exit = ExitRequest::with_source(7, cause);
142 assert_eq!(exit.code(), 7);
143 let prior = exit
144 .source()
145 .expect("prior cause rides behind the request")
146 .downcast_ref::<std::io::Error>()
147 .expect("prior cause keeps its type");
148 assert_eq!(prior.kind(), std::io::ErrorKind::NotFound);
149 let diag = Diagnostic::new(
150 "dsl.eval.async_cancelled",
151 "ASYNC task cancelled".to_string(),
152 Diagnostic::make_fields(&[]),
153 )
154 .with_source(Arc::new(exit));
155 assert_eq!(exit_code_of_diag(&diag), Some(7));
156 assert!(
157 diag.source()
158 .and_then(|cause| cause.downcast_ref::<ExitRequest>())
159 .is_some()
160 );
161 assert_eq!(
162 ExitRequest::new(7),
163 ExitRequest::with_source(
164 7,
165 Arc::new(std::io::Error::new(std::io::ErrorKind::NotFound, "gone"))
166 )
167 );
168 }
169
170 #[test]
171 fn diag_none_without_exit() {
172 let diag = Diagnostic::new(
173 "dsl.eval.async_cancelled",
174 "ASYNC task cancelled".to_string(),
175 Diagnostic::make_fields(&[]),
176 );
177 assert_eq!(exit_code_of_diag(&diag), None);
178 }
179
180 #[test]
181 fn keyed_exit_requested_carries_code_without_source() {
182 use std::error::Error as _;
183 let diag = crate::exec::diagnostics::eval_exit_requested(7);
184 assert_eq!(diag.key(), "dsl.eval.exit_requested");
185 assert_eq!(diag.message(), "EXIT requested with code 7");
186 assert_eq!(diag.fields().get("code"), Some(&"7".to_string()));
187 assert!(
188 diag.source().is_none(),
189 "no identical source to duplicate rendering"
190 );
191 assert_eq!(exit_code_of_diag(&diag), Some(7));
192 }
193
194 #[test]
195 fn describe_prefers_numeric_code() {
196 let status = oxdock_sys_test_utils::exit_status_from_code(3);
197 assert_eq!(super::describe_exit_status(&status), "3");
198 assert_eq!(super::exit_status_code(&status), Some(3));
199 }
200}