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
65
66
67
use crate::cache_pair::CachePair;
use crate::package::package::Package;

pub struct PackageCollection<T: CachePair> {
    directory: std::path::PathBuf,
    is_post_ensmallening: bool,
    packages: Vec<Package<T>>,
}

impl<T: CachePair> PackageCollection<T> {
    pub fn new(directory: std::path::PathBuf, is_post_ensmallening: bool) -> Self {
        let mut package_collection = Self {
            directory,
            is_post_ensmallening,
            packages: Vec::new(),
        };
        package_collection.load_packages();
        package_collection
    }

    fn load_packages(&mut self) {
        let package_directory = self.directory.clone();
        for entry in std::fs::read_dir(package_directory.clone()).unwrap() {
            let entry = entry.unwrap();
            let file_name = entry.file_name().into_string().unwrap();

            // Check if the file has enough characters to be a package
            // 7 characters counts for the shortest possible package name of
            // 1 character : H.1.toc
            if file_name.len() < 7 {
                continue;
            }

            // Check if the file is a valid header .toc file
            if !file_name.starts_with("H.") || !file_name.ends_with(".toc") {
                continue;
            }

            let package_name = file_name[2..file_name.len() - 4].to_string();

            let package = Package::new(
                package_directory.clone(),
                package_name,
                self.is_post_ensmallening,
            );
            self.packages.push(package);
        }
    }

    pub fn is_post_ensmallening(&self) -> bool {
        self.is_post_ensmallening
    }

    pub fn get_package(&self, package_name: &str) -> Option<&Package<T>> {
        self.packages
            .iter()
            .find(|package| package.name() == package_name)
    }

    pub fn directory(&self) -> &std::path::PathBuf {
        &self.directory
    }

    pub fn packages(&self) -> &Vec<Package<T>> {
        &self.packages
    }
}