Browse Source

Documented. Fixed lifetimes.

themultiplexer 8 years ago
parent
commit
2fbe0d38d5
3 changed files with 21 additions and 11 deletions
  1. 1
    0
      hw6/task2/src/command.rs
  2. 1
    2
      hw6/task2/src/main.rs
  3. 19
    9
      hw6/task2/src/shell.rs

+ 1
- 0
hw6/task2/src/command.rs View File

39
 
39
 
40
 impl Command {
40
 impl Command {
41
 
41
 
42
+    /// If the passed String exists, set the path of the Cd in the enum
42
     pub fn parse_cd(cmd: Option<&str>) -> Self {
43
     pub fn parse_cd(cmd: Option<&str>) -> Self {
43
         match cmd {
44
         match cmd {
44
             None => Command::Cd(None),
45
             None => Command::Cd(None),

+ 1
- 2
hw6/task2/src/main.rs View File

1
 use std::process;
1
 use std::process;
2
-use shell::Shell;
3
-use shell::{R, W};
2
+use shell::{Shell, R, W};
4
 use std::io;
3
 use std::io;
5
 
4
 
6
 mod command;
5
 mod command;

+ 19
- 9
hw6/task2/src/shell.rs View File

5
 use std::str::FromStr;
5
 use std::str::FromStr;
6
 use std::env;
6
 use std::env;
7
 
7
 
8
+///Tuple-Structs which hold Std-in and -out locks.
8
 pub struct R<'a>(pub StdinLock<'a>);
9
 pub struct R<'a>(pub StdinLock<'a>);
9
 pub struct W<'a>(pub StdoutLock<'a>);
10
 pub struct W<'a>(pub StdoutLock<'a>);
10
 
11
 
15
     pub name: String,
16
     pub name: String,
16
 }
17
 }
17
 
18
 
18
-impl <R:BufRead, W:Write> Shell<R, W> {
19
-    /// Creates a new Shell.
20
-    pub fn new(input: R, output: W, name: String) -> Self {
19
+impl <'a> Shell<R<'a>, W<'a>> {
20
+    /// Creates a new Shell with a name, input and output.
21
+    pub fn new(input: R<'a>, output: W<'a>, name: String) -> Self {
21
         Shell {
22
         Shell {
22
             reader : input,
23
             reader : input,
23
             writer : output,
24
             writer : output,
25
             name : name,
26
             name : name,
26
         }
27
         }
27
     }
28
     }
28
-    /// Initializes the Shell Loop
29
+    /// Initializes the Shell Loop.
30
+    /// Starts the shell.
29
     pub fn start(&mut self) -> Result<(), &str> {
31
     pub fn start(&mut self) -> Result<(), &str> {
30
         self.shell_loop()
32
         self.shell_loop()
31
     }
33
     }
32
 
34
 
33
-    /// Waits for user inputs.
35
+    /// Loops while exit hasn't been called.
36
+    /// When *prompt()* returns (the user hit enter) a Command is created through the FromStr-Trait.
37
+    /// If the Command-creation succeeds, run the command.
34
     fn shell_loop(&mut self) -> Result<(), &str> {
38
     fn shell_loop(&mut self) -> Result<(), &str> {
35
         while !self.should_exit {
39
         while !self.should_exit {
36
             if let Ok(line) = self.prompt() {
40
             if let Ok(line) = self.prompt() {
41
     }
45
     }
42
 
46
 
43
     /// Prints the shell prompt.
47
     /// Prints the shell prompt.
48
+    /// Waits for user input and returns the read line.
44
     fn prompt (&mut self) -> Result<String, &str> {
49
     fn prompt (&mut self) -> Result<String, &str> {
45
-
46
-
47
         match env::current_dir() {
50
         match env::current_dir() {
48
             Ok(pwd) => {
51
             Ok(pwd) => {
49
                 let _ = self.writer.write(format!("{} {} > ", self.name, pwd.display()).as_bytes());
52
                 let _ = self.writer.write(format!("{} {} > ", self.name, pwd.display()).as_bytes());
63
             Err(_) => return Err("Path Error"),
66
             Err(_) => return Err("Path Error"),
64
         }
67
         }
65
     }
68
     }
69
+
66
     /// Runs a command.
70
     /// Runs a command.
71
+    /// Currently only `cd` and `exit` are working.
67
     fn run(&mut self, command: Command) -> Result<(), CommandNotFoundError>{
72
     fn run(&mut self, command: Command) -> Result<(), CommandNotFoundError>{
68
 
73
 
69
         match command {
74
         match command {
78
 
83
 
79
 }
84
 }
80
 
85
 
86
+/// Implement Read-Trait for Tuple-Struct R
87
+/// Simply calls `read` of StdinLock
81
 impl <'a>Read for R<'a> {
88
 impl <'a>Read for R<'a> {
82
     fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
89
     fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
83
         self.0.read(buf)
90
         self.0.read(buf)
84
     }
91
     }
85
 }
92
 }
86
 
93
 
94
+/// Implement BufRead-Trait for Tuple-Struct R
95
+/// Simply calls `fill_buff` and `consume` from StdinLock
87
 impl <'a>BufRead for R<'a> {
96
 impl <'a>BufRead for R<'a> {
88
-
89
     fn fill_buf(&mut self) -> Result<&[u8], io::Error> {
97
     fn fill_buf(&mut self) -> Result<&[u8], io::Error> {
90
         self.0.fill_buf()
98
         self.0.fill_buf()
91
     }
99
     }
95
     }
103
     }
96
 }
104
 }
97
 
105
 
106
+/// Implement Write-Trait for Tuple-Struct W
107
+/// Simply calls `write` and `flush` from StdoutLock
98
 impl <'a>Write for W<'a> {
108
 impl <'a>Write for W<'a> {
99
     fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
109
     fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
100
         self.0.write(buf)
110
         self.0.write(buf)
101
     }
111
     }
102
-    
112
+
103
     fn flush(&mut self) -> Result<(),  io::Error> {
113
     fn flush(&mut self) -> Result<(),  io::Error> {
104
         self.0.flush()
114
         self.0.flush()
105
     }
115
     }

Loading…
Cancel
Save