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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
mod tests;
mod cache;

use core::fmt;
use std::fmt::Debug;

use serde::{Deserialize, Serialize};
use crate::cache::CompilerCache;
use std::sync::{RwLock, Arc};
use std::error::Error;

use std::collections::HashSet;

/// The main cache that holds on to the compiler cache
pub struct Wandbox {
    cache : Arc<RwLock<CompilerCache>>,
}
impl Wandbox {
    /// Initializes the cache for Wandbox requests
    ///
    /// You may also choose to block certain compilers or languages from being supported.
    /// This is useful if wandbox has any issues with certain compilers or languages.
    ///
    /// # Arguments
    /// * `comps` - A vector of compiler identifiers that the library should ignore
    /// * `langs` - A vector of language identifiers that the library should ignore
    /// # Example
    /// ```edition2018
    ///use std::collections::HashSet;
    ///use wandbox::Wandbox;
    ///
    ///#[tokio::main]
    ///async fn main() {
    ///    let mut set : HashSet<String> = HashSet::new();
    ///    set.insert(String::from("gcc-head"));
    ///
    ///    let wbox : Wandbox = match Wandbox::new(Some(set), None).await {
    ///        Ok(wbox) => wbox,
    ///        Err(e) => return println!("{}", e)
    ///    };
    /// }
    ///```
    pub async fn new(comps : Option<HashSet<String>>, langs : Option<HashSet<String>>) -> Result<Wandbox, Box<dyn Error>> {

        let mut cache : CompilerCache = cache::load().await?;

        if let Some(langs) = langs {
            cache = cache.into_iter().filter(|(_x, v)| !langs.contains(&v.name)).collect();
        }

        if let Some(comps) = comps {
            for (_k, v) in cache.iter_mut() {
                for str in &comps {
                    v.remove_compiler(str);
                }
            }
        }

        // adjust language names to lower
        for (_k, v) in cache.iter_mut() {
            for mut c in v.compilers.iter_mut() {
                c.language = c.language.to_ascii_lowercase();
            }
        }

        Ok(Wandbox {
            cache: Arc::new(RwLock::new(cache))
        })
    }

    /// Gets a list of compilers given a certain language
    ///
    /// # Arguments
    /// * `lang` - The language identifier to return the compilers for
    pub fn get_compilers(&self, lang : &str) -> Option<Vec<Compiler>> {
        let lock = self.cache.read().unwrap();
        let language_option = lock.get(lang);
        let lang = match language_option {
            Some(l) => l,
            None => return None
        };

        Some(lang.compilers.clone())
    }

    /// Returns a list of every language
    pub fn get_languages(&self) -> Vec<Language> {
        let lock = self.cache.read().unwrap();

        let mut vec : Vec<Language> = Vec::new();
        for (_k, v) in lock.iter() {
            vec.push(v.clone());
        }
        vec
    }

    /// Determines if the compiler string supplied is a valid compiler
    ///
    /// # Arguments
    /// * `c` - compiler identifier to check for
    //  n^2 :(
    pub fn is_valid_compiler_str(&self, c : &str) -> bool {
        // aquire our lock
        let lock = self.cache.read().unwrap();
        for (_l, k) in lock.iter() {
            for v in k.compilers.iter() {
                if v.name == c {
                    return true;
                }
            }
        }

        return false;
    }

    pub fn get_compiler_language_str(&self, c : &str) -> Option<String> {
        // aquire our lock
        let lock = self.cache.read().unwrap();

        for (_k, v) in lock.iter() {
            for comp in &v.compilers {
                if comp.name == c {
                    return Some(v.name.clone());
                }
            }
        }

        return None;
    }

    pub fn is_valid_language(&self, l : &str) -> bool {
        let lock = self.cache.read().unwrap();
        return lock.get(l).is_some();
    }

