Browse Source

Ran rustfmt on various files

Lorenz Bung 7 years ago
parent
commit
c88a64567d
3 changed files with 62 additions and 54 deletions
  1. 4
    1
      hw7/task1/src/hash256.rs
  2. 25
    17
      hw7/task1/src/main.rs
  3. 33
    36
      hw7/task2/src/lib.rs

+ 4
- 1
hw7/task1/src/hash256.rs View File

24
     let hash = Sha256::hash(bytes).hex();
24
     let hash = Sha256::hash(bytes).hex();
25
 
25
 
26
     if hash.ends_with(difficulty) {
26
     if hash.ends_with(difficulty) {
27
-        return Some(Solution{ number: number, hash: hash,});
27
+        return Some(Solution {
28
+            number: number,
29
+            hash: hash,
30
+        });
28
     }
31
     }
29
 
32
 
30
     None
33
     None

+ 25
- 17
hw7/task1/src/main.rs View File

14
     let diff = matches.value_of("difficulty").unwrap_or("1");
14
     let diff = matches.value_of("difficulty").unwrap_or("1");
15
     let mut time_measurement = false;
15
     let mut time_measurement = false;
16
 
16
 
17
-    if diff.chars().any( |c| !c.is_digit(16)) {
17
+    if diff.chars().any(|c| !c.is_digit(16)) {
18
         println!("Difficulty is not hexadecimal.");
18
         println!("Difficulty is not hexadecimal.");
19
         process::exit(1)
19
         process::exit(1)
20
     }
20
     }
37
                 if let Some(x) = hash256::verify_product(b, n, &diff.to_string()) {
37
                 if let Some(x) = hash256::verify_product(b, n, &diff.to_string()) {
38
                     let end = get_time();
38
                     let end = get_time();
39
                     println!("Number: {} --> hash: {}", x.number, x.hash);
39
                     println!("Number: {} --> hash: {}", x.number, x.hash);
40
-                    if time_measurement{
40
+                    if time_measurement {
41
                         let diff = end - start;
41
                         let diff = end - start;
42
                         let s = diff.num_seconds();
42
                         let s = diff.num_seconds();
43
                         let ms = diff.num_milliseconds();
43
                         let ms = diff.num_milliseconds();
60
     App::new("Hash256")
60
     App::new("Hash256")
61
         .version("1.0")
61
         .version("1.0")
62
         .author("Lorenz Bung & Joshua Rutschmann")
62
         .author("Lorenz Bung & Joshua Rutschmann")
63
-        .about("Calculates the Hashvalue of the given base, number and difficulty.")
64
-        .arg(Arg::with_name("base")
65
-            .value_name("base")
66
-            .help("The base of the hash to be calculated on.")
67
-            .takes_value(true)
68
-            .required(true))
69
-        .arg(Arg::with_name("difficulty")
70
-            .value_name("difficulty")
71
-            .help("The difficulty of the calculated hash.")
72
-            .takes_value(true)
73
-            .required(true))
74
-        .subcommand(SubCommand::with_name("timings")
75
-            .about("controls timing features")
76
-            .version("1.0")
77
-            .author("Lorenz Bung & Joshua Rutschmann"))
63
+        .about(
64
+            "Calculates the Hashvalue of the given base, number and difficulty.",
65
+        )
66
+        .arg(
67
+            Arg::with_name("base")
68
+                .value_name("base")
69
+                .help("The base of the hash to be calculated on.")
70
+                .takes_value(true)
71
+                .required(true),
72
+        )
73
+        .arg(
74
+            Arg::with_name("difficulty")
75
+                .value_name("difficulty")
76
+                .help("The difficulty of the calculated hash.")
77
+                .takes_value(true)
78
+                .required(true),
79
+        )
80
+        .subcommand(
81
+            SubCommand::with_name("timings")
82
+                .about("controls timing features")
83
+                .version("1.0")
84
+                .author("Lorenz Bung & Joshua Rutschmann"),
85
+        )
78
 }
86
 }

+ 33
- 36
hw7/task2/src/lib.rs View File

29
         match parts.next() {
29
         match parts.next() {
30
             Some("exit") => Ok(Command::Exit),
30
             Some("exit") => Ok(Command::Exit),
31
             Some("cd") => Ok(Command::parse_cd(parts.next())),
31
             Some("cd") => Ok(Command::parse_cd(parts.next())),
32
-            Some(_) => {
33
-                Ok(Command::Program(s.to_string()))
34
-            }
32
+            Some(_) => Ok(Command::Program(s.to_string())),
35
             None => Ok(Command::Empty),
33
             None => Ok(Command::Empty),
36
         }
34
         }
37
     }
35
     }
