Skip to main content

rust_expect/
dialog.rs

1//! Dialog-based interaction scripting.
2//!
3//! This module provides a high-level abstraction for scripting
4//! interactive terminal sessions using dialog definitions.
5//!
6//! Dialogs define a sequence of expect/send steps that can be executed
7//! against a session. They support variable substitution and branching.
8//!
9//! # Examples
10//!
11//! ## Basic Dialog
12//!
13//! ```
14//! use rust_expect::{Dialog, DialogStep};
15//!
16//! // Create a simple login dialog
17//! let dialog = Dialog::named("login")
18//!     .step(DialogStep::new("username")
19//!         .with_expect("login:")
20//!         .with_send("admin\n"))
21//!     .step(DialogStep::new("password")
22//!         .with_expect("password:")
23//!         .with_send("secret\n"));
24//!
25//! assert_eq!(dialog.len(), 2);
26//! ```
27//!
28//! ## With Variables
29//!
30//! ```
31//! use rust_expect::{Dialog, DialogStep};
32//!
33//! // Variables are substituted in send text
34//! let dialog = Dialog::named("login")
35//!     .variable("USER", "admin")
36//!     .variable("PASS", "secret123")
37//!     .step(DialogStep::new("username")
38//!         .with_expect("login:")
39//!         .with_send("${USER}\n"))
40//!     .step(DialogStep::new("password")
41//!         .with_expect("password:")
42//!         .with_send("${PASS}\n"));
43//!
44//! // Variables are substituted when executing
45//! assert_eq!(dialog.substitute("${USER}"), "admin");
46//! ```
47//!
48//! ## Using the Builder
49//!
50//! ```
51//! use rust_expect::DialogBuilder;
52//!
53//! let dialog = DialogBuilder::named("setup")
54//!     .var("HOST", "server.example.com")
55//!     .expect_send("prompt", "> ", "connect ${HOST}\n")
56//!     .expect_send("auth", "password:", "mypassword\n")
57//!     .build();
58//!
59//! assert_eq!(dialog.name, "setup");
60//! ```
61//!
62//! ## Async Execution
63//!
64//! ```no_run
65//! use rust_expect::{Session, Dialog, DialogStep};
66//!
67//! #[tokio::main]
68//! async fn main() -> Result<(), rust_expect::ExpectError> {
69//!     let mut session = Session::spawn("/bin/bash", &[]).await?;
70//!
71//!     let dialog = Dialog::named("example")
72//!         .step(DialogStep::new("prompt")
73//!             .with_expect("$ ")
74//!             .with_send("echo hello\n"));
75//!
76//!     let result = session.run_dialog(&dialog).await?;
77//!     assert!(result.success);
78//!     Ok(())
79//! }
80//! ```
81
82pub mod common;
83pub mod definition;
84pub mod executor;
85
86pub use common::*;
87pub use definition::{Dialog, DialogBuilder, DialogStep};
88pub use executor::{DialogExecutor, DialogResult, StepResult};