kernel_web/
lib.rs

1
2/// the purpose of this crate is to invoke kernel routines 
3pub mod kernel{
4
5    /// depend crates {colorful, process};
6    use colorful::{Color, Colorful};
7    use std::process::{Command, Child, Stdio};
8
9    /// Process struct acessible other crates within kernel module. This struct have two fields (command , args). 
10    #[derive(Debug, PartialEq)]
11    pub struct Process{
12
13        pub command : String,
14        pub args : String,
15    }
16
17
18    /// Kernellevel enumerate kernel level. i.e read, write etc.
19    #[derive(Debug, PartialEq)]
20    pub enum Kernellevel{
21
22        Write,
23        Read,
24        None,
25        Root,
26    }
27
28
29    /// new function takes two parameters as string and return struct. The purpose is to interact structs fields directly.
30    pub fn new(command : String , args : String) -> Process{
31
32        Process{
33            command,
34            args,
35        }
36    }
37
38    /// process have following methods are attached. These methods will either allocate permission or intiate the process.
39
40    impl Process{
41
42
43        /// kernel permission allocate permission to complete the task. These permission allow to complete process within timeframe.
44        pub fn kernel_permission(&self) -> Kernellevel {
45
46
47            let mut klevel : Kernellevel = Kernellevel::None;
48            if self.command.contains("ls "){
49
50                klevel = Kernellevel::Read
51            }
52
53            if self.command.contains("cat "){
54
55               klevel = Kernellevel::Read;
56            }
57
58            if self.command.contains("cd ") {
59
60                klevel = Kernellevel::None;
61            }
62
63            if self.command.contains("mkdir ") {
64
65                klevel = Kernellevel::None;
66            }
67
68            if self.command.contains("cp ") {
69
70                klevel = Kernellevel::None;
71            }
72
73            if self.command.contains("touch ") {
74
75                klevel = Kernellevel::Write;
76            }
77
78            if self.command.contains("rm ") {
79
80                klevel = Kernellevel::Write;
81            }
82
83            if self.command.contains("mv "){
84
85                klevel = Kernellevel::None;
86            }
87
88            if self.command.contains("sudo "){
89
90                klevel = Kernellevel::Root; 
91            }
92
93            if self.command.contains("top ") {
94
95                klevel = Kernellevel::None;
96            }
97
98            if self.command.contains("q") || self.command.contains("--q "){
99
100                klevel = Kernellevel::None;
101            }
102
103            if self.command.contains("? ") || self.command.contains("help "){
104
105                klevel = Kernellevel::None;
106            }
107
108            if self.command.contains("-v") || self.command.contains("--v "){
109
110                klevel = Kernellevel::None;
111            }
112
113            if self.command.contains("whatis") {
114
115                klevel = Kernellevel::None;
116            }
117
118            if self.command.contains("man "){
119
120                klevel = Kernellevel::None;
121            }
122
123            if self.command.contains("exit"){
124
125                klevel = Kernellevel::None;
126            }
127
128            klevel
129        }
130
131        /// purpose of listing is to view memory space. This method takes two parameters (self reference , level) & return  Result<Child>.
132        /// there may be possible level is not checked, then it will throw unexpected state or panic suitation.
133        /// there may be even possible that args are not included in commands.    
134    
135        pub fn listing(&self, level : Kernellevel) -> std::io::Result<Child> {
136        
137
138            if level == Kernellevel::None {
139                
140                println!("{}", " File Dispatch is not found".color(Color::Red).bold());
141            
142                panic!("file dispatch is not found");
143            
144            }
145            
146            if !(self.args.contains("-a")){
147
148                let child = Command::new(self.command.clone()).spawn().expect("process created");
149
150                println!("command = {:?}", child);
151                return Ok(child);
152
153            }else{
154
155                let child = Command::new(self.command.clone()).arg(self.args.clone()).spawn().expect("process created");
156                return Ok(child);
157            }
158
159        }
160
161        /// the purpose of "cat" is to view the records or content in a memory space. e.g cat file.txt.
162        /// cat takes two parameters similar to listing and return result.
163        /// there may be possible that level is not checked.    
164        pub fn cat_operation(&self, level : Kernellevel) -> std::io::Result<Child>{
165
166            if level == Kernellevel::None{
167
168                println!("{}", "File Dispatch is not found".color(Color::Red).bold());
169                panic!("file dispatch is not found");
170
171            }
172
173            let child = Command::new(self.command.clone()).arg(self.args.clone()).spawn().expect("cat command!");
174
175            Ok(child)
176
177        }
178
179        /// change directory is quite similar except the query will handle by stdout.  
180
181        pub fn change_direc(&self) -> std::io::Result<Child>{
182
183            let child = Command::new(self.command.clone()).arg(self.args.clone()).stdout(Stdio::piped()).spawn().expect("change your directory");
184
185            Ok(child)
186        }
187    }
188}
189
190
191#[cfg(test)]
192mod tests {
193    use crate::kernel;
194
195    #[test]
196    fn instance() {
197
198        let object : _ = kernel::new("ls ".to_string(), "-a ".to_string());
199
200        assert_ne!(object, kernel::Process{command : "".to_string(), args : "".to_string()});
201    }
202
203    #[test]
204    fn kernel_operation() {
205
206        let object : _ = kernel::new("ls ".to_string(), "-a ".to_string());
207
208        let pem : _ = object.kernel_permission();
209
210        assert_ne!(pem, kernel::Kernellevel::None);
211    }
212
213    #[test]
214    fn kernel_listing_command() {
215
216        let object : _ = kernel::new("ls ".to_string(), "".to_string());
217
218        let pem : _ = object.kernel_permission();
219
220        let list : _ = object.listing(pem);
221
222        let output : _ = list.expect("process created").wait_with_output().expect("child process running");
223
224        assert_eq!(output.stdout, Vec::<u8>::new());
225    }
226
227    #[test]
228    fn kernel_cat_operation() {
229
230        let object : _ = kernel::new("cat ".to_string(), "file.txt".to_string());
231
232        let pem : _ = object.kernel_permission();
233
234        let cat_op : _ = object.cat_operation(pem);
235
236        let allow :_ = cat_op.expect("child process created!").id();
237
238        assert_ne!(allow, 0);
239    }
240
241    #[test]
242    fn kernel_change_direc(){
243
244        let object : _ = kernel::new("cd ".to_string(), "lib/".to_string());
245
246        // let pem : _ = object.kernel_permission();
247
248        let cd_op : _ = object.change_direc();
249
250        let allow : _ = cd_op.expect("process created ...").wait_with_output().expect("child process running");
251
252        assert!(allow.status.success());
253
254    }
255}