use crate::codec::{CodecError, Decoder, Encoder};
use crate::types::{AnyTerm, Atom, Env};
impl<'id> Encoder<'id> for bool {
fn encode(&self, env: impl Env<'id>) -> Result<AnyTerm<'id>, CodecError> {
let name = if *self { "true" } else { "false" };
Atom::try_existing(env, name).expect("true/false atoms are always present").encode(env)
}
}
impl<'id> Decoder<'id> for bool {
fn decode(term: AnyTerm<'id>, env: impl Env<'id>) -> Result<Self, CodecError> {
let atom = Atom::decode(term, env)?;
if atom == Atom::try_existing(env, "true").expect("true atom is always present") {
Ok(true)
} else if atom == Atom::try_existing(env, "false").expect("false atom is always present") {
Ok(false)
} else {
Err(CodecError::WrongType)
}
}
}