summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 0f6d01d40cd580f4ff45352027639555c11d1cbe (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
123
124
125
126
use std::path::{PathBuf,Path};
use std::collections::HashMap;
use core::error::Error;
use std::time::SystemTime;
use std::fs::File;
use std::io::Read;

mod lexer;
mod parser;

pub struct Crimtag {
    fragments: HashMap<String, Fragment>,
    sources: Vec<FragmentSource>,
}

#[derive(PartialEq,Eq,Debug,Clone)]
enum FragmentSource {
    File {
        path: PathBuf,
        loaded: SystemTime,
    },
    Static,
    External {
        key: String,
    },
}

struct Fragment {
    name: String,
    source: usize,
    sections: HashMap<String, Section>,
}

struct Section {
    name: String,
    ast_root: Token,
}

type Properties = HashMap<String, String>;

enum Token {
    Root(Vec<Token>),
    Fragment(String,Properties,Vec<Token>),
    Section(String,Properties,Vec<Token>),
    Text(String),
}

struct State {
}

impl Crimtag {
    pub fn new() -> Self {
        Self {
            fragments: HashMap::new(),
            sources: vec![FragmentSource::Static],
        }
    }

    fn id_source(&mut self, src: &FragmentSource ) -> usize {
        for idx in 0..self.sources.len() {
            if self.sources[idx] == *src {
                return idx;
            }
        }

        // Nothing found, add a new one
        let idx = self.sources.len();
        self.sources.push( src.clone() );
        idx
    }

    pub fn load_file(&mut self, path: &Path) -> Result<(),Box::<dyn Error>> {
        let time = if let Ok(meta) = path.metadata() {
            if let Ok(t) = meta.modified() {
                t
            } else if let Ok(t) = meta.created() {
                t
            } else {
                SystemTime::now()
            }
        } else {
            SystemTime::now()
        };

        let mut p = parser::Parser::new();
        p.parse( self, &String::from_utf8( std::fs::read( path )? )? );

        Ok(())
    }

    pub fn load_static(&mut self, data: &str) -> Result<(),Box::<dyn Error>> {
        let mut p = parser::Parser::new();
        p.parse( self, &data );

        Ok(())
    }

    pub fn load_external(&mut self, key: &str, data: &str) -> Result<(),Box::<dyn Error>> {
        Ok(())
    }
}

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

    #[test]
    fn lexing() {
        let data = r#"Leading comment: [|fragment "basic" something="yeup"|>Hello there <|fragment|] Trailing text"#;
        let ll = lexer::Lexer::new( &data );
        for sym in ll {
            if let lexer::Symbol::Error{line,row,what} = sym {
                println!("Error {}:{}: {:?}", line, row, what );
                break;
            } else {
                println!("Symbol: {:?}", sym );
            }
        }
    }

    #[test]
    fn parsing() {
        let mut ct = Crimtag::new();
        ct.load_static(r#"Here is a sample [|frament "index" theme="standard"|><html><body>Hi</body></html><|fragment|] That was fun!"#);
    }
}