conpty-oxide 0.1.2

Correctness-first Windows ConPTY (pseudoconsole) library with sync and async (tokio) APIs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// SPDX-FileCopyrightText: 2026 conpty-oxide contributors <https://github.com/P4suta/conpty-oxide/graphs/contributors>
//
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Stable error classifications with opaque diagnostic context.

#[cfg(any(feature = "blocking", feature = "tokio", test))]
use std::ffi::OsString;
use std::fmt;
use std::io;
use std::path::PathBuf;

/// Convenience alias for results produced by this crate.
pub type Result<T> = std::result::Result<T, Error>;

/// The operation phase in which an [`Error`] occurred.
///
/// The classification is the stable part of the error contract. Diagnostic
/// context remains available through [`Display`](fmt::Display),
/// [`Debug`](fmt::Debug), and the [`source`](std::error::Error::source) chain
/// without making that context part of the crate's `SemVer` surface.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorKind {
    /// Loading or validating the `ConPTY` backend failed.
    Backend,
    /// Creating the pseudoconsole or its pipes failed.
    CreateConsole,
    /// Spawning the root process failed.
    Spawn,
    /// Resizing the pseudoconsole failed.
    Resize,
    /// Clearing the pseudoconsole failed.
    Clear,
    /// The selected backend does not provide a requested capability.
    UnsupportedFeature,
    /// A requested terminal size was invalid.
    InvalidSize,
    /// Waiting for the root process failed.
    Wait,
    /// Terminating the process tree failed.
    Kill,
    /// Reading from or writing to a pseudoconsole pipe failed.
    Io,
}

/// A failure produced by `conpty-oxide`.
///
/// The representation is intentionally private. Use [`Error::kind`] for
/// control flow, [`Error::io_error`] for a directly held OS error, and
/// [`Error::backend_error`] for backend initialization details.
pub struct Error {
    repr: ErrorRepr,
}

