html5_picture/
fs.rs

1//! Contains supporting functions that alter the file system.
2
3use {indicatif::ProgressBar, log::error, std::path::PathBuf};
4
5/// Calls ```get_output_working_dir``` for name conversion and creates the output directory on the filesystem.
6pub fn create_output_working_dir(input_dir: &PathBuf) -> Result<(), String> {
7    let path = crate::path::get_output_working_dir(input_dir)?;
8    if path.exists() {
9        return Ok(());
10    }
11    match std::fs::create_dir_all(path) {
12        Ok(_) => Ok(()),
13        Err(msg) => Err(msg.to_string()),
14    }
15}
16
17/// Recreates the input directory structure in the output working directory.
18pub fn create_output_directories(
19    input_dir: &PathBuf,
20    input_file_names: &Vec<PathBuf>,
21    progressbar: Option<ProgressBar>,
22) {
23    for file_name in input_file_names {
24        let mut f = match crate::path::remove_base_dir(&input_dir, file_name) {
25            Ok(f) => f,
26            Err(msg) => {
27                error!("{}", msg);
28                return;
29            }
30        };
31        f.pop();
32        let f = crate::path::get_output_working_dir(&input_dir)
33            .unwrap()
34            .join(f);
35        if !f.is_dir() {
36            match std::fs::create_dir_all(&f) {
37                Ok(()) => {
38                    if let Some(ref pb) = progressbar {
39                        pb.inc(1);
40                    }
41                }
42                Err(msg) => {
43                    match f.to_str() {
44                        Some(v) => {
45                            error!(
46                                "Could not create folder {}! Error: {}",
47                                v, msg
48                            );
49                        }
50                        None => {
51                            error!("Could not create folder! Error {}", msg);
52                        }
53                    };
54                }
55            };
56        }
57    }
58}