docchi_archiver2/imp/
read_archive_data_from_directory.rs

1use std::path::Path;
2use crate::{ArcResult, ArchiveOptions};
3use crate::imp::structs::archiver::Archiver;
4use std::collections::BTreeSet;
5use crate::imp::structs::archive_data::ArchiveData;
6
7pub fn read_archive_data_from_directory<
8    P : AsRef<Path>,
9    T : Send + 'static>(path : P,
10                        opt : &ArchiveOptions,
11                        converter : impl Fn(&str, &[u8]) -> T + Send + Sync + 'static) -> ArcResult<ArchiveData<T>>{
12    let mut btree : BTreeSet<String> = BTreeSet::new();
13    get_paths_from_dir(path, opt, &mut btree)?;
14    let mut archiver = Archiver::new(converter);
15    for path in btree {
16        let data = std::fs::read(&path)?;
17        archiver.archive(path, data);
18    }
19    let hoge = archiver.finish();
20    return hoge;
21}
22
23fn get_paths_from_dir<P : AsRef<Path>>(current_path : P, opt : &ArchiveOptions, btree : &mut BTreeSet<String>) -> ArcResult<()>{
24    let dirs = std::fs::read_dir(current_path)?;
25
26    for dir in dirs {
27        let de = dir?;
28
29        let meta = de.metadata()?;
30        if meta.is_file() {
31            let path = de.path();
32            let ext = path.extension().map_or("", |e| e.to_str().unwrap_or(""));
33            if opt.is_archived(ext){
34                btree.insert(path.to_string_lossy().to_string());
35            }
36        } else if meta.is_dir(){
37            if opt.archive_subfolders(){
38                get_paths_from_dir(de.path(), opt, btree)?;
39            }
40        }
41    }
42    Ok(())
43}