themultiplexer 8 роки тому
джерело
коміт
14e33816fb
2 змінених файлів з 31 додано та 18 видалено
  1. 26
    10
      hw6/task2/src/command.rs
  2. 5
    8
      hw6/task2/src/shell.rs

+ 26
- 10
hw6/task2/src/command.rs Переглянути файл

1
 use std::ffi::OsString;
1
 use std::ffi::OsString;
2
 use std::str::FromStr;
2
 use std::str::FromStr;
3
 use std::env;
3
 use std::env;
4
-use std::path::Path;
5
 
4
 
6
 pub enum Command {
5
 pub enum Command {
7
     Empty,
6
     Empty,
12
 pub struct CommandNotFoundError;
11
 pub struct CommandNotFoundError;
13
 
12
 
14
 impl FromStr for Command {
13
 impl FromStr for Command {
15
-
16
     type Err = CommandNotFoundError;
14
     type Err = CommandNotFoundError;
17
     fn from_str(s: &str) -> Result<Self, Self::Err> {
15
     fn from_str(s: &str) -> Result<Self, Self::Err> {
18
 
16
 
19
         let mut parts = s.split_whitespace();
17
         let mut parts = s.split_whitespace();
20
 
18
 
21
         match parts.next() {
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
 impl Command {
40
 impl Command {
41
+
32
     pub fn parse_cd(cmd: Option<&str>) -> Self {
42
     pub fn parse_cd(cmd: Option<&str>) -> Self {
33
         match cmd {
43
         match cmd {
34
             None => Command::Cd(None),
44
             None => Command::Cd(None),
38
 
48
 
39
     pub fn exec_cd(&self) {
49
     pub fn exec_cd(&self) {
40
         if let Command::Cd(None) = *self  {
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
         if let Command::Cd(Some(ref path)) = *self {
60
         if let Command::Cd(Some(ref path)) = *self {
47
             match path.to_str() {
61
             match path.to_str() {
48
-                Some(_) => {env::set_current_dir(path);},
62
+                Some(_) => {
63
+                    let _ = env::set_current_dir(path);
64
+                },
49
                 None => {},
65
                 None => {},
50
             }
66
             }
51
 
67
 

+ 5
- 8
hw6/task2/src/shell.rs Переглянути файл

27
     }
27
     }
28
 
28
 
29
     /// Waits for user inputs.
29
     /// Waits for user inputs.
30
-    fn shell_loop(&mut self) -> Result<(),&str> {
30
+    fn shell_loop(&mut self) -> Result<(), &str> {
31
         while !self.should_exit {
31
         while !self.should_exit {
32
             if let Ok(line) = self.prompt() {
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
         Ok(())
36
         Ok(())
42
 
42
 
43
         match env::current_dir() {
43
         match env::current_dir() {
44
             Ok(pwd) => {
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
                 let mut line: String = String::new();
48
                 let mut line: String = String::new();
49
                 let read_result = self.reader.read_line(&mut line);
49
                 let read_result = self.reader.read_line(&mut line);
63
     fn run(&mut self, command: Command) -> Result<(), CommandNotFoundError>{
63
     fn run(&mut self, command: Command) -> Result<(), CommandNotFoundError>{
64
 
64
 
65
         match command {
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
             Command::Exit => self.should_exit = true,
67
             Command::Exit => self.should_exit = true,
71
             Command::Cd(_) => {
68
             Command::Cd(_) => {
72
                 command.exec_cd();
69
                 command.exec_cd();

Завантаження…
Відмінити
Зберегти