more documentation

master
Avril 4 years ago
parent b813fe4b01
commit 7c86c6afda
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -7,6 +7,7 @@ use std::{
//TODO: Embed info about the file, the line number, the expression, etc? //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 /// Error type for parsing config errors
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {

@ -113,13 +113,20 @@ impl LispExt for Vec<Sexp>
} }
// Sexp doesn't use try_from(), so... // Sexp doesn't use try_from(), so...
/// Extension methods for getting typed values out of S-expression
pub trait TryIntoExt: Sized pub trait TryIntoExt: Sized
{ {
/// Return this expression as a List (`Vec<Sexp>`) if possible
fn try_into_list(self) -> Result<Vec<Sexp>, Error>; fn try_into_list(self) -> Result<Vec<Sexp>, Error>;
/// Return this expression as a non-descript atom if possible
fn try_into_atom(self) -> Result<Atom, Error>; fn try_into_atom(self) -> Result<Atom, Error>;
/// Return this expression as a String if possible (atom)
fn try_into_string(self) -> Result<String, Error>; fn try_into_string(self) -> Result<String, Error>;
/// Return this expression as an integer if possible (atom)
fn try_into_int(self) -> Result<i64, Error>; fn try_into_int(self) -> Result<i64, Error>;
/// Return this expression as a float if possible (atom)
fn try_into_float(self) -> Result<f64, Error>; fn try_into_float(self) -> Result<f64, Error>;
} }
@ -204,13 +211,21 @@ impl TryIntoExt for Atom
// To avoid some `clone()`s // To avoid some `clone()`s
/// Extension methods for getting typed references out of S-expression
pub trait TryGetExt: Sized pub trait TryGetExt: Sized
{ {
/// Try to get this expression as a reference `List` (slice of `Sexpr`).
fn try_get_list(&self) -> Result<&[Sexp], Error>; 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>; 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>; 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>; 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>; 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)),
}
}
}

@ -61,8 +61,7 @@ pub async fn global(to: &mut Config, path: impl AsRef<Path>) -> Result<(), error
if path.exists() { if path.exists() {
let mut file = File::open(path).await?; let mut file = File::open(path).await?;
let mut contents = if let Ok(Ok(wv)) = file.metadata().await let mut contents = if let Ok(Ok(wv)) = file.metadata().await
.map(|x| x.len()) .map(|x| usize::try_from(x.len())) {
.map(|x| usize::try_from(x)) {
let mut out = Vec::with_capacity(wv+3); let mut out = Vec::with_capacity(wv+3);
out.push(b'('); out.push(b'(');
out out

Loading…
Cancel
Save