parent
8fdd1a6353
commit
f27dceed24
@ -1,10 +1,74 @@
|
||||
//! Parses the config files
|
||||
use super::*;
|
||||
|
||||
use tokio::fs::File;
|
||||
use tokio::prelude::*;
|
||||
use sexp::{
|
||||
parse,
|
||||
Sexp,
|
||||
Atom,
|
||||
};
|
||||
|
||||
fn get_atom_string<'a, T>(maybe_atom: T) -> Result<&'a String, Error>
|
||||
where T: AsRef<Sexp> + 'a
|
||||
{
|
||||
match maybe_atom.as_ref() {
|
||||
Sexp::Atom(Atom::S(string)) => Ok(string),
|
||||
_ => Err(Error::InvalidSexp),
|
||||
}
|
||||
}
|
||||
|
||||
fn new_job(to: &mut Config, cdr: &[Sexp]) -> Result<(), Error> {
|
||||
to.job_dirs.push(PathBuf::from(get_atom_string(&cdr[0])?));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Parse a single config file
|
||||
pub fn global(to: &mut Config, path: impl AsRef<Path>) -> Result<(), error::Error>
|
||||
pub async fn global(to: &mut Config, path: impl AsRef<Path>) -> Result<(), error::Error>
|
||||
{
|
||||
let path = path.as_ref();
|
||||
|
||||
todo!();
|
||||
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()));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue