use oxdock_diagnostics::Diagnostic;
const FIELD_ERR: &str = "err";
pub fn os_reader_lock_poisoned() -> Diagnostic {
Diagnostic::new(
"pipe.slot.os_reader_lock_poisoned",
"os pipe reader lock poisoned".to_string(),
Diagnostic::make_fields(&[]),
)
}
pub fn os_writer_lock_poisoned() -> Diagnostic {
Diagnostic::new(
"pipe.slot.os_writer_lock_poisoned",
"os pipe writer lock poisoned".to_string(),
Diagnostic::make_fields(&[]),
)
}
pub fn os_handle_consumed() -> Diagnostic {
Diagnostic::new(
"pipe.slot.os_handle_consumed",
"os pipe handle has already been consumed by another process".to_string(),
Diagnostic::make_fields(&[]),
)
}
pub fn handle_lock_poisoned() -> Diagnostic {
Diagnostic::new(
"pipe.slot.handle_lock_poisoned",
"pipe handle lock poisoned".to_string(),
Diagnostic::make_fields(&[]),
)
}
pub fn os_pipe_creation_failed(err: &std::io::Error) -> Diagnostic {
let err_text = err.to_string();
let message = format!("failed to create OS pipe: {err_text}");
Diagnostic::new(
"pipe.slot.os_pipe_creation_failed",
message,
Diagnostic::make_fields(&[(FIELD_ERR, err_text.as_str())]),
)
.with_source(std::sync::Arc::new(std::io::Error::new(
err.kind(),
err_text,
)))
}
pub fn peek_unbound() -> Diagnostic {
Diagnostic::new(
"pipe.peek.unbound",
"cannot peek unbound pipe: bind it to a command first".to_string(),
Diagnostic::make_fields(&[]),
)
}
pub fn peek_os_materialized() -> Diagnostic {
Diagnostic::new(
"pipe.peek.os_materialized",
"cannot peek OS-materialized pipe: drain it through a bound command instead".to_string(),
Diagnostic::make_fields(&[]),
)
}
pub fn peek_failed(err: &std::io::Error) -> Diagnostic {
let err_text = err.to_string();
let message = format!("failed to peek pipe: {err_text}");
Diagnostic::new(
"pipe.peek.failed",
message,
Diagnostic::make_fields(&[(FIELD_ERR, err_text.as_str())]),
)
.with_source(std::sync::Arc::new(std::io::Error::new(
err.kind(),
err_text,
)))
}
pub fn captured_stdout_invalid_utf8(err: &std::string::FromUtf8Error) -> Diagnostic {
let err_text = err.to_string();
let message = format!("captured stdout is not valid UTF-8: {err_text}");
Diagnostic::new(
"pipe.spill.captured_stdout_invalid_utf8",
message,
Diagnostic::make_fields(&[(FIELD_ERR, err_text.as_str())]),
)
}
pub fn spill_drain_failed(err: &std::io::Error) -> Diagnostic {
let err_text = err.to_string();
let message = format!("failed to drain spill buffer: {err_text}");
Diagnostic::new(
"pipe.spill.drain_failed",
message,
Diagnostic::make_fields(&[(FIELD_ERR, err_text.as_str())]),
)
.with_source(std::sync::Arc::new(std::io::Error::new(
err.kind(),
err_text,
)))
}
#[cfg(test)]
mod tests {
use super::*;
fn io_err(msg: &str) -> std::io::Error {
std::io::Error::other(msg.to_string())
}
#[test]
fn each_failure_has_a_distinct_key() {
let err = io_err("boom");
let utf8_err = String::from_utf8(vec![0xff]).unwrap_err();
let keys = [
os_reader_lock_poisoned().key(),
os_writer_lock_poisoned().key(),
os_handle_consumed().key(),
handle_lock_poisoned().key(),
os_pipe_creation_failed(&err).key(),
peek_unbound().key(),
peek_os_materialized().key(),
peek_failed(&err).key(),
captured_stdout_invalid_utf8(&utf8_err).key(),
spill_drain_failed(&err).key(),
];
let mut seen = std::collections::HashSet::new();
for k in keys {
assert!(seen.insert(k), "duplicate key {k}");
}
}
#[test]
fn messages_match_canonical_wording() {
let err = io_err("boom");
assert_eq!(
os_reader_lock_poisoned().message(),
"os pipe reader lock poisoned"
);
assert_eq!(
os_writer_lock_poisoned().message(),
"os pipe writer lock poisoned"
);
assert_eq!(
os_handle_consumed().message(),
"os pipe handle has already been consumed by another process"
);
assert_eq!(
handle_lock_poisoned().message(),
"pipe handle lock poisoned"
);
assert_eq!(
os_pipe_creation_failed(&err).message(),
"failed to create OS pipe: boom"
);
assert_eq!(
peek_unbound().message(),
"cannot peek unbound pipe: bind it to a command first"
);
assert_eq!(
peek_os_materialized().message(),
"cannot peek OS-materialized pipe: drain it through a bound command instead"
);
assert_eq!(peek_failed(&err).message(), "failed to peek pipe: boom");
let utf8_err = String::from_utf8(vec![0xff]).unwrap_err();
assert!(
captured_stdout_invalid_utf8(&utf8_err)
.message()
.starts_with("captured stdout is not valid UTF-8: ")
);
assert_eq!(
spill_drain_failed(&err).message(),
"failed to drain spill buffer: boom"
);
}
#[test]
fn io_ctors_preserve_typed_source_chain() {
use std::error::Error as _;
let err = io_err("boom");
for diag in [
os_pipe_creation_failed(&err),
peek_failed(&err),
spill_drain_failed(&err),
] {
let source = diag.source().expect("io ctor keeps a source");
let io_source = source
.downcast_ref::<std::io::Error>()
.expect("source is the typed io error");
assert_eq!(io_source.kind(), std::io::ErrorKind::Other);
assert_eq!(io_source.to_string(), "boom");
}
}
}