#[derive(Debug, thiserror::Error)]
enum ErrorRepr {
    #[error("failed to initialize the ConPTY backend")]
    Backend(#[source] BackendError),
    #[cfg(any(feature = "blocking", feature = "tokio", test))]
    #[error("failed to create pseudoconsole")]
    CreateConsole(#[source] io::Error),
    #[cfg(any(feature = "blocking", feature = "tokio", test))]
    #[error("failed to spawn `{}`", .program.to_string_lossy())]
    Spawn {
        program: OsString,
        source: io::Error,
    },
    #[cfg(any(feature = "blocking", feature = "tokio", test))]
    #[error("failed to resize pseudoconsole")]
    Resize(#[source] io::Error),
    #[cfg(any(feature = "blocking", feature = "tokio", test))]
    #[error("failed to clear pseudoconsole")]
    Clear(#[source] io::Error),
    #[cfg(any(feature = "blocking", feature = "tokio", test))]
    #[error("the ConPTY backend does not support {feature}")]
    UnsupportedFeature { feature: &'static str },
    #[error(
        "invalid pseudoconsole size: {rows} rows x {cols} cols \
         (each dimension must be 1..={max})",
        max = crate::Size::MAX_DIMENSION
    )]
    InvalidSize { rows: u16, cols: u16 },
    #[cfg(any(feature = "blocking", feature = "tokio", test))]
    #[error("failed to wait for child process")]
    Wait(#[source] io::Error),
    #[cfg(any(feature = "blocking", feature = "tokio", test))]
    #[error("failed to kill child process")]
    Kill(#[source] io::Error),
    #[error("{0}")]
    Io(
        #[from]
        #[source]
        io::Error,
    ),
}

impl Error {
    /// Returns the stable classification of this failure.
    ///
    #[cfg_attr(
        feature = "blocking",
        doc = "# Examples",
        doc = "",
        doc = "```no_run",
        doc = "use conpty_oxide::blocking::Command;",
        doc = "",
        doc = "if let Err(error) = Command::new(\"does-not-exist.exe\").spawn() {",
        doc = "    eprintln!(\"{:?}: {error}\", error.kind());",
        doc = "}",
        doc = "```"
    )]
    #[must_use]
    pub const fn kind(&self) -> ErrorKind {
        match self.repr {
            ErrorRepr::Backend(_) => ErrorKind::Backend,
            #[cfg(any(feature = "blocking", feature = "tokio", test))]
            ErrorRepr::CreateConsole(_) => ErrorKind::CreateConsole,
            #[cfg(any(feature = "blocking", feature = "tokio", test))]
            ErrorRepr::Spawn { .. } => ErrorKind::Spawn,
            #[cfg(any(feature = "blocking", feature = "tokio", test))]
            ErrorRepr::Resize(_) => ErrorKind::Resize,
            #[cfg(any(feature = "blocking", feature = "tokio", test))]
            ErrorRepr::Clear(_) => ErrorKind::Clear,
            #[cfg(any(feature = "blocking", feature = "tokio", test))]
            ErrorRepr::UnsupportedFeature { .. } => ErrorKind::UnsupportedFeature,
            ErrorRepr::InvalidSize { .. } => ErrorKind::InvalidSize,
            #[cfg(any(feature = "blocking", feature = "tokio", test))]
            ErrorRepr::Wait(_) => ErrorKind::Wait,
            #[cfg(any(feature = "blocking", feature = "tokio", test))]
            ErrorRepr::Kill(_) => ErrorKind::Kill,
            ErrorRepr::Io(_) => ErrorKind::Io,
        }
    }

    /// Returns the directly held I/O error, when this failure has one.
    ///
    /// This does not walk the source chain. In particular, a backend failure
    /// returns `None`; call [`Error::backend_error`] and then
    /// [`BackendError::io_error`] for that case.
    #[must_use]
    pub const fn io_error(&self) -> Option<&io::Error> {
        match &self.repr {
            #[cfg(any(feature = "blocking", feature = "tokio", test))]
            ErrorRepr::CreateConsole(source)
            | ErrorRepr::Resize(source)
            | ErrorRepr::Clear(source)
            | ErrorRepr::Wait(source)
            | ErrorRepr::Kill(source)
            | ErrorRepr::Spawn { source, .. } => Some(source),
            ErrorRepr::Io(source) => Some(source),
            #[cfg(any(feature = "blocking", feature = "tokio", test))]
            ErrorRepr::UnsupportedFeature { .. } => None,
            ErrorRepr::Backend(_) | ErrorRepr::InvalidSize { .. } => None,
        }
    }

    /// Returns the backend failure when initialization or validation failed.
    #[must_use]
    pub const fn backend_error(&self) -> Option<&BackendError> {
        match &self.repr {
            ErrorRepr::Backend(source) => Some(source),
            #[cfg(any(feature = "blocking", feature = "tokio", test))]
            ErrorRepr::CreateConsole(_)
            | ErrorRepr::Spawn { .. }
            | ErrorRepr::Resize(_)
            | ErrorRepr::Clear(_)
            | ErrorRepr::UnsupportedFeature { .. }
            | ErrorRepr::Wait(_)
            | ErrorRepr::Kill(_) => None,
            ErrorRepr::InvalidSize { .. } | ErrorRepr::Io(_) => None,
        }
    }

    #[cfg(any(feature = "blocking", feature = "tokio", test))]
    pub(crate) const fn create_console(source: io::Error) -> Self {
        Self {
            repr: ErrorRepr::CreateConsole(source),
        }
    }

    #[cfg(any(feature = "blocking", feature = "tokio", test))]
    pub(crate) const fn spawn(program: OsString, source: io::Error) -> Self {
        Self {
            repr: ErrorRepr::Spawn { program, source },
        }
    }

    #[cfg(any(feature = "blocking", feature = "tokio", test))]
    pub(crate) const fn resize(source: io::Error) -> Self {
        Self {
            repr: ErrorRepr::Resize(source),
        }
    }

    #[cfg(any(feature = "blocking", feature = "tokio", test))]
    pub(crate) const fn clear(source: io::Error) -> Self {
        Self {
            repr: ErrorRepr::Clear(source),
        }
    }

    #[cfg(any(feature = "blocking", feature = "tokio", test))]
    pub(crate) const fn unsupported_feature(feature: &'static str) -> Self {
        Self {
            repr: ErrorRepr::UnsupportedFeature { feature },
        }
    }

    pub(crate) const fn invalid_size(rows: u16, cols: u16) -> Self {
        Self {
            repr: ErrorRepr::InvalidSize { rows, cols },
        }
    }

    #[cfg(any(feature = "blocking", feature = "tokio", test))]
    pub(crate) const fn wait(source: io::Error) -> Self {
        Self {
            repr: ErrorRepr::Wait(source),
        }
    }

    #[cfg(any(feature = "blocking", feature = "tokio", test))]
    pub(crate) const fn kill(source: io::Error) -> Self {
        Self {
            repr: ErrorRepr::Kill(source),
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.repr.fmt(f)
    }
}

impl fmt::Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Error")
            .field("kind", &self.kind())
            .field("context", &self.repr)
            .finish()
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        self.repr.source()
    }
}

impl From<BackendError> for Error {
    fn from(source: BackendError) -> Self {
        Self {
            repr: ErrorRepr::Backend(source),
        }
    }
}

impl From<io::Error> for Error {
    fn from(source: io::Error) -> Self {
        Self {
            repr: ErrorRepr::Io(source),
        }
    }
}

/// The failure class reported by a [`BackendError`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum BackendErrorKind {
    /// A requested `conpty.dll` could not be found or loaded.
    DllNotFound,
    /// A loaded DLL did not export a required function.
    MissingExport,
    /// A bundled DLL had no accompanying `OpenConsole.exe`.
    OpenConsoleMissing,
    /// The DLL and console host were not a validated matching pair.
    VersionMismatch,
    /// The system does not provide the required `ConPTY` API.
    Unsupported,
}

/// A failure while locating, loading, or validating a `ConPTY` backend.
///
/// The representation is private so paths, symbol names, and version strings
/// can evolve without becoming `SemVer` commitments.
pub struct BackendError {
    repr: BackendErrorRepr,
}

#[derive(Debug, thiserror::Error)]
enum BackendErrorRepr {
    #[error("conpty.dll not found in `{}`", .dir.display())]
    DllNotFound { dir: PathBuf, source: io::Error },
    #[error("`{}` is missing required export `{symbol}`", .dll.display())]
    MissingExport { dll: PathBuf, symbol: &'static str },
    #[error("OpenConsole.exe not found next to `{}`", .dll.display())]
    OpenConsoleMissing { dll: PathBuf },
    #[error(
        "version mismatch: `{}` reports {dll_version} \
         but its OpenConsole.exe reports {exe_version}",
        .dll.display()
    )]
    VersionMismatch {
        dll: PathBuf,
        dll_version: String,
        exe_version: String,
    },
    #[error(
        "ConPTY is not available on this version of Windows; \
         Windows 10 1809 (build 17763) or later is required"
    )]
    Unsupported,
}

