1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255

/// the purpose of this crate is to invoke kernel routines 
pub mod kernel{

    /// depend crates {colorful, process};
    use colorful::{Color, Colorful};
    use std::process::{Command, Child, Stdio};

    /// Process struct acessible other crates within kernel module. This struct have two fields (command , args). 
    #[derive(Debug, PartialEq)]
    pub struct Process{

        pub command : String,
        pub args : String,
    }


    /// Kernellevel enumerate kernel level. i.e read, write etc.
    #[derive(Debug, PartialEq)]
    pub enum Kernellevel{

        Write,
        Read,
        None,
        Root,
    }


    /// new function takes two parameters as string and return struct. The purpose is to interact structs fields directly.
    pub fn new(command : String , args : String) -> Process{

        Process{
            command,
            args,
        }
    }

    /// process have following methods are attached. These methods will either allocate permission or intiate the process.

    impl Process{


        /// kernel permission allocate permission to complete the task. These permission allow to complete process within timeframe.
        pub fn kernel_permission(&self) -> Kernellevel {


            let mut klevel : Kernellevel = Kernellevel::None;
            if self.command.contains("ls "){

                klevel = Kernellevel::Read
            }

            if self.command.contains("cat "){

               klevel = Kernellevel::Read;
            }

            if self.command.contains("cd ") {

                klevel = Kernellevel::None;
            }

            if self.command.contains("mkdir ") {

                klevel = Kernellevel::None;
            }

            if self.command.contains("cp ") {

                klevel = Kernellevel::None;
            }

            if self.command.contains("touch ") {

                klevel = Kernellevel::Write;
            }

            if self.command.contains("rm ") {

                klevel = Kernellevel::Write;
            }

            if self.command.contains("mv "){

                klevel = Kernellevel::None;
            }

            if self.command.contains("sudo "){

                klevel = Kernellevel::Root; 
            }

            if self.command.contains("top ") {

                klevel = Kernellevel::None;
            }

            if self.command.contains("q") || self.command.contains("--q "){

                klevel = Kernellevel::None;
            }

            if self.command.contains("? ") || self.command.contains("help "){

                klevel = Kernellevel::None;
            }

            if self.command.contains("-v") || self.command.contains("--v "){

                klevel = Kernellevel::None;
            }

            if self.command.contains("whatis") {

                klevel = Kernellevel::None;
            }

            if self.command.contains("man "){

                klevel = Kernellevel::None;
            }

            if self.command.contains("exit"){

                klevel = Kernellevel::None;
            }

            klevel
        }

        /// purpose of listing is to view memory space. This method takes two parameters (self reference , level) & return  Result<Child>.
        /// there may be possible level is not checked, then it will throw unexpected state or panic suitation.
        /// there may be even possible that args are not included in commands.    
    
        pub fn listing(&self, level : Kernellevel) -> std::io::Result<Child> {
        

            if level == Kernellevel::None {
                
                println!("{}", " File Dispatch is not found".color(Color::Red).bold());
            
                panic!("file dispatch is not found");
            
            }
            
            if !(self.args.contains("-a")){

                let child = Command::new(self.command.clone()).spawn().expect("process created");

                println!("input = {:?}", child);
                return Ok(child);

            }else{

                let child = Command::new(self.command.clone()).arg(self.args.clone()).spawn().expect("process created");
                return Ok(child);
            }

        }

        /// the purpose of "cat" is to view the records or content in a memory space. e.g cat file.txt.
        /// cat takes two parameters similar to listing and return result.
        /// there may be possible that level is not checked.    
        pub fn cat_operation(&self, level : Kernellevel) -> std::io::Result<Child>{

            if level == Kernellevel::None{

                println!("{}", "File Dispatch is not found".color(Color::Red).bold());
                panic!("file dispatch is not found");

            }

            let child = Command::new(self.command.clone()).arg(self.args.clone()).spawn().expect("cat command!");

            Ok(child)

        }

        /// change directory is quite similar except the query will handle by stdout.  

        pub fn change_direc(&self) -> std::io::Result<Child>{

            let child = Command::new(self.command.clone()).arg(self.args.clone()).stdout(Stdio::piped()).spawn().expect("change your directory");

            Ok(child)
        }
    }
}


#[cfg(test)]
mod tests {
    use crate::kernel;

    #[test]
    fn instance() {

        let object : _ = kernel::new("ls ".to_string(), "-a ".to_string());

        assert_ne!(object, kernel::Process{command : "".to_string(), args : "".to_string()});
    }

    #[test]
    fn kernel_operation() {

        let object : _ = kernel::new("ls ".to_string(), "-a ".to_string());

        let pem : _ = object.kernel_permission();

        assert_ne!(pem, kernel::Kernellevel::None);
    }

    #[test]
    fn kernel_listing_command() {

        let object : _ = kernel::new("ls ".to_string(), "".to_string());

        let pem : _ = object.kernel_permission();

        let list : _ = object.listing(pem);

        let output : _ = list.expect("process created").wait_with_output().expect("child process running");

        assert_eq!(output.stdout, Vec::<u8>::new());
    }

    #[test]
    fn kernel_cat_operation() {

        let object : _ = kernel::new("cat ".to_string(), "file.txt".to_string());

        let pem : _ = object.kernel_permission();

        let cat_op : _ = object.cat_operation(pem);

        let allow :_ = cat_op.expect("child process created!").id();

        assert_ne!(allow, 0);
    }

    #[test]
    fn kernel_change_direc(){

        let object : _ = kernel::new("cd ".to_string(), "lib/".to_string());

        // let pem : _ = object.kernel_permission();

        let cd_op : _ = object.change_direc();

        let allow : _ = cd_op.expect("process created ...").wait_with_output().expect("child process running");

        assert!(allow.status.success());

    }
}