ソースを参照

Documented. Fixed lifetimes.

themultiplexer 8年前
コミット
2fbe0d38d5
3個のファイルの変更21行の追加11行の削除
  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 ファイルの表示

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

+ 1
- 2
hw6/task2/src/main.rs ファイルの表示

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

+ 19
- 9
hw6/task2/src/shell.rs ファイルの表示

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

読み込み中…
キャンセル
保存