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
use fs3::FileExt;
use std::collections::HashMap;
use std::fs::{File, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};
use super::Metadata;
pub struct Output {
path: PathBuf,
index_file: File,
all_metadata: HashMap<String, Metadata>,
}
impl Output {
pub fn new<P>(path: P) -> Output
where
P: AsRef<Path>,
{
let path = path.as_ref().to_path_buf();
let _ = std::fs::create_dir_all(&path);
let index_file = OpenOptions::new()
.create(true)
.write(true)
.open(path.join("Snapshots.toml"))
.unwrap();
index_file.lock_exclusive().unwrap();
Output {
path,
index_file,
all_metadata: HashMap::new(),
}
}
pub fn store(&mut self, metadata: Metadata, screenshot: Vec<u8>) {
let digest = metadata.digest().to_string();
self.all_metadata.insert(digest.clone(), metadata);
File::create(self.path.join(digest + ".png"))
.unwrap()
.write_all(&screenshot)
.unwrap();
}
}
impl Drop for Output {
fn drop(&mut self) {
self.index_file.set_len(0).unwrap();
self.index_file
.write_all(
toml::to_string_pretty(&self.all_metadata)
.unwrap()
.as_bytes(),
)
.unwrap();
self.index_file.unlock().unwrap();
}
}