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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use std::borrow::Cow;
use std::collections::HashMap;

use memchr::memmem;

use crate::constants::{COLON, HYPHEN, NEWLINE, SPACE, UNDERSCORE};
use crate::node_pool::NodeID;
use crate::object::parse_node_property;
use crate::parse::parse_element;
use crate::types::{Cursor, MatchError, ParseOpts, Parseable, Parser, Result};
use crate::utils::Match;

const END_TOKEN: &str = ":end:\n";

#[derive(Debug, Clone)]
pub struct Drawer<'a> {
    pub children: Vec<NodeID>,
    pub name: &'a str,
}

impl<'a> Parseable<'a> for Drawer<'a> {
    fn parse(
        parser: &mut Parser<'a>,
        mut cursor: Cursor<'a>,
        parent: Option<NodeID>,
        parse_opts: ParseOpts,
    ) -> Result<NodeID> {
        let start = cursor.index;
        cursor.is_index_valid()?;
        cursor.skip_ws();
        cursor.word(":")?;

        let name_match = cursor.fn_until(|chr| {
            chr == COLON
                || chr == NEWLINE
                || !(chr.is_ascii_alphanumeric() || chr == HYPHEN || chr == UNDERSCORE)
        })?;

        cursor.index = name_match.end;
        cursor.word(":")?;
        cursor.skip_ws();
        if cursor.curr() != NEWLINE {
            return Err(MatchError::InvalidLogic);
        }
        cursor.next();
        let potential_loc = find_end(cursor).ok_or(MatchError::InvalidLogic)?;
        let end = potential_loc + cursor.index + END_TOKEN.len();

        let mut moving_loc = potential_loc + cursor.index - 1;
        while cursor[moving_loc] == SPACE {
            moving_loc -= 1;
        }

        if cursor[moving_loc] != NEWLINE {
            return Err(MatchError::InvalidLogic);
        }
        moving_loc += 1;

        // handle empty contents
        // :NAME:
        // :end:
        if cursor.index > moving_loc {
            cursor.index = moving_loc;
        }
        let mut children: Vec<NodeID> = Vec::new();
        let reserve_id = parser.pool.reserve_id();
        let mut temp_cursor = cursor.cut_off(moving_loc);

        // TODO: headings aren't elements, so they cannot be contained here
        // based on org-element, they break the formation of the drawer..
        while let Ok(element_id) =
            // use default parseopts since it wouldn't make sense for the contents
            // of the block to be interpreted as a list, or be influenced from the outside
            parse_element(parser, temp_cursor, Some(reserve_id), ParseOpts::default())
        {
            children.push(element_id);
            temp_cursor.index = parser.pool[element_id].end;
        }

        Ok(parser.alloc_with_id(
            Self {
                children,
                name: name_match.obj,
            },
            start,
            end,
            parent,
            reserve_id,
        ))
    }
}

pub type PropertyDrawer<'a> = HashMap<&'a str, Cow<'a, str>>;

pub(crate) fn parse_property(mut cursor: Cursor) -> Result<Match<PropertyDrawer>> {
    let start = cursor.index;
    cursor.is_index_valid()?;
    cursor.skip_ws();
    cursor.word(":")?;

    let name_match = cursor.fn_until(|chr| chr == COLON || chr == NEWLINE)?;

    if name_match.obj.to_ascii_lowercase() == "properties" {
        // yahoo
    } else {
        return Err(MatchError::InvalidLogic);
    }
    cursor.index = name_match.end;

    cursor.word(":")?;
    cursor.skip_ws();
    if cursor.curr() != NEWLINE {
        return Err(MatchError::InvalidLogic);
    }
    cursor.next();
    let potential_loc = find_end(cursor).ok_or(MatchError::InvalidLogic)?;
    let end = potential_loc + cursor.index + END_TOKEN.len();

    let mut moving_loc = potential_loc + cursor.index - 1;
    while cursor[moving_loc] == SPACE {
        moving_loc -= 1;
    }

    if cursor[moving_loc] != NEWLINE {
        return Err(MatchError::InvalidLogic);
    }
    moving_loc += 1;

    // handle empty contents
    // :properties:
    // :end:
    if cursor.index > moving_loc {
        cursor.index = moving_loc;
    }
    let mut children = HashMap::new();
    let mut temp_cursor = cursor.cut_off(moving_loc);
    loop {
        match parse_node_property(temp_cursor, &mut children) {
            Ok(node_end) => {
                temp_cursor.index = node_end;
            }
            Err(MatchError::EofError) => break,
            Err(e) => return Err(e),
        }
    }

    Ok(Match {
        start,
        end,
        obj: children,
    })
}

fn find_end(cursor: Cursor) -> Option<usize> {
    memmem::find(cursor.rest(), END_TOKEN.as_bytes())
}

#[cfg(test)]
mod tests {
    use crate::parse_org;

    #[test]
    fn basic_drawer() {
        let input = r"

:NAME:
hello
:end:

halloo
";

        let pool = parse_org(input);
        pool.print_tree();
    }
}