use crate::serde::Error;
use crate::{types::atom::Atom, Encoder, Env, Term};
atoms! {
nil,
ok,
error,
true_ = "true",
false_ = "false",
undefined,
nan,
inf,
neg_inf,
__struct__,
}
pub fn str_to_term<'a>(env: &Env<'a>, string: &str) -> Result<Term<'a>, Error> {
match Atom::try_from_bytes(*env, string.as_bytes()) {
Ok(Some(term)) => Ok(term.encode(*env)),
Ok(None) => Ok(string.encode(*env)),
_ => Err(Error::InvalidStringable),
}
}
pub fn term_to_string(term: &Term) -> Result<String, Error> {
if term.is_atom() {
term.atom_to_string().or(Err(Error::InvalidAtom))
} else {
Err(Error::InvalidStringable)
}
}