themultiplexer 8 lat temu
rodzic
commit
14e33816fb
2 zmienionych plików z 31 dodań i 18 usunięć
  1. 26
    10
      hw6/task2/src/command.rs
  2. 5
    8
      hw6/task2/src/shell.rs

+ 26
- 10
hw6/task2/src/command.rs Wyświetl plik

@@ -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
 

+ 5
- 8
hw6/task2/src/shell.rs Wyświetl plik

@@ -27,10 +27,10 @@ impl <R:BufRead, W:Write> Shell<R, W> {
27 27
     }
28 28
 
29 29
     /// Waits for user inputs.
30
-    fn shell_loop(&mut self) -> Result<(),&str> {
30
+    fn shell_loop(&mut self) -> Result<(), &str> {
31 31
         while !self.should_exit {
32 32
             if let Ok(line) = self.prompt() {
33
-                Command::from_str(&line).and_then(|cmd| self.run(cmd));
33
+                let _ = Command::from_str(&line).and_then(|cmd| self.run(cmd));
34 34
             }
35 35
         }
36 36
         Ok(())
@@ -42,8 +42,8 @@ impl <R:BufRead, W:Write> Shell<R, W> {
42 42
 
43 43
         match env::current_dir() {
44 44
             Ok(pwd) => {
45
-                self.writer.write(format!("{} {} > ", self.name, pwd.display()).as_bytes());
46
-                self.writer.flush();
45
+                let _ = self.writer.write(format!("{} {} > ", self.name, pwd.display()).as_bytes());
46
+                let _ = self.writer.flush();
47 47
 
48 48
                 let mut line: String = String::new();
49 49
                 let read_result = self.reader.read_line(&mut line);
@@ -63,10 +63,7 @@ impl <R:BufRead, W:Write> Shell<R, W> {
63 63
     fn run(&mut self, command: Command) -> Result<(), CommandNotFoundError>{
64 64
 
65 65
         match command {
66
-            Command::Empty => {
67
-                self.writer.write("Command not found\n".as_bytes());
68
-                self.writer.flush();
69
-                return Err(CommandNotFoundError);},
66
+            Command::Empty => {},
70 67
             Command::Exit => self.should_exit = true,
71 68
             Command::Cd(_) => {
72 69
                 command.exec_cd();

Ładowanie…
Anuluj
Zapisz