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
use failure::Error;
use oatie::doc::*;
use oatie::rtf::*;
use oatie::writer::CurWriter;
pub struct RandomCursorContext {
cur: CurWriter,
history: Vec<CurSpan>,
}
impl Default for RandomCursorContext {
fn default() -> Self {
RandomCursorContext {
cur: CurWriter::new(),
history: vec![],
}
}
}
pub fn random_cursor_span(
ctx: &mut RandomCursorContext,
span: &DocSpan<RtfSchema>,
) -> Result<(), Error> {
for elem in span {
match *elem {
DocGroup(_, ref span) => {
{
let mut c = ctx.cur.clone();
c.place(&CurElement::CurGroup);
c.exit_all();
ctx.history.push(c.result());
}
ctx.cur.begin();
random_cursor_span(ctx, span)?;
ctx.cur.exit();
}
DocText(_, ref text) => {
ensure!(text.char_len() > 0, "Empty char string");
for _ in 0..text.char_len() {
let mut c = ctx.cur.clone();
c.place(&CurElement::CurChar);
c.exit_all();
ctx.history.push(c.result());
ctx.cur.place(&CurElement::CurSkip(1));
}
}
}
}
Ok(())
}
pub fn random_cursor(doc: &Doc<RtfSchema>) -> Result<Vec<CurSpan>, Error> {
let mut ctx = RandomCursorContext::default();
random_cursor_span(&mut ctx, &doc.0)?;
Ok(ctx.history)
}