tek/edn/src/edn_iter.rs

45 lines
1.1 KiB
Rust

use crate::*;
pub enum EdnIterator<'a, T>{
Nil,
Sym(&'a T),
Exp(Vec<EdnIterator<'a, T>>)
}
impl<'a, T: AsRef<str>> EdnIterator<'a, T> {
pub fn new (item: &'a EdnItem<T>) -> Self {
use EdnItem::*;
match item {
Sym(t) => Self::Sym(t),
Exp(i) => Self::Exp(i.iter().map(EdnIterator::new).collect()),
_ => Self::Nil,
}
}
}
impl<'a, T: AsRef<str>> Iterator for EdnIterator<'a, T> {
type Item = &'a T;
fn next (&mut self) -> Option<Self::Item> {
use EdnIterator::*;
match self {
Sym(t) => {
let t = *t;
*self = Nil;
Some(t)
},
Exp(v) => match v.as_mut_slice() {
[a] => if let Some(next) = a.next() {
Some(next)
} else {
*self = Exp(v.split_off(1));
self.next()
},
_ => {
*self = Nil;
None
}
},
_ => {
*self = Nil;
None
}
}
}
}