From 7c86c6afda383ae48f63563eeb55ea996ff9c7ce Mon Sep 17 00:00:00 2001 From: Avril Date: Sun, 2 Aug 2020 16:44:56 +0100 Subject: [PATCH] more documentation --- src/config/error.rs | 1 + src/config/format.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++ src/config/parse.rs | 3 +-- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/config/error.rs b/src/config/error.rs index 662b5b4..8b14eee 100644 --- a/src/config/error.rs +++ b/src/config/error.rs @@ -7,6 +7,7 @@ use std::{ //TODO: Embed info about the file, the line number, the expression, etc? +// Will require writing own parser, that embeds such file metadata in its returned expressions. Maybe do this after functionality is implemented for program /// Error type for parsing config errors #[derive(Debug)] pub enum Error { diff --git a/src/config/format.rs b/src/config/format.rs index 6fc35a9..249c253 100644 --- a/src/config/format.rs +++ b/src/config/format.rs @@ -113,13 +113,20 @@ impl LispExt for Vec } // Sexp doesn't use try_from(), so... +/// Extension methods for getting typed values out of S-expression pub trait TryIntoExt: Sized { + /// Return this expression as a List (`Vec`) if possible fn try_into_list(self) -> Result, Error>; + + /// Return this expression as a non-descript atom if possible fn try_into_atom(self) -> Result; + /// Return this expression as a String if possible (atom) fn try_into_string(self) -> Result; + /// Return this expression as an integer if possible (atom) fn try_into_int(self) -> Result; + /// Return this expression as a float if possible (atom) fn try_into_float(self) -> Result; } @@ -204,13 +211,21 @@ impl TryIntoExt for Atom // To avoid some `clone()`s +/// Extension methods for getting typed references out of S-expression pub trait TryGetExt: Sized { + /// Try to get this expression as a reference `List` (slice of `Sexpr`). fn try_get_list(&self) -> Result<&[Sexp], Error>; + /// Try to get this expression as a reference atom. fn try_get_atom(&self) -> Result<&Atom, Error>; + /// Try to get this expression as a reference string (atom) fn try_get_string(&self) -> Result<&String, Error>; + + /// Try to get this expression as a reference integer (atom) fn try_get_int(&self) -> Result<&i64, Error>; + + /// Try to get this expression as a reference float (atom) fn try_get_float(&self) -> Result<&f64, Error>; } @@ -253,3 +268,39 @@ impl TryGetExt for Sexp } } } + +impl TryGetExt for Atom +{ + #[inline] + fn try_get_list(&self) -> Result<&[Sexp], Error> + { + Err(Error::bad_type(LispType::List, self)) + } + #[inline] + fn try_get_atom(&self) -> Result<&Atom, Error> + { + Ok(self) + } + + fn try_get_string(&self) -> Result<&String, Error> + { + match self { + Self::S(string) => Ok(string), + other => Err(Error::bad_type(LispType::String, other)), + } + } + fn try_get_int(&self) -> Result<&i64, Error> + { + match self { + Self::I(int) => Ok(int), + other => Err(Error::bad_type(LispType::Integer, other)), + } + } + fn try_get_float(&self) -> Result<&f64, Error> + { + match self { + Self::F(float) => Ok(float), + other => Err(Error::bad_type(LispType::Float, other)), + } + } +} diff --git a/src/config/parse.rs b/src/config/parse.rs index 132f2cd..dda2592 100644 --- a/src/config/parse.rs +++ b/src/config/parse.rs @@ -61,8 +61,7 @@ pub async fn global(to: &mut Config, path: impl AsRef) -> Result<(), error if path.exists() { let mut file = File::open(path).await?; let mut contents = if let Ok(Ok(wv)) = file.metadata().await - .map(|x| x.len()) - .map(|x| usize::try_from(x)) { + .map(|x| usize::try_from(x.len())) { let mut out = Vec::with_capacity(wv+3); out.push(b'('); out