>>109922300
happy queer-mo?
I too am writing useless trash, but I'll probably use this in "real software(tm)"
macro_rules! consume_til {
($depth:expr, $itr:ident, $($next:ident)::* ) => {
$itr.by_ref().filter_map(|line| $($next)::*($depth, line.as_ref())).next()
};
}
macro_rules! impl_from_into_iter {
($fn:ident, $cratety:ident, $unsiz_ty:ty, $split_on:expr) => {
pub fn $fn<'a, T>(content: &'a T) -> impl Iterator<Item = $cratety::Token<'a>>
where
T: AsRef<$unsiz_ty> + ?Sized,
{
let mut itr = content.as_ref().split($split_on);
// get first content line
let first = consume_til!(0, itr, bytes::parse_next);
std::iter::successors(first, move |(depth, _, kvpair)| {
if let Some(kvpair) = kvpair {
return match kvpair {
bytes::Token::Value(item) => {
Some((*depth, bytes::Token::Value(item), Some(bytes::Token::Close)))
}
bytes::Token::Close => Some((*depth, bytes::Token::Close, None)),
_ => unreachable!(),
};
} else if let ret @ Some(_) = consume_til!(*depth, itr, bytes::parse_next) {
return ret;
}
if *depth != 0 {
Some((0, bytes::Token::ErrUnderClose, None))
} else {
None
}
})
.map(|(_, tok, _)| tok.into())
}
};
}
impl_from_into_iter!(from_bytes_into_iter, bytes, [u8], |c| *c == b'\n');
impl_from_into_iter!(from_str_into_iter, string, str, '\n');