Skip to main content

Builtin

Trait Builtin 

Source
pub trait Builtin: Send + Sync {
    // Required method
    fn execute<'life0, 'life1, 'async_trait>(
        &'life0 self,
        ctx: Context<'life1>,
    ) -> Pin<Box<dyn Future<Output = Result<ExecResult>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
}
Expand description

Trait for implementing builtin commands.

All custom builtins must implement this trait. The trait requires Send + Sync for thread safety in async contexts.

§Example

use bashkit::{Bash, Builtin, BuiltinContext, ExecResult, async_trait};

struct Greet {
    default_name: String,
}

#[async_trait]
impl Builtin for Greet {
    async fn execute(&self, ctx: BuiltinContext<'_>) -> bashkit::Result<ExecResult> {
        let name = ctx.args.first()
            .map(|s| s.as_str())
            .unwrap_or(&self.default_name);
        Ok(ExecResult::ok(format!("Hello, {}!\n", name)))
    }
}

// Register the builtin
let bash = Bash::builder()
    .builtin("greet", Box::new(Greet { default_name: "World".into() }))
    .build();

§Return Values

Return ExecResult::ok for success with output, or ExecResult::err for errors with exit code.

Required Methods§

Source

fn execute<'life0, 'life1, 'async_trait>( &'life0 self, ctx: Context<'life1>, ) -> Pin<Box<dyn Future<Output = Result<ExecResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Execute the builtin command.

§Arguments
  • ctx - The execution context containing arguments, environment, and filesystem
§Returns
  • Ok(ExecResult) - Execution result with stdout, stderr, and exit code
  • Err(Error) - Fatal error that should abort execution

Implementors§