it just wonks

master
Avril 4 years ago
parent 79250c9b36
commit 1dc731f77f
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -27,6 +27,30 @@ impl From<Sexp> 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<Atom> for LispType impl From<Atom> for LispType
{ {
fn from(from: Atom) -> Self 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)),
}
}
}

@ -14,9 +14,9 @@ use sexp::{
fn get_atom_string<'a>(maybe_atom: &'a Sexp) -> Result<&'a String, Error> 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), 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<Path>) -> Result<(), error
let sexp = sexp.try_into_list()?; let sexp = sexp.try_into_list()?;
let (car,cdr) = sexp.try_split()?; 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() { match car.to_lowercase().trim() {
"jobs-dir" => add_job(to, cdr), "jobs-dir" => add_job(to, cdr),
"debug" => set_debug(to ,cdr), "debug" => set_debug(to ,cdr),
@ -84,8 +84,6 @@ pub async fn global(to: &mut Config, path: impl AsRef<Path>) -> Result<(), error
}?; }?;
} }
Ok(()) Ok(())
} else { } else {
Err(Error::NotFound(path.to_owned())) Err(Error::NotFound(path.to_owned()))
} }

Loading…
Cancel
Save