-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreator.rs
58 lines (48 loc) · 1.64 KB
/
creator.rs
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
use super::error::PluginWriteError;
use super::writer;
use super::PluginMeta;
use std::fs::File;
use std::io::BufWriter;
use std::path::{Path, PathBuf};
pub struct PluginCreator {
meta: PluginMeta,
files: Vec<PathBuf>,
}
impl PluginCreator {
pub fn new(meta: PluginMeta) -> PluginCreator {
PluginCreator {
meta,
files: Vec::new(),
}
}
#[inline]
pub fn add_file(&mut self, file: PathBuf) {
self.files.push(file);
}
pub fn create_plugin<P: AsRef<Path>>(&mut self, output: P) -> Result<(), PluginWriteError> {
let file = File::with_options().read(true).write(true).create_new(true).open(output)?;
// BufWriter is used to reduce system-write-calls and this improves performance
let write = BufWriter::new(file);
let mut writer = writer::PluginWriter { write };
writer.write_magic_number()?;
writer.flush()?;
writer.write_file_version()?;
writer.flush()?;
writer.write_meta(self.meta.clone())?;
writer.flush()?;
for path in self.files.iter() {
let path_buf: &PathBuf = path;
// read file to memory (I hope nobody creates code files larger than the maximum memory size)
let buf = std::fs::read(path_buf)?;
match path_buf.as_os_str().to_str() {
Some(path_str) => writer.write_file(path_str, &buf)?,
None => {
// sorry, for that explicit return
return Err(PluginWriteError::InvalidFileName);
}
};
writer.flush()?;
}
Ok(())
}
}