132
     fn run(&mut self, command: Command) -> Result<(), ErrorType> {
130
     fn run(&mut self, command: Command) -> Result<(), ErrorType> {
133
 
131
 
134
         match command {
132
         match command {
135
-            Command::Empty => {},
133
+            Command::Empty => {}
136
             Command::Program(cmd) => {
134
             Command::Program(cmd) => {
137
 
135
 
138
                 let commands = cmd.split('|');
136
                 let commands = cmd.split('|');
141
                 let needs_pipe = commands_vec.len() == 2;
139
                 let needs_pipe = commands_vec.len() == 2;
142
 
140
 
143
 
141
 
144
-                    let fi = fork();
145
-                    match fi {
146
-                        Ok(Parent { child: _child }) => {
147
-                            let _ = wait();
142
+                let fi = fork();
143
+                match fi {
144
+                    Ok(Parent { child: _child }) => {
145
+                        let _ = wait();
148
 
146
 
149
-                        }
150
-                        Ok(Child) => {
151
-                            if let Ok((reader, writer)) = pipe() {
152
-                                let sec = fork();
153
-                                match sec {
154
-                                    Ok(Parent { child: _child }) => {
155
-                                        let _ = wait();
156
-                                        if let Some(second) = commands_vec.get(1) {
157
-                                            match close(writer) {
158
-                                                Ok(_) => {},
159
-                                                Err(_) => return Err(ErrorType::BrokenPipeError),
160
-                                            }
161
-                                            dup2(reader, 0).unwrap();
162
-                                            self.execute(second);
147
+                    }
148
+                    Ok(Child) => {
149
+                        if let Ok((reader, writer)) = pipe() {
150
+                            let sec = fork();
151
+                            match sec {
152
+                                Ok(Parent { child: _child }) => {
153
+                                    let _ = wait();
154
+                                    if let Some(second) = commands_vec.get(1) {
155
+                                        match close(writer) {
156
+                                            Ok(_) => {}
157
+                                            Err(_) => return Err(ErrorType::BrokenPipeError),
163
                                         }
158
                                         }
159
+                                        dup2(reader, 0).unwrap();
160
+                                        self.execute(second);
164
                                     }
161
                                     }
165
-                                    Ok(Child) => {
166
-                                        if let Some(first) = commands_vec.get(0) {
167
-                                            if needs_pipe {
168
-                                                close(reader).unwrap();
169
-                                                dup2(writer, 1).unwrap();
170
-                                            }
171
-                                            self.execute(first);
162
+                                }
163
+                                Ok(Child) => {
164
+                                    if let Some(first) = commands_vec.get(0) {
165
+                                        if needs_pipe {
166
+                                            close(reader).unwrap();
167
+                                            dup2(writer, 1).unwrap();
172
                                         }
168
                                         }
169
+                                        self.execute(first);
173
                                     }
170
                                     }
174
-                                    Err(_) => return Err(ErrorType::ForkError),
175
                                 }
171
                                 }
172
+                                Err(_) => return Err(ErrorType::ForkError),
176
                             }
173
                             }
177
                         }
174
                         }
178
-                        Err(_) => return Err(ErrorType::ForkError),
179
                     }
175
                     }
176
+                    Err(_) => return Err(ErrorType::ForkError),
177
+                }
180
 
178
 
181
-            },
179
+            }
182
             Command::Exit => self.should_exit = true,
180
             Command::Exit => self.should_exit = true,
183
             Command::Cd(_) => {
181
             Command::Cd(_) => {
184
                 command.exec_cd();
182
                 command.exec_cd();
187
         Ok(())
185
         Ok(())
188
     }
186
     }
189
 
187
 
190
-    fn execute(&self, cmd: &str){
191
-        let parts:Vec<&str> = cmd.split_whitespace().collect();
192
-        let args:Vec<CString> = parts.iter().map(|f| CString::new(*f).unwrap()).collect();
188
+    fn execute(&self, cmd: &str) {
189
+        let parts: Vec<&str> = cmd.split_whitespace().collect();
190
+        let args: Vec<CString> = parts.iter().map(|f| CString::new(*f).unwrap()).collect();
193
         let t = args.into_boxed_slice();
191
         let t = args.into_boxed_slice();
194
 
192
 
195
         execvp(&t[0], &t).unwrap();
193
         execvp(&t[0], &t).unwrap();
196
     }
194
     }
197
 }
195
 }
198
-

Loading…
Cancel
Save