summaryrefslogtreecommitdiff
path: root/src/org.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/org.rs')
-rw-r--r--src/org.rs77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/org.rs b/src/org.rs
new file mode 100644
index 0000000..3ba918d
--- /dev/null
+++ b/src/org.rs
@@ -0,0 +1,77 @@
+use regex::Regex;
+
+#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Debug)]
+pub enum Org<'a> {
+ Unknown,
+ Header(u16, &'a str),
+}
+
+#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Debug)]
+pub struct OrgNode<'a> {
+ pub raw: &'a str,
+ pub e: Org<'a>,
+}
+
+type OrgDocument<'a> = Vec<OrgNode<'a>>;
+
+struct Parser<'a> {
+ doc: OrgDocument<'a>
+}
+
+impl<'a> Parser<'a> {
+ fn parse_line(&mut self, line: &'a str) {
+ lazy_static! {
+ static ref header: Regex = Regex::new(r"(\*)+ (.*)").unwrap();
+ }
+
+ if let Some(g) = header.captures(line) {
+ self.doc.push(OrgNode {
+ raw: line,
+ e: Org::Header(
+ g.get(1).unwrap().as_str().len() as u16,
+ g.get(2).unwrap().as_str()
+ )});
+ } else {
+ self.doc.push(OrgNode {
+ raw: line,
+ e: Org::Unknown
+ });
+ }
+ }
+
+ pub fn parse(input: &'a str) -> OrgDocument<'a> {
+ let mut parser = Parser{doc: vec!()};
+ for line in input.split('\n') {
+ parser.parse_line(line);
+ }
+ parser.doc
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn parse_unknown() {
+ let doc = "hello\nhello";
+ assert_eq!(Parser::parse(doc),
+ vec!(OrgNode {
+ raw: "hello",
+ e: Org::Unknown
+ }, OrgNode {
+ raw: "hello",
+ e: Org::Unknown
+ }));
+ }
+
+ #[test]
+ fn parse_header() {
+ let doc = "* hello";
+ assert_eq!(Parser::parse(doc),
+ vec!(OrgNode {
+ raw: doc,
+ e: Org::Header(1, "hello")
+ }));
+ }
+}