openrunner-rs 1.0.1

A Rust library for running OpenScript
Documentation
//! Convenience macros for OpenRunner script execution.
//!
//! This module provides ergonomic macros that simplify common script execution patterns.

/// Run OpenScript code with optional configuration.
///
/// This macro provides a convenient way to run OpenScript code with either default
/// options or custom configuration.
///
/// # Examples
///
/// ```rust
/// use openrunner_rs::{run_script, ScriptOptions};
/// use std::time::Duration;
///
/// # #[tokio::main]
/// # async fn main() -> openrunner_rs::Result<()> {
/// // Run with default options. This assumes `openscript` is in the PATH.
/// // let result = run_script!("echo 'Hello from default openscript!'").await?;
///
/// // Run with custom options (shell for testing)
/// let options = ScriptOptions::new().openscript_path("/bin/sh");
/// let result = run_script!("echo 'Hello, World!'", options).await?;
///
/// // Run with additional configuration
/// let options = ScriptOptions::new()
///     .openscript_path("/bin/sh")
///     .timeout(Duration::from_secs(30));
/// let result = run_script!("echo 'Hello, World!'", options).await?;
/// # Ok(())
/// # }
/// ```
#[macro_export]
macro_rules! run_script {
    ($script:expr) => {
        $crate::run($script, $crate::ScriptOptions::default())
    };
    ($script:expr, $options:expr) => {
        $crate::run($script, $options)
    };
}

/// Spawn an OpenScript process with optional configuration.
///
/// This macro provides a convenient way to spawn OpenScript processes without
/// waiting for completion.
///
/// # Examples
///
/// ```rust
/// use openrunner_rs::{spawn_script, ScriptOptions};
///
/// # #[tokio::main]
/// # async fn main() -> openrunner_rs::Result<()> {
/// // Spawn with default options. This assumes `openscript` is in the PATH.
/// // let child = spawn_script!("echo 'Background task'").await?;
///
/// // Spawn with custom options (shell for testing)
/// let options = ScriptOptions::new().openscript_path("/bin/sh");
/// let spawn_result = spawn_script!("echo 'Background task'", options).await?;
///
/// // Spawn with additional configuration
/// let options = ScriptOptions::new()
///     .openscript_path("/bin/sh")
///     .env("DEBUG", "1");
/// let spawn_result_2 = spawn_script!("echo 'Background task'", options).await?;
///
/// // Wait for completion
/// let output = spawn_result.child.wait_with_output().await?;
/// # Ok(())
/// # }
/// ```
#[macro_export]
macro_rules! spawn_script {
    ($script:expr) => {
        $crate::spawn($script, $crate::ScriptOptions::default())
    };
    ($script:expr, $options:expr) => {
        $crate::spawn($script, $options)
    };
}

/// Run an OpenScript file with optional configuration.
///
/// This macro provides a convenient way to run OpenScript files with either
/// default options or custom configuration.
///
/// # Examples
///
/// ```rust
/// use openrunner_rs::{run_file_script, ScriptOptions};
/// use std::path::PathBuf;
///
/// # #[tokio::main]
/// # async fn main() -> openrunner_rs::Result<()> {
/// # use std::io::Write;
/// # let mut file = tempfile::NamedTempFile::new().unwrap();
/// # writeln!(file, "echo 'test'").unwrap();
/// # let script_path = file.path().to_path_buf();
///
/// // Run with default options. This assumes `openscript` is in the PATH.
/// // let result = run_file_script!(&script_path).await?;
///
/// // Run with custom options (shell for testing)
/// let options = ScriptOptions::new().openscript_path("/bin/sh");
/// let result = run_file_script!(&script_path, options).await?;
///
/// // Run with additional configuration
/// let options = ScriptOptions::new()
///     .openscript_path("/bin/sh")
///     .env("MODE", "production");
/// let result = run_file_script!(&script_path, options).await?;
/// # Ok(())
/// # }
/// ```
#[macro_export]
macro_rules! run_file_script {
    ($path:expr) => {
        $crate::run_file($path, $crate::ScriptOptions::default())
    };
    ($path:expr, $options:expr) => {
        $crate::run_file($path, $options)
    };
}