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
use serde_json;
use serde_json::Value;
use std::fs;
use std::process::{Command, Stdio};
use std::io::prelude::*;

use serde::{Serialize, Deserialize};

#[derive(Debug)]
pub struct Input<'a> {
    filename: &'a str,
    input_file: String,
    cmd_params: CmdParam,
}

#[derive(Debug)]
pub struct Runner {
    stdout: String,
    stderr: String,
}
pub trait Spawn {
    fn run(&self) -> Runner;
    fn run_compiled(&self) -> Runner;
    fn run_interpreted(&self) -> Runner;
}
impl<'a> Spawn for Input<'a> {
    fn run(&self) -> Runner {
        let result: Runner;
        if self.cmd_params.file_type == 1 {
            result = Spawn::run_interpreted(self);
        } else {
            result = Spawn::run_compiled(self);
        }
        result
    }

    fn run_compiled(&self) -> Runner {
        let filepath = self.filename;
        let arg_vec = self.cmd_params.command.clone();
        let mut new_arg: Vec<String> = Vec::new();
        {
            for index in 0..arg_vec.len() {
                let arg = &arg_vec[index];
                if arg == "file_path" {
                    new_arg.push(String::from("src/temp/") + self.filename.clone());
                } else {
                    new_arg.push(arg.to_string());
                }
             }
        }
        let file_path = &(String::from("src/temp/") + &filepath + &self.cmd_params.lang_ext);
        new_arg.push(file_path.to_string());
        
        let compile = Command::new(self.cmd_params.default_path.clone())
            .args(&new_arg)            
            .output()
            .expect("fail to build");
        let mut s = String::from("out");
        // println!("Hey {:?}", compile.stderr);
        if compile.stderr.len() == 0 {
            let execute = match Command::new(&(String::from("src/temp/") + &filepath))
                .stdin(Stdio::piped())
                .stdout(Stdio::piped())
                .spawn() {
                    Err(why) => panic!("couldn't spawn wc: {:?}", why),
                    Ok(process) => process,
                };
            match execute.stdin.unwrap().write_all(&fs::read(self.input_file.clone()).unwrap()) {
                Err(why) => panic!("couldn't write to wc stdin: {:?}", why),
                Ok(_) => println!("sent pangram to wc"),
            }
            s = String::new();
            match execute.stdout.unwrap().read_to_string(&mut s) {
                Err(why) => panic!("couldn't read wc stdout: {:?}",why),
                Ok(_) => (),
            }
        }
        
        match cleanup_file(filepath, &self.cmd_params.lang_ext, self.cmd_params.file_type) {
            Ok(_) => (),
            Err(e) => panic!("Hey {:?}", e),
        };
        Runner {
            stdout: s,
            stderr: String::from("err"),
        }
    }
    fn run_interpreted(&self) -> Runner {
        let filepath = self.filename;
        println!("{}", filepath);
        let execute = match Command::new(self.cmd_params.default_path.clone())
            .arg(String::from("src/temp/") + &filepath + &self.cmd_params.lang_ext)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .spawn() {
                Err(why) => panic!("couldn't spawn wc: {:?}", why),
                Ok(process) => process,
            };
        match execute.stdin.unwrap().write_all(&fs::read(self.input_file.clone()).unwrap()) {
            Err(why) => panic!("couldn't write to wc stdin: {:?}", why),
            Ok(_) => println!("sent pangram to wc"),
        }
        let mut s = String::new();
        match execute.stdout.unwrap().read_to_string(&mut s) {
            Err(why) => panic!("couldn't read wc stdout: {:?}",why),
            Ok(_) => (),
         }
        
        match cleanup_file(filepath, &self.cmd_params.lang_ext, self.cmd_params.file_type) {
            Ok(_) => (),
            Err(e) => panic!("Hey {:?}", e),
        };
        Runner {
            stdout: s,
            stderr: String::from("err"),
        }
    }
}

#[derive(Debug)]
pub struct ClientReq<'r> {
    code_file: Vec<u8>,
    file_type: &'r str,
    input_file: Vec<Vec<u8>>,
}

pub fn create_spawn(req: ClientReq) {
    file_runner_command(req.file_type);
}

#[warn(dead_code)]
fn save_to_temp(file: Vec<u8>, name: String) {
    match fs::write(name, file) {
        Ok(_) => (),
        Err(e) => panic!(e),
    }
}
#[derive(Serialize, Deserialize, Debug)]
struct CmdParam {
    command: Vec<String>,
    default_path: String,
    #[serde(rename = "type")]
    file_type: u8,
    lang_ext: String,
}

#[warn(dead_code)]
fn file_runner_command(ext: &str) -> CmdParam {
    let json_cont = match fs::read_to_string("config/lang.json") {
        Ok(v) => v,
        Err(e) => panic!("JSON FILE:: {:?}", e),
    };
    let data: Value = serde_json::from_str(&json_cont[..]).unwrap();
    let data1 = data.get(ext).unwrap();
    let params: CmdParam = serde_json::from_value(data1.clone()).unwrap();
    params
}

fn cleanup_file(filepath: &str, extension: &str, file_type: u8) -> std::io::Result<()> {
    if file_type == 1 {
        fs::remove_file(String::from("src/temp/") + &filepath + extension)?;
    } else {
        fs::remove_file(String::from("src/temp/") + &filepath)?;
        fs::remove_file(String::from("src/temp/") + &filepath + extension)?;
    }
    Ok(())
}

#[cfg(test)]
mod test {
    use super::*;
    use std::fs;
    use uuid::Uuid;
    #[test]
    fn py_run() {
        let cont = fs::read("src/temp/hello.py").unwrap();
        let filename: String = Uuid::new_v4().to_string();
        let ext: &str = "py2";
        let cmd_params: CmdParam = file_runner_command(ext);
        let input_filename = String::from("src/temp/input.in");
        save_to_temp(
            cont,
            String::from("src/temp/") + &filename.clone() + &cmd_params.lang_ext,
        );
        let input: &Input = &Input {
            filename: &filename,
            cmd_params: cmd_params,
            input_file: input_filename,
        };
        let result: Runner = Spawn::run(input);
        println!("hey {:?}", result);
    }
    #[test]
    fn c_run() {
        let cont = fs::read("src/temp/hello.c").unwrap();
        let filename: String = Uuid::new_v4().to_string();
        let ext: &str = "c";
        let cmd_params: CmdParam = file_runner_command(ext);
        let input_filename = String::from("src/temp/input.in");
        save_to_temp(
            cont,
            String::from("src/temp/") + &filename.clone() + &cmd_params.lang_ext,
        );

        let input: &Input = &Input {
            filename: &filename,
            cmd_params: cmd_params,
            input_file: input_filename,
        };
        let result: Runner = Spawn::run(input);
        println!("hey {:?}", result);
    }
    #[test]
    fn go_run() {

    }
}