You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.2 KiB

//! Parses the config files
use super::*;
use tokio::fs::File;
use tokio::prelude::*;
use sexp::{
parse,
Sexp,
Atom,
};
fn get_atom_string<'a>(maybe_atom: &'a Sexp) -> Result<&'a String, Error>
{
match &maybe_atom {
Sexp::Atom(Atom::S(string)) => Ok(string),
_ => Err(Error::InvalidSexp),
}
}
fn new_job(to: &mut Config, cdr: &[Sexp]) -> Result<(), Error> {
let what = &cdr[0];
let what = get_atom_string(what)?;
to.job_dirs.push(PathBuf::from(what));
Ok(())
}
/// Parse a single config file
pub async fn global(to: &mut Config, path: impl AsRef<Path>) -> Result<(), error::Error>
{
let path = path.as_ref();
if path.exists() {
let mut file = File::open(path).await?;
let mut contents = vec![];
file.read_to_end(&mut contents).await?;
let parsed = sexp::parse(std::str::from_utf8(&contents[..])?)?;
if let Sexp::List(sexp) = parsed {
for sexp in sexp.into_iter() {
if let Sexp::List(sexp) = sexp {
let car = &sexp[0];
let cdr = &sexp[1..];
match car {
Sexp::List(_) => return Err(Error::InvalidSexp),
Sexp::Atom(car) => {
if let Atom::S(car) = car {
match car.to_lowercase().trim() {
"jobs-dir" => new_job(to, cdr),
"debug" => new_job(to, cdr),
"allow" => new_job(to, cdr),
"deny" => new_job(to, cdr),
_ => return Err(Error::Unknown),
}?;
} else {
return Err(Error::InvalidSexp);
}
}
}
}
else {
return Err(Error::InvalidSexp);
}
}
} else {
return Err(Error::InvalidSexp);
}
Ok(())
} else {
return Err(Error::NotFound(path.to_owned()));
}
}