diff --git a/src/config/format.rs b/src/config/format.rs index 0188c2b..ee8ab39 100644 --- a/src/config/format.rs +++ b/src/config/format.rs @@ -27,6 +27,30 @@ impl From for LispType } } + +impl From<&Sexp> for LispType +{ + fn from(from: &Sexp) -> Self + { + match from { + Sexp::Atom(atom) => atom.into(), + Sexp::List(_) => Self::List + } + } +} + +impl From<&Atom> for LispType +{ + fn from(from: &Atom) -> Self + { + match from { + Atom::F(_) => Self::Float, + Atom::I(_) => Self::Integer, + Atom::S(_) => Self::String, + } + } +} + impl From for LispType { fn from(from: Atom) -> Self @@ -175,3 +199,55 @@ impl TryIntoExt for Atom } } + + +// To avoid some `clone()`s +pub trait TryGetExt: Sized +{ + fn try_get_list(&self) -> Result<&[Sexp], Error>; + fn try_get_atom(&self) -> Result<&Atom, Error>; + + fn try_get_string(&self) -> Result<&String, Error>; + fn try_get_int(&self) -> Result<&i64, Error>; + fn try_get_float(&self) -> Result<&f64, Error>; +} + +impl TryGetExt for Sexp +{ + fn try_get_list(&self) -> Result<&[Sexp], Error> + { + match self { + Self::List(list) => Ok(&list[..]), + other => Err(Error::bad_type(LispType::List, other)), + } + } + fn try_get_atom(&self) -> Result<&Atom, Error> + { + match self { + Self::Atom(atom) => Ok(atom), + other => Err(Error::bad_type(LispType::Atom, other)), + } + } + + fn try_get_string(&self) -> Result<&String, Error> + { + match self { + Self::Atom(Atom::S(string)) => Ok(string), + other => Err(Error::bad_type(LispType::String, other)), + } + } + fn try_get_int(&self) -> Result<&i64, Error> + { + match self { + Self::Atom(Atom::I(int)) => Ok(int), + other => Err(Error::bad_type(LispType::Integer, other)), + } + } + fn try_get_float(&self) -> Result<&f64, Error> + { + match self { + Self::Atom(Atom::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 8555970..fac0c9c 100644 --- a/src/config/parse.rs +++ b/src/config/parse.rs @@ -14,9 +14,9 @@ use sexp::{ fn get_atom_string<'a>(maybe_atom: &'a Sexp) -> Result<&'a String, Error> { - match &maybe_atom { + match maybe_atom { Sexp::Atom(Atom::S(string)) => Ok(string), - &opt => Err(Error::bad_type(LispType::String, opt.to_owned())), + opt => Err(Error::bad_type(LispType::String, opt)), } } @@ -74,7 +74,7 @@ pub async fn global(to: &mut Config, path: impl AsRef) -> Result<(), error let sexp = sexp.try_into_list()?; let (car,cdr) = sexp.try_split()?; - let car = car.clone()/* <- i'm too lazy to make ref versions of these `try_into_*` functions, so this will do*/.try_into_string()?; + let car = car.try_get_string()?; match car.to_lowercase().trim() { "jobs-dir" => add_job(to, cdr), "debug" => set_debug(to ,cdr), @@ -84,8 +84,6 @@ pub async fn global(to: &mut Config, path: impl AsRef) -> Result<(), error }?; } Ok(()) - - } else { Err(Error::NotFound(path.to_owned())) }