From b813fe4b016434bf420b5f47fa47f08d1a68c845 Mon Sep 17 00:00:00 2001 From: Avril Date: Sun, 2 Aug 2020 05:38:04 +0100 Subject: [PATCH] being wonk is fine actually --- example.rori | 13 ++++++------- src/config/error.rs | 16 +++++++++++++++- src/config/format.rs | 4 +++- src/config/parse.rs | 13 +++++++++---- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/example.rori b/example.rori index af2c82e..b2711b8 100644 --- a/example.rori +++ b/example.rori @@ -1,7 +1,6 @@ -((jobs-dir "/etc/rori.kron") - (debug t) - (allow - :all) - (deny - (:user "flon") - (:uid 8008))) +(jobs-dir "/etc/rori.kron") +(debug t) +(allow :all) +(deny + (:user "flon") + (:uid 8008)) diff --git a/src/config/error.rs b/src/config/error.rs index f21502a..662b5b4 100644 --- a/src/config/error.rs +++ b/src/config/error.rs @@ -5,16 +5,30 @@ use std::{ path::PathBuf, }; + +//TODO: Embed info about the file, the line number, the expression, etc? +/// Error type for parsing config errors #[derive(Debug)] pub enum Error { + + /// Internal IO error IO(io::Error), + /// S-expression syntax error Syntax(sexp::Error), + + /// File not found, or was a directory NotFound(PathBuf), + /// File contained invalid UTF-8 InvalidUtf8, + /// Invalid or unexpected type TypeError{expected:LispType, got: LispType}, + /// Expected a list with at least one element UnexpectedEmpty, - UnknownDefinition(String), + /// Unknown definition string + UnknownDefinition(String), + + /// Non-descript other error Unknown, } diff --git a/src/config/format.rs b/src/config/format.rs index 060a2f8..6fc35a9 100644 --- a/src/config/format.rs +++ b/src/config/format.rs @@ -13,6 +13,8 @@ pub enum LispType String, Integer, Float, + + /// A non-descript atom, either String, Integer, Float, or the empty list. Atom, } @@ -85,7 +87,7 @@ impl std::fmt::Display for LispType Self::String => "string", Self::Integer => "integer", Self::Float => "float", - Self::Atom => "atom", + Self::Atom => "nil", }) } } diff --git a/src/config/parse.rs b/src/config/parse.rs index e3126f5..132f2cd 100644 --- a/src/config/parse.rs +++ b/src/config/parse.rs @@ -2,10 +2,12 @@ use super::*; use std::{ convert::TryFrom, + marker::Unpin, }; use tokio::{ prelude::*, fs::File, + io::AsyncRead, }; use sexp::{ Sexp, @@ -61,12 +63,15 @@ pub async fn global(to: &mut Config, path: impl AsRef) -> Result<(), error let mut contents = if let Ok(Ok(wv)) = file.metadata().await .map(|x| x.len()) .map(|x| usize::try_from(x)) { - Vec::with_capacity(wv) + let mut out = Vec::with_capacity(wv+3); + out.push(b'('); + out } else { - Vec::new() + vec![b'('] }; - file.read_to_end(&mut contents).await?; - + file.read_to_end(&mut contents).await?; + contents.push(b')'); + let parsed = sexp::parse(std::str::from_utf8(&contents[..])?)?; let sexp = parsed.try_into_list()?;