use std::error::Error;
use std::fs;
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct EJConfig {
pub name: String,
pub description: String,
pub author: Author,
pub source: String,
pub runtime: String,
pub output: String,
pub global: bool
}
#[derive(Serialize, Deserialize, Debug)]
pub struct Author {
pub name: String,
pub email: String,
}
pub fn parse_ej_config(file: PathBuf) -> Result<EJConfig, Box<dyn Error>> {
let contents = fs::read_to_string(file)?;
EJConfig::from_json(&contents).map_err(|e| Box::new(e) as Box<dyn Error>)
}
pub fn get_ej_config(dir: &str) -> Vec<PathBuf> {
let mut configs = Vec::new();
if let Ok(entries) = fs::read_dir(dir) {
for entry in entries.flatten() {
let path = entry.path();
if path.is_file() && path.extension().and_then(|s| s.to_str()) == Some("ejconfig") {
configs.push(path);
}
}
}
configs
}
impl EJConfig {
pub fn from_json(json_string: &str) -> Result<EJConfig, serde_json::Error> {
serde_json::from_str(json_string)
}
pub fn to_string(self) -> Result<String, serde_json::Error> {
serde_json::to_string(&self)
}
}