|
|
@@ -1,7 +1,6 @@
|
|
1
|
1
|
use std::ffi::OsString;
|
|
2
|
2
|
use std::str::FromStr;
|
|
3
|
3
|
use std::env;
|
|
4
|
|
-use std::path::Path;
|
|
5
|
4
|
|
|
6
|
5
|
pub enum Command {
|
|
7
|
6
|
Empty,
|
|
|
@@ -12,23 +11,34 @@ pub enum Command {
|
|
12
|
11
|
pub struct CommandNotFoundError;
|
|
13
|
12
|
|
|
14
|
13
|
impl FromStr for Command {
|
|
15
|
|
-
|
|
16
|
14
|
type Err = CommandNotFoundError;
|
|
17
|
15
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
18
|
16
|
|
|
19
|
17
|
let mut parts = s.split_whitespace();
|
|
20
|
18
|
|
|
21
|
19
|
match parts.next() {
|
|
22
|
|
- Some("exit") => return Ok(Command::Exit),
|
|
23
|
|
- Some("cd") => return Ok(Command::parse_cd(parts.next())),
|
|
24
|
|
- Some(_) => return Ok(Command::Empty),
|
|
25
|
|
- None => return Err(CommandNotFoundError),
|
|
|
20
|
+ Some("exit") => {
|
|
|
21
|
+ Ok(Command::Exit)
|
|
|
22
|
+ },
|
|
|
23
|
+ Some("cd") => {
|
|
|
24
|
+ Ok(Command::parse_cd(parts.next()))
|
|
|
25
|
+ },
|
|
|
26
|
+ Some(cmd) => {
|
|
|
27
|
+ //For use in next homework, to execute programs.
|
|
|
28
|
+ let _ = cmd;
|
|
|
29
|
+ Err(CommandNotFoundError)
|
|
|
30
|
+ },
|
|
|
31
|
+ None => {
|
|
|
32
|
+ Ok(Command::Empty)
|
|
|
33
|
+ },
|
|
26
|
34
|
}
|
|
27
|
35
|
}
|
|
28
|
36
|
|
|
29
|
37
|
}
|
|
30
|
38
|
|
|
|
39
|
+
|
|
31
|
40
|
impl Command {
|
|
|
41
|
+
|
|
32
|
42
|
pub fn parse_cd(cmd: Option<&str>) -> Self {
|
|
33
|
43
|
match cmd {
|
|
34
|
44
|
None => Command::Cd(None),
|
|
|
@@ -38,14 +48,20 @@ impl Command {
|
|
38
|
48
|
|
|
39
|
49
|
pub fn exec_cd(&self) {
|
|
40
|
50
|
if let Command::Cd(None) = *self {
|
|
41
|
|
- let home = env::home_dir().unwrap();
|
|
42
|
|
- let home_path = home.as_path();
|
|
43
|
|
- env::set_current_dir(home_path);
|
|
|
51
|
+ let possible_home = env::home_dir();
|
|
|
52
|
+
|
|
|
53
|
+ if let Some(home) = possible_home {
|
|
|
54
|
+ let home_path = home.as_path();
|
|
|
55
|
+ let _ = env::set_current_dir(home_path);
|
|
|
56
|
+ }
|
|
|
57
|
+
|
|
44
|
58
|
}
|
|
45
|
59
|
|
|
46
|
60
|
if let Command::Cd(Some(ref path)) = *self {
|
|
47
|
61
|
match path.to_str() {
|
|
48
|
|
- Some(_) => {env::set_current_dir(path);},
|
|
|
62
|
+ Some(_) => {
|
|
|
63
|
+ let _ = env::set_current_dir(path);
|
|
|
64
|
+ },
|
|
49
|
65
|
None => {},
|
|
50
|
66
|
}
|
|
51
|
67
|
|