Skip to content
Snippets Groups Projects
adapters.rs 4.32 KiB
Newer Older
use yaml_rust::{YamlLoader, YamlEmitter, ScanError, Yaml};
use std::{fs, result};
use std::error::Error;
use std::path::Path;
use std::collections::HashMap;
use std::process::{Command, Stdio};
use tera::{Context, Tera};
use linked_hash_map::LinkedHashMap;
Francesco's avatar
Francesco committed
use crate::constants::*;
fn check_type() -> bool {
    true
pub fn check_syntax(path: &Path) -> Result<bool, Box<dyn Error + '_>> {
    // Getting filename
    let filename = Path::new(path).file_name().unwrap().to_str().unwrap();
    // Reading the file as a string
    let conf_file = match fs::read_to_string(path) {
        Ok(file) => file,
        Err(err) => return Err(Box::new(err)),
    };
    // Loading previously read string with yamlloader to check the syntax
    match YamlLoader::load_from_str(&conf_file) {
        Ok(content) => {
            let conf = content[0].as_hash().unwrap(); // Getting config
            let mut line = 2;
            // Checking if the binary path has been given
            if !conf.contains_key(&Yaml::String(String::from("bin"))) {
                return Err(Box::new(JobConfError::new(JobConfErrorType::MissingPathField, 0, filename)))
            }else {  
                // Checking if list of parameters exist within the file
                if !conf.contains_key(&Yaml::String(String::from("parameters"))) {
                    return Err(Box::new(JobConfError::new(JobConfErrorType::MissingParametersList, 1, filename)))
                }else {
                    // Checking if the parameters list isn't empy
                    let params = conf[&Yaml::String(String::from("parameters"))].as_vec().unwrap();
                    if params.len() == 0 {
                        return Err(Box::new(JobConfError::new(JobConfErrorType::MissingParametersList, 2, filename)))
                    }else {
                        // Checking the syntax of each given parameter
                        for entry in params {
                            let param = match entry.as_hash() {
                                Some(p) => p,
                                None => return Err(Box::new(JobConfError::new(JobConfErrorType::MissingParametersList, line, filename))),
                            };
                            let value_type = String::from("string");
                            let named: bool = false;
                            // Checking the parameter name
                            if param.contains_key(&Yaml::String(String::from("name"))){
                                line += 1;
                            }else {

                            }
                            if param.contains_key(&Yaml::String(String::from("value-type"))){
                                line += 1;
                            }else {
                                
                            }
                            if param.contains_key(&Yaml::String(String::from("default-value"))){
                                let value = param.get(&Yaml::String(String::from("default-value")));
                                if check_type() == true {

                                }
                                line += 1;
                            }
                            if param.contains_key(&Yaml::String(String::from("commnet"))){
                                line += 1;
                            }
                            if param.contains_key(&Yaml::String(String::from("required"))){
                                line += 1;
                            }else {
                                
                            }
                            if param.contains_key(&Yaml::String(String::from("named"))){
                                line += 1;
                            }else {
                                
                            }
                            if named {
                            line += 1;
                        return Ok(true);
                    }
                }
            }
        },
        Err(err) => return Err(Box::new(err)),
    }
}

fn generate_command() {

}

pub fn generate_sbatch() {

}


#[cfg(test)]
mod tests {
    use super::check_type;
Francesco's avatar
Francesco committed
    use super::check_syntax;
    use super::generate_command;
    use super::generate_sbatch;

    #[test]
    fn it_works() {
        let result = 2 + 2;
        assert_eq!(result, 4);
    }
}