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
use std::collections::HashMap;

use crate::package::package::Package;
use crate::cache_pair::CachePair;

pub struct PackageCollection<T: CachePair> {
    directory: std::path::PathBuf,
    is_post_ensmallening: bool,
    packages: HashMap<String, 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: HashMap::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).unwrap() {
            let entry = entry.unwrap();
            let file_name = entry.file_name().into_string().unwrap();

            let start = file_name.find('.');
            let end = file_name.rfind('.');

            if start.is_none() || end.is_none() || start.unwrap() == end.unwrap() {
                continue;
            }

            let mut directory = entry.path();
            directory.pop();

            let package_name = file_name[start.unwrap() + 1..end.unwrap()].to_string();
            let package: Package<T> =
                Package::new(directory, package_name.clone(), self.is_post_ensmallening);
            self.packages.insert(package_name, 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.get(package_name)
    }

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

    pub fn packages(&self) -> &HashMap<String, Package<T>> {
        &self.packages
    }
}