summaryrefslogtreecommitdiffstats
path: root/src/backends/python.rs
blob: 57bd624ce90ad9cccf36fbf5db9ff4b6da4f8590 (plain) (blame)
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
use crate::backends::{Backend, RunError};
use crate::serde_duration::deserialize_duration;
use crate::wait::{wait_child, ChildExitStatus};
use serde_derive::Deserialize;
use std::path::Path;
use std::process::Command;
use std::time::Duration;

#[derive(Debug, Deserialize, Default)]
pub struct PythonBackend {
    template: Option<String>,
    version: Option<String>,

    #[serde(default = "default_timeout", deserialize_with = "deserialize_duration")]
    timeout: Duration,
}

fn default_timeout() -> Duration {
    Duration::from_secs(1)
}

impl PythonBackend {
    fn get_interpreter(&self) -> String {
        format!("python{}", self.version.as_ref().unwrap_or(&String::new()))
    }
}

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) -> Result<ChildExitStatus, RunError> {
        let timer = std::time::Instant::now();

        let child = Command::new(self.get_interpreter())
            .arg(fname.as_os_str())
            .spawn()?;

        Ok(wait_child(child, self.timeout, timer)?)
    }
}