Skip to main content

CommandContext

Struct CommandContext 

Source
pub struct CommandContext {
    pub base_command: String,
    pub words: Vec<Word>,
    pub env_vars: Vec<(String, String)>,
    pub redirection: Option<Redirection>,
    pub accumulated_env: HashMap<String, String>,
}
Expand description

Context for evaluating a single command segment.

Fields§

§base_command: String

The base command name (e.g. “git”, “ls”, “cargo”).

§words: Vec<Word>

All words in the command (pre-tokenized by tree-sitter or shlex).

§env_vars: Vec<(String, String)>

Leading KEY=VALUE environment variable assignments.

§redirection: Option<Redirection>

Detected output redirection, if any.

§accumulated_env: HashMap<String, String>

Environment variables accumulated from prior segments in a compound command (e.g. export FOO=bar ; git push makes FOO=bar available to the git push segment).

Implementations§

Source§

impl CommandContext

Source

pub fn from_command(raw: &str) -> Self

Build a CommandContext from a raw command string.

Used for simple (non-compound) command evaluation and in tests.

Source

pub fn from_segment(segment: &ShellSegment) -> Self

Build a CommandContext from a parsed ShellSegment.

Uses the segment’s pre-tokenized words field directly — tree-sitter already handles word boundaries correctly, including preserving substitution syntax ($(...), backticks) as single tokens.

Redirection is detected by parsing the segment’s command text (inline redirections like cat > file) or inherited from the segment’s redirection field (wrapping-construct redirections like for ... done > file).

Source

pub fn env_satisfies(&self, required: &HashMap<String, String>) -> bool

Check if all required env var entries are satisfied.

For each entry, checks the command’s inline env vars first (exact key+value match), then falls back to the process environment (std::env::var). Returns true only if ALL entries match. Some entries may come from inline env and others from the process environment — each is checked independently.

Config values are shell-expanded (~, $HOME, $VAR) before comparison, since shells expand these in env assignments before they reach the process.

Source

pub fn args(&self) -> &[Word]

Get words after skipping env vars and the base command.

Source

pub fn has_flag(&self, flag: &str) -> bool

Check if any word matches a flag.

Source

pub fn has_any_flag(&self, flags: &[&str]) -> bool

Check if any word matches any of the given flags.

Trait Implementations§

Source§

impl Debug for CommandContext

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.