openrunner_rs/
macros.rs

1//! Convenience macros for OpenRunner script execution.
2//!
3//! This module provides ergonomic macros that simplify common script execution patterns.
4
5/// Run OpenScript code with optional configuration.
6///
7/// This macro provides a convenient way to run OpenScript code with either default
8/// options or custom configuration.
9///
10/// # Examples
11///
12/// ```rust
13/// use openrunner_rs::{run_script, ScriptOptions};
14/// use std::time::Duration;
15///
16/// # #[tokio::main]
17/// # async fn main() -> openrunner_rs::Result<()> {
18/// // Run with default options. This assumes `openscript` is in the PATH.
19/// // let result = run_script!("echo 'Hello from default openscript!'").await?;
20///
21/// // Run with custom options (shell for testing)
22/// let options = ScriptOptions::new().openscript_path("/bin/sh");
23/// let result = run_script!("echo 'Hello, World!'", options).await?;
24///
25/// // Run with additional configuration
26/// let options = ScriptOptions::new()
27///     .openscript_path("/bin/sh")
28///     .timeout(Duration::from_secs(30));
29/// let result = run_script!("echo 'Hello, World!'", options).await?;
30/// # Ok(())
31/// # }
32/// ```
33#[macro_export]
34macro_rules! run_script {
35    ($script:expr) => {
36        $crate::run($script, $crate::ScriptOptions::default())
37    };
38    ($script:expr, $options:expr) => {
39        $crate::run($script, $options)
40    };
41}
42
43/// Spawn an OpenScript process with optional configuration.
44///
45/// This macro provides a convenient way to spawn OpenScript processes without
46/// waiting for completion.
47///
48/// # Examples
49///
50/// ```rust
51/// use openrunner_rs::{spawn_script, ScriptOptions};
52///
53/// # #[tokio::main]
54/// # async fn main() -> openrunner_rs::Result<()> {
55/// // Spawn with default options. This assumes `openscript` is in the PATH.
56/// // let child = spawn_script!("echo 'Background task'").await?;
57///
58/// // Spawn with custom options (shell for testing)
59/// let options = ScriptOptions::new().openscript_path("/bin/sh");
60/// let spawn_result = spawn_script!("echo 'Background task'", options).await?;
61///
62/// // Spawn with additional configuration
63/// let options = ScriptOptions::new()
64///     .openscript_path("/bin/sh")
65///     .env("DEBUG", "1");
66/// let spawn_result_2 = spawn_script!("echo 'Background task'", options).await?;
67///
68/// // Wait for completion
69/// let output = spawn_result.child.wait_with_output().await?;
70/// # Ok(())
71/// # }
72/// ```
73#[macro_export]
74macro_rules! spawn_script {
75    ($script:expr) => {
76        $crate::spawn($script, $crate::ScriptOptions::default())
77    };
78    ($script:expr, $options:expr) => {
79        $crate::spawn($script, $options)
80    };
81}
82
83/// Run an OpenScript file with optional configuration.
84///
85/// This macro provides a convenient way to run OpenScript files with either
86/// default options or custom configuration.
87///
88/// # Examples
89///
90/// ```rust
91/// use openrunner_rs::{run_file_script, ScriptOptions};
92/// use std::path::PathBuf;
93///
94/// # #[tokio::main]
95/// # async fn main() -> openrunner_rs::Result<()> {
96/// # use std::io::Write;
97/// # let mut file = tempfile::NamedTempFile::new().unwrap();
98/// # writeln!(file, "echo 'test'").unwrap();
99/// # let script_path = file.path().to_path_buf();
100///
101/// // Run with default options. This assumes `openscript` is in the PATH.
102/// // let result = run_file_script!(&script_path).await?;
103///
104/// // Run with custom options (shell for testing)
105/// let options = ScriptOptions::new().openscript_path("/bin/sh");
106/// let result = run_file_script!(&script_path, options).await?;
107///
108/// // Run with additional configuration
109/// let options = ScriptOptions::new()
110///     .openscript_path("/bin/sh")
111///     .env("MODE", "production");
112/// let result = run_file_script!(&script_path, options).await?;
113/// # Ok(())
114/// # }
115/// ```
116#[macro_export]
117macro_rules! run_file_script {
118    ($path:expr) => {
119        $crate::run_file($path, $crate::ScriptOptions::default())
120    };
121    ($path:expr, $options:expr) => {
122        $crate::run_file($path, $options)
123    };
124}