use crate::vm::value::{ConsCell, Value};

pub struct ProperListIter<'a, E> {
    pub(super) head: Option<&'a Value>,
    pub(super) error: Option<E>,
}

impl<'a, E> Iterator for ProperListIter<'a, E> {
    type Item = Result<&'a Value, E>;

    fn next(&mut self) -> Option<Self::Item> {
        let head = self.head.take()?;
        match head {
            Value::Cons(cons) => {
                let ConsCell(car, cdr) = cons.as_ref();
                self.head = Some(cdr);
                Some(Ok(car))
            }
            Value::Nil => None,
            _ => self.error.take().map(Err),
        }
    }
}
