1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use std::marker::PhantomData;
use crate::str::SharedString;
use crate::{block::Block, primitive::Entity, util::cut};
use anyhow::{anyhow, Result};
pub fn parse<B, E>(input: &[u8]) -> Result<SubtextIterator<B, E>>
where
E: From<Entity> + AsRef<Entity>,
B: From<Block<E>>,
{
let input = SharedString::try_from_byte_slice(input)
.map_err(|_| anyhow!("Could not interpret bytes as UTF-8"))?;
Ok(SubtextIterator::new(input))
}
pub struct SubtextIterator<B, E>
where
E: From<Entity> + AsRef<Entity>,
B: From<Block<E>>,
{
input: SharedString,
output_type: PhantomData<(B, E)>,
}
impl<B, E> SubtextIterator<B, E>
where
E: From<Entity> + AsRef<Entity>,
B: From<Block<E>>,
{
pub fn new(input: SharedString) -> Self {
SubtextIterator {
input,
output_type: PhantomData {},
}
}
}
impl<B, E> Iterator for SubtextIterator<B, E>
where
E: From<Entity> + AsRef<Entity>,
B: From<Block<E>>,
{
type Item = B;
fn next(&mut self) -> Option<Self::Item> {
if self.input.len() > 0 {
match crate::block::parse(self.input.clone()) {
Ok((block, steps)) => {
let steps = usize::min(steps, self.input.len());
self.input = match cut(&self.input, steps) {
Ok(input) => input,
_ => "".into(),
};
Some(B::from(block))
}
Err(error) => {
warn!("Failed to parse block: {:?}", error);
None
}
}
} else {
None
}
}
}