    pub fn get_default_compiler(&self, l : &str) -> Option<String> {
        let lock = self.cache.read().unwrap();
        if let Some(lang) = lock.get(l) {
            Some(lang.compilers.get(0).expect("awd").name.clone())
        }
        else {
            None
        }
    }
}

/// Representation of a compiler
#[derive(Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct Compiler {
    #[serde(rename = "compiler-option-raw")]
    pub compiler_option_raw : bool,
    #[serde(rename = "display-compile-command")]
    pub display_compile_command : String,
    #[serde(rename = "runtime-option-raw")]
    pub runtime_option_raw : bool,

    pub version : String,
    pub language : String,
    pub name : String,
}
impl Clone for Compiler {
    fn clone(&self) -> Self {
        Compiler {
            compiler_option_raw : self.compiler_option_raw,
            display_compile_command : self.display_compile_command.clone(),
            runtime_option_raw : self.runtime_option_raw,
            version : self.version.clone(),
            language : self.language.clone(),
            name : self.name.clone(),
        }
    }
}
impl fmt::Debug for Compiler {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "[{} {}] : {}", self.name, self.version, self.language)
    }
}

/// A builder to allow you to easily build requests
///
/// ```edition2018
///use std::collections::HashSet;
///use tokio::macros::*;
///use std::error::Error;
///use wandbox::{Wandbox, CompilationBuilder};
///#[tokio::main]
///async fn main() {
///    let wbox : Wandbox = match Wandbox::new(None, None).await {
///        Ok(wbox) => wbox,
///        Err(e) => return println!("{}", e)
///    };
///    let mut builder = CompilationBuilder::new();
///    builder.target("gcc-6.3.0");
///    builder.options_str(vec!["-Wall", "-Werror"]);
///    builder.code("#include<iostream>\nint main()\n{\nstd::cout<<\"test\";\n}");
///    let result = match builder.build(&wbox) {
///        Ok(res) => res,
///        Err(e) => return println!("{}", e)
///    };
///}
/// ```
#[derive(Default, Serialize)]
pub struct CompilationBuilder {
    #[serde(skip)]
    target : String,
    pub lang : String,
    compiler : String,
    code : String,
    stdin : String,
    #[serde(skip)]
    options : Vec<String>,
    #[serde(rename = "compiler-option-raw")]
    compiler_options_raw : String,
    save : bool
}
impl CompilationBuilder {
    /// Creates a new CompilationBuilder with default values to be filled in later
    pub fn new() -> CompilationBuilder {
        return CompilationBuilder { ..Default::default()}
    }

    /// Sets the target of the compilation
    ///
    /// # Arguments
    /// * `target` - The target of a compilation, this can be a language ('c++'), or a compiler ('gcc-head')
    pub fn target(&mut self, target : &str) -> () {
        self.target = target.trim().to_string();
    }

    /// Sets the code to be compiled
    ///
    /// # Arguments
    /// * `code` - String of code to be compiled
    pub fn code(&mut self, code : &str)  -> () {
        self.code = code.trim().to_string();
    }

    /// Sets the stdin to directed towards the application
    ///
    /// # Arguments
    /// * `stdin` - program input
    pub fn stdin(&mut self, stdin : &str) -> () {
        self.stdin = stdin.trim().to_string();
    }

    /// Determines whether or not Wandbox saves the compilation & replies with a link for you
    ///
    /// # Arguments
    /// * `save` - true if Wandbox should save this compilation
    pub fn save(&mut self, save : bool) -> () {
        self.save = save;
    }

    /// Sets the list of compilation options. Useful for languages like c++ to pass linker/optimization
    /// flags.
    ///
    /// # Arguments
    /// * `options` - A list of compiler options i.e ["-Wall", "-Werror"]
    pub fn options(&mut self, options : Vec<String>) -> () {
        self.options = options;
    }

