aboutsummaryrefslogtreecommitdiffstats
path: root/src/builtins.rs
blob: f4c8c443c687bd6e6ac75b0dc8ef2a91faa2a898 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use snafu::{ResultExt, Snafu};

#[derive(Debug, Snafu)]
pub enum Error {
    #[snafu(display("unknown builtin {}", cmd))]
    UnknownBuiltin { cmd: String },

    #[snafu(display("failed to cd to {}: {}", dir, source))]
    Chdir { dir: String, source: nix::Error },

    #[snafu(display("cd requires a directory to be given"))]
    ChdirParams,
}

pub fn exec(cmd: &str, args: &[String]) -> Result<Builtin, Error> {
    Builtin::new(cmd, args)
}

pub struct Builtin {
    cmd: String,
    args: Vec<String>,
    done: bool,
}

impl Builtin {
    fn new(cmd: &str, args: &[String]) -> Result<Self, Error> {
        match cmd {
            "cd" => Ok(Builtin {
                cmd: cmd.to_string(),
                args: args.to_vec(),
                done: false,
            }),
            _ => Err(Error::UnknownBuiltin {
                cmd: cmd.to_string(),
            }),
        }
    }
}

#[must_use = "streams do nothing unless polled"]
impl futures::stream::Stream for Builtin {
    type Item = crate::eval::CommandEvent;
    type Error = Error;

    fn poll(&mut self) -> futures::Poll<Option<Self::Item>, Self::Error> {
        if self.done {
            return Ok(futures::Async::Ready(None));
        }

        self.done = true;
        let res = match self.cmd.as_ref() {
            "cd" => cd(&self.args),
            _ => Err(Error::UnknownBuiltin {
                cmd: self.cmd.clone(),
            }),
        };
        res.map(|_| {
            futures::Async::Ready(Some(
                crate::eval::CommandEvent::BuiltinExit,
            ))
        })
    }
}

fn cd(args: &[String]) -> Result<(), Error> {
    if let Some(dir) = args.get(0) {
        nix::unistd::chdir(dir.as_str()).context(Chdir { dir: dir.clone() })
    } else {
        Err(Error::ChdirParams)
    }
}