summaryrefslogtreecommitdiffstats
path: root/src/parse.rs
blob: 42ead6e87e6853db33cce0a51814e4fe29fca525 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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(),
        })
    }
}