    /// Sets the list of compilation options. Useful for languages like c++ to pass linker/optimization
    /// flags.
    ///
    /// This version allows you to pass a `Vec<&str>`
    ///
    /// # Arguments
    /// * `options` - A list of compiler options i.e ["-Wall", "-Werror"]
    pub fn options_str(&mut self, options : Vec<&str>) -> () {
        self.options = options.into_iter().map(|f| f.to_owned()).collect();
    }

    /// Finalizes the builder & prepares itself for compilation dispatch.
    ///
    /// # Arguments
    /// * `wb` - An instance of the Wandbox cache to resolve the compilation target
    pub fn build(&mut self, wb : &Wandbox) -> Result<(), WandboxError> {
        self.compiler_options_raw = self.options.join("\n");

        if wb.is_valid_language(&self.target) {
            let comp = match wb.get_default_compiler(&self.target) {
                Some(def) => def,
                None => return Err(WandboxError::new("Unable to determine default compiler for input language"))
            };
            self.compiler = comp;
            self.lang = self.target.clone();
        }
        else if wb.is_valid_compiler_str(&self.target) {
            let lang = match wb.get_compiler_language_str(&self.target) {
                Some(lang) => lang,
                None => return Err(WandboxError::new("Unable to determine language for compiler}"))
            };

            self.lang = lang;
            self.compiler = self.target.clone();
        }
        else {
            return Err(WandboxError::new("Unable to find compiler or language for target"));
        }
        Ok(())
    }

    /// Dispatches the built request to Wandbox
    pub async fn dispatch(&self) -> Result<CompilationResult, WandboxError> {
        let client = reqwest::Client::new();

        let result = client.post("https://wandbox.org/api/compile.json")
            .json(&self)
            .header("Content-Type", "application/json; charset=utf-8")
            .send().await;

        let response = match result {
            Ok(r) => r,
            Err(e) => return Err(WandboxError::new(&format!("{}", e)))
        };

        let status_code = response.status().clone();
        let res : CompilationResult = match response.json().await {
            Ok(res) => res,
            Err(_e) => return Err(WandboxError::new(&format!("Wandbox replied with: {}\n\
            This could mean WandBox is experiencing an outage, or a network connection error has occured", status_code)))
        };
        return Ok(res);
    }
}

/// Information regarding the result of a compilation request.
#[derive(Default, Deserialize)]
pub struct CompilationResult {
    #[serde(default)]
    pub status : String,
    #[serde(default)]
    pub signal : String,
    #[serde(rename = "compiler_output", default)]
    pub compiler_stdout : String,
    #[serde(rename = "compiler_error", default)]
    pub compiler_stderr : String,
    #[serde(rename = "compiler_message", default)]
    pub compiler_all : String,
    #[serde(rename = "program_output", default)]
    pub program_stdout : String,
    #[serde(rename = "program_error", default)]
    pub program_stderr : String,
    #[serde(rename = "program_message", default)]
    pub program_all : String,
    #[serde(default)]
    pub permlink : String,
    #[serde(default)]
    pub url : String,
}

impl fmt::Debug for CompilationResult {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "[{} {}] {}: {}", self.status, self.signal, self.compiler_all, self.program_all)
    }
}


/// A representation of a language with a list of it's compilers
#[derive(Hash, Eq, PartialEq, Debug, Clone)]
pub struct Language {
    pub name : String,
    pub compilers : Vec<Compiler>
}

impl Language {
    fn remove_compiler(&mut self, str : &str) {
        let mut copy = self.compilers.clone();
        copy = copy.into_iter().filter(|v| v.name != str).collect();
        self.compilers = copy;
    }
}


#[derive(Debug)]
pub struct WandboxError {
    details: String
}

impl WandboxError {
    fn new(msg: &str) -> WandboxError {
        WandboxError{details: msg.to_string()}
    }
}

impl fmt::Display for WandboxError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f,"{}",self.details)
    }
}

impl std::error::Error for WandboxError {
    fn description(&self) -> &str {
        &self.details
    }
}