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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use crate::{MarkdownExt, Share};
use equt_md_frontmatter::FrontMatter;
use std::cell::{Ref, RefCell};
enum TailState {
Origin,
Tail,
}
#[derive(new)]
pub struct Tail<I, H, F, G, T>
where
G: IntoIterator<Item = T, IntoIter = H>,
H: Iterator<Item = T>,
F: Fn(Ref<Option<FrontMatter>>) -> G,
{
frontmatter: Share<RefCell<Option<FrontMatter>>>,
origin: I,
f: F,
#[new(default)]
tail: Option<H>,
#[new(value = "TailState::Origin")]
state: TailState,
}
impl<I, H, F, G, T> Iterator for Tail<I, H, F, G, T>
where
I: Iterator<Item = T>,
H: Iterator<Item = T>,
G: IntoIterator<Item = T, IntoIter = H>,
F: Fn(Ref<Option<FrontMatter>>) -> G,
{
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
match self.state {
TailState::Tail => {
if self.tail.is_none() {
self.tail = Some((self.f)(self.frontmatter.upgrade().unwrap().borrow()).into_iter());
}
self.tail.as_mut().unwrap().next()
}
TailState::Origin => match self.origin.next() {
node @ Some(_) => node,
None => {
self.state = TailState::Tail;
self.next()
}
},
}
}
}
impl<I, H, F, G, T> MarkdownExt<T> for Tail<I, H, F, G, T>
where
I: Iterator<Item = T>,
H: Iterator<Item = T>,
G: IntoIterator<Item = T, IntoIter = H>,
F: Fn(Ref<Option<FrontMatter>>) -> G,
{
fn frontmatter(&mut self) -> &mut Share<RefCell<Option<FrontMatter>>> {
&mut self.frontmatter
}
}
#[cfg(test)]
mod test {
use super::*;
use std::rc::Rc;
#[test]
fn normal() {
assert_eq!(
Tail::new(Rc::new(RefCell::new(None)).into(), 1..3, |_| 3..6).collect::<Vec<_>>(),
vec![1, 2, 3, 4, 5]
);
}
}