rust_expect/backend.rs
1//! Backend module for different transport implementations.
2//!
3//! This module provides various backends for session communication,
4//! including PTY for local processes and SSH for remote connections.
5
6mod pty;
7
8// Export AsyncPty and PtyHandle for Unix platforms
9#[cfg(unix)]
10pub use pty::{AsyncPty, PtyHandle};
11pub use pty::{EnvMode, PtyConfig, PtySpawner, PtyTransport};
12// Export WindowsAsyncPty and WindowsPtyHandle for Windows platforms
13#[cfg(windows)]
14pub use pty::{WindowsAsyncPty, WindowsPtyHandle};
15
16// SSH backend is conditionally compiled
17#[cfg(feature = "ssh")]
18pub mod ssh;
19
20/// Trait for session backends.
21pub trait Backend {
22 /// The transport type produced by this backend.
23 type Transport;
24
25 /// Check if the backend is available.
26 fn is_available(&self) -> bool;
27
28 /// Get the backend name.
29 fn name(&self) -> &'static str;
30}
31
32/// Available backend types.
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34pub enum BackendType {
35 /// Local PTY backend.
36 Pty,
37 /// SSH backend for remote connections.
38 Ssh,
39 /// Mock backend for testing.
40 Mock,
41}
42
43impl BackendType {
44 /// Check if this backend is available.
45 #[must_use]
46 pub const fn is_available(self) -> bool {
47 match self {
48 Self::Pty => cfg!(unix) || cfg!(windows),
49 Self::Ssh => cfg!(feature = "ssh"),
50 Self::Mock => cfg!(feature = "mock"),
51 }
52 }
53
54 /// Get the backend name.
55 #[must_use]
56 pub const fn name(self) -> &'static str {
57 match self {
58 Self::Pty => "pty",
59 Self::Ssh => "ssh",
60 Self::Mock => "mock",
61 }
62 }
63}