Begin implemetation of config parser

master
not manx 4 years ago
parent 8fdd1a6353
commit f27dceed24
Signed by: C-xC-c
GPG Key ID: F52ED472284EF2F4

1
Cargo.lock generated

@ -533,6 +533,7 @@ dependencies = [
"bytes",
"fnv",
"lazy_static",
"memchr",
"mio",
"num_cpus",
"pin-project-lite",

@ -12,7 +12,7 @@ default = ["threaded"]
threaded = ["tokio/rt-threaded"]
[dependencies]
tokio = {version = "0.2", features=["time", "macros", "io-driver", "sync", "rt-core"]}
tokio = {version = "0.2", features=["time", "macros", "io-driver", "sync", "rt-core", "fs"]}
notify = "4.0"
futures= "0.3"
sexp = "1.1"

@ -2,13 +2,16 @@
use super::*;
use std::{
io,
path::PathBuf,
};
#[derive(Debug)]
pub enum Error {
IO(io::Error),
Syntax(sexp::Error),
NotFound(PathBuf),
InvalidUtf8,
InvalidSexp,
Unknown,
}
@ -30,7 +33,9 @@ impl std::fmt::Display for Error
match self {
Self::IO(io) => write!(f, "i/o error: {}", io),
Self::Syntax(sy) => write!(f, "s-expression syntax error: {}", sy),
Self::NotFound(path) => write!(f, "File not found: {:?}", path),
Self::InvalidUtf8 => write!(f, "Invalid UTF8 decoded string or some such"),
Self::InvalidSexp => write!(f, "Invalid s-expression"),
_ => write!(f, "unknown error")
}
}
@ -59,3 +64,11 @@ impl From<Box<sexp::Error>> for Error
Self::Syntax(*from)
}
}
impl From<std::str::Utf8Error> for Error
{
fn from(from: std::str::Utf8Error) -> Self
{
Self::InvalidUtf8
}
}

@ -33,6 +33,8 @@ pub struct Config
pub job_dirs: Vec<PathBuf>,
/// The rules for users
pub user_rules: Vec<UserRule>,
/// Enable debug
pub debug: bool,
}
impl Default for Config
@ -40,6 +42,6 @@ impl Default for Config
#[inline]
fn default() -> Self
{
Self{job_dirs: Vec::new(), user_rules: Vec::new()}
Self{job_dirs: Vec::new(), user_rules: Vec::new(), debug: false}
}
}

@ -40,5 +40,4 @@ pub struct Job
what: Command,
}
const DEFAULT_JOB_DIR: &str = "/etc/rori.kron";

@ -20,9 +20,9 @@ pub use global::*;
/// Parse a single config file
#[inline]
pub fn parse_global_single(path: impl AsRef<Path>) -> Result<Config, error::Error>
pub async fn parse_global_single(path: impl AsRef<Path>) -> Result<Config, error::Error>
{
let mut cfg = Config::default();
parse::global(&mut cfg, path)?;
parse::global(&mut cfg, path).await?;
Ok(cfg)
}

@ -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()));
}
}

@ -62,12 +62,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
debug!("Initialised");
let (mut tx, h) = do_thing_every().await?;
println!("{:?}", config::parse_global_single("/home/manx/flon.txt").await.expect("Waaaaaah"));
loop {
time::delay_for(Duration::from_secs(6)).await;
tx.send(()).await?;
}
h.await?;
//let (mut tx, h) = do_thing_every().await?;
// loop {
// time::delay_for(Duration::from_secs(6)).await;
// tx.send(()).await?;
// }
// h.await?;
Ok(())
}

Loading…
Cancel
Save