Skip to main content

Bash

Struct Bash 

Source
pub struct Bash { /* private fields */ }
Expand description

Main entry point for Bashkit.

Provides a virtual bash interpreter with an in-memory virtual filesystem.

Implementations§

Source§

impl Bash

Source

pub fn new() -> Self

Create a new Bash instance with default settings.

Source

pub fn builder() -> BashBuilder

Create a new BashBuilder for customized configuration.

Source

pub async fn exec(&mut self, script: &str) -> Result<ExecResult>

Execute a bash script and return the result.

This method first validates that the script does not exceed the maximum input size, then parses the script with a timeout, AST depth limit, and fuel limit, then executes the resulting AST.

Source

pub async fn exec_streaming( &mut self, script: &str, output_callback: OutputCallback, ) -> Result<ExecResult>

Execute a bash script with streaming output.

Like exec, but calls output_callback with incremental (stdout_chunk, stderr_chunk) pairs as output is produced. Callbacks fire after each loop iteration, command list element, and top-level command.

The full result is still returned in ExecResult for callers that need it.

§Example
use bashkit::Bash;
use std::sync::{Arc, Mutex};

let chunks: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
let chunks_cb = chunks.clone();
let mut bash = Bash::new();
let result = bash.exec_streaming(
    "for i in 1 2 3; do echo $i; done",
    Box::new(move |stdout, _stderr| {
        chunks_cb.lock().unwrap().push(stdout.to_string());
    }),
).await?;
assert_eq!(result.stdout, "1\n2\n3\n");
assert_eq!(*chunks.lock().unwrap(), vec!["1\n", "2\n", "3\n"]);
Source

pub fn fs(&self) -> Arc<dyn FileSystem>

Get a clone of the underlying filesystem.

Provides direct access to the virtual filesystem for:

  • Pre-populating files before script execution
  • Reading binary file outputs after execution
  • Injecting test data or configuration
§Example
use bashkit::Bash;
use std::path::Path;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let mut bash = Bash::new();
    let fs = bash.fs();

    // Pre-populate config file
    fs.mkdir(Path::new("/config"), false).await?;
    fs.write_file(Path::new("/config/app.txt"), b"debug=true\n").await?;

    // Bash script can read pre-populated files
    let result = bash.exec("cat /config/app.txt").await?;
    assert_eq!(result.stdout, "debug=true\n");

    // Bash creates output, read it directly
    bash.exec("echo 'done' > /output.txt").await?;
    let output = fs.read_file(Path::new("/output.txt")).await?;
    assert_eq!(output, b"done\n");
    Ok(())
}
Source

pub fn shell_state(&self) -> ShellState

Capture the current shell state (variables, env, cwd, options).

Returns a serializable snapshot of the interpreter state. Combine with InMemoryFs::snapshot() for full session persistence.

§Example
use bashkit::Bash;

let mut bash = Bash::new();
bash.exec("x=42").await?;

let state = bash.shell_state();

bash.exec("x=99").await?;
bash.restore_shell_state(&state);

let result = bash.exec("echo $x").await?;
assert_eq!(result.stdout, "42\n");
Source

pub fn restore_shell_state(&mut self, state: &ShellState)

Restore shell state from a previous snapshot.

Restores variables, env, cwd, arrays, aliases, traps, and options. Does not restore functions or builtins — those remain as-is.

Trait Implementations§

Source§

impl Default for Bash

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Bash

§

impl !RefUnwindSafe for Bash

§

impl Send for Bash

§

impl Sync for Bash

§

impl Unpin for Bash

§

impl UnsafeUnpin for Bash

§

impl !UnwindSafe for Bash

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.