Skip to main content

CommandRegistry

Struct CommandRegistry 

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

Registry of named commands.

Commands registered here are used by handle_pipeline and handle_parallel to dispatch each stage of a pipeline.

§Examples

use wasi_shell::{CommandRegistry, handle_pipeline, IoContext};
use std::io::{self, Write};

let mut reg = CommandRegistry::with_builtins();

// Add a custom command
reg.register("greet", |args, ctx| {
    let name = args.get(1).map(|s| s.as_str()).unwrap_or("world");
    writeln!(ctx.stdout, "Hello, {}!", name).map_err(|e| e.to_string())
});

handle_pipeline("greet Rust", Box::new(io::empty()), Box::new(io::stdout()), &reg, wasibox_core::CancellationToken::new()).unwrap();

Implementations§

Source§

impl CommandRegistry

Source

pub fn new() -> Self

Create an empty registry with a wasibox_core fallback.

Any command name not explicitly registered will be forwarded to wasibox_core::execute_with_context.

Source

pub fn with_builtins() -> Self

Create a registry pre-loaded with shell built-in commands (cd, help, exit, sl) and a wasibox_core fallback for all core utilities (echo, cat, grep, seq, head, …).

Source

pub fn command_names(&self) -> Vec<&str>

Returns the names of all explicitly registered commands (excluding the fallback).

Source

pub fn register<F>(&mut self, name: impl Into<String>, handler: F)
where F: Fn(&[String], &mut IoContext) -> Result<(), String> + Send + Sync + 'static,

Register (or replace) a command.

Source

pub fn set_fallback<F>(&mut self, handler: F)
where F: Fn(&[String], &mut IoContext) -> Result<(), String> + Send + Sync + 'static,

Set a fallback handler invoked when no explicit command matches.

Source

pub fn remove_fallback(&mut self)

Remove the fallback handler. Unknown commands will return an error.

Source

pub fn execute( &self, args: &[String], ctx: &mut IoContext, ) -> Result<(), String>

Look up and execute a command.

Trait Implementations§

Source§

impl Default for CommandRegistry

Source§

fn default() -> Self

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

Auto Trait Implementations§

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, 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.