image_reducer/workspace/
config.rs

1use super::*;
2
3impl Default for TinyConfig {
4    fn default() -> Self {
5        Self { writable: false, log_level: LevelFilter::Info, database: Default::default() }
6    }
7}
8
9impl TinyConfig {
10    pub fn with_writable(mut self, on: bool) -> Self {
11        self.writable = on;
12        self
13    }
14    pub fn with_database(mut self, on: bool) -> Self {
15        self.database = on;
16        self
17    }
18    pub fn with_log_level(mut self, level: LevelFilter) -> Self {
19        self.log_level = level;
20        self
21    }
22    pub fn database() -> TinyResult<PathBuf> {
23        let dir = find_directory_or_create(&current_exe()?, "target")?;
24        Ok(dir.join("tiny-png.db"))
25    }
26}
27
28impl TinyConfig {
29    pub fn initialize(&mut self, workspace: PathBuf) -> TinyResult<TinyWorkspace> {
30        logger(self.log_level);
31        log::info!("Workspace initialized\n{}", workspace.display());
32        let database = if self.database { TinyConfig::database()? } else { PathBuf::new() };
33        let mut out = TinyWorkspace {
34            workspace,
35            writable: self.writable,
36            database,
37            reduced: Default::default(),
38            start: SystemTime::now(),
39            files: Default::default(),
40        };
41        out.load_database()?;
42        Ok(out)
43    }
44}
45
46impl TinyWorkspace {
47    fn load_database(&mut self) -> TinyResult {
48        if self.database.to_string_lossy().is_empty() {
49            return Ok(());
50        }
51        if !self.database.exists() {
52            return Ok(());
53        }
54        let _ = read(&self.database)?;
55        // self.files =
56        log::info!("Database initialized\n{}", self.database.display());
57        Ok(())
58    }
59    fn drop_database(&self) -> TinyResult {
60        // self.database
61        Ok(())
62    }
63}
64
65impl Drop for TinyWorkspace {
66    fn drop(&mut self) {
67        if let Err(e) = self.drop_database() {
68            eprintln!("{}", e)
69        }
70    }
71}