use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Arg {
Text(String),
Glob(String),
}
impl Arg {
pub fn as_str(&self) -> &str {
match self {
Arg::Text(v) | Arg::Glob(v) => v,
}
}
pub fn as_text(&self) -> &str {
match self {
Arg::Text(v) | Arg::Glob(v) => v,
}
}
pub fn into_text(self) -> String {
match self {
Arg::Text(v) | Arg::Glob(v) => v,
}
}
pub fn parse<T: std::str::FromStr>(&self) -> Result<T, T::Err> {
self.as_str().parse()
}
pub fn as_owned_text(&self) -> String {
self.as_str().to_string()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Redirect {
pub source_fd: usize,
pub op: String,
pub target: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Command {
pub args: Vec<String>,
pub redirects: Vec<Redirect>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Pipeline {
pub commands: Vec<Command>,
pub negate: bool,
pub pipe_fail: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScriptNode {
Pipeline(Pipeline),
Seq(Box<ScriptNode>, SeqOp, Box<ScriptNode>),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SeqOp {
Semicolon,
Ampersand,
Or,
And,
}
impl fmt::Display for SeqOp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let repr = match self {
SeqOp::Semicolon => ";",
SeqOp::Ampersand => "&",
SeqOp::Or => "||",
SeqOp::And => "&&",
};
write!(f, "{repr}")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn redirect_repr() {
let r = Redirect {
source_fd: 1,
op: ">".into(),
target: "x".into(),
};
assert_eq!(r.source_fd, 1);
assert_eq!(r.target, "x");
}
}