Skip to content

Commit

Permalink
Refactor pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
jpikl committed Jul 9, 2023
1 parent 7d69fd6 commit 647ae55
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 35 deletions.
20 changes: 8 additions & 12 deletions src/commands/x/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mod parser;
mod pattern;

use crate::commands::meta::CommandMeta;
use crate::commands::x::parser::Item;
use crate::commands::x::parser::Parser;
use crate::commands::x::pattern::Item;
use crate::commands::x::pattern::Pattern;
use crate::components::Buffering;
use crate::components::ConsumeToWrite;
use crate::components::ProduceFromRead;
Expand Down Expand Up @@ -74,15 +74,11 @@ impl ExecArgs {
}
}

#[derive(Debug, Clone)]
struct Pattern(Vec<Item>);

impl FromStr for Pattern {
type Err = parser::Error;
type Err = pattern::Error;

fn from_str(input: &str) -> std::result::Result<Self, Self::Err> {
let escape = ExecArgs::parse_escape();
Parser::new(input, escape).parse().map(Self)
Pattern::from_str_with_escape(input, ExecArgs::parse_escape())
}
}

Expand All @@ -104,11 +100,11 @@ impl Init<ExecArgs> for Exec {
fn init(args: ExecArgs, global_args: &GlobalArgs) -> Self {
let mut children: Vec<Child> = Vec::new();

for item in args.pattern.0 {
for item in args.pattern.items() {
if let Item::Expression(pipeline) = item {
for command in pipeline {
let child = std::process::Command::new(command.name)
.args(command.args)
let child = std::process::Command::new(&command.name)
.args(&command.args)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
Expand Down
59 changes: 36 additions & 23 deletions src/commands/x/parser.rs → src/commands/x/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,7 @@ const ESCAPED_LF: char = 'n';
const ESCAPED_CR: char = 'c';
const ESCAPED_TAB: char = 't';

#[derive(Debug, Clone)]
pub enum Item {
Constant(String),
Expression(Vec<Command>),
}

#[derive(Debug, Clone)]
pub struct Command {
pub name: String,
pub args: Vec<String>,
}
pub type Result<T> = std::result::Result<T, Error>;

#[derive(Error, Debug)]
pub struct Error {
Expand All @@ -37,17 +27,6 @@ pub struct Error {
position: usize,
}

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let padding = " ".repeat(self.position);
writeln!(f, "syntax error at position {}", self.position.yellow())?;
writeln!(f)?;
writeln!(f, "{}", self.input)?;
writeln!(f, "{}{}", padding, "^".red().bold())?;
writeln!(f, "{}{}", padding, self.kind.red().bold())
}
}

#[derive(Debug, Error)]
pub enum ErrorKind {
#[error("the previous {} was not closed", EXPR_START)]
Expand All @@ -64,7 +43,41 @@ pub enum ErrorKind {
MissingExpressionEnd,
}

pub type Result<T> = std::result::Result<T, Error>;
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let padding = " ".repeat(self.position);
writeln!(f, "syntax error at position {}", self.position.yellow())?;
writeln!(f)?;
writeln!(f, "{}", self.input)?;
writeln!(f, "{}{}", padding, "^".red().bold())?;
writeln!(f, "{}{}", padding, self.kind.red().bold())
}
}

#[derive(Debug, Clone)]
pub struct Pattern(Vec<Item>);

#[derive(Debug, Clone)]
pub enum Item {
Constant(String),
Expression(Vec<Command>),
}

#[derive(Debug, Clone)]
pub struct Command {
pub name: String,
pub args: Vec<String>,
}

impl Pattern {
pub fn from_str_with_escape(input: &str, escape: char) -> Result<Pattern> {
Parser::new(input, escape).parse().map(Self)
}

pub fn items(&self) -> &Vec<Item> {
&self.0
}
}

pub struct Parser<'a> {
input: String,
Expand Down

0 comments on commit 647ae55

Please sign in to comment.