aboutsummaryrefslogtreecommitdiffstats
path: root/src/util/mdx_collect.rs
blob: 02921a4736006fccc09d44840412a2d39c5f4789 (plain) (blame)
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
//! Collect info for MDX.

use crate::event::{Event, Kind, Name};
use crate::util::slice::{Position, Slice};
use alloc::{string::String, vec, vec::Vec};

pub type Stop = (usize, usize);

#[derive(Debug)]
pub struct Result {
    pub value: String,
    pub stops: Vec<Stop>,
}

pub fn collect(
    events: &[Event],
    bytes: &[u8],
    from: usize,
    names: &[Name],
    stop: &[Name],
) -> Result {
    let mut result = Result {
        value: String::new(),
        stops: vec![],
    };
    let mut index = from;

    while index < events.len() {
        if events[index].kind == Kind::Enter {
            if names.contains(&events[index].name) {
                // Include virtual spaces, and assume void.
                let value = Slice::from_position(
                    bytes,
                    &Position {
                        start: &events[index].point,
                        end: &events[index + 1].point,
                    },
                )
                .serialize();
                result
                    .stops
                    .push((result.value.len(), events[index].point.index));
                result.value.push_str(&value);
            }
        } else if stop.contains(&events[index].name) {
            break;
        }

        index += 1;
    }

    result
}