summaryrefslogtreecommitdiffstats
path: root/src/parse.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-12-21 22:58:44 -0500
committerJesse Luehrs <doy@tozt.net>2021-12-21 22:58:44 -0500
commitfea9c70861326fdc1c670e965b1c214741f27be7 (patch)
tree359fb8b4eb29be72ba60c1c27714d8769435a534 /src/parse.rs
parent3fb13ac480f13c286dc23b340271a1a8706cdf75 (diff)
downloadnbsh-fea9c70861326fdc1c670e965b1c214741f27be7.tar.gz
nbsh-fea9c70861326fdc1c670e965b1c214741f27be7.zip
refactor a bit to start working on better parsing
Diffstat (limited to 'src/parse.rs')
-rw-r--r--src/parse.rs63
1 files changed, 56 insertions, 7 deletions
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<String>) {
- 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<Word>,
+}
+
+impl Exe {
+ pub fn exe(&self) -> &str {
+ &self.exe.word
+ }
+
+ pub fn args(&self) -> impl Iterator<Item = &str> {
+ self.args.iter().map(|arg| arg.word.as_ref())
+ }
+}
+
+pub enum Command {
+ Exe(Exe),
+ And(Vec<Command>),
+ Or(Vec<Command>),
+ Both(Vec<Command>),
+ Pipe(Vec<Command>),
+}
+
+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(),
+ })
+ }
}