use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};
use serde::Deserialize;
use super::ScriptError;
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct ScriptFile {
pub(crate) scripts: BTreeMap<String, ScriptEntry>,
}
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct ScriptEntry {
pub(crate) program: String,
#[serde(default)]
pub(crate) args: Vec<String>,
#[serde(default)]
pub(crate) cwd: Option<PathBuf>,
#[serde(default)]
pub(crate) env: BTreeMap<String, String>,
}
impl ScriptFile {
pub(crate) fn load(root: &Path) -> Result<Self, ScriptError> {
let path = root.join("s.script");
let text = fs::read_to_string(&path).map_err(|source| ScriptError::Read {
path: path.clone(),
source,
})?;
let file: Self =
toml::from_str(&text).map_err(|source| ScriptError::Parse { path, source })?;
for name in file.scripts.keys() {
if !super::name::valid(name) {
return Err(ScriptError::InvalidName(name.clone()));
}
}
Ok(file)
}
}