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.

98 lines
2.7 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),
}
}
#[inline]
fn add_job(to: &mut Config, cdr: &[Sexp]) -> Result<(), Error> {
to.job_dirs.push(PathBuf::from(get_atom_string(&cdr[0])?));
Ok(())
}
#[inline]
fn set_debug(to: &mut Config, cdr: &[Sexp]) -> Result<(), Error> {
if !to.debug {
let debug = get_atom_string(&cdr[0])?;
to.debug = match debug.to_lowercase().trim() {
"t" => true,
"nil" => false,
_ => return Err(Error::Unknown)
};
} else {
// Should be debug is set.
return Err(Error::Unknown)
}
Ok(())
}
#[inline]
fn mwee(to: &mut Config, cdr: &[Sexp]) -> Result<(), Error> {
println!("{:?}", cdr);
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" => add_job(to, cdr),
"debug" => set_debug(to ,cdr),
"allow" => mwee(to, cdr),
"deny" => mwee(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()));
}
}