impl BackendError {
    /// Returns the stable classification of this backend failure.
    #[must_use]
    pub const fn kind(&self) -> BackendErrorKind {
        match self.repr {
            BackendErrorRepr::DllNotFound { .. } => BackendErrorKind::DllNotFound,
            BackendErrorRepr::MissingExport { .. } => BackendErrorKind::MissingExport,
            BackendErrorRepr::OpenConsoleMissing { .. } => BackendErrorKind::OpenConsoleMissing,
            BackendErrorRepr::VersionMismatch { .. } => BackendErrorKind::VersionMismatch,
            BackendErrorRepr::Unsupported => BackendErrorKind::Unsupported,
        }
    }

    /// Returns the directly held I/O error, when this failure has one.
    #[must_use]
    pub const fn io_error(&self) -> Option<&io::Error> {
        match &self.repr {
            BackendErrorRepr::DllNotFound { source, .. } => Some(source),
            BackendErrorRepr::MissingExport { .. }
            | BackendErrorRepr::OpenConsoleMissing { .. }
            | BackendErrorRepr::VersionMismatch { .. }
            | BackendErrorRepr::Unsupported => None,
        }
    }

    pub(crate) const fn dll_not_found(dir: PathBuf, source: io::Error) -> Self {
        Self {
            repr: BackendErrorRepr::DllNotFound { dir, source },
        }
    }

    pub(crate) const fn missing_export(dll: PathBuf, symbol: &'static str) -> Self {
        Self {
            repr: BackendErrorRepr::MissingExport { dll, symbol },
        }
    }

    pub(crate) const fn open_console_missing(dll: PathBuf) -> Self {
        Self {
            repr: BackendErrorRepr::OpenConsoleMissing { dll },
        }
    }

    pub(crate) const fn version_mismatch(
        dll: PathBuf,
        dll_version: String,
        exe_version: String,
    ) -> Self {
        Self {
            repr: BackendErrorRepr::VersionMismatch {
                dll,
                dll_version,
                exe_version,
            },
        }
    }

    pub(crate) const fn unsupported() -> Self {
        Self {
            repr: BackendErrorRepr::Unsupported,
        }
    }
}

impl fmt::Display for BackendError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.repr.fmt(f)
    }
}

impl fmt::Debug for BackendError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("BackendError")
            .field("kind", &self.kind())
            .field("context", &self.repr)
            .finish()
    }
}

impl std::error::Error for BackendError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        self.repr.source()
    }
}

#[cfg(test)]
#[path = "error_tests.rs"]
mod tests;