summaryrefslogtreecommitdiff
path: root/src/org.rs
blob: 84df9c26b8a38799d0b863fedd58915dae011202 (plain)
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
use regex::Regex;
use std::str;

#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
pub enum Org {
    Unknown(String),
    Header(u16, String),
    Properties(Vec<(String, String)>)
}

type OrgDocument = Vec<Org>;

macro_rules! matchers {
    (
            $($id:ident <- $e:expr;)*
    ) => {
        lazy_static! {
            $(static ref $id: Regex = Regex::new($e).unwrap();)*
        }
    }
}

matchers! {
    header           <- r"^(\*)+ (.*)";
    properties_start <- r"^\s*:PROPERTIES:\s*$";
    property         <- r"^\s*:([^:]*): (.*)$";
    properties_end   <- r"^\s*:END:\s*$";
}

struct Parser<I: Iterator<Item=String>> {
    doc: OrgDocument,
    iter: I,
}

impl<I: Iterator<Item=String>> Parser<I> {
    fn go(&mut self) {
        while let Some(line) = self.iter.next() {
            if let Some(g) = header.captures(&line) {
                self.doc.push(
                    Org::Header(
                        g.get(1).unwrap().as_str().len() as u16,
                        g.get(2).unwrap().as_str().to_string()));
            } else if properties_start.is_match(&line) {
                &mut self.go_properties(line.clone());
            } else {
                self.doc.push(Org::Unknown(line.to_string()));
            }
        }
    }

    fn go_properties(&mut self, first_line: String) {
        let mut fallback = vec!(first_line);
        let mut properties = vec!();

        while let Some(line) = self.iter.next() {
            fallback.push(line.clone());

            if let Some(g) = property.captures(&line) {
                properties.push((
                    g.get(1).unwrap().as_str().to_string(),
                    g.get(2).unwrap().as_str().to_string()));
            } else if properties_end.is_match(&line) {
                break;
            } else {
                self.doc.push(Org::Unknown(fallback.join("\n")));
                return;
            }
        }
        self.doc.push(Org::Properties(properties));
    }
}

pub fn parse(input: &str) -> OrgDocument {
    let mut parser = Parser{
        doc: vec!(),
        iter: input.split('\n').map(|line| line.to_string())
    };
    parser.go();
    parser.doc
}

#[cfg(test)]
mod tests {
    use super::*;

    fn s<S: AsRef<str>>(m: S) -> String {
        m.as_ref().to_string()
    }

    #[test]
    fn parse_unknown() {
        let doc = "hello\nhello";
        assert_eq!(parse(doc),
                   vec!(Org::Unknown("hello".to_string()),
                        Org::Unknown("hello".to_string())));
    }

    #[test]
    fn parse_header() {
        let doc = "* hello";
        assert_eq!(parse(doc),
                   vec!(Org::Header(1, "hello".to_string())));
        assert_eq!(parse(" * hello"),
                   vec!(Org::Unknown(" * hello".to_string())));
    }

    #[test]
    fn parse_properties() {
        let doc = ":PROPERTIES:
                   :VERSION: 1.0
                   :ANIMAL: dog
                   :END:";
        assert_eq!(parse(doc),
                   vec!(Org::Properties(vec!(
                       (s("VERSION"), s("1.0")),
                       (s("ANIMAL"), s("dog"))))));
        let doc = ":PROPERTIES:\ninvalid\n:END:";
        assert_eq!(parse(doc),
                   vec!(Org::Unknown(s(":PROPERTIES:\ninvalid")),
                        Org::Unknown(s(":END:"))));
    }
}