From fea9c70861326fdc1c670e965b1c214741f27be7 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 21 Dec 2021 22:58:44 -0500 Subject: refactor a bit to start working on better parsing --- src/parse.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 7 deletions(-) (limited to 'src/parse.rs') diff --git a/src/parse.rs b/src/parse.rs index 84e8daa..42ead6e 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -1,8 +1,57 @@ -pub fn cmd(full_cmd: &str) -> (String, Vec) { - let mut parts = full_cmd.split(' '); - let cmd = parts.next().unwrap(); - ( - cmd.to_string(), - parts.map(std::string::ToString::to_string).collect(), - ) +pub struct Word { + word: String, + interpolate: bool, +} + +impl Word { + fn new(word: String) -> Self { + Self { + word, + interpolate: true, + } + } + + fn literal(word: String) -> Self { + Self { + word, + interpolate: false, + } + } +} + +pub struct Exe { + exe: Word, + args: Vec, +} + +impl Exe { + pub fn exe(&self) -> &str { + &self.exe.word + } + + pub fn args(&self) -> impl Iterator { + self.args.iter().map(|arg| arg.word.as_ref()) + } +} + +pub enum Command { + Exe(Exe), + And(Vec), + Or(Vec), + Both(Vec), + Pipe(Vec), +} + +impl Command { + pub fn parse(full_cmd: &str) -> Self { + let mut parts = full_cmd.split(' '); + let cmd = parts.next().unwrap(); + Self::Exe(Exe { + exe: Word::new(cmd.to_string()), + args: parts + .map(std::string::ToString::to_string) + .map(Word::new) + .collect(), + }) + } } -- cgit v1.2.3-54-g00ecf