use crate::errors::AppError;
use std::io::Read;
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
pub fn read_stdin_with_timeout(secs: u64) -> Result<String, AppError> {
let (tx, rx) = mpsc::channel::<std::io::Result<String>>();
thread::spawn(move || {
let mut buf = String::new();
let result = std::io::stdin().read_to_string(&mut buf).map(|_| buf);
let _ = tx.send(result);
});
match rx.recv_timeout(Duration::from_secs(secs)) {
Ok(Ok(buf)) => Ok(buf),
Ok(Err(e)) => Err(AppError::Io(e)),
Err(mpsc::RecvTimeoutError::Timeout) => Err(AppError::Internal(anyhow::anyhow!(
"stdin read timed out after {secs}s; pipe must close within timeout window"
))),
Err(mpsc::RecvTimeoutError::Disconnected) => Err(AppError::Internal(anyhow::anyhow!(
"stdin reader thread disconnected unexpectedly"
))),
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::time::Instant;
#[test]
fn read_stdin_with_timeout_returns_internal_error_on_timeout() {
let start = Instant::now();
let result = read_stdin_with_timeout(1);
let elapsed = start.elapsed();
match result {
Err(AppError::Internal(e)) => {
assert!(e.to_string().contains("timed out"), "unexpected error: {e}");
assert!(elapsed.as_secs_f64() >= 0.9 && elapsed.as_secs_f64() < 2.5);
}
Ok(_) | Err(AppError::Io(_)) => {
}
Err(other) => panic!("unexpected error variant: {other:?}"),
}
}
}