diff options
author | syn <isaqtm@gmail.com> | 2019-12-27 10:19:55 +0300 |
---|---|---|
committer | syn <isaqtm@gmail.com> | 2019-12-27 10:19:55 +0300 |
commit | 4b062197e6dd67311e7cfb2c05f28e20c6809daa (patch) | |
tree | 2961151b2b19b582e4d41b47fb832aa2838f5e6d /src/backends | |
download | evr-4b062197e6dd67311e7cfb2c05f28e20c6809daa.tar.gz |
Something is done
Diffstat (limited to 'src/backends')
-rw-r--r-- | src/backends/clang.rs | 21 | ||||
-rw-r--r-- | src/backends/mod.rs | 20 | ||||
-rw-r--r-- | src/backends/python.rs | 43 |
3 files changed, 84 insertions, 0 deletions
diff --git a/src/backends/clang.rs b/src/backends/clang.rs new file mode 100644 index 0000000..5a0c37f --- /dev/null +++ b/src/backends/clang.rs @@ -0,0 +1,21 @@ +use serde_derive::{ Serialize, Deserialize }; +use crate::backends::Backend; +use std::path::Path; + +#[derive(Debug, Serialize, Deserialize, Default)] +pub struct ClangBackend { + template: Option<String> +} + +impl Backend for ClangBackend { + fn get_template(&self) -> Option<&str> { + match self.template { + Some(ref t) => Some(t), + None => None + } + } + + fn run(&self, _fname: &Path) -> std::io::Result<()> { + todo!(); + } +}
\ No newline at end of file diff --git a/src/backends/mod.rs b/src/backends/mod.rs new file mode 100644 index 0000000..729daa5 --- /dev/null +++ b/src/backends/mod.rs @@ -0,0 +1,20 @@ +use std::path::{ Path, PathBuf }; + +pub mod python; +pub mod clang; + +pub use python::PythonBackend; +pub use clang::ClangBackend; + +pub trait Backend { + fn get_template(&self) -> Option<&str>; + fn run(&self, fname: &Path) -> std::io::Result<()>; + + fn try_guess_test(&self, fname: &Path) -> Option<PathBuf> { + let maybe_test = fname.with_extension("txt"); + if maybe_test.exists() { + return Some(maybe_test); + } + None + } +} diff --git a/src/backends/python.rs b/src/backends/python.rs new file mode 100644 index 0000000..2776eb2 --- /dev/null +++ b/src/backends/python.rs @@ -0,0 +1,43 @@ +use serde_derive::{ Serialize, Deserialize }; +use crate::backends::Backend; +use std::process::{ Command, Stdio }; +use std::path::Path; +use log::trace; +use std::io::{ Error, ErrorKind }; + +#[derive(Debug, Serialize, Deserialize, Default)] +pub struct PythonBackend { + template: Option<String>, + version: Option<String>, +} + +impl Backend for PythonBackend { + fn get_template(&self) -> Option<&str> { + match self.template { + Some(ref t) => Some(t), + None => None + } + } + + fn run(&self, fname: &Path) -> std::io::Result<()> { + let interpreter = format!("python{}", self.version.as_ref().unwrap_or(&String::new())); + let stdio = match self.try_guess_test(fname) { + Some(test_file) => Stdio::from(std::fs::File::open(test_file)?), + None => Stdio::piped() + }; + let timer = std::time::SystemTime::now(); + let mut child = Command::new(interpreter) + .arg(fname.as_os_str()) + .stdin(stdio) + .spawn()?; + + let status = child.wait()?; + if !status.success() { + return Err(Error::new(ErrorKind::Other, + "Process exited with non-zero exit code")); + } + trace!("elapsed: {:#?}", timer.elapsed()); + + Ok(()) + } +}
\ No newline at end of file |