1use std::io;
7
8#[derive(Debug, thiserror::Error)]
13pub enum PtyError {
14 #[error("failed to create PTY: {0}")]
16 Create(#[source] io::Error),
17
18 #[error("failed to spawn process: {0}")]
20 Spawn(#[source] io::Error),
21
22 #[error("PTY I/O error: {0}")]
24 Io(#[from] io::Error),
25
26 #[error("failed to set terminal attributes: {0}")]
28 SetAttributes(#[source] io::Error),
29
30 #[error("failed to get terminal attributes: {0}")]
32 GetAttributes(#[source] io::Error),
33
34 #[error("failed to resize PTY: {0}")]
36 Resize(#[source] io::Error),
37
38 #[error("PTY has been closed")]
40 Closed,
41
42 #[error("child process exited with status: {0}")]
44 ProcessExited(i32),
45
46 #[cfg(unix)]
48 #[error("child process killed by signal: {0}")]
49 ProcessSignaled(i32),
50
51 #[error("failed to send signal: {0}")]
53 Signal(#[source] io::Error),
54
55 #[error("failed to wait for child: {0}")]
57 Wait(#[source] io::Error),
58
59 #[error("invalid window size: {width}x{height}")]
61 InvalidWindowSize {
62 width: u16,
64 height: u16,
66 },
67
68 #[error("operation timed out")]
70 Timeout,
71
72 #[cfg(unix)]
74 #[error("Unix error: {message}")]
75 Unix {
76 message: String,
78 errno: i32,
80 },
81
82 #[cfg(windows)]
84 #[error("Windows error: {message} (code: {code})")]
85 Windows {
86 message: String,
88 code: u32,
90 },
91
92 #[cfg(windows)]
94 #[error("ConPTY is not available on this Windows version")]
95 ConPtyNotAvailable,
96}
97
98pub type Result<T> = std::result::Result<T, PtyError>;
100
101#[cfg(unix)]
102impl From<rustix::io::Errno> for PtyError {
103 fn from(errno: rustix::io::Errno) -> Self {
104 Self::Io(io::Error::from_raw_os_error(errno.raw_os_error()))
105 }
106}
107
108#[cfg(test)]
109mod tests {
110 use super::*;
111
112 #[test]
113 fn error_display() {
114 let err = PtyError::Closed;
115 assert_eq!(err.to_string(), "PTY has been closed");
116 }
117
118 #[test]
119 fn error_from_io() {
120 let io_err = io::Error::new(io::ErrorKind::NotFound, "not found");
121 let pty_err: PtyError = io_err.into();
122 assert!(matches!(pty_err, PtyError::Io(_)));
123 }
124}