aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 17b1c6fc13d7d0667d1154054884d81a5cea0d7b (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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
use std::{fs, path};
use std::io::ErrorKind;
use toml;

enum PathType {
    Exclude,
    DoNotScan,
    Okay,
}

pub struct Config {
    config: toml::Value,
    commands: toml::Value,
}
impl Config {
    pub fn new() -> Config {
        println!("Using built-in config");
        Config {
            config: toml::toml!{
                source = "src"
                build_dir = "build"

                do_not_scan = []
                exclude = []

                file_delim = ":?"
                command_delim = ":!"
                commands_file = "commands.toml"
            },
            commands: toml::toml!{
                empty = ""
            }
        }
    }

    pub fn import(config: &str) -> Config {
        println!("Using found config");
        let config: toml::Value = toml::from_str(config).expect("Could not load config");
        let config = config.get("config").expect("Could not find \"[config]\" field");
        
        // Attempt to read commands file
        let commands: toml::Value = match config.get("commands_file") {
            Some(n) => {
                if n.is_str() {
                    let file_string = n.as_str().unwrap();
                    let file_string = match fs::read_to_string(file_string) {
                        Ok(n) => n,
                        Err(_) => {
                            println!("Could not load commands from file");
                            String::from("[commands]")
                        },
                    };
                    toml::from_str(file_string.as_str()).expect("Invalid toml")
                } else {
                    panic!()
                }
            },
            None => {println!("Could not load commands from file"); toml::toml!{[commands]}},
        };
        let commands = commands.get("commands").expect("Could not find \"[commands]\" field");

        Config {
            config: config.clone(),
            commands: commands.clone(),
        }
    }

    pub fn get_str(&self, string: &str) -> Option<&str> {
        match self.config.get(string) {
            Some(n) => n.as_str(),
            None => None
        }
    }
    
    pub fn get_cmd_str(&self, string: &str) -> Option<&str> {
        match self.commands.get(string) {
            Some(n) => n.as_str(),
            None => None
        }
    }
}

pub struct S3G {
    config: Config,
}
impl S3G {
    pub fn new(config: Config) -> S3G {
        S3G {
            config,
        }
    }

    pub fn run(&self) {
        let source = self.config.get_str("source").unwrap();
        let build = self.config.get_str("build_dir").unwrap();
    
        // Create build directory or panic
        S3G::create_dir(&path::PathBuf::from(build)).unwrap();
    
    
        self.directory_scan(path::PathBuf::from(source));
    }

    fn create_dir(path: &path::PathBuf) -> Result<(), ()> {
        match fs::create_dir(path) {
            Ok(_) => (),
            Err(n) => match n.kind() {
                ErrorKind::AlreadyExists => (),
                _ => return Err(()),
            }
        }
        return Ok(())
    }
    
    fn remove_path_head(path: &path::PathBuf) -> path::PathBuf {
        let mut path = path.to_str().unwrap().split("/");
        path.next();
        let path: path::PathBuf = path.collect();
    
        return path
    }
    
    fn append_path_head(path: &path::PathBuf, head: &str) -> path::PathBuf {
        let mut head = path::PathBuf::from(head);
        head.push(path);
        return head
    }
    
    fn replace_path_head(path: &path::PathBuf, head: &str) -> path::PathBuf {
        let path = S3G::remove_path_head(path);
        S3G::append_path_head(&path, head)
    }
    
    fn check_path(&self, path: &path::PathBuf) -> PathType {
        // GET EXCLUDE AND DNS LISTS
        let excludes: Vec<path::PathBuf> = vec![path::PathBuf::from("nav.html")];
        let dns: Vec<path::PathBuf> = vec![path::PathBuf::from("assets/")];
    
        // Convert path to str for comparisons
        let path = path.to_str().unwrap();
    
        // Excludes
        for ex_path in excludes {
            let ex_path = ex_path.to_str().unwrap();
            if &path.contains(&ex_path) == &true {
                return PathType::Exclude;
            }
        }
        
        // Do not scans (copy files)
        for dns_path in dns {
            let dns_path = dns_path.to_str().unwrap();
            if &path.contains(&dns_path) == &true {
                return PathType::DoNotScan;
            }
        }
    
        return PathType::Okay;
    }
    
    fn copy_file(&self, path: &path::PathBuf) {
        let build_path = S3G::replace_path_head(&path, "build/");
        match fs::copy(path, build_path) {
            Ok(_) => (),
            Err(n) => {
                if n.kind() != ErrorKind::AlreadyExists {
                    panic!()
                }
            },
        }
    }
    
    fn directory_scan(&self, path: path::PathBuf) {
        println!("Scanning: {:?}", &path);
    
        // Check the path
        match self.check_path(&path) {
            PathType::Exclude => return,
            _ => (),
        };
    
        // Create the dir in the build tree
        let build_path = S3G::replace_path_head(&path, "build/");
        S3G::create_dir(&build_path).unwrap();
    
        // Iterate over path contents
        for x in fs::read_dir(path).unwrap() {
            let x = x.as_ref().unwrap().path();
            if x.is_dir() {
                self.directory_scan(x);
            } else {
                match self.file_scan(&x) {
                    None => (),
                    Some(n) => self.file_write(&x, n).unwrap(),
                }
            }
            
    
        }
    }
    
    fn file_scan(&self, path: &path::PathBuf) -> Option<String> {
        // Check path
        match self.check_path(&path) {
            PathType::Okay => println!("Scanning: \"{}\"", &path.to_str().unwrap()),
            PathType::DoNotScan => {
                self.copy_file(&path);
                println!("Not scanning: \"{}\"", &path.to_str().unwrap());
                return None
            },
            PathType::Exclude => (),
        };
    
        // File scanning
        let mut file_string = String::new();

        let string = match fs::read_to_string(&path) {
            Ok(n) => n,
            Err(_) => {
                println!("Could not read file at: \"{}\", Continuing", &path.to_str().unwrap());
                String::from("")
            }
        };
    
        for line in string.lines() {

            // NEED TO FOR LOOP ON COMMANDS AND FILES PREFIXES
            if line.contains(":?") {
                let v: Vec<&str> = line.trim().split(":?").collect();
                println!("{:?}", v);
                for s in v {
                    let s = match s {
                        "" => String::from(""),
                        // Scan the file from the path
                        n => self.file_scan(&path::PathBuf::from(n)).unwrap_or(String::from("")),
                    };

                    // Push matching and scanned string
                    file_string.push_str(&s);
                    file_string.push('\n');
                }
            } else {
                // Push unmatching line
                file_string.push_str(line);
                file_string.push('\n');
            }
        }

        // Return the final file_string
        if file_string != "" {
            return Some(file_string);
        } else {
            return None;
        }
    }
    
    fn file_write(&self, path: &path::PathBuf, contents: String) -> Result<(),()> {

        match self.check_path(&path) {
            PathType::Exclude => return Ok(()),
            _ => (),
        }
    
        let dest = S3G::replace_path_head(&path, "build/");
        match fs::write(dest, contents) {
            Ok(_) => Ok(()),
            Err(_) => Err(()),
        }
    }
}