use std::io;
#[derive(Debug, thiserror::Error)]
pub enum PtyError {
#[error("failed to create PTY: {0}")]
Create(#[source] io::Error),
#[error("failed to spawn process: {0}")]
Spawn(#[source] io::Error),
#[error("PTY I/O error: {0}")]
Io(#[from] io::Error),
#[error("failed to set terminal attributes: {0}")]
SetAttributes(#[source] io::Error),
#[error("failed to get terminal attributes: {0}")]
GetAttributes(#[source] io::Error),
#[error("failed to resize PTY: {0}")]
Resize(#[source] io::Error),
#[error("PTY has been closed")]
Closed,
#[error("child process exited with status: {0}")]
ProcessExited(i32),
#[cfg(unix)]
#[error("child process killed by signal: {0}")]
ProcessSignaled(i32),
#[error("failed to send signal: {0}")]
Signal(#[source] io::Error),
#[error("failed to wait for child: {0}")]
Wait(#[source] io::Error),
#[error("invalid window size: {width}x{height}")]
InvalidWindowSize {
width: u16,
height: u16,
},
#[error("operation timed out")]
Timeout,
#[cfg(unix)]
#[error("Unix error: {message}")]
Unix {
message: String,
errno: i32,
},
#[cfg(windows)]
#[error("Windows error: {message} (code: {code})")]
Windows {
message: String,
code: u32,
},
#[cfg(windows)]
#[error("ConPTY is not available on this Windows version")]
ConPtyNotAvailable,
}
pub type Result<T> = std::result::Result<T, PtyError>;
#[cfg(unix)]
impl From<rustix::io::Errno> for PtyError {
fn from(errno: rustix::io::Errno) -> Self {
Self::Io(io::Error::from_raw_os_error(errno.raw_os_error()))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_display() {
let err = PtyError::Closed;
assert_eq!(err.to_string(), "PTY has been closed");
}
#[test]
fn error_from_io() {
let io_err = io::Error::new(io::ErrorKind::NotFound, "not found");
let pty_err: PtyError = io_err.into();
assert!(matches!(pty_err, PtyError::Io(_)));
}
}