Przeglądaj źródła

Made task2 more complex :|

themultiplexer 8 lat temu
rodzic
commit
47b62ba722
3 zmienionych plików z 68 dodań i 17 usunięć
  1. 10
    1
      hw6/task2/src/command.rs
  2. 6
    1
      hw6/task2/src/main.rs
  3. 52
    15
      hw6/task2/src/shell.rs

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

@@ -1,3 +1,6 @@
1
+use std::ffi::OsString;
2
+use std::str::FromStr;
3
+
1 4
 enum Command {
2 5
     Empty,
3 6
     Exit,
@@ -6,6 +9,12 @@ enum Command {
6 9
 
7 10
 impl Command {}
8 11
 
12
+struct CommandNotFoundError();
13
+
9 14
 impl FromStr for Command {
10
-    fn from_str(s: &str) -> Result<Self, Self::Err> {}
15
+
16
+    type Err = CommandNotFoundError;
17
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
18
+
19
+    }
11 20
 }

+ 6
- 1
hw6/task2/src/main.rs Wyświetl plik

@@ -1,8 +1,13 @@
1 1
 use std::process;
2
+use shell::Shell;
3
+
2 4
 mod shell;
5
+mod command;
3 6
 
4 7
 fn main() {
5
-    let mut s = Shell::new(..);
8
+    let reader = shell::R;
9
+    let writer = shell::W;
10
+    let mut s = Shell::new(reader, writer, "schell".to_string());
6 11
     match s.start() {
7 12
         Ok(_) => process::exit(0),
8 13
         Err(_) => process::exit(1),

+ 52
- 15
hw6/task2/src/shell.rs Wyświetl plik

@@ -1,8 +1,13 @@
1 1
 use std::io;
2 2
 use std::io::prelude::*;
3
-use command;
3
+use std::io::BufRead;
4
+use std::io::Read;
4 5
 
5
-struct Shell<R, W> {
6
+
7
+pub struct R;
8
+pub struct W;
9
+
10
+pub struct Shell<R, W> {
6 11
     pub reader: R,
7 12
     pub writer: W,
8 13
     pub should_exit: bool,
@@ -13,40 +18,72 @@ impl Shell<R, W> {
13 18
     /// Creates a new Shell.
14 19
     pub fn new(input: R, output: W, name: String) -> Self {
15 20
         Shell {
16
-            reader = input,
17
-            writer = output,
18
-            should_exit = false,
19
-            name = name,
21
+            reader : input,
22
+            writer : output,
23
+            should_exit : false,
24
+            name : name,
20 25
         }
21 26
     }
22 27
     /// Initializes the Shell Loop
23
-    pub fn start() -> Result<Self, &str> {
24
-        shell_loop();
28
+    pub fn start<'a>(&self) -> Result<(), &'a str> {
29
+        self.shell_loop();
30
+        Ok(())
25 31
     }
32
+
26 33
     /// Waits for user inputs.
27
-    fn shell_loop() -> Result {
34
+    fn shell_loop(&self) -> Result<(),()> {
28 35
         loop {
29 36
         }
30 37
     }
38
+
39
+    /*
31 40
     /// Prints the shell prompt.
32 41
     fn prompt() -> Result<Option, &str> {
33 42
     }
34 43
     /// Runs a command.
35 44
     fn run(&str) {
36 45
     }
46
+
47
+    */
37 48
 }
38 49
 
39
-impl BufRead for Shell<R, W> {
40
-    fn fill_buf(&mut self) -> Result<&[u8]> {
41
-        stdin.fill_buf()
50
+
51
+impl Read for R {
52
+    fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
53
+        let stdin = io::stdin();
54
+        let mut stdin = stdin.lock();
55
+
56
+
57
+        let buffer = stdin.read(buf);
58
+
59
+        match buffer {
60
+            Ok(x) => {
61
+                println!("{} ||| {:?}", x, buffer);
62
+                stdin.consume(x);
63
+                Ok(x)
64
+            },
65
+            Err(_) => Err(io::Error::new(io::ErrorKind::InvalidInput, "jhrthhzruzkikz")),
66
+        }
67
+    }
68
+}
69
+
70
+impl BufRead for R {
71
+
72
+    fn fill_buf(&mut self) -> Result<&[u8], io::Error> {
73
+        Ok(&[11])
42 74
     }
75
+
43 76
     fn consume(&mut self, amt: usize) {
77
+        let mut buffer = self.fill_buf;
78
+        if buffer.len <= amt {
79
+
80
+        }
44 81
     }
45 82
 }
46 83
 
47
-impl Write for Shell<R, W> {
48
-    fn write(&mut self, buf: &[u8]) -> Result<usize> {
84
+impl Write for W {
85
+    fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
49 86
     }
50
-    fn flush(&mut self) -> Result<()> {
87
+    fn flush(&mut self) -> Result<(),  io::Error> {
51 88
     }
52 89
 }

Ładowanie…
Anuluj
Zapisz