#[derive(Debug, Clone, PartialEq)]
pub enum Redirect {
StdoutOverwrite(String),
StdoutAppend(String),
StdinFrom(String),
}
#[derive(Debug, Clone, PartialEq)]
pub struct SimpleCommand {
pub cmd: String,
pub args: Vec<String>,
pub redirects: Vec<Redirect>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Pipeline {
pub commands: Vec<SimpleCommand>,
}
impl Pipeline {
pub fn extract_ai_filter(&self) -> Option<(String, Pipeline)> {
let last = self.commands.last()?;
if last.cmd != "ai" {
return None;
}
let prompt = last.args.join(" ");
if prompt.is_empty() {
return None;
}
let remaining = Pipeline {
commands: self.commands[..self.commands.len() - 1].to_vec(),
};
if remaining.commands.is_empty() {
return None;
}
Some((prompt, remaining))
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Connector {
And,
Or,
Semi,
}
#[derive(Debug, Clone, PartialEq)]
pub struct CommandList {
pub first: Pipeline,
pub rest: Vec<(Connector, Pipeline)>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ParseError(pub String);
impl std::fmt::Display for ParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}